tendermint/types/vote.go

120 lines
3.2 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"
. "github.com/tendermint/go-common"
2015-11-10 13:10:43 -08:00
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
2014-09-14 15:37:32 -07:00
)
var (
ErrVoteUnexpectedStep = errors.New("Unexpected step")
ErrVoteInvalidValidatorIndex = errors.New("Invalid round vote validator index")
ErrVoteInvalidValidatorAddress = errors.New("Invalid round vote validator address")
ErrVoteInvalidSignature = errors.New("Invalid round vote signature")
ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
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 {
ValidatorAddress []byte `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
2015-11-10 13:10:43 -08:00
Height int `json:"height"`
Round int `json:"round"`
Type byte `json:"type"`
2016-08-16 14:59:19 -07:00
BlockID BlockID `json:"block_id"` // zero if vote is nil.
2015-11-01 11:34:08 -08:00
Signature crypto.SignatureEd25519 `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-11-16 17:20:44 -06:00
wire.WriteJSON(
struct {
ChainID string `json:"chain_id"`
Vote struct {
BlockID BlockID `json:"block_id"`
Height int `json:"height"`
Round int `json:"round"`
Signature crypto.Signature `json:"signature"`
Type byte `json:"type"`
ValidatorAddress []byte `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
} `json: "vote"`
}{
chainID,
struct {
BlockID BlockID `json:"block_id"`
Height int `json:"height"`
Round int `json:"round"`
Signature crypto.Signature `json:"signature"`
Type byte `json:"type"`
ValidatorAddress []byte `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
}{
vote.BlockID,
vote.Height,
vote.Round,
vote.Signature,
vote.Type,
vote.ValidatorAddress,
vote.ValidatorIndex,
},
},
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:
2015-07-19 23:42:52 +00:00
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}",
vote.ValidatorIndex, Fingerprint(vote.ValidatorAddress),
vote.Height, vote.Round, vote.Type, typeString,
2016-08-16 14:59:19 -07:00
Fingerprint(vote.BlockID.Hash), vote.Signature)
}