From b93a22c8b25aad14119286db7b5388f61d296117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 22 Apr 2020 09:10:06 +0300 Subject: [PATCH] core: fix nitpick to only retrieve head once --- core/headerchain.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/headerchain.go b/core/headerchain.go index e9c04c6546..a31b497f0d 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -149,7 +149,8 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er if ptd == nil { return NonStatTy, consensus.ErrUnknownAncestor } - localTd := hc.GetTd(hc.currentHeaderHash, hc.CurrentHeader().Number.Uint64()) + head := hc.CurrentHeader().Number.Uint64() + localTd := hc.GetTd(hc.currentHeaderHash, head) externTd := new(big.Int).Add(header.Difficulty, ptd) // Irrelevant of the canonical status, write the td and header to the database @@ -166,7 +167,7 @@ func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, er // 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() < hc.CurrentHeader().Number.Uint64()) || + (externTd.Cmp(localTd) == 0 && header.Number.Uint64() < head) || (externTd.Cmp(localTd) == 0 && mrand.Float64() < 0.5) { // If the header can be added into canonical chain, adjust the // header chain markers(canonical indexes and head header flag).