github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/lite/base_verifier.go (about)

     1  package lite
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	lerr "github.com/tendermint/tendermint/lite/errors"
     9  	"github.com/tendermint/tendermint/types"
    10  )
    11  
    12  var _ Verifier = (*BaseVerifier)(nil)
    13  
    14  // BaseVerifier lets us check the validity of SignedHeaders at height or
    15  // later, requiring sufficient votes (> 2/3) from the given valset.
    16  // To verify blocks produced by a blockchain with mutable validator sets,
    17  // use the DynamicVerifier.
    18  // TODO: Handle unbonding time.
    19  type BaseVerifier struct {
    20  	chainID string
    21  	height  int64
    22  	valset  *types.ValidatorSet
    23  }
    24  
    25  // NewBaseVerifier returns a new Verifier initialized with a validator set at
    26  // some height.
    27  func NewBaseVerifier(chainID string, height int64, valset *types.ValidatorSet) *BaseVerifier {
    28  	if valset.IsNilOrEmpty() {
    29  		panic("NewBaseVerifier requires a valid valset")
    30  	}
    31  	return &BaseVerifier{
    32  		chainID: chainID,
    33  		height:  height,
    34  		valset:  valset,
    35  	}
    36  }
    37  
    38  // Implements Verifier.
    39  func (bv *BaseVerifier) ChainID() string {
    40  	return bv.chainID
    41  }
    42  
    43  // Implements Verifier.
    44  func (bv *BaseVerifier) Verify(signedHeader types.SignedHeader) error {
    45  
    46  	// We can't verify commits for a different chain.
    47  	if signedHeader.ChainID != bv.chainID {
    48  		return errors.Errorf("BaseVerifier chainID is %v, cannot verify chainID %v",
    49  			bv.chainID, signedHeader.ChainID)
    50  	}
    51  
    52  	// We can't verify commits older than bv.height.
    53  	if signedHeader.Height < bv.height {
    54  		return errors.Errorf("BaseVerifier height is %v, cannot verify height %v",
    55  			bv.height, signedHeader.Height)
    56  	}
    57  
    58  	// We can't verify with the wrong validator set.
    59  	if !bytes.Equal(signedHeader.ValidatorsHash,
    60  		bv.valset.Hash()) {
    61  		return lerr.ErrUnexpectedValidators(signedHeader.ValidatorsHash, bv.valset.Hash())
    62  	}
    63  
    64  	// Do basic sanity checks.
    65  	err := signedHeader.ValidateBasic(bv.chainID)
    66  	if err != nil {
    67  		return errors.Wrap(err, "in verify")
    68  	}
    69  
    70  	// Check commit signatures.
    71  	err = bv.valset.VerifyCommit(
    72  		bv.chainID, signedHeader.Commit.BlockID,
    73  		signedHeader.Height, signedHeader.Commit)
    74  	if err != nil {
    75  		return errors.Wrap(err, "in verify")
    76  	}
    77  
    78  	return nil
    79  }