tendermint/lite/commit.go

85 lines
2.4 KiB
Go
Raw Normal View History

2017-11-09 17:37:18 -05:00
package lite
2017-10-24 12:34:36 +02:00
import (
"bytes"
"errors"
"fmt"
2017-10-24 12:34:36 +02:00
"github.com/tendermint/tendermint/types"
)
2019-07-08 13:32:08 +04:00
// FullCommit contains a SignedHeader (the block's header and a commit that
// signs it), the validator set which signed the commit, and the next validator
// set. The next validator set (which is proven from the block header) allows
// us to revert to block-by-block updating of lite Verifier's latest validator
// set, even in the face of arbitrarily large power changes.
2017-10-24 12:34:36 +02:00
type FullCommit struct {
SignedHeader types.SignedHeader `json:"signed_header"`
Validators *types.ValidatorSet `json:"validator_set"`
NextValidators *types.ValidatorSet `json:"next_validator_set"`
2017-10-24 12:34:36 +02:00
}
// NewFullCommit returns a new FullCommit.
2018-06-19 23:55:15 -07:00
func NewFullCommit(signedHeader types.SignedHeader, valset, nextValset *types.ValidatorSet) FullCommit {
2017-10-24 12:34:36 +02:00
return FullCommit{
SignedHeader: signedHeader,
Validators: valset,
2018-06-19 23:55:15 -07:00
NextValidators: nextValset,
2017-10-24 12:34:36 +02:00
}
}
2019-07-08 13:32:08 +04:00
// ValidateFull validates the components and ensures consistency.
// It also checks that Validators actually signed the SignedHeader.Commit.
// If > 2/3 did not sign the Commit from fc.Validators, it is not a valid
// commit!
2018-06-25 16:31:59 -07:00
func (fc FullCommit) ValidateFull(chainID string) error {
if fc.Validators.Size() == 0 {
2019-07-08 13:32:08 +04:00
return errors.New("empty FullCommit.Validators")
2017-10-24 12:34:36 +02:00
}
2019-04-25 15:11:30 -04:00
if !bytes.Equal(fc.SignedHeader.ValidatorsHash, fc.Validators.Hash()) {
2019-07-08 13:32:08 +04:00
return fmt.Errorf("header has ValidatorsHash %X but valset hash is %X",
fc.SignedHeader.ValidatorsHash,
fc.Validators.Hash(),
)
2017-10-24 12:34:36 +02:00
}
2019-04-25 15:11:30 -04:00
if fc.NextValidators.Size() == 0 {
2019-07-08 13:32:08 +04:00
return errors.New("empty FullCommit.NextValidators")
2017-10-24 12:34:36 +02:00
}
2019-04-25 15:11:30 -04:00
if !bytes.Equal(fc.SignedHeader.NextValidatorsHash, fc.NextValidators.Hash()) {
2019-07-08 13:32:08 +04:00
return fmt.Errorf("header has next ValidatorsHash %X but next valset hash is %X",
fc.SignedHeader.NextValidatorsHash,
fc.NextValidators.Hash(),
)
2017-10-24 12:34:36 +02:00
}
2019-04-25 15:11:30 -04:00
err := fc.SignedHeader.ValidateBasic(chainID)
if err != nil {
return err
2017-10-24 12:34:36 +02:00
}
2019-04-25 15:11:30 -04:00
// Validate the signatures on the commit.
hdr, cmt := fc.SignedHeader.Header, fc.SignedHeader.Commit
2019-04-25 15:11:30 -04:00
return fc.Validators.VerifyCommit(hdr.ChainID, cmt.BlockID, hdr.Height, cmt)
2017-10-24 12:34:36 +02:00
}
// Height returns the height of the header.
func (fc FullCommit) Height() int64 {
if fc.SignedHeader.Header == nil {
panic("should not happen")
}
2019-07-08 13:32:08 +04:00
return fc.SignedHeader.Height
}
// ChainID returns the chainID of the header.
func (fc FullCommit) ChainID() string {
if fc.SignedHeader.Header == nil {
panic("should not happen")
}
2019-07-08 13:32:08 +04:00
return fc.SignedHeader.ChainID
}