2015-03-22 19:00:08 -07:00
|
|
|
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-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-03-16 17:28:53 -07:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-03-16 17:28:53 -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-12-09 18:49:04 -08:00
|
|
|
// Commit votes get aggregated into the next block's Validaiton.
|
|
|
|
// See the whitepaper for details.
|
2014-09-14 15:37:32 -07:00
|
|
|
type Vote struct {
|
2015-05-01 17:26:49 -07:00
|
|
|
Height uint `json:"height"`
|
|
|
|
Round uint `json:"round"`
|
|
|
|
Type byte `json:"type"`
|
|
|
|
BlockHash []byte `json:"block_hash"` // empty if vote is nil.
|
|
|
|
BlockParts PartSetHeader `json:"block_parts"` // zero if vote is nil.
|
|
|
|
Signature account.SignatureEd25519 `json:"signature"`
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
2014-12-23 01:35:54 -08:00
|
|
|
// Types of votes
|
|
|
|
const (
|
2015-04-12 17:46:16 -07:00
|
|
|
VoteTypePrevote = byte(0x01)
|
|
|
|
VoteTypePrecommit = byte(0x02)
|
|
|
|
VoteTypeCommit = byte(0x03)
|
2014-12-23 01:35:54 -08:00
|
|
|
)
|
|
|
|
|
2015-05-29 17:53:57 -04:00
|
|
|
func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
|
2015-05-30 20:18:43 -04:00
|
|
|
binary.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
|
2015-05-01 17:26:49 -07:00
|
|
|
binary.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err)
|
|
|
|
binary.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
|
|
|
|
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 {
|
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
|
|
|
case VoteTypeCommit:
|
2014-10-30 03:32:09 -07:00
|
|
|
typeString = "Commit"
|
2014-10-15 20:15:38 -07:00
|
|
|
default:
|
|
|
|
panic("Unknown vote type")
|
|
|
|
}
|
2014-10-30 03:32:09 -07:00
|
|
|
|
2015-05-05 16:58:26 -07:00
|
|
|
return fmt.Sprintf("Vote{%v/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature)
|
2014-10-15 20:15:38 -07:00
|
|
|
}
|