What is nonce in Ethereum?
If you refer to the Glossary at Github Wiki, there are two types of nonce used in Ethereum.
- 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
- 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.