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

opt: syncer: optimize syncFork for one-epoch forks #6275

Merged
merged 1 commit into from
Feb 29, 2024
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
29 changes: 29 additions & 0 deletions pkg/chainsync/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ loop:
// A => D->E>F
// B-C(knownTip)
func (syncer *Syncer) syncFork(ctx context.Context, incoming *types.TipSet, known *types.TipSet) ([]*types.TipSet, error) {
incomingParentsTsk := incoming.Parents()
commonParent := false
for _, incomingParent := range incomingParentsTsk.Cids() {
if known.Contains(incomingParent) {
commonParent = true
}
}

if commonParent {
// known contains at least one of incoming's Parents => the common ancestor is known's Parents (incoming's Grandparents)
// in this case, we need to return {incoming.Parents()}
incomingParents, err := syncer.chainStore.GetTipSet(ctx, incomingParentsTsk)
if err != nil {
// fallback onto the network
tips, err := syncer.exchangeClient.GetBlocks(ctx, incoming.Parents(), 1)
if err != nil {
return nil, fmt.Errorf("failed to fetch incomingParents from the network: %w", err)
}

if len(tips) == 0 {
return nil, fmt.Errorf("network didn't return any tipsets")
}

incomingParents = tips[0]
}

return []*types.TipSet{incomingParents}, nil
}

// TODO: Does this mean we always ask for ForkLengthThreshold blocks from the network, even if we just need, like, 2?
// Would it not be better to ask in smaller chunks, given that an ~ForkLengthThreshold is very rare?
tips, err := syncer.exchangeClient.GetBlocks(ctx, incoming.Parents(), int(policy.ChainFinality))
Expand Down
9 changes: 9 additions & 0 deletions venus-shared/types/tipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func (ts *TipSet) Defined() bool {
return ts != nil && len(ts.blocks) > 0
}

func (ts *TipSet) Contains(oc cid.Cid) bool {
for _, c := range ts.cids {
if c == oc {
return true
}
}
return false
}

func (ts *TipSet) Equals(ots *TipSet) bool {
if ts == nil && ots == nil {
return true
Expand Down
Loading