2017-07-09 14:10:00 -04:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-07-25 12:10:48 -04:00
|
|
|
|
|
|
|
"github.com/tendermint/go-crypto"
|
2017-07-09 14:10:00 -04:00
|
|
|
)
|
|
|
|
|
2017-07-25 12:29:38 -04:00
|
|
|
type ErrEvidenceInvalid struct {
|
|
|
|
Evidence Evidence
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *ErrEvidenceInvalid) Error() string {
|
|
|
|
return fmt.Sprintf("Invalid evidence: %v. Evidence: %v", err.Error, err.Evidence)
|
|
|
|
}
|
|
|
|
|
2017-07-09 14:10:00 -04:00
|
|
|
// Evidence represents any provable malicious activity by a validator
|
|
|
|
type Evidence interface {
|
|
|
|
Verify() error
|
|
|
|
Address() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------
|
|
|
|
|
|
|
|
type DuplicateVoteEvidence struct {
|
2017-07-25 12:10:48 -04:00
|
|
|
PubKey crypto.PubKey
|
|
|
|
VoteA *Vote
|
|
|
|
VoteB *Vote
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the address of the validator
|
|
|
|
func (dve *DuplicateVoteEvidence) Address() []byte {
|
2017-07-25 12:10:48 -04:00
|
|
|
return dve.PubKey.Address()
|
2017-07-09 14:10:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify returns an error if the two votes aren't from the same validator, for the same H/R/S, but for different blocks
|
|
|
|
func (dve *DuplicateVoteEvidence) Verify() 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("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
|
|
|
|
}
|
|
|
|
|
2017-08-21 14:15:09 -04:00
|
|
|
// Address must be the same
|
2017-07-09 14:10:00 -04:00
|
|
|
if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
|
|
|
|
return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
|
|
|
|
}
|
2017-08-21 14:15:09 -04:00
|
|
|
// XXX: Should we enforce index is the same ?
|
2017-07-09 14:10:00 -04:00
|
|
|
if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
|
|
|
|
return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockIDs must be different
|
|
|
|
if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
|
|
|
|
return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote!", dve.VoteA.BlockID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signatures must be valid
|
2017-07-25 12:29:38 -04:00
|
|
|
if !dve.PubKey.Verify(SignBytes(chainID, dve.VoteA), dve.VoteA.Signature) {
|
|
|
|
return ErrVoteInvalidSignature
|
|
|
|
}
|
2017-08-21 14:15:09 -04:00
|
|
|
if !dve.PubKey.Verify(SignBytes(chainID, dve.VoteB), dve.VoteB.Signature) {
|
|
|
|
return ErrVoteInvalidSignature
|
|
|
|
}
|
2017-07-09 14:10:00 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|