fix: init state from wasm contracts when state fully cached

This commit is contained in:
ppedziwiatr
2022-03-05 17:01:33 +01:00
committed by Piotr Pędziwiatr
parent b529e7bc7c
commit 459a28f059
8 changed files with 13 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ let contract: Contract<ExampleContractState>;
interface ExampleContractState {
counter: number;
}
describe('Testing the SmartWeave client', () => {
let contractSrc: string;

View File

@@ -9,6 +9,8 @@ import { addFunds, mineBlock } from './_helpers';
interface ExampleContractState {
counter: number;
firstName: string;
lastName: string;
}
describe('Testing the SmartWeave client for WASM contract', () => {
@@ -36,8 +38,6 @@ describe('Testing the SmartWeave client for WASM contract', () => {
});
LoggerFactory.INST.logLevel('error');
LoggerFactory.INST.logLevel('debug', 'WasmContractHandlerApi');
LoggerFactory.INST.logLevel('debug', 'WASM');
smartweave = SmartWeaveNodeFactory.memCached(arweave);
@@ -79,7 +79,10 @@ describe('Testing the SmartWeave client for WASM contract', () => {
});
it('should properly read initial state', async () => {
expect((await contract.readState()).state.counter).toEqual(0);
const contractState = (await contract.readState()).state;
expect(contractState.counter).toEqual(0);
expect(contractState.firstName).toEqual("first_ppe");
expect(contractState.lastName).toEqual("last_ppe");
});
it('should properly register interactions', async () => {
@@ -105,7 +108,7 @@ describe('Testing the SmartWeave client for WASM contract', () => {
function: 'increment'
});
expect(result.gasUsed).toEqual(9079017);
expect(result.gasUsed).toEqual(9053274);
});
it('should return stable gas results', async () => {
@@ -120,7 +123,7 @@ describe('Testing the SmartWeave client for WASM contract', () => {
}
results.forEach((result) => {
expect(result.gasUsed).toEqual(9079017);
expect(result.gasUsed).toEqual(9053274);
});
});

View File

@@ -40,6 +40,7 @@ export class CacheableStateEvaluator extends DefaultStateEvaluator {
const cachedState = executionContext.cachedState;
if (cachedState?.cachedHeight === requestedBlockHeight) {
executionContext.handler?.initState(cachedState.cachedValue.state);
return cachedState.cachedValue;
}
@@ -93,6 +94,7 @@ export class CacheableStateEvaluator extends DefaultStateEvaluator {
// if cache is up-to date - return immediately to speed-up the whole process
if (missingInteractions.length === 0 && cachedState) {
this.cLogger.debug(`State up to requested height [${requestedBlockHeight}] fully cached!`);
executionContext.handler?.initState(cachedState.cachedValue.state);
return cachedState.cachedValue;
}
}

View File

@@ -45,7 +45,7 @@ export class DefaultCreateContract implements CreateContract {
throw new Error(`No info about source type in wasm binary. Did you forget to export global "type" value?`);
}
// @ts-ignore
const type = module.instance.exports.type.value;
const type = module.instance.exports.type();
if (!wasmTypeMapping.has(type)) {
throw new Error(`Unknown wasm source type ${type}`);
}

View File

@@ -61,7 +61,7 @@ export abstract class DefaultStateEvaluator implements StateEvaluator {
let currentState = baseState.state;
const validity = baseState.validity;
executionContext.handler.initState(currentState);
executionContext?.handler.initState(currentState);
this.logger.info(
`Evaluating state for ${contractDefinition.txId} [${missingInteractions.length} non-cached of ${sortedInteractions.length} all]`

Binary file not shown.

Binary file not shown.