tendermint/block/vote.go

67 lines
1.7 KiB
Go
Raw Normal View History

2014-12-17 01:37:13 -08:00
package block
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/tendermint/account"
2014-09-14 15:37:32 -07:00
. "github.com/tendermint/tendermint/binary"
2014-10-30 03:32:09 -07:00
. "github.com/tendermint/tendermint/common"
2014-09-14 15:37:32 -07:00
)
const (
VoteTypePrevote = byte(0x00)
2014-09-14 15:37:32 -07:00
VoteTypePrecommit = byte(0x01)
VoteTypeCommit = byte(0x02)
)
var (
ErrVoteUnexpectedStep = errors.New("Unexpected step")
2014-09-14 15:37:32 -07:00
ErrVoteInvalidAccount = errors.New("Invalid round vote account")
ErrVoteInvalidSignature = errors.New("Invalid round vote signature")
ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
ErrVoteConflictingSignature = errors.New("Conflicting round vote signature")
)
// Represents a prevote, precommit, or commit vote for proposals from validators.
// 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 {
Height uint
Round uint
2014-10-30 03:32:09 -07:00
Type byte
BlockHash []byte // empty if vote is nil.
BlockParts PartSetHeader // zero if vote is nil.
Signature SignatureEd25519
2014-09-14 15:37:32 -07:00
}
func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) {
WriteUvarint(vote.Height, w, n, err)
WriteUvarint(vote.Round, w, n, err)
WriteByte(vote.Type, w, n, err)
WriteByteSlice(vote.BlockHash, w, n, err)
WriteBinary(vote.BlockParts, 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 {
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
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
return fmt.Sprintf("%v{%v/%v %X#%v %v}", typeString, vote.Height, vote.Round, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature)
2014-10-15 20:15:38 -07:00
}