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: require non-zero headers for Verify #184

Merged
merged 3 commits into from
May 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
27 changes: 26 additions & 1 deletion headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import (
func TestVerify(t *testing.T) {
suite := NewTestSuite(t)
trusted := suite.GenDummyHeaders(1)[0]
var zero *DummyHeader

next := func() *DummyHeader {
next := *suite.NextHeader()
return &next
}

tests := []struct {
trusted *DummyHeader
prepare func() *DummyHeader
err error
soft bool
}{
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.VerifyFailure = true
Expand All @@ -34,6 +37,7 @@ func TestVerify(t *testing.T) {
err: ErrDummyVerify,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.VerifyFailure = true
Expand All @@ -43,17 +47,20 @@ func TestVerify(t *testing.T) {
soft: true, // soft because non-adjacent
},
{
trusted: trusted,
prepare: func() *DummyHeader {
return next()
},
},
{
trusted: trusted,
prepare: func() *DummyHeader {
return nil
},
err: header.ErrZeroHeader,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.Chainid = "gtmb"
Expand All @@ -62,6 +69,7 @@ func TestVerify(t *testing.T) {
err: header.ErrWrongChainID,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.Timestamp = untrusted.Timestamp.Truncate(time.Minute * 10)
Expand All @@ -70,6 +78,7 @@ func TestVerify(t *testing.T) {
err: header.ErrUnorderedTime,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.Timestamp = untrusted.Timestamp.Add(time.Minute)
Expand All @@ -78,6 +87,7 @@ func TestVerify(t *testing.T) {
err: header.ErrFromFuture,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.HeightI = trusted.Height()
Expand All @@ -86,18 +96,33 @@ func TestVerify(t *testing.T) {
err: header.ErrKnownHeader,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
untrusted := next()
untrusted.HeightI += 100000
return untrusted
},
err: header.ErrHeightFromFuture,
},
{
trusted: trusted,
prepare: func() *DummyHeader {
return zero
},
err: header.ErrZeroHeader,
},
{
trusted: zero,
prepare: func() *DummyHeader {
return next()
},
err: header.ErrZeroHeader,
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
err := header.Verify(trusted, test.prepare(), 0)
err := header.Verify(test.trusted, test.prepare(), 0)
if test.err != nil {
var verErr *header.VerifyError
assert.ErrorAs(t, err, &verErr)
Expand Down
6 changes: 5 additions & 1 deletion verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers

// Verify verifies untrusted Header against trusted following general Header checks and
// custom user-specific checks defined in Header.Verify
// custom user-specific checks defined in Header.Verify.
//
// Given headers must be non-zero
// If heightThreshold is zero, uses DefaultHeightThreshold.
// Always returns VerifyError.
func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
Expand Down Expand Up @@ -50,6 +51,9 @@ func verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error {
heightThreshold = DefaultHeightThreshold
}

if trstd.IsZero() {
return ErrZeroHeader
}
if untrstd.IsZero() {
return ErrZeroHeader
}
Expand Down
Loading