If not provided then the nonce will be read from account's state on the blockchain (it is part of the blockchain state).
However, it can sometimes be useful to specify it explicitly. It might be useful, for example, if you need to re-send a transaction with a higher gas price if the first one is not being mined. If you send it again with the same nonce it will (may) over-write the first one.
So, the nonce does appear in all transactions whether you specify it or not, and it does protect against replay.
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
balance.
- 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.
Best Answer
Summary
From:
address with nonces out of sequence.From:
address with nonces out of sequence by, for example:Details below.
What Happens When The Transaction Nonce Is Too Low?
I've created two fresh accounts on my private --dev network using
I have started a mining geth instance in a separate window, so the first account (coinbase) is getting deposited with more funds as new blocks are being mined.
In my main window I have started a geth instance attaching to the mining instance.
I send my first transaction from my first account to my second account
Here are the transaction details for the first transaction. A nonce of 0 has been automatically allocated for this transaction.
I send a second transaction from my first account to my second account, specifying a nonce of 0, and get the expected result of "Nonce too low".
What Happens When The Transaction Nonce Is Too High?
I now send a third transaction from my first account to my second account, specifying a nonce of 10000, and get a transaction hash back that the transaction has been sent to the transaction pool.
In the mining window, a message shows the transaction has been received. However the transaction is never mined.
I try to retrieve the transaction details for my third transactions. The blockHash and blockNumber remain null for ever.
I check the transaction pool status and I am assuming that the transaction with nonce 10000 is in the queue.
I try sending a fourth transaction with a nonce of 1. The transaction gets mined.
So I try sending a fifth transaction with a nonce of 3 (there is now a gap as the last valid nonce is 1). The transaction goes into the transaction pool queue and does not get mined.
I send a sixth transaction with a nonce of 2 (this fills in the gap between the last valid nonce of 1 and the queued transaction with a nonce of 3).
Both the transactions with the nonces of 2 and 3 now get mined.
I check the transaction pool status and the transaction with nonce 10000 is still in the queue, and will remain forever.
I shut down my mining geth instance and my attached geth instance, and restart both of them. I now check the transaction pool status and the transaction with nonce 10000 has disappeared.
Transaction Pool Source Code
Looking at core/tx_pool.go (#48-50), there is a maximum queue size of 64 transactions for transactions with out-of-order nonce sequence per sending address.
And core/tx_pool.go (#436-456) shows the code that removes transactions if the queue is too full:
Crash Testing Geth With Too High A Nonce
sudo swapoff -a
on Linux) and running other memory hogging programs.