-
Notifications
You must be signed in to change notification settings - Fork 44
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
feat: block non-deterministic JS API for VM2 #448
Open
asiaziola
wants to merge
1
commit into
main
Choose a base branch
from
asiaziola/block-non-deterministic-js-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
108 changes: 108 additions & 0 deletions
108
src/__tests__/integration/basic/non-deterministic.test.ts
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,108 @@ | ||
import fs from 'fs'; | ||
|
||
import ArLocal from 'arlocal'; | ||
import { JWKInterface } from 'arweave/node/lib/wallet'; | ||
import path from 'path'; | ||
import { Contract } from '../../../contract/Contract'; | ||
import { Warp } from '../../../core/Warp'; | ||
import { WarpFactory } from '../../../core/WarpFactory'; | ||
import { LoggerFactory } from '../../../logging/LoggerFactory'; | ||
import { DeployPlugin } from 'warp-contracts-plugin-deploy'; | ||
import { VM2Plugin } from 'warp-contracts-plugin-vm2'; | ||
|
||
let arlocal: ArLocal; | ||
let warpVm: Warp; | ||
let contractVm: Contract<any>; | ||
|
||
describe('Testing the Warp client', () => { | ||
let contractSrc: string; | ||
|
||
let wallet: JWKInterface; | ||
let walletAddress: string; | ||
let contractTxIdVm; | ||
|
||
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(1802, false); | ||
await arlocal.start(); | ||
|
||
LoggerFactory.INST.logLevel('error'); | ||
warpVm = WarpFactory.forLocal(1802).use(new DeployPlugin()).use(new VM2Plugin()); | ||
|
||
({ jwk: wallet, address: walletAddress } = await warpVm.generateWallet()); | ||
contractSrc = fs.readFileSync(path.join(__dirname, '../data/non-deterministic.js'), 'utf8'); | ||
|
||
// deploying contract using the new SDK. | ||
({ contractTxId: contractTxIdVm } = await warpVm.deploy({ | ||
wallet, | ||
initState: JSON.stringify({}), | ||
src: contractSrc | ||
})); | ||
|
||
contractVm = warpVm.contract(contractTxIdVm); | ||
contractVm.connect(wallet); | ||
}); | ||
|
||
afterAll(async () => { | ||
await arlocal.stop(); | ||
}); | ||
|
||
it('should not allow to use Math.random', async () => { | ||
await contractVm.writeInteraction({ function: 'mathRandom' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use Date.now', async () => { | ||
await contractVm.writeInteraction({ function: 'dateNow' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use Date', async () => { | ||
await contractVm.writeInteraction({ function: 'date' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use setTimeout', async () => { | ||
await contractVm.writeInteraction({ function: 'setTimeout' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use setInterval', async () => { | ||
await contractVm.writeInteraction({ function: 'setInterval' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use weakMap', async () => { | ||
await contractVm.writeInteraction({ function: 'weakMap' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should not allow to use weakRef', async () => { | ||
await contractVm.writeInteraction({ function: 'weakRef' }); | ||
|
||
expect(Object.keys((await contractVm.readState()).cachedValue.state).length).toBe(0); | ||
}); | ||
|
||
it('should allow to use some specific Date', async () => { | ||
await contractVm.writeInteraction({ function: 'specificDate' }); | ||
|
||
const state = (await contractVm.readState()).cachedValue.state; | ||
expect(Object.keys(state).length).toEqual(1); | ||
expect(state['specificDate']).toEqual(new Date('2001-08-20')); | ||
}); | ||
|
||
it('should allow to use some deterministic Math methods', async () => { | ||
await contractVm.writeInteraction({ function: 'mathMax' }); | ||
|
||
const state = (await contractVm.readState()).cachedValue.state; | ||
expect(Object.keys(state).length).toEqual(2); | ||
expect(state['mathMax']).toEqual(3); | ||
}); | ||
}); |
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,70 @@ | ||
export async function handle(state, action) { | ||
const input = action.input; | ||
|
||
if (input.function === 'mathRandom') { | ||
const number = Math.random(); | ||
state.number = number; | ||
return { state }; | ||
} | ||
|
||
if (input.function === 'mathMax') { | ||
const number = Math.max(1, 2, 3); | ||
state['mathMax'] = number; | ||
return { state }; | ||
} | ||
|
||
if (input.function === 'dateNow') { | ||
const date = Date.now(); | ||
state['dateNow'] = date; | ||
return { state }; | ||
} | ||
|
||
if (input.function === 'date') { | ||
const date = new Date(); | ||
state['date'] = date; | ||
return { state }; | ||
} | ||
|
||
if (input.function === 'specificDate') { | ||
const date = new Date('2001-08-20'); | ||
state['specificDate'] = date; | ||
return { state }; | ||
} | ||
|
||
if (input.function === 'setTimeout') { | ||
setTimeout(() => { | ||
state['test'] = 1; | ||
console.log("Delayed for 1 second."); | ||
}, 1000); | ||
|
||
return state; | ||
} | ||
|
||
if (input.function === 'setInterval') { | ||
setInterval(() => { | ||
state['test'] = 1; | ||
console.log("Interval."); | ||
}, 1000); | ||
|
||
return state; | ||
} | ||
|
||
if (input.function === 'weakMap') { | ||
const wm = new WeakMap([ | ||
[{name: 'John'}, 'John Doe'], | ||
[{name: 'Jane'}, 'Jane Doe'], | ||
]); | ||
state['weakMap'] = wm; | ||
|
||
return { state }; | ||
} | ||
|
||
if (input.function === 'weakRef') { | ||
const wr = new WeakRef({name: 'John Doe'}); | ||
state['weakRef'] = wr; | ||
|
||
return { state }; | ||
} | ||
|
||
throw new ContractError(`No function supplied or function not recognised: "${input.function}"`); | ||
} |
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.
commit: warp-contracts/warp-contracts-plugins@cacb84e
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.
https://chat.google.com/room/AAAAaeyBQQM/s9kLQquV3H4/s9kLQquV3H4?cls=10