Skip to content

Commit

Permalink
fix(sync): enforce ForkLengthThreshold for synced chain (filecoin-pro…
Browse files Browse the repository at this point in the history
  • Loading branch information
schomatis authored and bibibong committed Feb 4, 2021
1 parent aebda6d commit f04e844
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion chain/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ func (syncer *Syncer) syncFork(ctx context.Context, incoming *types.TipSet, know
return nil, ErrForkCheckpoint
}

// TODO: Does this mean we always ask for ForkLengthThreshold blocks from the network, even if we just need, like, 2?
// TODO: Does this mean we always ask for ForkLengthThreshold blocks from the network, even if we just need, like, 2? Yes.
// Would it not be better to ask in smaller chunks, given that an ~ForkLengthThreshold is very rare?
tips, err := syncer.Exchange.GetBlocks(ctx, incoming.Parents(), int(build.ForkLengthThreshold))
if err != nil {
Expand All @@ -1443,6 +1443,10 @@ func (syncer *Syncer) syncFork(ctx context.Context, incoming *types.TipSet, know
if err != nil {
return nil, xerrors.Errorf("failed to load next local tipset: %w", err)
}
// Track the fork length on our side of the synced chain to enforce
// `ForkLengthThreshold`. Initialized to 1 because we already walked back
// one tipset from `known` (our synced head).
forkLengthInHead := 1

for cur := 0; cur < len(tips); {
if nts.Height() == 0 {
Expand All @@ -1459,6 +1463,13 @@ func (syncer *Syncer) syncFork(ctx context.Context, incoming *types.TipSet, know
if nts.Height() < tips[cur].Height() {
cur++
} else {
// Walk back one block in our synced chain to try to meet the fork's
// height.
forkLengthInHead++
if forkLengthInHead > int(build.ForkLengthThreshold) {
return nil, ErrForkTooLong
}

// We will be forking away from nts, check that it isn't checkpointed
if nts.Key() == chkpt {
return nil, ErrForkCheckpoint
Expand Down

0 comments on commit f04e844

Please sign in to comment.