tendermint/blocks/vote.go

85 lines
2.0 KiB
Go
Raw Normal View History

2014-09-14 15:37:32 -07:00
package blocks
import (
"errors"
2014-10-15 20:15:38 -07:00
"fmt"
2014-09-14 15:37:32 -07:00
"io"
. "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.
2014-09-14 15:37:32 -07:00
type Vote struct {
2014-10-30 03:32:09 -07:00
Height uint32
Round uint16
Type byte
BlockHash []byte // empty if vote is nil.
BlockParts PartSetHeader // zero if vote is nil.
2014-09-14 15:37:32 -07:00
Signature
}
func ReadVote(r io.Reader, n *int64, err *error) *Vote {
return &Vote{
2014-10-30 03:32:09 -07:00
Height: ReadUInt32(r, n, err),
Round: ReadUInt16(r, n, err),
Type: ReadByte(r, n, err),
BlockHash: ReadByteSlice(r, n, err),
BlockParts: ReadPartSetHeader(r, n, err),
Signature: ReadSignature(r, n, err),
2014-09-14 15:37:32 -07:00
}
}
func (v *Vote) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, v.Height, &n, &err)
WriteUInt16(w, v.Round, &n, &err)
WriteByte(w, v.Type, &n, &err)
WriteByteSlice(w, v.BlockHash, &n, &err)
2014-10-30 03:32:09 -07:00
WriteBinary(w, v.BlockParts, &n, &err)
2014-09-14 15:37:32 -07:00
WriteBinary(w, v.Signature, &n, &err)
return
}
2014-10-07 19:37:20 -07:00
func (v *Vote) GetSignature() Signature {
return v.Signature
}
func (v *Vote) SetSignature(sig Signature) {
v.Signature = sig
2014-09-14 15:37:32 -07:00
}
2014-10-15 20:15:38 -07:00
2014-10-31 18:35:38 -07:00
func (v *Vote) Copy() *Vote {
vCopy := *v
return &vCopy
}
2014-10-15 20:15:38 -07:00
func (v *Vote) String() string {
2014-10-30 03:32:09 -07:00
var typeString string
2014-10-15 20:15:38 -07:00
switch v.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, v.Height, v.Round, Fingerprint(v.BlockHash), v.BlockParts, v.Signature)
2014-10-15 20:15:38 -07:00
}