52

recently I got fascinated by blockchain, and started learning Ethereum. I came across the concept of the nonce in Ethereum.

After researching I found out that nonce is used for two purposes.

a. To keep transaction related to an account in order. i.e if there is a transaction with nonce 3 related to some account in the txpool and some transaction will nonce 4 arrived then that transaction won't be mined until and unless transaction with nonce 3 is mined.

I understand this.

But I have some question that I am not able to find?

i) nonce value is related to an address, right? So two address can have same nonce at the same time i.e. account with Address A can have the latest nonce 2 similarly account with address B can also have latest nonce 2.

ii) Do all nodes in the network will have same latest nonce value for all accounts available in the network?

That is, if there are three accounts and 2 miners then and nonce value for three accounts are 1,2,3 respectively, then both miner will have the same nonce.

Assumption: Both miners are in sync

b) how is nonce used to prevent a double spend?

3

2 Answers 2

66

What is nonce in Ethereum?

If you refer to the Glossary at Github Wiki, there are two types of nonce used in Ethereum.

  1. Account nonce - It's simply the transaction count of an account

Account nonce: a transaction counter in each account. This prevents replay attacks where a transaction sending eg. 20 coins from A to B can be replayed by B over and over to continually drain A's balance.

  1. Proof of work nonce - The random value in a block that was used get the proof of work satisfied (depending on the difficulty at the time).

Proof of work nonce: a meaningless value in a block which can be adjusted in order to try to satisfy the proof of work condition

Here you are referring to the account nonce, which is the transaction count from that account.

i)Nonce value is related to an address right? So two address can have same nonce at the same time i.e account with Address A can have latest nonce 2 similarly account with address B can also have latest nonce 2.

Yes as explained above, it's the transaction count and hence it's related to the account. As it's an incrementing number two accounts can have the same nonce at the same time.

ii)Does all node in the network will have same latest nonce value for all accounts available in the network?

When the nodes get synced with blockchain, eventually they will. Since a node accepts the longest valid chain there's no issue of all the nodes not getting updated with latest nonce value at the very moment a transaction is mined. Once synced, they will have the same nonce value for the account. (It's obvious because in a blockchain all the nodes are supposed to have the same block data. So after a certain amount of time you can be pretty much sure that the transaction is confirmed, hence the nonce value)

b)nonce is used to prevent double spend how?

Avoiding double spend means to stop using the same amount twice. If the nonce is set to the same only one of the transactions will be mined and most of the time that can be the one with higher gas price, but however no double spend will happen as only one transaction will take place at the end in either case.

Another trick avoided by nonce is using higher gas prices to get a transaction sent later be mined before an earlier one from the same account.

Consider a situation where A sends a transaction of X ETH to B as a payment and A's account has only that X ETH in his account. Once the transaction was sent to be mined A can send another transaction of X ETH with a higher gas price to one of his other account letting the first transaction sent to be mined later by getting a higher priority in the pending transaction queue. But since Ethereum takes nonce into consideration this is not allowed as the nonce of the later transaction is higher than the previous one.

You may refer this question as well.

5
  • 3
    upvoted, for mentioning PoW nonce. Commented Sep 30, 2017 at 1:44
  • 1
    Assuming the first transaction has been confirmed x number of times, right? Otherwise, what prevents me from broadcasting the doublespend with higher gas price, and with the same nonce?
    – Santa
    Commented Dec 23, 2017 at 17:25
  • Regarding: "Account nonce - It's simply the transaction count of an account" This is not quite true - the nonce is incremented every time a CREATE opcode is executed, since a single transaction may emit many CREATE instructions this number may be significantly higher than the number of transactions. Commented Nov 23, 2018 at 16:44
  • "Account nonce: a transaction counter in each account. This prevents replay attacks where a transaction sending eg. 20 coins from A to B can be replayed by B over and over to continually drain A's balance." this is what github glossary says. Do you have any source to refer? Commented Nov 28, 2018 at 8:08
  • If nonces did not exist, replay would be possible without front-running. No gas manipulation would be needed. The reason is that, for purposes of legitimately allowing the same amount to be sent twice from one account to another, an honest verifier would have to accept multiple transactions with the same from/to/amount tuple. As such, an adversary would simply be able to re-send the same transaction. The nonce asks the sender to sign a new message when the same amount is transferred to the same account, and thus the verifier can now safely drop transactions that are simply re-transmitted.
    – dionyziz
    Commented Mar 27, 2020 at 10:31
10

Firstly, it's 'nonce', not 'nounce'.

i) Yes, each address has a nonce. Addresses can have the same nonce, and often do.

ii) Yes, all nodes on the same blockchain have recorded the same nonce for each address, because the nonces are stored inside the blockchain.

b) The nonce is basically just a counter that keeps track of how many transactions an address has sent.

Suppose an address A has 1 ETH.

First, it tries to send 1 ETH to address B. Let's call this transaction A0 because it was signed by A, and it has nonce 0. Now, A tries to double spend this 1 ETH by sending another transaction of 1 ETH to C: let's call it transaction A1. A1 will be rejected immediately by all nodes, because every node can see that A1 must come after A0, and after A0 there isn't enough ETH in A to send A1.

So you can't double-spend with different nonces.

A might try a different strategy: to double spend with the same nonce. First A sends transaction A0 to B, and then it sends another transaction A0 to C. Both have the same nonce, and the same source, but a different destination. Only one of the A0 transactions will be accepted into the blockchain, because all nodes can see that the nonce 0 was used twice by A, and this is not allowed.

So you also can't double-spend with the same nonce.

5
  • 1
    How does that work with multiple outstanding transactions, that are valid? Assume I have 2 ETH. When I sent 1ETH to A with nonce 0, and 1 ETH to B with nonce 1, and for some reason B1 transaction gets accepted first, does A0 get cancelled, because there's an accepted tx on the chain now with a higher nonce? Or would it still go through?
    – LOST
    Commented Nov 4, 2019 at 20:54
  • 1
    Also, how am I supposed to generate nonces in offline scenario. Say I have two devices, that keep internal but separate track of tx count. When signing a transaction, that is to be submitted some time later how can I ensure there is no nonce collision?
    – LOST
    Commented Nov 4, 2019 at 20:58
  • 3
    @LOST Transactions always get confirmed in order of nonce. Nonce 5 will always be confirmed after nonce 4. If not transaction with nonce 4 does not exist, nonce 5 will never be confirmed.
    – Jesbus
    Commented Nov 5, 2019 at 21:04
  • 2
    @LOST If there is no communication between the devices, and they must each be able to sign a transaction at any time for later broadcast, there is no way to prevent a nonce collision.
    – Jesbus
    Commented Nov 5, 2019 at 21:10
  • 1
    @jesses-busman well, here goes a use case :(
    – LOST
    Commented Nov 5, 2019 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.