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!
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 executemrand.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 02:12