diff --git a/chain/sync.go b/chain/sync.go index b83709387..68435a011 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -507,7 +507,7 @@ func (syncer *Syncer) collectHeaders(from *types.TipSet, to *types.TipSet) ([]*t log.Warn("syncing local: ", at) ts, err := syncer.store.LoadTipSet(at) if err != nil { - if err == bstore.ErrNotFound { + if xerrors.Is(err, bstore.ErrNotFound) { log.Info("tipset not found locally, starting sync: ", at) break } diff --git a/chain/sync_test.go b/chain/sync_test.go index 47b8f4ec3..8267b2389 100644 --- a/chain/sync_test.go +++ b/chain/sync_test.go @@ -283,11 +283,8 @@ func TestSyncMining(t *testing.T) { require.NoError(t, tu.mn.LinkAll()) tu.connect(client, 0) - fmt.Println("waiting for sync...") tu.waitUntilSync(0, client) - fmt.Println("after wait until sync") - //tu.checkHeight("client", client, H) tu.compareSourceState(client) diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index 4d7bf6f2f..2821de0b5 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -174,3 +174,7 @@ func PowerCmp(eproof ElectionProof, mpow, totpow BigInt) bool { hp := BigFromBytes(h[:]) return hp.LessThan(out) } + +func (t *Ticket) Equals(ot *Ticket) bool { + return bytes.Equal(t.VDFResult, ot.VDFResult) +} diff --git a/chain/types/tipset.go b/chain/types/tipset.go index 5484b476a..3337a823e 100644 --- a/chain/types/tipset.go +++ b/chain/types/tipset.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "sort" "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log" @@ -39,13 +40,33 @@ func (ts *TipSet) UnmarshalJSON(b []byte) error { return err } - ts.cids = ets.Cids - ts.blks = ets.Blocks - ts.height = ets.Height + ots, err := NewTipSet(ets.Blocks) + if err != nil { + return err + } + + *ts = *ots + return nil } +func tipsetSortFunc(blks []*BlockHeader) func(i, j int) bool { + return func(i, j int) bool { + ti := blks[i].LastTicket() + tj := blks[j].LastTicket() + + if ti.Equals(tj) { + log.Warn("blocks have same ticket") + return blks[i].Cid().KeyString() < blks[j].Cid().KeyString() + } + + return ti.Less(tj) + } +} + func NewTipSet(blks []*BlockHeader) (*TipSet, error) { + sort.Slice(blks, tipsetSortFunc(blks)) + var ts TipSet ts.cids = []cid.Cid{blks[0].Cid()} ts.blks = blks