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?

up vote 31 down vote accepted

What is nonce in Ethereum?

If you refer 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. And since 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 all the nodes getting updated at the very moment a transaction is sent, about the nonce of the particular account. So once synced, yes they have the same nonce value for a account. (It's obvious because in a blockchain all the nodes are supposed to have the same block data.)

b)nonce is used to prevent double spend how?

The 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.

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.

You may refer this question as well.

  • 2
    upvoted, for mentioning PoW nonce. – Prashant Prabhakar Singh Sep 30 '17 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 Dec 23 '17 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. – silasdavis Nov 23 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? – Achala Dissanayake Nov 28 at 8:08

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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