Transaction Nonce

Alexandre Ramos
Coinmonks
3 min readOct 11, 2022

--

Photo by GuerrillaBuzz Crypto PR on Unsplash

A nonce is one of the most important components of a transaction and it’s vital for an account-based protocol (like Ethereum).

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

(Ethereum Yellow Paper)

The nonce is an attribute of the originating address; It is calculated dynamically, by counting the number of confirmed transactions that have originated from an address.

Nonce is a zero-based counter, the first transaction is 0.

Let’s look at two scenarios to see the importance of the nonce in a transaction:

  1. This example shows why counting and including nonces in transactions in order of creation is important. Let’s say you need to make two transactions, one of 1 ether and another one of 2 ether. You sign and send the first transaction (1 ether) because it is more important and urgent and then you sign and send the second transaction (2 ether). But you remember that your wallet only has 2 ether and only one of those transactions will be accepted by the network.You can think that since you sent the 1 ether transaction first that transaction will be accepted and the second transaction will be rejected. But in a world of a decentralized system like Ethereum, nodes may receive transactions in any order; There’s no guarantee that the first transaction will be mined before the second transaction, these transactions could easily sended to different nodes. Without nonce, it would be random to which transaction will be accepted or rejected.However, with the nonce added, the first transaction you sent will have a nonce of,10 (for example purposes) while the second transaction will have the next nonce value (i.e., 11). That being included, if the second transaction gets to the node first, it will be ignored until the transactions with nonces from 0 to 10 have been processed, even if it’s received first.
  2. This second example shows how nonce can be a feature to protect transaction duplication. Let’s say you are transferring 2 ether to pay for a product, so you signed and send the transaction, then a node receives that transaction to be verified and included on the blockchain, but without a nonce this transaction can be easily duplicated and a person can “replay ” the transaction and send it multiple times until drain all you ether from your wallet (a nonce makes every single transaction unique). This is called the replay attack.

Conclusion

In summary, the nonce is an up-to-date count of the number of confirmed transactions that have originated from an account.

Your wallet is responsible for keeping track of nonces for each address it manages.

This was a quick and short explanation of what a nonce is and its importance.

There’s more to cover on this subject, especially if you are building your own wallet or some other application that originates transactions.

Thanks.

New to trading? Try crypto trading bots or copy trading

--

--

Alexandre Ramos
Coinmonks

Software Engineer, Blockchain Enthusiast & Crypto Investor