Skip to content

Commit

Permalink
Add new flag for modifying the confidence
Browse files Browse the repository at this point in the history
  • Loading branch information
Terryhung committed Oct 2, 2023
1 parent a6e1997 commit 3b489c5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions commands/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,16 @@ func SyncWait(ctx context.Context, lapi lily.LilyAPI, watch bool) error {
}

type syncOpts struct {
config string
storage string
config string
storage string
confidence int
}

var syncFlags syncOpts

type SyncingState struct {
UnsyncedBlockHeadersByEpoch map[int64][]*blocks.UnsyncedBlockHeader
Confidence int64
sync.Mutex
}

Expand All @@ -280,6 +282,13 @@ var SyncIncomingBlockCmd = &cli.Command{
Usage: "Specify the storage to use, if persisting the displayed output.",
Destination: &syncFlags.storage,
},
&cli.IntFlag{
Name: "confidence",
Usage: "Sets the size of the cache used to hold tipsets for possible reversion before being committed to the database.",
EnvVars: []string{"LILY_CONFIDENCE"},
Value: 2,
Destination: &syncFlags.confidence,
},
},
Action: func(cctx *cli.Context) error {
ctx := lotuscli.ReqContext(cctx)
Expand Down Expand Up @@ -317,6 +326,7 @@ var SyncIncomingBlockCmd = &cli.Command{

state := &SyncingState{
UnsyncedBlockHeadersByEpoch: make(map[int64][]*blocks.UnsyncedBlockHeader),
Confidence: int64(syncFlags.confidence),
}

go detectOrphanBlocks(ctx, lapi, state)
Expand All @@ -340,10 +350,10 @@ func detectOrphanBlocks(ctx context.Context, lapi lily.LilyAPI, state *SyncingSt
}

// Check old tipset
targetEpoch := latestEpoch - 5
targetEpoch := latestEpoch - state.Confidence
oldEpoches := []int64{}
for epoch, unsyncedBlocks := range state.UnsyncedBlockHeadersByEpoch {
if epoch <= int64(targetEpoch) {
if epoch <= targetEpoch {
// Store the old tipset, we should clear it after checking
oldEpoches = append(oldEpoches, epoch)

Expand All @@ -352,8 +362,9 @@ func detectOrphanBlocks(ctx context.Context, lapi lily.LilyAPI, state *SyncingSt
log.Errorf("Error at getting the old tipset: %v", err)
continue
}
log.Infof("Get header cids: %v at Height: %v", latestEpoch, oldTs.Cids(), oldTs.Height())
log.Infof("Get header cids: %v at Height: %v", oldTs.Cids(), oldTs.Height())

// Verify whether the unsynced block exists within the tipset or not.
cidMap := make(map[string]bool)
for _, cid := range oldTs.Cids() {
cidMap[cid.String()] = true
Expand Down

0 comments on commit 3b489c5

Please sign in to comment.