From fd2f903e28b0a03b2468442c0352af74cd8c5e98 Mon Sep 17 00:00:00 2001 From: Martin Ondejka Date: Wed, 9 Aug 2023 13:26:57 +0200 Subject: [PATCH] Add actionState to the wrapping request --- src/ZekoBridge.test.ts | 6 ++++++ src/ZekoBridge.ts | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ZekoBridge.test.ts b/src/ZekoBridge.test.ts index 81218ace2b..53fdc94a29 100644 --- a/src/ZekoBridge.test.ts +++ b/src/ZekoBridge.test.ts @@ -160,6 +160,8 @@ describe('ZekoBridge', () => { requests.push( new WrappingRequest({ id: counter, + actionState: + requests.at(-1)?.getNextActionState() ?? Reducer.initialActionState, ...action, }) ); @@ -328,6 +330,8 @@ describe('ZekoBridge', () => { requests.push( new WrappingRequest({ id: counter, + actionState: + requests.at(-1)?.getNextActionState() ?? Reducer.initialActionState, ...action, }) ); @@ -428,6 +432,8 @@ describe('ZekoBridge', () => { requests.push( new WrappingRequest({ id: counter, + actionState: + requests.at(-1)?.getNextActionState() ?? Reducer.initialActionState, ...action, }) ); diff --git a/src/ZekoBridge.ts b/src/ZekoBridge.ts index fad99758bc..0a7f3544e7 100644 --- a/src/ZekoBridge.ts +++ b/src/ZekoBridge.ts @@ -27,6 +27,7 @@ export const TREE_CAPACITY = new MerkleTree(TREE_HEIGHT).leafCount; export class WrappingRequest extends Struct({ id: Field, + actionState: Field, amount: UInt64, tokenId: Field, receiver: PublicKey, @@ -39,6 +40,18 @@ export class WrappingRequest extends Struct({ return this.id.toBigInt() % TREE_CAPACITY; } + toAction() { + return new WrappingRequestAction({ + amount: this.amount, + tokenId: this.tokenId, + receiver: this.receiver, + }); + } + + getNextActionState() { + return updateActionsState(this.actionState, this.toAction().hash()); + } + static buildMerkleTree(requests: WrappingRequest[]): MerkleTree { const tree = new MerkleTree(TREE_HEIGHT); @@ -85,6 +98,7 @@ export class WrappingRequestAction extends Struct({ const witnesses = actions.map((action) => { const request = new WrappingRequest({ id: currentState.counter, + actionState: currentState.actionState, amount: action.amount, tokenId: action.tokenId, receiver: action.receiver, @@ -145,7 +159,7 @@ export class WrappingRequestAction extends Struct({ proof = proof === undefined - ? await ActionsRecursiveReducer.first(publicInput, witnessesBatch) + ? await ActionsRecursiveReducer.init(publicInput, witnessesBatch) : await ActionsRecursiveReducer.step( publicInput, proof, @@ -205,6 +219,7 @@ export class ActionsBatch extends Struct({ ); const request = new WrappingRequest({ id: stateAux.counter, + actionState: stateAux.actionState, amount: action.amount, tokenId: action.tokenId, receiver: action.receiver, @@ -265,7 +280,7 @@ export const ActionsRecursiveReducer = Experimental.ZkProgram({ publicInput: ReducerPublicInput, methods: { - first: { + init: { privateInputs: [ActionsBatch], method(publicInput: ReducerPublicInput, batch: ActionsBatch) {