From 856c29631e18042d8b70d732ab0643e1279f43ec Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 14:01:10 -0700 Subject: [PATCH 1/9] feat(actions-and-reducer.mdx): update reducer api snippet --- .../feature-overview/actions-and-reducer.mdx | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index 97fdba7ae..2dddf8845 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -100,16 +100,21 @@ A zkApp can retrieve events and actions from one or more Mina archive nodes. If ### Reducer - API reference ```ts -reducer = Reducer({ actionType: AsFieldElements }); - -this.reducer.dispatch(action: A): void; - -this.reducer.reduce( - actions: A[][], - stateType: AsFieldElements, - reduce: (state: S, action: A) => S, - initial: { state: S, actionState: Field } -): { state: S, actionState: Field }; +reducer = Reducer({ actionType: FlexibleProvablePure }); + +this.reducer.dispatch(action: Action): void; + +this.reducer.reduce( + actions: MerkleList>, + stateType: Provable, + reduce: (state: State, action: Action) => State, + initial: State, + options?: { + maxUpdatesWithActions?: number; + maxActionsPerUpdate?: number; + skipActionStatePrecondition?: boolean; +} +): State; Reducer.initialActionState: Field; ``` From e9e4fb5012d2f9a2016f8d27b294229069785e6c Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 14:11:01 -0700 Subject: [PATCH 2/9] feat(actions-and-reducer.mdx): update getActions --- .../feature-overview/actions-and-reducer.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index 2dddf8845..fff31b106 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -116,13 +116,13 @@ this.reducer.reduce( } ): State; -Reducer.initialActionState: Field; + ``` -In the near future, we want to add a function to retrieve actions from an archive node: +This function to retrieves a list of previoulsy emmited actions from an archive node: ```ts -this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): A[][]; +this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): MerkleList>; ``` Use `getActions` for testing with a simulated `LocalBlockchain`. See [Testing zkApps Locally](/zkapps/writing-a-zkapp/introduction-to-zkapps/testing-zkapps-locally). From 0803a62efdce874070c1de7f13fcfe12bd33816f Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 14:19:32 -0700 Subject: [PATCH 3/9] feat(actions-and-reducer.mdx): add section on action state --- .../feature-overview/actions-and-reducer.mdx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index fff31b106..52fd2bfa3 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -119,11 +119,18 @@ this.reducer.reduce( ``` -This function to retrieves a list of previoulsy emmited actions from an archive node: +The `getActions` function retrieves a list of previoulsy emmited actions from an archive node: ```ts -this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): MerkleList>; +let pendingActions = this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): MerkleList>; ``` +The final action state can be accessed on `pendingActions.hash`. + +```ts +let endActionState = pendingActions.hash; +``` +If the optional `endActionState` parameter is provided, the list of actions will be fetched up to that state. +In that case, `pendingActions.hash` is guaranteed to equal `endActionState`. Use `getActions` for testing with a simulated `LocalBlockchain`. See [Testing zkApps Locally](/zkapps/writing-a-zkapp/introduction-to-zkapps/testing-zkapps-locally). From 39c535394dc7e0b68f7ba8f4152065f088675a3b Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 14:47:57 -0700 Subject: [PATCH 4/9] feat(actions-and-reducer.mdx): update example reduce --- .../feature-overview/actions-and-reducer.mdx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index 52fd2bfa3..e8202b331 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -73,17 +73,16 @@ let stateType = Bool; // example actions data let actions = [[Field(1000)], [Field(2)], [Field(100)]]; -// state and actionState before applying actions +// state before applying actions let initial = { state: Bool(false), - actionState: Reducer.initialActionState, }; -let { state, actionState } = this.reducer.reduce( +let newState = this.reducer.reduce( actions, stateType, (state: Bool, action: Field) => state.or(action.equals(1000)), - initial + initial: ); ``` From 6f1873de1628beaff110d8643d911d4af1e7c8b3 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 15:33:55 -0700 Subject: [PATCH 5/9] feat(actions-and-reducers.mdx): remove actionState from return of reduce --- .../writing-a-zkapp/feature-overview/actions-and-reducer.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index e8202b331..3d7551b4c 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -88,7 +88,7 @@ let newState = this.reducer.reduce( The `acc` shown earlier is now `state`; you must pass in the state's type as a parameter and pass in an `actionState` which refers to one particular point in the action's history. -Like `Array.reduce`, `Reducer.reduce` takes a callback that has the signature `(state: S, action: A) => S`, where `S` is the `stateType` and `A` is the `actionType`. It returns the result of applying all the actions, in order, to the initial `state`. In this example, the returned `state` is `Bool(true)` because one of the actions in the list is `Field(1000)`. Reduce also returns the new actionState -- so you can store it to use when you reduce the next batch of actions. One last difference to JavaScript `reduce` is that it takes a _list of lists_ of actions, instead of a flat list. Each of the sublists are the actions that were dispatched in one account update (for example, while running one smart contract method). +Like `Array.reduce`, `Reducer.reduce` takes a callback that has the signature `(state: State, action: Action) => State`, where `State` is the `stateType` and `Action` is the `actionType`. It returns the result of applying all the actions, in order, to the initial `state`. In this example, the returned `state` is `Bool(true)` because one of the actions in the list is `Field(1000)`. One last difference to JavaScript `reduce` is that it takes a _list of lists_ of actions, instead of a flat list. Each of the sublists are the actions that were dispatched in one account update (for example, while running one smart contract method). As an astute reader, you might have noticed that this use of `state` is eerily similar to a standard "Elm architecture" that scans over an implicit infinite stream of actions (though here they are aggregated in chunks). This problem is familiar to web developers through its instantiation by using the Redux library or by using the `useReducer` hook in React. From b9408ed90235eb0a614ddcdc646897e486a5fc90 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Mon, 24 Jun 2024 15:41:47 -0700 Subject: [PATCH 6/9] fix(actions-and-reducers.mdx): remove extra colon in code snippet --- .../writing-a-zkapp/feature-overview/actions-and-reducer.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index 3d7551b4c..06e7aa460 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -82,7 +82,7 @@ let newState = this.reducer.reduce( actions, stateType, (state: Bool, action: Field) => state.or(action.equals(1000)), - initial: + initial ); ``` From 56c509e6982e2de933c2db4f336060b7f14fb254 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 26 Jun 2024 14:23:42 -0600 Subject: [PATCH 7/9] fix(actions-and-reducers.mdx): fix spelling error --- .../writing-a-zkapp/feature-overview/actions-and-reducer.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index 06e7aa460..a109d6455 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -118,7 +118,7 @@ this.reducer.reduce( ``` -The `getActions` function retrieves a list of previoulsy emmited actions from an archive node: +The `getActions` function retrieves a list of previously emitted actions from an archive node: ```ts let pendingActions = this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): MerkleList>; From 767bbc702b706335ae111878c69e8bfbf3fe80df Mon Sep 17 00:00:00 2001 From: ymekuria Date: Wed, 26 Jun 2024 14:27:58 -0600 Subject: [PATCH 8/9] fix(actions-and-reducers.mdx): remove extra space --- .../writing-a-zkapp/feature-overview/actions-and-reducer.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index a109d6455..fe0046a18 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -82,7 +82,7 @@ let newState = this.reducer.reduce( actions, stateType, (state: Bool, action: Field) => state.or(action.equals(1000)), - initial + initial ); ``` From 9cd2fcf1a3ab42c1dbd972e7b3da1eb964735016 Mon Sep 17 00:00:00 2001 From: ymekuria Date: Thu, 27 Jun 2024 01:22:22 -0600 Subject: [PATCH 9/9] feat(actions-and-reducers.mdx): update getActions description --- .../writing-a-zkapp/feature-overview/actions-and-reducer.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx index fe0046a18..91390c34b 100644 --- a/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx +++ b/docs/zkapps/writing-a-zkapp/feature-overview/actions-and-reducer.mdx @@ -118,7 +118,7 @@ this.reducer.reduce( ``` -The `getActions` function retrieves a list of previously emitted actions from an archive node: +The `getActions` function retrieves a list of previously emitted actions: ```ts let pendingActions = this.reducer.getActions({ fromActionState?: Field, endActionState?: Field }): MerkleList>;