122 lines
3.3 KiB
Go
Raw Normal View History

2014-09-14 15:37:32 -07:00
package consensus
import (
2014-10-18 01:42:33 -07:00
"fmt"
2014-09-14 15:37:32 -07:00
"io"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/blocks"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/state"
2014-09-14 15:37:32 -07:00
)
// Proof of lock.
// +2/3 of validators' prevotes for a given blockhash (or nil)
2014-09-14 15:37:32 -07:00
type POL struct {
2014-10-30 03:32:09 -07:00
Height uint32
Round uint16
BlockHash []byte // Could be nil, which makes this a proof of unlock.
BlockParts PartSetHeader // When BlockHash is nil, this is zero.
Votes []Signature // Vote signatures for height/round/hash
Commits []RoundSignature // Commit signatures for height/hash
2014-09-14 15:37:32 -07:00
}
func ReadPOL(r io.Reader, n *int64, err *error) *POL {
return &POL{
2014-10-30 03:32:09 -07:00
Height: ReadUInt32(r, n, err),
Round: ReadUInt16(r, n, err),
BlockHash: ReadByteSlice(r, n, err),
BlockParts: ReadPartSetHeader(r, n, err),
Votes: ReadSignatures(r, n, err),
Commits: ReadRoundSignatures(r, n, err),
2014-09-14 15:37:32 -07:00
}
}
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)
2014-10-30 03:32:09 -07:00
WriteBinary(w, pol.BlockParts, &n, &err)
2014-09-14 15:37:32 -07:00
WriteSignatures(w, pol.Votes, &n, &err)
2014-10-30 03:32:09 -07:00
WriteRoundSignatures(w, pol.Commits, &n, &err)
2014-09-14 15:37:32 -07:00
return
}
// Returns whether +2/3 have voted/committed for BlockHash.
func (pol *POL) Verify(vset *state.ValidatorSet) error {
2014-09-14 15:37:32 -07:00
talliedVotingPower := uint64(0)
2014-10-30 03:32:09 -07:00
voteDoc := BinaryBytes(&Vote{
Height: pol.Height, Round: pol.Round, Type: VoteTypePrevote,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
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
}
2014-10-30 03:32:09 -07:00
for _, rsig := range pol.Commits {
round := rsig.Round
sig := rsig.Signature
2014-09-14 15:37:32 -07:00
// 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-17 16:48:27 -07:00
if round >= pol.Round {
return Errorf("Invalid commit round %v for POL %v", round, pol)
}
2014-10-07 19:37:20 -07:00
2014-10-30 03:32:09 -07:00
commitDoc := BinaryBytes(&Vote{
Height: pol.Height, Round: round, Type: VoteTypeCommit,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
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))
}
}
2014-10-18 01:42:33 -07:00
func (pol *POL) Description() string {
if pol == nil {
return "nil-POL"
} else {
2014-10-30 03:32:09 -07:00
return fmt.Sprintf("POL{H:%v R:%v BH:%X}", pol.Height, pol.Round,
Fingerprint(pol.BlockHash), pol.BlockParts)
2014-10-18 01:42:33 -07:00
}
}