Suppose in blockchain network we have 5 nodes and we have new transaction done by first node of network, so now rest of 4 node will mine the transaction to commit it in blockchain network, will the nonce be same for all node mining the same transaction?
1 Answer
A miner (honest) more or less,in high level, runs the following algorithm:
- Listens the network for transactions and blocks.
- Constructs a block that includes: a) all valid transactions b) a reference to the previous block (parent hash) c) new state.
- Search for a proof of work based on the constructed block in step 2.
- If she finds a block she broadcast it to the network.
- If another miner found a block she drops her previous work and continue the mining in the most recent block.
Proof-of-Work (high level):
H(state || txs || nonce || parent-blockid) < T
where T is the target.
To find a proof of work each miner runs the following algorithm (high level / pseudocode):
do {
nonce = rand();
} while (H(state || txs || nonce || parent-blockid) >= T);
return nonce;
So the nonce is totally random and that's what miners try to find: a nonce that satisfies a condition.
The first miner to find a block wins and all other honest miners drop their work and start mining on the new block (miners adopt the "heaviest" chain).
Previous answer (account nonce)
The nonce is part of the state of the account. The global state of Ethereum consists of all accounts. A change in the state of an account is a change in the global state. All nodes of the network have to agree on that state. The agreement to the new global state is being made with the use of a consensus algorithm such as Proof-of-Work (PoW). At the end all nodes will agree on the same nonce.
-
So how the nonce is decided? That this nonce will be same across network, mean all nodes are mining and calculating their own nonce and matching the hash(block data + nonce) with targetHash value whether it is less than or equal to targetHash value so there are chances that many nodes find such type of nonce that satisfy the condition Commented May 22, 2019 at 10:50
-
Oh sorry my mistake. I thought you meant the account nonce. I will update my answer accordingly.– LaxmanaCommented May 22, 2019 at 10:59
-
In this code do { nonce = rand(); } while (H(state || txs || nonce || parent-blockid) >= T); return nonce; your hash of H((state || txs || nonce || parent-blockid))) is grater than Target Hash but I read that is should be less than or equal to. Correct me if I am wrong Commented May 22, 2019 at 11:54
-
The do-while loop should run until a valid nonce is found. So the condition in the do-loop says if the H(...) is bigger than T continiue looping. If not stop and return the valid nonce.– LaxmanaCommented May 22, 2019 at 11:59
-
oh yes thank you, mean it will return the nonce which is less than or equal to target hash when it exit from loop Commented May 22, 2019 at 12:03