Nonces
In Ethereum, each account is associated with a unique identifier known as a nonce. For externally owned accounts (EOAs), the nonce fulfills three key functions: it prevents replay attacks on the network, ensures transactions are executed in the intended sequence, and acts as a unique identifier in the formula for deriving addresses. The nonce is incremented after each transaction is executed.
In the context of smart contracts, the nonce has a singular purpose: it determines
the address of a contract deployed from another contract. When a new contract is
created using the create
or create2
functions, the nonce is increased to signify
the deployment of a new contract. Unlike EOAs, which can only increment their nonce
by one per transaction, smart contracts have the ability to increase their nonce
multiple times within a single transaction.
Conversely, ZKsync features native account abstraction, allowing accounts to leverage the nonce for both replay attack protection and address derivation of created contracts. Given that accounts in ZKsync can be smart contracts, they may deploy several contracts in a single transaction.
In order to maintain the expected and convenient use of a nonce in both transaction validation and contract deployment contexts, ZKsync introduces two different nonces:
- Transaction nonce
- Deployment nonce
The transaction nonce is used for the transaction validation, while the deployment nonce is incremented in the event of contract deployment. This way, accounts may send many transactions by following only one nonce value and the contract may deploy many other contracts without conflicting with the transaction nonce.
There are also other minor differences between ZKsync and Ethereum nonce management:
- Newly created contracts begin with a deployment nonce value of zero. This contrasts with Ethereum, where, following the specifications of EIP-161, the nonce for newly created contracts starts at one.
- On ZKsync, the deployment nonce is incremented only if the deployment succeeds. On Ethereum nonce on deployment is updated even in case creation failed.