Tutorial 10: Account Updates
The fundamental data structure that Mina transactions are built from is called an account update. Account updates are a flexible and powerful data structure that can express all kinds of updates, events, and preconditions you use to develop smart contracts.
The account updates limit has been increased from ~6 to 16 segments per transaction. See the Mesa upgrade overview for more details.
Each zkApp transaction constructed by o1js is composed of one or more AccountUpdate classes, which are a set of instructions for the Mina network to perform, such as altering on-chain state, emitting an event, and so on.
Each AccountUpdate can make assertions about its account, apply updates to its account, and make assertions about its child AccountUpdates.
Transactions are structured as a list of trees of AccountUpdates applied with a pre-order traversal.
Permissions, Preconditions, and Composability
Permissions, preconditions, composability, and tokens are the core features of zkApps that are implemented using AccountUpdates.
To learn more, see these o1js docs:
In this tutorial, you learn the essential account update features.
AccountUpdate contents
The AccountUpdate class is a set of instructions for the Mina network. It includes preconditions (conditions that must be true for the account update to be applied) and a list of state updates that need to be authorized by a signature or proof.
Each AccountUpdate class has these components:
-
PublicKey: The account address for the account update -
TokenId: A unique hash representing the custom token. Defaults to the MINA TokenId (1).Together,
PublicKeyandTokenIduniquely identify an account on the Mina network. -
Preconditions: Conditions that must be true for the account update to be applied. Corresponds to assertions in an o1js method. -
Updates: Things changed by the account update, such as including the zkApp state, permissions, and verification key. -
BalanceChange: Any changes to the balance -
Authorization: How the zkApp is authorized; must be a proof (corresponding to the verification key on the account), a signature, or none. See Authorizations.
Other AccountUpdate components are available to use, but are not covered in this tutorial:
MayUseToken: Whether the zkApp has permissions to manipulate its token.Layout: Allows for assertions about the structure of anAccountUpdate.
Account updates for a non-upgradable zkApp
If the verification key cannot be changed, the zkApp smart contract is considered non-upgradeable. The setVerificationKey permission sets the ability to change the verification key of the account.
Now, you can start building an example zkApp to explore permissions, preconditions, and composability with AccountUpdates.
Visualize transactions
To visualize transactions, use the mina-transaction-visualizer library. Install this library to use in your own zkApp:
npm install mina-transaction-visualizer --save
Smart Contracts
In this tutorial, you build two smart contracts:
ProofsOnlyZkApp: Non-upgradeable proof onlySecondaryZkApp: the other zkApp
The full source code for this tutorial is provided in the examples/zkapps/10-account-updates directory on GitHub.
Non-upgradeable proof only
The full example code is provided in the examples/zkapps/10-account-updates/src/ProofsOnlyZkApp.ts file.
Goal: Configure this zkApp to be modifiable only by using proofs.
For this example, the zkApp is not upgradable after it is deployed. This means that while the zkApp developer owns the private key to initially deploy the zkApp, after its first deployment, the zkApp requires proof authorization and consequently can be updated only transactions that fulfill the zkApp smart contract logic. The private key is no longer useful for anything.
This zkApp has methods that call other methods to let you explore the impacts to a transaction's account updates.
- Start by adding the main contents of the zkApp:
export class ProofsOnlyZkApp extends SmartContract {
@state(Field) num = State<Field>();
@state(Field) calls = State<Field>();
async deploy() {
await super.deploy();
this.account.permissions.set({
...Permissions.default(),
setDelegate: Permissions.proof(),
setPermissions: Permissions.proof(),
setVerificationKey: {
auth: Permissions.proof(),
txnVersion: TransactionVersion.current(),
},
setZkappUri: Permissions.proof(),
setTokenSymbol: Permissions.proof(),
incrementNonce: Permissions.proof(),
setVotingFor: Permissions.proof(),
setTiming: Permissions.proof(),
});
}
@method async init() {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertFalse();
super.init();
this.num.set(Field(1));
this.calls.set(Field(0));
}
...
This code configures the zkApp as described and initializes the zkApp with the values you want.
By asserting that provedState is false in init(), you ensure that init() cannot be called again after the zkApp is set up during the initial deployment. Without this assertion, your zkApp could be reset by anyone calling the init() method on your zkApp.
This assertion is a recommended best practice for most zkApps.
- Next, add two functions:
...
@method async add(incrementBy: Field) {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertTrue();
const num = this.num.getAndRequireEquals();
this.num.set(num.add(incrementBy));
await this.incrementCalls();
}
@method async incrementCalls() {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertTrue();
const calls = this.calls.getAndRequireEquals();
this.calls.set(calls.add(Field(1)));
}
...
These methods also assert provedState is true to ensure the zkApp was initialized as expected because provedState becomes true after init() is invoked.
This assertion is a recommended best practice for most zkApps.
The add() method calls the incrementCalls() method. You can see how this is reflected in the add() transaction's AccountUpdate structure.
- Finally, add one more function,
callSecondary(), that calls a different zkApp:
...
@method async callSecondary(secondaryAddr: PublicKey) {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertTrue();
const secondaryContract = new SecondaryZkApp(secondaryAddr);
const num = this.num.getAndRequireEquals();
await secondaryContract.add(num);
// NOTE this gets the state at the start of the transaction
this.num.set(secondaryContract.num.get());
await this.incrementCalls();
}
}
The callSecondary() method takes the address of the other zkApp, SecondaryZkApp, and calls a method on it. Note that the impact of calling that method occurs after this set of AccountUpdates—so when you call secondaryContract.num.get(), it gets the value before this transaction is applied.
Finally, look briefly at SecondaryZkApp.ts that contains:
export class SecondaryZkApp extends SmartContract {
@state(Field) num = State<Field>();
async deploy() {
await super.deploy();
this.account.permissions.set({
...Permissions.default(),
});
}
@method async init() {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertFalse();
super.init();
this.num.set(Field(12));
}
@method async add(incrementBy: Field) {
this.account.provedState.getAndRequireEquals();
this.account.provedState.get().assertTrue();
const num = this.num.getAndRequireEquals();
this.num.set(num.add(incrementBy));
}
}
You declare functions for initializing the account and the add() method that is called from the earlier ProofOnlyZkApp.