What is nonce in Ethereum? How does it prevent double spending?

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

  2. 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).

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

i)Nounce value is related to an address right?So two address can have same nounce at the same time i.e account with Address A can have latest nounce 2 similarly account with address B can also have latest nounce 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 nounce 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 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.

  • 说到底,eth转账速度还是慢
  • 慢的原因一个是hash碰撞
  • 另外一个是nounce的原因
  • 目前eth出块速度一分钟5批,一批两百个左右

你可能感兴趣的:(What is nonce in Ethereum? How does it prevent double spending?)