- package types
-
- import (
- "bytes"
- "errors"
- "fmt"
- "strings"
- "time"
-
- "github.com/tendermint/tendermint/crypto"
- "github.com/tendermint/tendermint/crypto/merkle"
- "github.com/tendermint/tendermint/crypto/tmhash"
- tmjson "github.com/tendermint/tendermint/libs/json"
- tmrand "github.com/tendermint/tendermint/libs/rand"
- tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
- )
-
- // Evidence represents any provable malicious activity by a validator.
- type Evidence interface {
- Height() int64 // height of the equivocation
- Time() time.Time // time of the equivocation
- Address() []byte // address of the equivocating validator
- Bytes() []byte // bytes which comprise the evidence
- Hash() []byte // hash of the evidence
- Verify(chainID string, pubKey crypto.PubKey) error // verify the evidence
- Equal(Evidence) bool // check equality of evidence
-
- ValidateBasic() error
- String() string
- }
-
- const (
- // MaxEvidenceBytes is a maximum size of any evidence (including amino overhead).
- MaxEvidenceBytes int64 = 444
- )
-
- // ErrEvidenceInvalid wraps a piece of evidence and the error denoting how or why it is invalid.
- type ErrEvidenceInvalid struct {
- Evidence Evidence
- ErrorValue error
- }
-
- // NewErrEvidenceInvalid returns a new EvidenceInvalid with the given err.
- func NewErrEvidenceInvalid(ev Evidence, err error) *ErrEvidenceInvalid {
- return &ErrEvidenceInvalid{ev, err}
- }
-
- // Error returns a string representation of the error.
- func (err *ErrEvidenceInvalid) Error() string {
- return fmt.Sprintf("Invalid evidence: %v. Evidence: %v", err.ErrorValue, err.Evidence)
- }
-
- // ErrEvidenceOverflow is for when there is too much evidence in a block.
- type ErrEvidenceOverflow struct {
- MaxNum int
- GotNum int
- }
-
- // NewErrEvidenceOverflow returns a new ErrEvidenceOverflow where got > max.
- func NewErrEvidenceOverflow(max, got int) *ErrEvidenceOverflow {
- return &ErrEvidenceOverflow{max, got}
- }
-
- // Error returns a string representation of the error.
- func (err *ErrEvidenceOverflow) Error() string {
- return fmt.Sprintf("Too much evidence: Max %d, got %d", err.MaxNum, err.GotNum)
- }
-
- //-------------------------------------------
-
- func EvidenceToProto(evidence Evidence) (*tmproto.Evidence, error) {
- if evidence == nil {
- return nil, errors.New("nil evidence")
- }
-
- switch evi := evidence.(type) {
- case *DuplicateVoteEvidence:
- pbevi := evi.ToProto()
- tp := &tmproto.Evidence{
- Sum: &tmproto.Evidence_DuplicateVoteEvidence{
- DuplicateVoteEvidence: pbevi,
- },
- }
- return tp, nil
-
- default:
- return nil, fmt.Errorf("toproto: evidence is not recognized: %T", evi)
- }
- }
-
- func EvidenceFromProto(evidence *tmproto.Evidence) (Evidence, error) {
- if evidence == nil {
- return nil, errors.New("nil evidence")
- }
-
- switch evi := evidence.Sum.(type) {
- case *tmproto.Evidence_DuplicateVoteEvidence:
- return DuplicateVoteEvidenceFromProto(evi.DuplicateVoteEvidence)
- default:
- return nil, errors.New("evidence is not recognized")
- }
- }
-
- func init() {
- tmjson.RegisterType(&DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence")
- }
-
- //-------------------------------------------
-
- // DuplicateVoteEvidence contains evidence a validator signed two conflicting
- // votes.
- type DuplicateVoteEvidence struct {
- VoteA *Vote `json:"vote_a"`
- VoteB *Vote `json:"vote_b"`
-
- Timestamp time.Time `json:"timestamp"`
- }
-
- var _ Evidence = &DuplicateVoteEvidence{}
-
- // NewDuplicateVoteEvidence creates DuplicateVoteEvidence with right ordering given
- // two conflicting votes. If one of the votes is nil, evidence returned is nil as well
- func NewDuplicateVoteEvidence(vote1, vote2 *Vote, time time.Time) *DuplicateVoteEvidence {
- var voteA, voteB *Vote
- if vote1 == nil || vote2 == nil {
- return nil
- }
- if strings.Compare(vote1.BlockID.Key(), vote2.BlockID.Key()) == -1 {
- voteA = vote1
- voteB = vote2
- } else {
- voteA = vote2
- voteB = vote1
- }
- return &DuplicateVoteEvidence{
- VoteA: voteA,
- VoteB: voteB,
-
- Timestamp: time,
- }
- }
-
- // String returns a string representation of the evidence.
- func (dve *DuplicateVoteEvidence) String() string {
- return fmt.Sprintf("DuplicateVoteEvidence{VoteA: %v, VoteB: %v, Time: %v}", dve.VoteA, dve.VoteB, dve.Timestamp)
- }
-
- // Height returns the height this evidence refers to.
- func (dve *DuplicateVoteEvidence) Height() int64 {
- return dve.VoteA.Height
- }
-
- // Time returns time of the latest vote.
- func (dve *DuplicateVoteEvidence) Time() time.Time {
- return dve.Timestamp
- }
-
- // Address returns the address of the validator.
- func (dve *DuplicateVoteEvidence) Address() []byte {
- return dve.VoteA.ValidatorAddress
- }
-
- // Hash returns the hash of the evidence.
- func (dve *DuplicateVoteEvidence) Bytes() []byte {
- pbe := dve.ToProto()
- bz, err := pbe.Marshal()
- if err != nil {
- panic(err)
- }
-
- return bz
- }
-
- // Hash returns the hash of the evidence.
- func (dve *DuplicateVoteEvidence) Hash() []byte {
- pbe := dve.ToProto()
- bz, err := pbe.Marshal()
- if err != nil {
- panic(err)
- }
-
- return tmhash.Sum(bz)
- }
-
- // Verify returns an error if the two votes aren't conflicting.
- //
- // To be conflicting, they must be from the same validator, for the same H/R/S,
- // but for different blocks.
- func (dve *DuplicateVoteEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
- // H/R/S must be the same
- if dve.VoteA.Height != dve.VoteB.Height ||
- dve.VoteA.Round != dve.VoteB.Round ||
- dve.VoteA.Type != dve.VoteB.Type {
- return fmt.Errorf("h/r/s does not match: %d/%d/%v vs %d/%d/%v",
- dve.VoteA.Height, dve.VoteA.Round, dve.VoteA.Type,
- dve.VoteB.Height, dve.VoteB.Round, dve.VoteB.Type)
- }
-
- // Address must be the same
- if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
- return fmt.Errorf("validator addresses do not match: %X vs %X",
- dve.VoteA.ValidatorAddress,
- dve.VoteB.ValidatorAddress,
- )
- }
-
- // BlockIDs must be different
- if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
- return fmt.Errorf(
- "block IDs are the same (%v) - not a real duplicate vote",
- dve.VoteA.BlockID,
- )
- }
-
- // pubkey must match address (this should already be true, sanity check)
- addr := dve.VoteA.ValidatorAddress
- if !bytes.Equal(pubKey.Address(), addr) {
- return fmt.Errorf("address (%X) doesn't match pubkey (%v - %X)",
- addr, pubKey, pubKey.Address())
- }
- va := dve.VoteA.ToProto()
- vb := dve.VoteB.ToProto()
- // Signatures must be valid
- if !pubKey.VerifySignature(VoteSignBytes(chainID, va), dve.VoteA.Signature) {
- return fmt.Errorf("verifying VoteA: %w", ErrVoteInvalidSignature)
- }
- if !pubKey.VerifySignature(VoteSignBytes(chainID, vb), dve.VoteB.Signature) {
- return fmt.Errorf("verifying VoteB: %w", ErrVoteInvalidSignature)
- }
-
- return nil
- }
-
- // Equal checks if two pieces of evidence are equal.
- func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
- if _, ok := ev.(*DuplicateVoteEvidence); !ok {
- return false
- }
- pbdev := dve.ToProto()
- bz, err := pbdev.Marshal()
- if err != nil {
- panic(err)
- }
-
- var evbz []byte
- if ev, ok := ev.(*DuplicateVoteEvidence); ok {
- evpb := ev.ToProto()
- evbz, err = evpb.Marshal()
- if err != nil {
- panic(err)
- }
- }
-
- // just check their hashes
- dveHash := tmhash.Sum(bz)
- evHash := tmhash.Sum(evbz)
- return bytes.Equal(dveHash, evHash)
- }
-
- // ValidateBasic performs basic validation.
- func (dve *DuplicateVoteEvidence) ValidateBasic() error {
- if dve == nil {
- return errors.New("empty duplicate vote evidence")
- }
-
- if dve.VoteA == nil || dve.VoteB == nil {
- return fmt.Errorf("one or both of the votes are empty %v, %v", dve.VoteA, dve.VoteB)
- }
- if err := dve.VoteA.ValidateBasic(); err != nil {
- return fmt.Errorf("invalid VoteA: %w", err)
- }
- if err := dve.VoteB.ValidateBasic(); err != nil {
- return fmt.Errorf("invalid VoteB: %w", err)
- }
- // Enforce Votes are lexicographically sorted on blockID
- if strings.Compare(dve.VoteA.BlockID.Key(), dve.VoteB.BlockID.Key()) >= 0 {
- return errors.New("duplicate votes in invalid order")
- }
- return nil
- }
-
- func (dve *DuplicateVoteEvidence) ToProto() *tmproto.DuplicateVoteEvidence {
- voteB := dve.VoteB.ToProto()
- voteA := dve.VoteA.ToProto()
- tp := tmproto.DuplicateVoteEvidence{
- VoteA: voteA,
- VoteB: voteB,
- Timestamp: dve.Timestamp,
- }
- return &tp
- }
-
- func DuplicateVoteEvidenceFromProto(pb *tmproto.DuplicateVoteEvidence) (*DuplicateVoteEvidence, error) {
- if pb == nil {
- return nil, errors.New("nil duplicate vote evidence")
- }
-
- vA, err := VoteFromProto(pb.VoteA)
- if err != nil {
- return nil, err
- }
-
- vB, err := VoteFromProto(pb.VoteB)
- if err != nil {
- return nil, err
- }
-
- dve := NewDuplicateVoteEvidence(vA, vB, pb.Timestamp)
-
- return dve, dve.ValidateBasic()
- }
-
- //--------------------------------------------------
-
- // EvidenceList is a list of Evidence. Evidences is not a word.
- type EvidenceList []Evidence
-
- // Hash returns the simple merkle root hash of the EvidenceList.
- func (evl EvidenceList) Hash() []byte {
- // These allocations are required because Evidence is not of type Bytes, and
- // golang slices can't be typed cast. This shouldn't be a performance problem since
- // the Evidence size is capped.
- evidenceBzs := make([][]byte, len(evl))
- for i := 0; i < len(evl); i++ {
- evidenceBzs[i] = evl[i].Bytes()
- }
- return merkle.HashFromByteSlices(evidenceBzs)
- }
-
- func (evl EvidenceList) String() string {
- s := ""
- for _, e := range evl {
- s += fmt.Sprintf("%s\t\t", e)
- }
- return s
- }
-
- // Has returns true if the evidence is in the EvidenceList.
- func (evl EvidenceList) Has(evidence Evidence) bool {
- for _, ev := range evl {
- if ev.Equal(evidence) {
- return true
- }
- }
- return false
- }
-
- //-------------------------------------------- MOCKING --------------------------------------
-
- // unstable - use only for testing
-
- // assumes the round to be 0 and the validator index to be 0
- func NewMockDuplicateVoteEvidence(height int64, time time.Time, chainID string) *DuplicateVoteEvidence {
- val := NewMockPV()
- return NewMockDuplicateVoteEvidenceWithValidator(height, time, val, chainID)
- }
-
- func NewMockDuplicateVoteEvidenceWithValidator(height int64, time time.Time,
- pv PrivValidator, chainID string) *DuplicateVoteEvidence {
- pubKey, _ := pv.GetPubKey()
- voteA := makeMockVote(height, 0, 0, pubKey.Address(), randBlockID(), time)
- vA := voteA.ToProto()
- _ = pv.SignVote(chainID, vA)
- voteA.Signature = vA.Signature
- voteB := makeMockVote(height, 0, 0, pubKey.Address(), randBlockID(), time)
- vB := voteB.ToProto()
- _ = pv.SignVote(chainID, vB)
- voteB.Signature = vB.Signature
- return NewDuplicateVoteEvidence(voteA, voteB, time)
- }
-
- func makeMockVote(height int64, round, index int32, addr Address,
- blockID BlockID, time time.Time) *Vote {
- return &Vote{
- Type: tmproto.SignedMsgType(2),
- Height: height,
- Round: round,
- BlockID: blockID,
- Timestamp: time,
- ValidatorAddress: addr,
- ValidatorIndex: index,
- }
- }
-
- func randBlockID() BlockID {
- return BlockID{
- Hash: tmrand.Bytes(tmhash.Size),
- PartSetHeader: PartSetHeader{
- Total: 1,
- Hash: tmrand.Bytes(tmhash.Size),
- },
- }
- }
|