The nonce of Ethereum


What is nonce?

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

All transactions of Ethereum are based on account, which is different from bitcoin based on utxo. Therefore, each transaction needs to be recorded in order. The nonce value is in this order. Nonce is the attribute of the original address of the transaction. It is not stored on the Ethereum blockchain, but calculated by calculating the number of transactions sent from an address.

nonce +1

For each transaction initiated, nonce adds one. For the explanation of initiation:
1. Each transaction sent by external account (EOA);
2. Contract Wallet

The transfer in transaction, contract call and other contracts belong to internal call, so the nonce value remains unchanged.

Role 1: transaction order

Suppose you want to send two transactions with values of 1 and 4 Eth and want them to be packaged sequentially. After sending a transaction, you continue to send a second transaction.
Now, without random numbers, it’s impossible for miners to know your intention to maintain transaction order.
If the nonce of your first transaction (1 ETH) is 0 (assuming a new account), then the nonce of the 4 eth transaction is 1. The miner can pack the transaction in the order of nonce.

Function 2: prevent replay attack

If there is no nonce during transfer, the parameters are as follows

    " value":" 10000000000000000000“, // 10 ETH
    " data":"",
    "DSA, some of your signatures"

After the transaction is serialized, for example:


After the transaction is packaged, the other party will receive 10 eth, but anyone can see the transaction, copy and paste it, submit it to Ethereum’s network repeatedly, and exhaust your balance, which is called replay attack.

If a transaction contains nonce, the same nonce transaction can only be packaged once.

2. How to use nonce?

The following figure shows how the transaction is packaged,

The nonce of Ethereum

Inside Ethereum, there is a txpool, which is a pool for storing transactions. Transactions in wallets or nodes will be added to the transaction pool. When blocks are packaged, they will be extracted from the pool. After blocks are generated, consensus blocks and transactions will be linked. Therefore, the transaction will have a pending status or be discarded by the transaction pool.

When initiating a transfer or creating a contract, query the current nonce value from Ethereum network through Web3, use this value as the nonce value of the current transaction, and send it to Ethereum network.

Send transaction eth_ sendTransaction

Creates new message call transaction or a contract creation, if the data field contains code.

Need to pass parameter nonce, the official document for nonce

nonce: QUANTITY - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.

Get nonce eth_ getTransactionCount


params: [
   QUANTITY  // latest, pending


curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xf40629b5F96567270794F0F29E55Ac9daDE14fFd","earliest"],"id":1}'

The nonce of Ethereum


There are 5 transfer out transactions in the actual account, and the nonce of the last transaction is 4. Note that the nonce count starts at 0, so if you want to continue sending transactions, use 5 as the nonce for the next transaction.

If there is a transaction in the pending state, that is, a transaction that is not packaged by the miner, to read nonce, you need to change the parameter topendingOtherwise, the correct count will not be obtained. Transactions without block confirmations can be ‘cancelled’ or accelerated.

If the transaction is packaged, i.e. not pending, it is irreversible and cannot be cancelled!

3. Accelerate and cancel the transaction of Ethereum

Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce

Based on the characteristics of nonce, self increment and uniqueness, the same nonce can be used to re initiate transactions to achieve acceleration.

Accelerate Ethereum trading case

  1. Suppose there is a transaction a: gas price = 5, nonce = 1. Because gas price is too low, the transaction is always in the pending state after broadcasting.
  2. Create transaction B, adjust gas price to a more reasonable level, such as 10, 20, nonce = 1, and publish the transaction again. The miner will choose a higher price deal.
  3. At this time, in the miner’s transaction pool, transaction B is packaged. Miner will check transaction a and find that nonce already exists. If miner thinks that transaction a is unreasonable, miner will automatically discard the transaction

Cancel the deal

  1. Based on the case of accelerated trading.
  2. Create transaction B, set value = 0, payee = sender, set higher gas price, broadcast transaction
  3. The original deal is discarded, and the new deal will be packaged by the miners without losing any money. But you still have to pay for the B deal

4. Exception handling



  1. Pending pending pending transactions in the account;
  2. The new transaction is the same as the transaction nonce of pending;
  3. The gas price of the new transaction is lower and cannot replace the pending transaction

The nonce of Ethereum

5. Several rules used by nonce

  1. Transactions with too low a nonce get indirectly rejected when nonce is too small (less than the current nonce value);
  2. Transactions with too high a nonce get placed in the transaction pool queue;
  3. If transactions with nonces that fill the gap between the last valid nonce and the too high nonce are sent and the nonce sequence is complete, all the transactions in the sequence will get processed and minimized
  4. The transaction queue only stores up to 64 transactions sent from the same account. That is to say, if you want to transfer in batches, the same node should not send more than 64 transactions. The transaction pool queue will only hold a maximum of 64 transactions with the same From:address with nonces out of sequence.
  5. When the geth instances are shut down and started, transactions in the transaction pool queue disappear
  6. If the current nonce is appropriate, but the account balance is insufficient, it will be rejected by Ethereum;

6. References

  • Ehereum yellow paper
  • web3 api