1 min read

What does nonce do in Ethereum?

Ethereum Blockchain Transactions Diagram

Ethereum is a decentralized network of nodes. When you send a transaction to the Ethereum blockchain, it gets sent into the mempool until some miner mines it and includes it in a valid block.

Suppose you want to send two transactions transferring 3 ETH and 6 ETH respectively and want them to be processed sequentially. You would not wait after sending one transaction till it gets mined and included in a block. Instead, you would send out both transactions one after the other.

The nonce is one of the most critical and least understood components of an ethereum transaction.

An ethereum transaction is composed of the following components:

  • nonce - Number of confirmed transactions this account has previously sent
  • gasPrice - Price of gas (in wei) the originator is willing to pay for this transaction
  • to - Recipient of this transaction (smart contract or EOA)
  • value - How much ether this transaction is sending
  • data - Any binary data payload
  • v,r,s - Three components of ECDSA signature of originating account holder

As explained in the Ethereum Yellow Paper, a nonce is defined as:-      

A scalar value equal to the number of transactions sent from this address or, in the case of accounts with associated code, the number of contract-creations made by this account.

Without a nonce, it would be impossible for miners to know your intent to maintain the order of transactions.

However, with the nonce, if your first transaction (3 ETH) has nonce 0 (assuming it's a new account), then 6 ETH transactions will have nonce 1. Now, the transaction with 6 ETH would not be processed unless the previous transaction of 3 ETH (with lower nonce) is processed. Hence, maintaining the sequence of transactions.

If there are gaps in nonces, all subsequent transactions sit in the mempool, waiting for the gap to be filled.

For example: if the total transaction confirmation count for account X is 8 (nonce = 8), and a transaction with (nonce = 10) from account X is broadcasted to the network, it will sit in mempool till another transaction from account X with (nonce = 9) is broadcasted and mined. Thus, filling the gap.