Recently, I did go deep into the code of reorganisation in blockchain, but I'm confused by the mrand.Float64() in WriteBlockWithState. Here is the code: original code position

// If the total difficulty is higher than our known, add it to the canonical chain
// Second clause in the if statement reduces the vulnerability to selfish mining.
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
reorg := externTd.Cmp(localTd) > 0
currentBlock = bc.CurrentBlock()
if !reorg && externTd.Cmp(localTd) == 0 {
    // Split same-difficulty blocks by number, then at random
    reorg = block.NumberU64() < currentBlock.NumberU64() || (block.NumberU64() == currentBlock.NumberU64() && mrand.Float64() < 0.5)

I know when system receive a block getting the same total difficult and same height, it will use mrand.Float64() to get a "random" number between 0-1 to decide whether reorg or not. My question is how will it ensure different geth client will get the same result by this? I mean, the geth client can start at different time and may execute mrand.Float64() different times, hence get different result and different chain. Am I missing something? Please help!

  • An answer here states that ties are resolved randomly ethereum.stackexchange.com/a/13750. If you have two blocks with the same difficulty you can choose either to be fair with both miners it is better to let the network decide with the next block. – Ismael Jul 18 '18 at 15:27
  • Thanks for your response first. I check the link, but the reason why the random number can get the same result is not clarity; Considering this situation: A and B on block height 50 get a tie and hence execute the mrand.Float64(), on block height 150 B stoped and restart from last known head block(after 50 block) while A not. Then on block height 200, they receive a tie and have to execute mrand.Float64() one more time. but this is the second time A excute and first time B excute becasue of restart. So may get different result. Is this correct? – Manxiaqu Jul 19 '18 at 2:12
  • Yes, every node will choose a different random number, in theory half of the network will choose one value and the other half the other (the exact proportion is 50% because of the delay in network propagation). The ties do not last very long if nodes A and B separate at block 50, then after a few blocks one of them will be in the majority fork and the other will have to switch to the majority fork as soon as possible. – Ismael Jul 19 '18 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.