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

feat(sync): bifurcation for syncTarget #219

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
new algo
  • Loading branch information
cristaloleg committed Nov 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9b8a974a8db6513ecdc037425cd3d5bdd040673a
33 changes: 22 additions & 11 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
@@ -179,7 +179,7 @@ func (s *Syncer[H]) verify(ctx context.Context, newHead H) (bool, error) {
}

if verErr.SoftFailure {
err := s.verifySkipping(ctx, sbjHead.Height(), newHead)
err := s.verifySkipping(ctx, sbjHead, newHead)
return false, err
}

@@ -208,31 +208,42 @@ Client tries to apply height 1000 against 750 and if there is sufficient overlap
it applies 1000 as the subjective head.
If not, it downloads the halfway point and retries the process.
*/
func (s *Syncer[H]) verifySkipping(ctx context.Context, subjHeight uint64, networkHeader H) error {
func (s *Syncer[H]) verifySkipping(ctx context.Context, subjHead, networkHeader H) error {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
subjHeight := subjHead.Height()

cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
diff := networkHeader.Height() - subjHeight
if diff <= 0 {
panic(fmt.Sprintf("implementation bug: diff is %d", diff))
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}

for diff > 0 {
diff = diff / 2
subjHeight += diff
for diff > 1 {
candidateHeight := subjHeight + diff/2

subjHeader, err := s.getter.GetByHeight(ctx, subjHeight)
candidateHeader, err := s.getter.GetByHeight(ctx, candidateHeight)
if err != nil {
return err
}

_, _ = s.subjectiveHead(ctx)

if err := header.Verify(subjHeader, networkHeader); err == nil {
return nil
if err := header.Verify(subjHead, candidateHeader); err != nil {
// candidate failed, go deeper in 1st half.
diff = diff / 2
continue
}

if err := header.Verify(subjHeader, networkHeader); err == nil {
// candidate was validated properly, update subjHead.
subjHead = candidateHeader
// TODO: s.setSubjectiveHead(ctx, subjHead)
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved

if err := header.Verify(subjHead, networkHeader); err == nil {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
// network head validate properly, return success.
return nil
}

// new subjHead failed, go deeper in 2nd half.
subjHeight = subjHead.Height()
diff = networkHeader.Height() - subjHeight
}

return &NewValidatorSetCantBeTrustedError{
NetHeadHeight: networkHeader.Height(),
NetHeadHash: networkHeader.Hash(),