light: convert validation panics to errors (#7597)

This commit is contained in:
Sam Kleinman
2022-01-14 16:18:50 -05:00
committed by GitHub
parent 887cb219ab
commit 2199e0a8a1
2 changed files with 18 additions and 10 deletions

View File

@@ -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) {

View File

@@ -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
}