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: add VerifyForwards and VerifyBackwards #212

Draft
wants to merge 1 commit into
base: backward-sync
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ func TestVerify(t *testing.T) {
})
}
}

func TestVerifyForwards(t *testing.T) {
}

func TestVerifyBackwards(t *testing.T) {
}
46 changes: 44 additions & 2 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,48 @@ import (
// NOTE: Compared against subjective head which is guaranteed to be non-expired
const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers

// VerifyForwards verifies untrusted header against trusted following general header checks and
// custom user-specific checks defined in Header.Verify.
func VerifyForwards[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
// general mandatory verification
err := verify(trstd, untrstd, heightThreshold)
if err != nil {
return &VerifyError{Reason: err}
}

// TODO: trstd.Height < untrstd.Height

// user defined verification
err = trstd.Verify(untrstd)
if err == nil {
return nil
}

// TODO: process error.
return nil
}

// VerifyBackwards verifies untrusted header against trusted following general header checks and
// custom user-specific checks defined in Header.Verify.
func VerifyBackwards[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
// general mandatory verification
err := verify(trstd, untrstd, heightThreshold)
if err != nil {
return &VerifyError{Reason: err}
}

// TODO: trstd.Height > untrstd.Height

// user defined verification
err = trstd.Verify(untrstd)
if err == nil {
return nil
}

// TODO: process error.
return nil
}

// Verify verifies untrusted Header against trusted following general Header checks and
// custom user-specific checks defined in Header.Verify.
//
Expand All @@ -18,7 +60,7 @@ const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers
// Always returns VerifyError.
func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
// general mandatory verification
err := verify[H](trstd, untrstd, heightThreshold)
err := verify(trstd, untrstd, heightThreshold)
if err != nil {
return &VerifyError{Reason: err}
}
Expand Down Expand Up @@ -75,14 +117,14 @@ func verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
if known {
return fmt.Errorf("%w: '%d' <= current '%d'", ErrKnownHeader, untrstd.Height(), trstd.Height())
}

// reject headers with height too far from the future
// this is essential for headers failed non-adjacent verification
// yet taken as sync target
adequateHeight := untrstd.Height()-trstd.Height() < heightThreshold
if !adequateHeight {
return fmt.Errorf("%w: '%d' - current '%d' >= threshold '%d'", ErrHeightFromFuture, untrstd.Height(), trstd.Height(), heightThreshold)
}

return nil
}

Expand Down
Loading