Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small chain export optimization #4536

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions chain/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,11 +1312,13 @@ func (cs *ChainStore) WalkSnapshot(ctx context.Context, ts *types.TipSet, inclRe

var cids []cid.Cid
if !skipOldMsgs || b.Height > ts.Height()-inclRecentRoots {
mcids, err := recurseLinks(cs.bs, walked, b.Messages, []cid.Cid{b.Messages})
if err != nil {
return xerrors.Errorf("recursing messages failed: %w", err)
if walked.Visit(b.Messages) {
mcids, err := recurseLinks(cs.bs, walked, b.Messages, []cid.Cid{b.Messages})
if err != nil {
return xerrors.Errorf("recursing messages failed: %w", err)
}
cids = mcids
}
cids = mcids
}

if b.Height > 0 {
Expand All @@ -1331,12 +1333,14 @@ func (cs *ChainStore) WalkSnapshot(ctx context.Context, ts *types.TipSet, inclRe
out := cids

if b.Height == 0 || b.Height > ts.Height()-inclRecentRoots {
cids, err := recurseLinks(cs.bs, walked, b.ParentStateRoot, []cid.Cid{b.ParentStateRoot})
if err != nil {
return xerrors.Errorf("recursing genesis state failed: %w", err)
}
if walked.Visit(b.ParentStateRoot) {
cids, err := recurseLinks(cs.bs, walked, b.ParentStateRoot, []cid.Cid{b.ParentStateRoot})
if err != nil {
return xerrors.Errorf("recursing genesis state failed: %w", err)
}

out = append(out, cids...)
out = append(out, cids...)
}
}

for _, c := range out {
Expand Down
58 changes: 58 additions & 0 deletions chain/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/blockstore"
Expand Down Expand Up @@ -107,3 +108,60 @@ func TestChainExportImport(t *testing.T) {
t.Fatal("imported chain differed from exported chain")
}
}

func TestChainExportImportFull(t *testing.T) {
cg, err := gen.NewGenerator()
if err != nil {
t.Fatal(err)
}

var last *types.TipSet
for i := 0; i < 100; i++ {
ts, err := cg.NextTipSet()
if err != nil {
t.Fatal(err)
}

last = ts.TipSet.TipSet()
}

buf := new(bytes.Buffer)
if err := cg.ChainStore().Export(context.TODO(), last, 100, false, buf); err != nil {
t.Fatal(err)
}

nbs := blockstore.NewTemporary()
cs := store.NewChainStore(nbs, datastore.NewMapDatastore(), nil, nil)
root, err := cs.Import(buf)
if err != nil {
t.Fatal(err)
}

err = cs.SetHead(last)
if err != nil {
t.Fatal(err)
}

if !root.Equals(last) {
t.Fatal("imported chain differed from exported chain")
}

sm := stmgr.NewStateManager(cs)
for i := 0; i < 100; i++ {
ts, err := cs.GetTipsetByHeight(context.TODO(), abi.ChainEpoch(i), nil, false)
if err != nil {
t.Fatal(err)
}

st, err := sm.ParentState(ts)
if err != nil {
t.Fatal(err)
}

// touches a bunch of actors
_, err = sm.GetCirculatingSupply(context.TODO(), abi.ChainEpoch(i), st)
if err != nil {
t.Fatal(err)
}
}
}