tendermint/consensus/pol.go

102 lines
2.9 KiB
Go
Raw Normal View History

2014-09-14 15:37:32 -07:00
package consensus
import (
"io"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/blocks"
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/state"
)
// Proof of lock.
// +2/3 of validators' (bare) votes for a given blockhash (or nil)
type POL struct {
Height uint32
Round uint16
BlockHash []byte // Could be nil, which makes this a proof of unlock.
Votes []Signature // Vote signatures for height/round/hash
Commits []Signature // Commit signatures for height/hash
CommitRounds []uint16 // Rounds of the commits, less than POL.Round.
}
func ReadPOL(r io.Reader, n *int64, err *error) *POL {
return &POL{
Height: ReadUInt32(r, n, err),
Round: ReadUInt16(r, n, err),
BlockHash: ReadByteSlice(r, n, err),
Votes: ReadSignatures(r, n, err),
Commits: ReadSignatures(r, n, err),
CommitRounds: ReadUInt16s(r, n, err),
}
}
func (pol *POL) WriteTo(w io.Writer) (n int64, err error) {
WriteUInt32(w, pol.Height, &n, &err)
WriteUInt16(w, pol.Round, &n, &err)
WriteByteSlice(w, pol.BlockHash, &n, &err)
WriteSignatures(w, pol.Votes, &n, &err)
WriteSignatures(w, pol.Commits, &n, &err)
WriteUInt16s(w, pol.CommitRounds, &n, &err)
return
}
// Returns whether +2/3 have voted/committed for BlockHash.
func (pol *POL) Verify(vset *ValidatorSet) error {
talliedVotingPower := uint64(0)
2014-10-07 23:11:04 -07:00
voteDoc := BinaryBytes(&Vote{Height: pol.Height, Round: pol.Round,
Type: VoteTypeBare, BlockHash: pol.BlockHash})
2014-09-14 15:37:32 -07:00
seenValidators := map[uint64]struct{}{}
for _, sig := range pol.Votes {
// Validate
if _, seen := seenValidators[sig.SignerId]; seen {
return Errorf("Duplicate validator for vote %v for POL %v", sig, pol)
}
2014-10-11 21:27:58 -07:00
_, val := vset.GetById(sig.SignerId)
if val == nil {
2014-09-14 15:37:32 -07:00
return Errorf("Invalid validator for vote %v for POL %v", sig, pol)
}
2014-10-11 21:27:58 -07:00
if !val.VerifyBytes(voteDoc, sig) {
2014-09-14 15:37:32 -07:00
return Errorf("Invalid signature for vote %v for POL %v", sig, pol)
}
// Tally
2014-10-11 21:27:58 -07:00
seenValidators[val.Id] = struct{}{}
talliedVotingPower += val.VotingPower
2014-09-14 15:37:32 -07:00
}
for i, sig := range pol.Commits {
round := pol.CommitRounds[i]
// Validate
if _, seen := seenValidators[sig.SignerId]; seen {
return Errorf("Duplicate validator for commit %v for POL %v", sig, pol)
}
2014-10-11 21:27:58 -07:00
_, val := vset.GetById(sig.SignerId)
if val == nil {
2014-09-14 15:37:32 -07:00
return Errorf("Invalid validator for commit %v for POL %v", sig, pol)
}
2014-10-07 19:37:20 -07:00
2014-10-07 23:11:04 -07:00
commitDoc := BinaryBytes(&Vote{Height: pol.Height, Round: round,
Type: VoteTypeCommit, BlockHash: pol.BlockHash}) // TODO cache
2014-10-11 21:27:58 -07:00
if !val.VerifyBytes(commitDoc, sig) {
2014-09-14 15:37:32 -07:00
return Errorf("Invalid signature for commit %v for POL %v", sig, pol)
}
// Tally
2014-10-11 21:27:58 -07:00
seenValidators[val.Id] = struct{}{}
talliedVotingPower += val.VotingPower
2014-09-14 15:37:32 -07:00
}
if talliedVotingPower > vset.TotalVotingPower()*2/3 {
return nil
} else {
return Errorf("Invalid POL, insufficient voting power %v, needed %v",
talliedVotingPower, (vset.TotalVotingPower()*2/3 + 1))
}
}