Skip to content

Commit

Permalink
Merge pull request #258 from filecoin-project/fix/sort-tipsets
Browse files Browse the repository at this point in the history
fix sync tests by sorting tipsets
  • Loading branch information
magik6k authored Oct 1, 2019
2 parents c34968a + 317e83a commit 6a72926
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions chain/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions chain/types/blockheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 24 additions & 3 deletions chain/types/tipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"sort"

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6a72926

Please sign in to comment.