Skip to content

Commit

Permalink
feat: require non-zero headers for Verify (#184)
Browse files Browse the repository at this point in the history
* docs: document trusted header in Verify

* stricted verification

* Update verify.go

Co-authored-by: Hlib Kanunnikov <[email protected]>

---------

Co-authored-by: Hlib Kanunnikov <[email protected]>
  • Loading branch information
cristaloleg and Wondertan authored May 29, 2024
1 parent 1c1458b commit edbc71d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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

0 comments on commit edbc71d

Please sign in to comment.