From 044f680a048a236aa2852c437432c058adf32bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 22 Apr 2020 09:14:14 +0300 Subject: [PATCH] core: don't reorg if received chain is longer at same diff --- core/headerchain.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/headerchain.go b/core/headerchain.go index a31b497f0d..a6028d8b9a 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -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 // Second clause in the if statement reduces the vulnerability to selfish mining. // Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf - if externTd.Cmp(localTd) > 0 || - (externTd.Cmp(localTd) == 0 && header.Number.Uint64() < head) || - (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { + reorg := externTd.Cmp(localTd) > 0 + if !reorg && externTd.Cmp(localTd) == 0 { + 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 // header chain markers(canonical indexes and head header flag). //