-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ppe/uncommitted state #311
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f61e83b
feat: uncommitte state - removing 'currentTx' inf. loop hack
ppedziwiatr de6c831
feat: uncommitted state - readContractState
ppedziwiatr 40e7eb5
feat: uncommitted state for internal writes
ppedziwiatr 5600310
v1.2.48-beta.0
ppedziwiatr 6cd7ce6
chore: cleanup
ppedziwiatr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/__tests__/integration/data/thethar/simple-thethar-contract.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(() => { | ||
// src/thetAR/actions/read/userOrder.ts | ||
var create = async (state, action) => { | ||
const param = action.input.params; | ||
|
||
const tokenState = await SmartWeave.contracts.readContractState(state.token); | ||
//let orderQuantity = param.price; | ||
let orderQuantity = tokenState.allowances[action.caller][SmartWeave.contract.id]; | ||
logger.error(" CREATE Taking tokens: " + orderQuantity); | ||
await SmartWeave.contracts.write(state.token, { function: "transferFrom", sender: action.caller, recipient: SmartWeave.contract.id, amount: orderQuantity }); | ||
state.orders.push(orderQuantity); | ||
|
||
//await SmartWeave.contracts.readContractState(state.token); | ||
return { state }; | ||
}; | ||
|
||
var cancel = async (state, action) => { | ||
const param = action.input.params; | ||
|
||
let orderQuantity = state.orders[param.orderId]; | ||
logger.error("CANCEL Returning tokens: " + orderQuantity); | ||
await SmartWeave.contracts.write(state.token, { function: "transfer", to: action.caller, amount: orderQuantity }); | ||
|
||
state.orders.splice(param.orderId, 1); | ||
return { state }; | ||
}; | ||
|
||
// src/thetAR/contract.ts | ||
async function handle(state, action) { | ||
const func = action.input.function; | ||
switch (func) { | ||
case "create": | ||
return await create(state, action); | ||
case "cancel": | ||
return await cancel(state, action); | ||
default: | ||
throw new ContractError(`No function supplied or function not recognised: "${func}"`); | ||
} | ||
} | ||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/__tests__/integration/internal-writes/simple-broken-thethar.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import fs from 'fs'; | ||
import ArLocal from 'arlocal'; | ||
import path from 'path'; | ||
import {mineBlock} from '../_helpers'; | ||
import {WarpFactory} from '../../../core/WarpFactory'; | ||
import {LoggerFactory} from '../../../logging/LoggerFactory'; | ||
|
||
const PORT = 1970; | ||
|
||
var simpleThetharTxId; | ||
var arlocal, arweave, warp, walletJwk; | ||
var erc20Contract, simpleThetarContract; | ||
|
||
describe('flow with broken behaviour', () => { | ||
|
||
beforeAll(async () => { | ||
// note: each tests suit (i.e. file with tests that Jest is running concurrently | ||
// with another files has to have ArLocal set to a different port!) | ||
arlocal = new ArLocal(PORT, false); | ||
await arlocal.start(); | ||
LoggerFactory.INST.logLevel('error'); | ||
|
||
warp = WarpFactory.forLocal(PORT); | ||
({jwk: walletJwk} = await warp.generateWallet()); | ||
arweave = warp.arweave; | ||
}); | ||
|
||
afterAll(async () => { | ||
await arlocal.stop(); | ||
}); | ||
|
||
const deployJS = async () => { | ||
const walletAddress = await arweave.wallets.jwkToAddress(walletJwk); | ||
|
||
// deploy TAR pst | ||
const erc20Src = fs.readFileSync(path.join(__dirname, '../data/staking/erc-20.js'), 'utf8'); | ||
|
||
const tarInit = { | ||
symbol: 'TAR', | ||
name: 'ThetAR exchange token', | ||
decimals: 2, | ||
totalSupply: 20000, | ||
balances: { | ||
[walletAddress]: 10000, | ||
}, | ||
allowances: {}, | ||
settings: null, | ||
owner: walletAddress, | ||
canEvolve: true, | ||
evolve: '', | ||
}; | ||
|
||
const erc20TxId = (await warp.createContract.deploy({ | ||
wallet: walletJwk, | ||
initState: JSON.stringify(tarInit), | ||
src: erc20Src, | ||
})).contractTxId; | ||
erc20Contract = warp.contract(erc20TxId); | ||
erc20Contract.setEvaluationOptions({ | ||
internalWrites: true, | ||
allowUnsafeClient: true, | ||
allowBigInt: true, | ||
}).connect(walletJwk); | ||
|
||
// deploy thetAR contract | ||
const simpleThetharSrc = fs.readFileSync(path.join(__dirname, '../data/thethar/simple-thethar-contract.js'), 'utf8'); | ||
const contractInit = { | ||
token: erc20TxId, | ||
orders: [] | ||
}; | ||
|
||
simpleThetharTxId = (await warp.createContract.deploy({ | ||
wallet: walletJwk, | ||
initState: JSON.stringify(contractInit), | ||
src: simpleThetharSrc, | ||
})).contractTxId; | ||
simpleThetarContract = warp.contract(simpleThetharTxId); | ||
simpleThetarContract.setEvaluationOptions({ | ||
internalWrites: true, | ||
allowUnsafeClient: true, | ||
allowBigInt: true, | ||
}).connect(walletJwk); | ||
}; | ||
|
||
const create = async (quantity) => { | ||
await erc20Contract.writeInteraction({ | ||
function: 'approve', | ||
spender: simpleThetharTxId, | ||
amount: quantity | ||
}); | ||
|
||
await mineBlock(warp); | ||
|
||
const txId = (await simpleThetarContract.writeInteraction({ | ||
function: 'create' | ||
})).originalTxId; | ||
|
||
await mineBlock(warp); | ||
|
||
console.log('AFTER: ', JSON.stringify(await simpleThetarContract.readState())); | ||
} | ||
|
||
const cancel = async (orderId) => { | ||
console.log('cancel order...'); | ||
|
||
const txId = await simpleThetarContract.writeInteraction({ | ||
function: 'cancel', | ||
params: { | ||
orderId: orderId | ||
} | ||
}); | ||
await mineBlock(warp); | ||
|
||
console.log('AFTER: ', JSON.stringify(await simpleThetarContract.readState())); | ||
} | ||
|
||
|
||
const readFull = async () => { | ||
const warp = WarpFactory.forLocal(PORT); | ||
|
||
let contract = warp.contract(simpleThetharTxId); | ||
contract.setEvaluationOptions({ | ||
internalWrites: true, | ||
allowUnsafeClient: true, | ||
allowBigInt: true | ||
}).connect(walletJwk); | ||
|
||
const result = await contract.readState(); | ||
|
||
console.log('Contract: ', JSON.stringify(result, null, " ")); | ||
|
||
return result; | ||
} | ||
|
||
it('correctly evaluate deferred state', async () => { | ||
await deployJS(); | ||
await create(1); | ||
await cancel(0); | ||
|
||
console.error("========= READ FULL ==========") | ||
const result = await readFull(); | ||
expect(result.cachedValue.state.orders.length).toEqual(0); | ||
|
||
const errorMessages = result.cachedValue.errorMessages; | ||
for (let errorMessageKey in errorMessages) { | ||
expect(errorMessages[errorMessageKey]).not.toContain('TransferFromZero'); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
amount
the sender balance (transfer-FROM-zero
suggests that)? Shouldn't we check whetheramount <= 0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a real contract - yes.
This contract is a simplified, stripped-down version (I believe there's a warn in the header -
warp/src/__tests__/integration/data/staking/erc-20.js
Line 2 in 5c8acc0