core: don't reorg if received chain is longer at same diff

This commit is contained in:
Péter Szilágyi 2020-04-22 09:14:14 +03:00
parent b93a22c8b2
commit 044f680a04
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D

View file

@ -166,9 +166,15 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er
// If the total difficulty is higher than our known, add it to the canonical chain // 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. // Second clause in the if statement reduces the vulnerability to selfish mining.
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
if externTd.Cmp(localTd) > 0 || reorg := externTd.Cmp(localTd) > 0
(externTd.Cmp(localTd) == 0 && header.Number.Uint64() < head) || if !reorg && externTd.Cmp(localTd) == 0 {
(externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { if header.Number.Uint64() < head {
reorg = true
} else if header.Number.Uint64() == head {
reorg = mrand.Float64() < 0.5
}
}
if reorg {
// If the header can be added into canonical chain, adjust the // If the header can be added into canonical chain, adjust the
// header chain markers(canonical indexes and head header flag). // header chain markers(canonical indexes and head header flag).
// //