fix: verifying user and contract evaluation options

This commit is contained in:
ppe
2022-12-05 13:41:43 +01:00
committed by just_ppe
parent 3adb9f1a52
commit 5bfb55a510
6 changed files with 72 additions and 76 deletions

View File

@@ -90,7 +90,8 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
const newSource = fs.readFileSync(path.join(__dirname, '../data/token-pst-unsafe.js'), 'utf8');
const newSrcTxId = await pst.save({ src: newSource }, warp.environment);
const srcTx = await warp.createSourceTx({ src: newSource }, wallet);
const newSrcTxId = await warp.saveSourceTx(srcTx);
await mineBlock(warp);
const evolveResponse = await pst.evolve(newSrcTxId);

View File

@@ -9,7 +9,6 @@ import { PstContract, PstState } from '../../../contract/PstContract';
import { Warp } from '../../../core/Warp';
import { WarpFactory } from '../../../core/WarpFactory';
import { LoggerFactory } from '../../../logging/LoggerFactory';
import exp from 'constants';
describe('Testing unsafe client in nested contracts with "skip" option', () => {
let safeContractSrc, unsafeContractSrc: string;
@@ -146,7 +145,8 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
});
await mineBlock(warp);
const unsafeSrcTxId = await foreignSafePst.save({ src: unsafeContractSrc }, warp.environment);
const srcTx = await warp.createSourceTx({ src: unsafeContractSrc }, wallet);
const unsafeSrcTxId = await warp.saveSourceTx(srcTx);
await mineBlock(warp);
await foreignSafePst.evolve(unsafeSrcTxId);

View File

@@ -123,16 +123,13 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
});
it('should block write from foreign unsafe contract (1)', async () => {
console.log('======= FIRST WRITE =====');
const unsafeWriteTx = await foreignUnsafePst.writeInteraction({
function: 'writeForeign',
contractTxId: contractTxId
});
await mineBlock(warp);
console.log('======= Read main contract =======');
const result = await pst.readState();
console.log(result.cachedValue.validity);
expect(result.cachedValue.validity[unsafeWriteTx.originalTxId]).toBeFalsy();
// should not change from previous test
expect((result.cachedValue.state as any).foreignCallsCounter).toEqual(1);
@@ -141,23 +138,21 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
// this does not work properly. Second write from the unsafe contract
// makes the previous write valid.
it('should block write from foreign unsafe contract (2)', async () => {
console.log('======= NEXT WRITE =====');
const unsafeWriteTx = await foreignUnsafePst.writeInteraction({
function: 'writeForeign',
contractTxId: contractTxId
});
await mineBlock(warp);
console.log('======= Read main contract =======');
const result = await pst.readState();
console.log(result.cachedValue.validity);
expect(result.cachedValue.validity[unsafeWriteTx.originalTxId]).toBeFalsy();
// should not change from previous test
expect((result.cachedValue.state as any).foreignCallsCounter).toEqual(1);
});
it('should block write from foreign safe contract that evolved to unsafe', async () => {
const unsafeSrcTxId = await foreignSafePst.save({ src: unsafeContractSrc }, warp.environment);
const srcTx = await warp.createSourceTx({ src: unsafeContractSrc }, wallet);
const unsafeSrcTxId = await warp.saveSourceTx(srcTx);
await mineBlock(warp);
await foreignSafePst.evolve(unsafeSrcTxId);

View File

@@ -5,11 +5,7 @@ describe('Evaluation options evaluator', () => {
const warp = WarpFactory.forLocal();
it('should properly set root evaluation options', async () => {
const contract = warp.contract(null).setEvaluationOptions({
internalWrites: false,
useVM2: true,
unsafeClient: 'skip'
});
const contract = warp.contract(null);
expect(new EvaluationOptionsEvaluator(contract.evaluationOptions(), {}).rootOptions).toEqual({
allowBigInt: false,
@@ -25,13 +21,20 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
unsafeClient: 'skip',
unsafeClient: 'throw',
updateCacheForEachInteraction: false,
useVM2: true,
useVM2: false,
waitForConfirmation: false,
walletBalanceUrl: 'http://nyc-1.dev.arweave.net:1984/'
});
contract.setEvaluationOptions({
allowBigInt: true,
useVM2: true,
internalWrites: true,
gasLimit: 3453453
});
expect(
new EvaluationOptionsEvaluator(contract.evaluationOptions(), {
allowBigInt: true,
@@ -53,7 +56,7 @@ describe('Evaluation options evaluator', () => {
saveState: false
},
throwOnInternalWriteError: true,
unsafeClient: 'skip',
unsafeClient: 'throw',
updateCacheForEachInteraction: false,
useVM2: true,
waitForConfirmation: false,
@@ -90,32 +93,12 @@ describe('Evaluation options evaluator', () => {
});
expect(
new EvaluationOptionsEvaluator(contract2.evaluationOptions(), {
allowBigInt: true,
useVM2: false,
internalWrites: true,
gasLimit: 3453453
function () {
const result = new EvaluationOptionsEvaluator(contract2.evaluationOptions(), {
useVM2: false
}).rootOptions
).toEqual({
allowBigInt: true,
cacheEveryNInteractions: -1,
gasLimit: 3453453,
ignoreExceptions: true,
internalWrites: true,
maxCallDepth: 5,
maxInteractionEvaluationTimeSeconds: 60,
mineArLocalBlocks: true,
sequencerUrl: 'https://d1o5nlqr4okus2.cloudfront.net/',
stackTrace: {
saveState: false
},
throwOnInternalWriteError: true,
unsafeClient: 'allow',
updateCacheForEachInteraction: false,
useVM2: false,
waitForConfirmation: false,
walletBalanceUrl: 'http://nyc-1.dev.arweave.net:1984/'
});
}
).toThrow("Option {useVM2} differs, user: [true], manifest: [false]");
});
it('should properly set foreign evaluation options - unsafeClient - allow', async () => {

View File

@@ -1,4 +1,3 @@
import { EvaluationManifest } from './deploy/CreateContract';
import { EvaluationOptions } from '../core/modules/StateEvaluator';
import { deepCopy } from '../utils/utils';
@@ -107,11 +106,30 @@ export class EvaluationOptionsEvaluator {
};
/**
* @param evaluationOptions evaluation options set via {@link Contract.setEvaluationOptions}
* @param manifestOptions evaluation options from contract's manifest
* @param userSetOptions evaluation options set via {@link Contract.setEvaluationOptions}
* @param manifestOptions evaluation options from the root contract's manifest (i.e. the contract that
* the user is trying to read - e.g. via warp.contract(<txId>).readState();
*/
constructor(evaluationOptions: EvaluationOptions, manifestOptions: Partial<EvaluationOptions>) {
this.rootOptions = Object.freeze(Object.assign({}, evaluationOptions, manifestOptions || {}));
constructor(userSetOptions: EvaluationOptions, manifestOptions: Partial<EvaluationOptions>) {
if (manifestOptions) {
for (const k in manifestOptions) {
if (userSetOptions[k] !== manifestOptions[k]) {
throw new Error(`Option {${k}} differs, user: [${userSetOptions[k]}], manifest: [${manifestOptions[k]}]`);
}
}
/* if (userSetOptions.internalWrites !== undefined && (userSetOptions.internalWrites != manifestOptions.internalWrites)) {
throw new Error('User and contract manifest not compatible - internalWrites');
}
if (userSetOptions.unsafeClient && (manifestOptions.unsafeClient != userSetOptions.unsafeClient)) {
throw new Error('User and contract manifest not compatible - unsafeClient');
}
if (userSetOptions.throwOnInternalWriteError && (manifestOptions.throwOnInternalWriteError != userSetOptions.throwOnInternalWriteError)) {
throw new Error('User and contract manifest not compatible - throwOnInternalWriteError');
}*/
}
this.rootOptions = Object.freeze(Object.assign({}, userSetOptions, manifestOptions || {}));
}
/**

View File

@@ -7,8 +7,7 @@ import {JWKInterface} from 'arweave/node/lib/wallet';
async function main() {
let wallet: JWKInterface = readJSON('./.secrets/33F0QHcb22W7LwWR1iRC8Az1ntZG09XQ03YWuw2ABqA.json');
;
LoggerFactory.INST.logLevel('none');
LoggerFactory.INST.logLevel('error');
LoggerFactory.INST.logLevel('debug', 'ExecutionContext');
const logger = LoggerFactory.INST.create('deploy');
@@ -26,7 +25,7 @@ async function main() {
const initialState = fs.readFileSync(path.join(__dirname, 'data/js/token-pst.json'), 'utf8');
// case 1 - full deploy, js contract
const {contractTxId, srcTxId} = await warp.createContract.deploy({
const {contractTxId, srcTxId} = await warp.deploy({
wallet,
initState: initialState,
src: jsContractSrc,
@@ -64,7 +63,7 @@ async function main() {
});*/
const contract = warp.contract<any>(contractTxId)
.setEvaluationOptions({sequencerUrl: "http://localhost:5666/", internalWrites: false, unsafeClient: 'throw'})
.setEvaluationOptions({internalWrites: false, unsafeClient: 'throw'})
.connect(wallet);
await Promise.all([