tendermint/types/vote.go

72 lines
2.1 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")
ErrVoteInvalidAccount = errors.New("Invalid round vote account")
ErrVoteInvalidSignature = errors.New("Invalid round vote signature")
ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
2014-09-14 15:37:32 -07:00
)
type ErrVoteConflictingSignature struct {
VoteA *Vote
VoteB *Vote
}
func (err *ErrVoteConflictingSignature) Error() string {
return "Conflicting round vote signature"
}
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 {
2015-11-10 13:10:43 -08:00
Height int `json:"height"`
Round int `json:"round"`
Type byte `json:"type"`
BlockHash []byte `json:"block_hash"` // empty if vote is nil.
BlockPartsHeader PartSetHeader `json:"block_parts_header"` // 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
}
2014-12-23 01:35:54 -08:00
// Types of votes
const (
VoteTypePrevote = byte(0x01)
VoteTypePrecommit = byte(0x02)
2014-12-23 01:35:54 -08:00
)
2015-11-10 13:10:43 -08:00
func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
2015-07-25 15:45:45 -07:00
wire.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
wire.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts_header":%v`, vote.BlockHash, vote.BlockPartsHeader)), w, n, err)
2015-07-25 15:45:45 -07:00
wire.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), 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 16:42:52 -07: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/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockPartsHeader, vote.Signature)
2014-10-15 20:15:38 -07:00
}