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
3 changes: 2 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Header[H any] interface {
// New creates new instance of a header.
// It exists to overcome limitation of Go's type system.
// See:
// https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#pointer-method-example
//
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
//https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#pointer-method-example
New() H
// IsZero reports whether Header is a zero value of it's concrete type.
IsZero() bool
Expand Down
6 changes: 6 additions & 0 deletions headertest/dummy_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type DummyHeader struct {
// SoftFailure allows for testing scenarios where a header would fail
// verification with SoftFailure set to true
SoftFailure bool

// VerifyFn can be used to change header.Verify behaviour per header.
VerifyFn func(hdr *DummyHeader) error `json:"-"`
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}

func RandDummyHeader(t *testing.T) *DummyHeader {
Expand Down Expand Up @@ -100,6 +103,9 @@ func (d *DummyHeader) IsExpired(period time.Duration) bool {
}

func (d *DummyHeader) Verify(hdr *DummyHeader) error {
if d.VerifyFn != nil {
return d.VerifyFn(hdr)
}
if hdr.VerifyFailure {
return &header.VerifyError{Reason: ErrDummyVerify, SoftFailure: hdr.SoftFailure}
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (timeoutStore[H]) Append(ctx context.Context, _ ...H) error {
return ctx.Err()
}

func (timeoutStore[H]) GetRange(ctx context.Context, _ uint64, _ uint64) ([]H, error) {
func (timeoutStore[H]) GetRange(ctx context.Context, _, _ uint64) ([]H, error) {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
<-ctx.Done()
return nil, ctx.Err()
}
72 changes: 70 additions & 2 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sync
import (
"context"
"errors"
"fmt"
"time"

"github.com/celestiaorg/go-header"
Expand Down Expand Up @@ -173,7 +174,13 @@ func (s *Syncer[H]) verify(ctx context.Context, newHead H) (bool, error) {
}

var verErr *header.VerifyError
if errors.As(err, &verErr) && !verErr.SoftFailure {
if errors.As(err, &verErr) {
if verErr.SoftFailure {
err := s.verifySkipping(ctx, sbjHead, newHead)
var errValSet *NewValidatorSetCantBeTrustedError
return errors.As(err, &errValSet), err
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}

logF := log.Warnw
if errors.Is(err, header.ErrKnownHeader) {
logF = log.Debugw
Expand All @@ -186,7 +193,68 @@ func (s *Syncer[H]) verify(ctx context.Context, newHead H) (bool, error) {
"reason", verErr.Reason)
}

return verErr.SoftFailure, err
return false, err
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}

// verifySkipping will try to find such headers in range (subjHead, networkHeader)
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
// that can be verified by subjHead, literally:
//
// header.Verify(subjHead, candidate)
//
// and also such headers can verify `networkHeader`, literally
//
// header.Verify(candidate, networkHeader)
//
// When such candidates cannot be found [NewValidatorSetCantBeTrustedError] will be returned.
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 > 1 {
candidateHeight := subjHeight + diff/2

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

if err := header.Verify(subjHead, candidateHeader); err != nil {
// candidate failed, go deeper in 1st half.
diff = diff / 2
continue
}
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved

// 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(),
}
}

type NewValidatorSetCantBeTrustedError struct {
NetHeadHeight uint64
NetHeadHash []byte
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}

func (e *NewValidatorSetCantBeTrustedError) Error() string {
return fmt.Sprintf("sync: new validator set cant be trusted: head %d, attempted %x", e.NetHeadHeight, e.NetHeadHash)
}

// isExpired checks if header is expired against trusting period.
Expand Down
Loading
Loading