Skip to content

Instantly share code, notes, and snippets.

@itoonx
Created April 22, 2018 20:30
Show Gist options
  • Save itoonx/06e9c6b7a91dd61af5d2ce9410d6b5c6 to your computer and use it in GitHub Desktop.
Save itoonx/06e9c6b7a91dd61af5d2ce9410d6b5c6 to your computer and use it in GitHub Desktop.
Ethereum Nonce
A nonce is just a sequential number tied to every transaction that represents the number of transactions the sender account has made on the network. You can think of it the same way as the auto-incrementing id field in a SQL database. It's a mechanism to ensure that the same transaction isn't submitted twice.
Example: You create a brand new Ethereum account. Your friend Alex sends you 5 ETH. This doesn't effect your account's nonce because you weren't the sender account. You then send 1 ETH to your friend John. The transaction that sends that 1 ETH needs to have the nonce of 0, because it's the first transaction your account is sending. Later that day, you send 2 ETH to your friend Bradley. That transaction will need to have a nonce of 1.
If you're going to set the nonce programmatically in a Dapp you can do it with web3 like this web3.eth.getTransactionCount(acct) because the number of transactions that an account has sent, will be the next nonce that it needs to use.
To proactively answer your next question.
What happens if I submit a transaction with a nonce that has already been transacted?
It will be rejected by the network
What happens if I submit a nonce that's higher than the correct next nonce to use?
The transaction will get stored in the mempool of the nodes in a pending state. It will remain in the mempool of the node until transactions with all nonces between the last valid incremental nonce for the account and the transaction stored in the mempool have been transacted to the network, and then the transaction will be executed as normal. It's possible that the nodes eventually drop your transaction out of the mempool if it's been waiting there for too long.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment