2015-03-22 19:00:08 -07:00
|
|
|
package types
|
2014-09-14 15:37:32 -07:00
|
|
|
|
|
|
|
import (
|
2017-08-28 19:46:38 -04:00
|
|
|
"bytes"
|
2014-09-14 15:37:32 -07:00
|
|
|
"errors"
|
2014-10-15 20:15:38 -07:00
|
|
|
"fmt"
|
2014-09-14 15:37:32 -07:00
|
|
|
"io"
|
2017-12-11 18:34:46 +01:00
|
|
|
"time"
|
2014-09-14 15:37:32 -07:00
|
|
|
|
2015-11-10 13:10:43 -08:00
|
|
|
"github.com/tendermint/go-crypto"
|
2015-10-22 17:39:06 -07:00
|
|
|
"github.com/tendermint/go-wire"
|
2017-04-27 19:01:18 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-12-27 14:46:24 -05:00
|
|
|
ErrVoteUnexpectedStep = errors.New("Unexpected step")
|
|
|
|
ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index")
|
|
|
|
ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address")
|
|
|
|
ErrVoteInvalidSignature = errors.New("Invalid signature")
|
|
|
|
ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
|
|
|
|
ErrVoteNonDeterministicSignature = errors.New("Non-deterministic signature")
|
|
|
|
ErrVoteNil = errors.New("Nil vote")
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
2016-07-24 14:32:08 -07:00
|
|
|
type ErrVoteConflictingVotes struct {
|
2017-07-25 12:10:48 -04:00
|
|
|
*DuplicateVoteEvidence
|
2015-03-16 17:28:53 -07:00
|
|
|
}
|
|
|
|
|
2016-07-24 14:32:08 -07:00
|
|
|
func (err *ErrVoteConflictingVotes) Error() string {
|
2017-07-25 12:10:48 -04:00
|
|
|
return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConflictingVoteError(val *Validator, voteA, voteB *Vote) *ErrVoteConflictingVotes {
|
|
|
|
return &ErrVoteConflictingVotes{
|
|
|
|
&DuplicateVoteEvidence{
|
|
|
|
PubKey: val.PubKey,
|
|
|
|
VoteA: voteA,
|
|
|
|
VoteB: voteB,
|
|
|
|
},
|
|
|
|
}
|
2015-03-16 17:28:53 -07:00
|
|
|
}
|
|
|
|
|
2016-09-05 17:33:02 -07:00
|
|
|
// Types of votes
|
|
|
|
// TODO Make a new type "VoteType"
|
|
|
|
const (
|
|
|
|
VoteTypePrevote = byte(0x01)
|
|
|
|
VoteTypePrecommit = byte(0x02)
|
|
|
|
)
|
|
|
|
|
|
|
|
func IsVoteTypeValid(type_ byte) bool {
|
|
|
|
switch type_ {
|
|
|
|
case VoteTypePrevote:
|
|
|
|
return true
|
|
|
|
case VoteTypePrecommit:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 01:26:51 -05:00
|
|
|
// Address is hex bytes. TODO: crypto.Address
|
|
|
|
type Address = cmn.HexBytes
|
|
|
|
|
2014-12-23 01:35:54 -08:00
|
|
|
// Represents a prevote, precommit, or commit vote from validators for consensus.
|
2014-09-14 15:37:32 -07:00
|
|
|
type Vote struct {
|
2018-01-06 01:26:51 -05:00
|
|
|
ValidatorAddress Address `json:"validator_address"`
|
2016-12-18 00:10:14 -05:00
|
|
|
ValidatorIndex int `json:"validator_index"`
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64 `json:"height"`
|
2016-12-18 00:10:14 -05:00
|
|
|
Round int `json:"round"`
|
2017-12-11 18:34:46 +01:00
|
|
|
Timestamp time.Time `json:"timestamp"`
|
2016-12-18 00:10:14 -05:00
|
|
|
Type byte `json:"type"`
|
|
|
|
BlockID BlockID `json:"block_id"` // zero if vote is nil.
|
|
|
|
Signature crypto.Signature `json:"signature"`
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:10:43 -08:00
|
|
|
func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
|
2016-12-02 05:01:47 -05:00
|
|
|
wire.WriteJSON(CanonicalJSONOnceVote{
|
|
|
|
chainID,
|
|
|
|
CanonicalVote(vote),
|
|
|
|
}, w, n, err)
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2014-10-15 20:15:38 -07:00
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (vote *Vote) Copy() *Vote {
|
|
|
|
voteCopy := *vote
|
|
|
|
return &voteCopy
|
2014-10-31 18:35:38 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 18:49:04 -08:00
|
|
|
func (vote *Vote) String() string {
|
2015-06-26 17:54:02 -07:00
|
|
|
if vote == nil {
|
|
|
|
return "nil-Vote"
|
|
|
|
}
|
2014-10-30 03:32:09 -07:00
|
|
|
var typeString string
|
2014-12-09 18:49:04 -08:00
|
|
|
switch vote.Type {
|
2014-10-20 19:02:10 -07:00
|
|
|
case VoteTypePrevote:
|
2014-10-30 03:32:09 -07:00
|
|
|
typeString = "Prevote"
|
2014-10-15 20:15:38 -07:00
|
|
|
case VoteTypePrecommit:
|
2014-10-30 03:32:09 -07:00
|
|
|
typeString = "Precommit"
|
2014-10-15 20:15:38 -07:00
|
|
|
default:
|
2017-04-27 19:01:18 -04:00
|
|
|
cmn.PanicSanity("Unknown vote type")
|
2014-10-15 20:15:38 -07:00
|
|
|
}
|
2014-10-30 03:32:09 -07:00
|
|
|
|
2017-12-11 18:34:46 +01:00
|
|
|
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v @ %s}",
|
2017-04-27 19:01:18 -04:00
|
|
|
vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
|
2016-07-01 17:47:31 -04:00
|
|
|
vote.Height, vote.Round, vote.Type, typeString,
|
2017-12-11 18:34:46 +01:00
|
|
|
cmn.Fingerprint(vote.BlockID.Hash), vote.Signature,
|
|
|
|
CanonicalTime(vote.Timestamp))
|
2016-07-24 14:32:08 -07:00
|
|
|
}
|
2017-07-25 12:29:38 -04:00
|
|
|
|
|
|
|
func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
|
2017-08-28 19:46:38 -04:00
|
|
|
if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
|
2017-07-25 12:29:38 -04:00
|
|
|
return ErrVoteInvalidValidatorAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pubKey.VerifyBytes(SignBytes(chainID, vote), vote.Signature) {
|
|
|
|
return ErrVoteInvalidSignature
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|