Browse Source

light: convert validation panics to errors (#7597)

pull/7606/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
2199e0a8a1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions
  1. +2
    -1
      light/client_benchmark_test.go
  2. +16
    -9
      light/verifier.go

+ 2
- 1
light/client_benchmark_test.go 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) {


+ 16
- 9
light/verifier.go 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
}

Loading…
Cancel
Save