This repository provides a minimal example demonstrating the use of the BatchReducer functionality in the Mina Protocol, as presented in o1-labs/o1js PR #1676. The example implements a simple counter smart contract that accumulates a running total of values submitted in batches using Mina’s BatchReducer. This example also includes tests to verify the correct behavior of the counter and batch reducer.
-
Clone the repository:
git clone https://github.com/your-username/mina-batch-reducer-counter.git cd mina-batch-reducer-counter -
Install dependencies:
npm install
The Add smart contract maintains a running total of values added through batch processing.
-
State Variables:
totalSum: Stores the accumulated total.actionStateandactionStack: Used by the batch reducer to maintain the state and action stack for batching.
-
Methods:
add(value: Field): Dispatches an action to add a non-negative value to the total.batchReduce(batch: Batch, proof: BatchProof): Processes a batch of actions, updatingtotalSumby adding values from the batch.
The test file verifies the correct behavior of the Add contract:
- Deploys the contract locally on a Mina test network.
- Dispatches multiple actions and then reduces them in batches.
- Asserts that
totalSumreflects only the values processed through the batch reducer. - Confirms that the actions are processed only once by checking
totalSumafter multiple reductions.
To run the tests, use the following command:
npm testThe tests will:
- Deploy the
Addcontract and verify the initial state. - Dispatch actions and perform batch reduction.
- Verify that
totalSumis updated as expected after each batch reduction.