From d8a517f9925a4a160338f8b005a7f6f365173c61 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Fri, 12 Jun 2020 00:47:57 -0400 Subject: [PATCH] Syncer::InformNewHead can quickly fail if height is impossibly large --- chain/sync.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/chain/sync.go b/chain/sync.go index 07d470d28..c5dde625c 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -50,6 +50,10 @@ import ( "github.com/filecoin-project/lotus/metrics" ) +// Blocks that are more than MaxHeightDrift epochs above +//the theoretical max height based on systime are quickly rejected +const MaxHeightDrift = 5 + var log = logging.Logger("chain") var LocalIncoming = "incoming" @@ -134,6 +138,11 @@ func (syncer *Syncer) InformNewHead(from peer.ID, fts *store.FullTipSet) bool { return false } + if syncer.IsEpochBeyondCurrMax(fts.TipSet().Height()) { + log.Errorf("Received block with impossibly large height %d", fts.TipSet().Height()) + return false + } + for _, b := range fts.Blocks { if reason, ok := syncer.bad.Has(b.Cid()); ok { log.Warnf("InformNewHead called on block marked as bad: %s (reason: %s)", b.Cid(), reason) @@ -1380,3 +1389,13 @@ func (syncer *Syncer) getLatestBeaconEntry(ctx context.Context, ts *types.TipSet return nil, xerrors.Errorf("found NO beacon entries in the 20 blocks prior to given tipset") } + +func (syncer *Syncer) IsEpochBeyondCurrMax(epoch abi.ChainEpoch) bool { + g, err := syncer.store.GetGenesis() + if err != nil { + return false + } + + now := uint64(time.Now().Unix()) + return epoch > (abi.ChainEpoch((now-g.Timestamp)/build.BlockDelay) + MaxHeightDrift) +}