Skip to content

Commit bdf1d1c

Browse files
TerryhungTerry
andauthored
fix: run more backfill tipsetkey during import chain (#1222)
* backfill tipsetkey during import chain * Add option for init to control backfill range --------- Co-authored-by: Terry <[email protected]>
1 parent fd2168e commit bdf1d1c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

commands/init.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
)
1818

1919
var initFlags struct {
20-
repo string
21-
config string
22-
importSnapshot string
20+
repo string
21+
config string
22+
importSnapshot string
23+
backfillTipsetKeyRange int
2324
}
2425

2526
var InitCmd = &cli.Command{
@@ -45,6 +46,13 @@ var InitCmd = &cli.Command{
4546
EnvVars: []string{"LILY_SNAPSHOT"},
4647
Destination: &initFlags.importSnapshot,
4748
},
49+
&cli.IntFlag{
50+
Name: "backfill-tipsetkey-range",
51+
Usage: "Determine the extent of backfilling from the head.",
52+
EnvVars: []string{"LILY_BACKFILL_TIPSETKEY_RANGE"},
53+
Value: 3600,
54+
Destination: &initFlags.backfillTipsetKeyRange,
55+
},
4856
},
4957
Action: func(c *cli.Context) error {
5058
lotuslog.SetupLogLevels()
@@ -80,7 +88,7 @@ var InitCmd = &cli.Command{
8088
}
8189

8290
if initFlags.importSnapshot != "" {
83-
if err := util.ImportChain(ctx, r, initFlags.importSnapshot, true); err != nil {
91+
if err := util.ImportChain(ctx, r, initFlags.importSnapshot, true, initFlags.backfillTipsetKeyRange); err != nil {
8492
return err
8593
}
8694
}

commands/util/import.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/filecoin-project/lotus/storage/sealer/ffiwrapper"
2323
"github.com/mitchellh/go-homedir"
2424

25+
"github.com/filecoin-project/lotus/chain/types"
2526
"golang.org/x/xerrors"
2627
"gopkg.in/cheggaaa/pb.v1"
2728
)
@@ -92,7 +93,7 @@ func ImportFromFsFile(ctx context.Context, r repo.Repo, fs fs.File, snapshot boo
9293
return nil
9394
}
9495

95-
func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool) (err error) {
96+
func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool, backfillTipsetkeyRange int) (err error) {
9697
var rd io.Reader
9798
var l int64
9899
if strings.HasPrefix(fname, "http://") || strings.HasPrefix(fname, "https://") {
@@ -189,6 +190,13 @@ func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool)
189190
return fmt.Errorf("importing chain failed: %w", err)
190191
}
191192

193+
// The cst.Import function will only backfill 1800 epochs of tipsetkey,
194+
// Hence, the function is to backfill more epochs covered by the snapshot.
195+
err = backfillTipsetKey(ctx, ts, cst, backfillTipsetkeyRange)
196+
if err != nil {
197+
log.Errorf("backfill tipsetkey failed: %w", err)
198+
}
199+
192200
if err := cst.FlushValidationCache(ctx); err != nil {
193201
return fmt.Errorf("flushing validation cache failed: %w", err)
194202
}
@@ -222,3 +230,21 @@ func ImportChain(ctx context.Context, r repo.Repo, fname string, snapshot bool)
222230

223231
return nil
224232
}
233+
234+
func backfillTipsetKey(ctx context.Context, root *types.TipSet, cs *store.ChainStore, backfillRange int) (err error) {
235+
ts := root
236+
log.Infof("backfilling the tipsetkey into chainstore, attempt to backfill the last %v epochs starting from the head.", backfillRange)
237+
for i := 0; i < backfillRange; i++ {
238+
err = cs.PersistTipset(ctx, ts)
239+
if err != nil {
240+
return
241+
}
242+
parentTsKey := ts.Parents()
243+
ts, err = cs.LoadTipSet(ctx, parentTsKey)
244+
if ts == nil || err != nil {
245+
log.Infof("Only able to load the last %d tipsets", i)
246+
break
247+
}
248+
}
249+
return
250+
}

0 commit comments

Comments
 (0)