From a9164e3e14e8157d147cea07122346e39d97f52e Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 27 May 2024 10:50:20 +0200 Subject: [PATCH 1/3] docs: document trusted header in Verify --- verify.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/verify.go b/verify.go index 0c1f3e9e..fe0c0568 100644 --- a/verify.go +++ b/verify.go @@ -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. // +// If trusted header is zero, no error is returned. // If heightThreshold is zero, uses DefaultHeightThreshold. // Always returns VerifyError. func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { From c7047112595619a4f9515fc52ee91a482ce8504d Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 27 May 2024 14:47:36 +0200 Subject: [PATCH 2/3] stricted verification --- headertest/verify_test.go | 27 ++++++++++++++++++++++++++- verify.go | 5 ++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/headertest/verify_test.go b/headertest/verify_test.go index 2774c056..35456991 100644 --- a/headertest/verify_test.go +++ b/headertest/verify_test.go @@ -14,6 +14,7 @@ import ( func TestVerify(t *testing.T) { suite := NewTestSuite(t) trusted := suite.GenDummyHeaders(1)[0] + var zero *DummyHeader next := func() *DummyHeader { next := *suite.NextHeader() @@ -21,11 +22,13 @@ func TestVerify(t *testing.T) { } tests := []struct { + trusted *DummyHeader prepare func() *DummyHeader err error soft bool }{ { + trusted: trusted, prepare: func() *DummyHeader { untrusted := next() untrusted.VerifyFailure = true @@ -34,6 +37,7 @@ func TestVerify(t *testing.T) { err: ErrDummyVerify, }, { + trusted: trusted, prepare: func() *DummyHeader { untrusted := next() untrusted.VerifyFailure = true @@ -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" @@ -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) @@ -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) @@ -78,6 +87,7 @@ func TestVerify(t *testing.T) { err: header.ErrFromFuture, }, { + trusted: trusted, prepare: func() *DummyHeader { untrusted := next() untrusted.HeightI = trusted.Height() @@ -86,6 +96,7 @@ func TestVerify(t *testing.T) { err: header.ErrKnownHeader, }, { + trusted: trusted, prepare: func() *DummyHeader { untrusted := next() untrusted.HeightI += 100000 @@ -93,11 +104,25 @@ func TestVerify(t *testing.T) { }, 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) diff --git a/verify.go b/verify.go index fe0c0568..d0bec2fb 100644 --- a/verify.go +++ b/verify.go @@ -13,7 +13,7 @@ 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. // -// If trusted header is zero, no error is returned. +// If trusted or untrstd header is zero, error is returned. // If heightThreshold is zero, uses DefaultHeightThreshold. // Always returns VerifyError. func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { @@ -51,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 } From 95c0c18e5dda57cd50be762bd1077667c1d36c21 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Mon, 27 May 2024 15:15:51 +0200 Subject: [PATCH 3/3] Update verify.go Co-authored-by: Hlib Kanunnikov --- verify.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/verify.go b/verify.go index d0bec2fb..72888ef8 100644 --- a/verify.go +++ b/verify.go @@ -13,7 +13,7 @@ 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. // -// If trusted or untrstd header is zero, error is returned. +// 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 {