Ethereum Transaction Nonces - What are they?
Tags: ethereum, transaction
Published: 2023-06-18 23:27:40

---
title: Ethereum Transaction Nonces - What are they?
published: true
date: 2023-05-28 17:00:00 UTC
tags: Ethereum,Transaction
canonical_url: https://byteatati.me/transaction-nonces
---

 This post was originally published **[on my blog!](https://byteatati.me/blog/transaction-nonces)**

* * *

If you’ve ever been on a blockchain explorer, you’ve probably seen a field named “nonce.” For example, this is how it looks on [Etherscan.io](https://etherscan.io/).

![A transaction with nonce 5](https://byteatati.me/images/posts/transaction-nonces/etherscan.png)
_This transaction has a nonce of 5._

What is this, exactly, and why is it used?

# What is a Nonce?

In the context of Ethereum, a nonce is a unique, sequential number associated with each transaction initiated by an account. In English, this is a counter that increases with each transaction. This counter is associated with an account, meaning that each address has its own nonce.

# Why are Nonces Used?

Nonces have two primary uses:

1. Executing transactions in the correct order
2. Preventing replay attacks

## Maintaining Transaction Order

The first and most important function of nonces is to maintain the order of transactions. Ethereum is a decentralized system, meaning there are many thousands of computers. If you submit multiple transactions close together, it is inevitable that the second will propagate to some nodes before the first. Without a nonce, the second transaction would be executed before the first, which could cause unintended consequences such as a fork.

This is a problem that occurs in Bitcoin. Because Bitcoin does not use nonces, it is possible for transactions to be executed out of order. This means that, if you send multiple transactions, the one that is executed first isn’t necessarily the first one sent.

If an Ethereum node receives a transaction with a nonce that is higher than the next expected nonce, it will wait until the missing transaction is received before executing the transaction. This ensures that transactions are executed in the correct order, even if they are received out of order. However, if you accidentally send a transaction with a nonce that is too large, perhaps from a bug in your code, it may result in a transaction that is executed in the future, when you’ve already forgotten about it.

## Preventing Replay Attacks

Another critical function of nonces is to prevent replay attacks. A replay attack occurs when an attacker intercepts a valid transaction and maliciously resubmits it, causing unintended consequences. By including a nonce in each transaction, Ethereum mitigates this risk.

Let’s say that Alice sends Bob 1 ETH. Without a nonce, Bob could propagate the exact same transaction to the network that was signed by Alice, and send multiple ETH to Bob. However, because the nonce is there, the network just ignores that the same transaction was submitted twice.

When a transaction is included in the Ethereum blockchain, the nonce associated with that transaction is recorded. Subsequent transactions from the same account must have nonces that are sequentially higher. If an attacker tries to replay a previous transaction, the nonce will not match the expected value, and the network will reject the replayed transaction as invalid. This ensures that each transaction can only be executed once, safeguarding the integrity of the Ethereum network.

# Nonce Calculation and Transaction Submission

The calculation of nonces is, in theory, pretty simple. You just need to know the nonce for the latest transaction for an account and increment it. This is usually pretty simple because you can just query the blockchain for the latest nonce.

However, in applications that use hot wallets, this can be a bit more complicated. For example, if you have a centralized exchange that needs to allow users to withdraw money, then you need to have a server to control the hot wallet. Let’s say that your exchange becomes a hit, and many users start using it. This creates a scalability problem, where you have to use multiple computers to sign transactions at the same time.

This is where the problem lies. When multiple computers work together, it’s difficult to sync the nonce. Take the following example scenario:

```
<!-- HTML_TAG_START -->Latest transaction nonce: 5
Computer A creates a transaction with nonce 6
Computer B creates a transaction with nonce 6 <-- It doesn't know that computer A is creating a transaction with the same nonce!
Computer A signs and submits the transaction <-- This will succeed, because it's the first transaction with nonce 6
Computer B signs and submits the transaction <-- This will fail because the nonce is already used!<!-- HTML_TAG_END -->
```

This is a problem that is difficult to solve. Most exchanges take either one of two approaches:

1. **Use a single computer to sign transactions** - Cannot scale, but easy to implement
2. **Use multiple hot wallets, one for each computer** - Can scale, but more complicated to implement, and more expensive to maintain

## Conclusion

While a transaction nonce might seem like an insignificant concept of Ethereum that’s usually abstracted, it’s actually a critical component of the network. Without nonces, Ethereum would be vulnerable to replay attacks, and transactions would be executed out of order. This would cause a lot of problems and would make Ethereum much less useful.

Not just that, nonces bring a whole new set of complications that are harder to solve than you might think. Mainly, using concurrency while signing transactions can quickly become a hot mess!