From 12eed004aed1b9ac311f1f0787bea10dc1336349 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 10 Dec 2019 22:37:51 +0100 Subject: [PATCH] fix all panics from calling NewTipSet on an empty set of blocks --- chain/sync.go | 6 ++++++ chain/types/tipset.go | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/chain/sync.go b/chain/sync.go index e594749e9..b65341825 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -941,6 +941,12 @@ func (syncer *Syncer) syncFork(ctx context.Context, from *types.TipSet, to *type } for cur := 0; cur < len(tips); { + if nts.Height() == 0 { + if !syncer.Genesis.Equals(nts) { + return nil, xerrors.Errorf("somehow synced chain that linked back to a different genesis (bad genesis: %s)", nts.Key()) + } + return nil, xerrors.Errorf("synced chain forked at genesis, refusing to sync") + } if nts.Equals(tips[cur]) { return tips[:cur+1], nil diff --git a/chain/types/tipset.go b/chain/types/tipset.go index 1ed15289e..e8d830bc5 100644 --- a/chain/types/tipset.go +++ b/chain/types/tipset.go @@ -10,6 +10,7 @@ import ( "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log" cbg "github.com/whyrusleeping/cbor-gen" + "golang.org/x/xerrors" ) var log = logging.Logger("types") @@ -95,6 +96,10 @@ func tipsetSortFunc(blks []*BlockHeader) func(i, j int) bool { } func NewTipSet(blks []*BlockHeader) (*TipSet, error) { + if len(blks) == 0 { + return nil, xerrors.Errorf("NewTipSet called with zero length array of blocks") + } + sort.Slice(blks, tipsetSortFunc(blks)) var ts TipSet