tendermint/types/vote.go

92 lines
2.3 KiB
Go
Raw Normal View History

package types
2014-09-14 15:37:32 -07:00
import (
"errors"
2014-10-15 20:15:38 -07:00
"fmt"
2014-09-14 15:37:32 -07:00
"io"
2015-11-10 13:10:43 -08:00
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
2017-04-27 16:01:18 -07:00
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
2014-09-14 15:37:32 -07:00
)
var (
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")
2017-10-31 12:32:07 -07:00
ErrVoteNil = errors.New("Nil vote")
2014-09-14 15:37:32 -07:00
)
type ErrVoteConflictingVotes struct {
VoteA *Vote
VoteB *Vote
}
func (err *ErrVoteConflictingVotes) Error() string {
return "Conflicting votes"
}
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
}
}
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 {
2017-04-27 16:01:18 -07:00
ValidatorAddress data.Bytes `json:"validator_address"`
2016-12-17 21:10:14 -08:00
ValidatorIndex int `json:"validator_index"`
Height int64 `json:"height"`
2016-12-17 21:10:14 -08:00
Round int `json:"round"`
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 02:01:47 -08: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
func (vote *Vote) Copy() *Vote {
voteCopy := *vote
return &voteCopy
2014-10-31 18:35:38 -07:00
}
func (vote *Vote) String() string {
if vote == nil {
return "nil-Vote"
}
2014-10-30 03:32:09 -07:00
var typeString string
switch vote.Type {
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 16:01:18 -07:00
cmn.PanicSanity("Unknown vote type")
2014-10-15 20:15:38 -07:00
}
2014-10-30 03:32:09 -07:00
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v}",
2017-04-27 16:01:18 -07:00
vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
vote.Height, vote.Round, vote.Type, typeString,
2017-04-27 16:01:18 -07:00
cmn.Fingerprint(vote.BlockID.Hash), vote.Signature)
}