From 2199e0a8a1f0d492d6e5476523b663cd6a99e0f3 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Fri, 14 Jan 2022 16:18:50 -0500 Subject: [PATCH] light: convert validation panics to errors (#7597) --- light/client_benchmark_test.go | 3 ++- light/verifier.go | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/light/client_benchmark_test.go b/light/client_benchmark_test.go index 38b44d271..59eb79766 100644 --- a/light/client_benchmark_test.go +++ b/light/client_benchmark_test.go @@ -2,6 +2,7 @@ package light_test import ( "context" + "errors" "testing" "time" @@ -57,7 +58,7 @@ func (impl *providerBenchmarkImpl) LightBlock(ctx context.Context, height int64) } func (impl *providerBenchmarkImpl) ReportEvidence(_ context.Context, _ types.Evidence) error { - panic("not implemented") + return errors.New("not implemented") } func BenchmarkSequence(b *testing.B) { diff --git a/light/verifier.go b/light/verifier.go index ee4bfb053..f6156c5de 100644 --- a/light/verifier.go +++ b/light/verifier.go @@ -38,9 +38,12 @@ func VerifyNonAdjacent( trustingPeriod time.Duration, now time.Time, maxClockDrift time.Duration, - trustLevel tmmath.Fraction) error { + trustLevel tmmath.Fraction, +) error { - checkRequiredHeaderFields(trustedHeader) + if err := checkRequiredHeaderFields(trustedHeader); err != nil { + return err + } if untrustedHeader.Height == trustedHeader.Height+1 { return errors.New("headers must be non adjacent in height") @@ -106,12 +109,15 @@ func VerifyAdjacent( untrustedVals *types.ValidatorSet, // height=X+1 trustingPeriod time.Duration, now time.Time, - maxClockDrift time.Duration) error { + maxClockDrift time.Duration, +) error { - checkRequiredHeaderFields(trustedHeader) + if err := checkRequiredHeaderFields(trustedHeader); err != nil { + return err + } if len(trustedHeader.NextValidatorsHash) == 0 { - panic("next validators hash in trusted header is empty") + return errors.New("next validators hash in trusted header is empty") } if untrustedHeader.Height != trustedHeader.Height+1 { @@ -268,17 +274,18 @@ func verifyNewHeaderAndVals( return nil } -func checkRequiredHeaderFields(h *types.SignedHeader) { +func checkRequiredHeaderFields(h *types.SignedHeader) error { if h.Height == 0 { - panic("height in trusted header must be set (non zero") + return errors.New("height in trusted header must be set (non zero") } zeroTime := time.Time{} if h.Time == zeroTime { - panic("time in trusted header must be set") + return errors.New("time in trusted header must be set") } if h.ChainID == "" { - panic("chain ID in trusted header must be set") + return errors.New("chain ID in trusted header must be set") } + return nil }