Skip to content

Commit

Permalink
WIP: expose evolveState from HandlerApi
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiszczatowski committed Apr 7, 2023
1 parent 3b5fae3 commit c94cf64
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/modules/impl/DefaultStateEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export abstract class DefaultStateEvaluator implements StateEvaluator {
currentState = newState.state as State;
// we need to update the state in the wasm module
// TODO: opt - reuse wasm handlers...
executionContext?.handler.initState(currentState);
executionContext.handler.initState(currentState);

validity[missingInteraction.id] = newState.validity[missingInteraction.id];
if (newState.errorMessages?.[missingInteraction.id]) {
Expand Down
5 changes: 4 additions & 1 deletion src/core/modules/impl/handler/AbstractContractHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EvolveState } from 'contract/Contract';
import { ContractDefinition } from '../../../../core/ContractDefinition';
import { ExecutionContext } from '../../../../core/ExecutionContext';
import { EvalStateResult } from '../../../../core/modules/StateEvaluator';
Expand Down Expand Up @@ -26,7 +27,9 @@ export abstract class AbstractContractHandler<State> implements HandlerApi<State
interactionData: InteractionData<Input>
): Promise<InteractionResult<State, Result>>;

abstract initState(state: State);
abstract initState(state: State): void;

abstract evolveState(): Promise<EvolveState>;

abstract maybeCallStateConstructor(
initialState: State,
Expand Down
9 changes: 8 additions & 1 deletion src/core/modules/impl/handler/JsHandlerApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EvolveState } from 'contract/Contract';
import { GQLNodeInterface } from 'legacy/gqlResult';
import { ContractDefinition } from '../../../../core/ContractDefinition';
import { ExecutionContext } from '../../../../core/ExecutionContext';
Expand Down Expand Up @@ -47,6 +48,12 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {
// eslint-disable-next-line
initState(state: State) {}

evolveState(): Promise<EvolveState> {
// implement me
// how to implement is? For JS contracts we have no snapshot of 'current' state
return null;
}

async maybeCallStateConstructor<Input>(
initialState: State,
executionContext: ExecutionContext<State>
Expand Down Expand Up @@ -149,7 +156,7 @@ export class JsHandlerApi<State> extends AbstractContractHandler<State> {
// Will be caught below as unexpected exception.
throw new Error(`Unexpected result from contract: ${JSON.stringify(handlerResult)}`);
} catch (err) {
await this.swGlobal.kv.rollback();
this.swGlobal.kv.rollback();
switch (err.name) {
case KnownErrors.ContractError:
return {
Expand Down
8 changes: 8 additions & 0 deletions src/core/modules/impl/handler/WasmHandlerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export class WasmHandlerApi<State> extends AbstractContractHandler<State> {
}
}

evolveState() {
if ('evolveState' in this.wasmExports) {
return this.wasmExports.evolveState();
} else {
return this.doGetCurrentState();
}
}

private async doHandleLegacy(action: ContractInteraction<unknown>): Promise<any> {
// pre- warp_contract macro contracts
const handleResult = await this.wasmExports.handle(action.input);
Expand Down

0 comments on commit c94cf64

Please sign in to comment.