tendermint/consensus/pol.go

97 lines
2.7 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
. "github.com/tendermint/tendermint/account"
2014-12-17 01:37:13 -08:00
. "github.com/tendermint/tendermint/block"
2014-09-14 15:37:32 -07:00
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/state"
2014-09-14 15:37:32 -07:00
)
// Each signature of a POL (proof-of-lock, see whitepaper) is
// either a prevote or a commit.
// Commits require an additional round which is strictly less than
// the POL round. Prevote rounds are equal to the POL round.
type POLVoteSignature struct {
Round uint
Signature SignatureEd25519
}
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 {
Height uint
Round uint
BlockHash []byte // Could be nil, which makes this a proof of unlock.
BlockParts PartSetHeader // When BlockHash is nil, this is zero.
Votes []POLVoteSignature // Prevote and commit signatures in ValidatorSet order.
2014-09-14 15:37:32 -07:00
}
2014-12-17 01:37:13 -08:00
// Returns whether +2/3 have prevoted/committed for BlockHash.
func (pol *POL) Verify(valSet *state.ValidatorSet) error {
if uint(len(pol.Votes)) != valSet.Size() {
return Errorf("Invalid POL votes count: Expected %v, got %v",
valSet.Size(), len(pol.Votes))
}
2014-09-14 15:37:32 -07:00
talliedVotingPower := uint64(0)
prevoteDoc := SignBytes(&Vote{
2014-10-30 03:32:09 -07:00
Height: pol.Height, Round: pol.Round, Type: VoteTypePrevote,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
seenValidators := map[string]struct{}{}
2014-12-17 01:37:13 -08:00
for idx, vote := range pol.Votes {
// vote may be zero, in which case skip.
if vote.Signature.IsZero() {
continue
}
voteDoc := prevoteDoc
_, val := valSet.GetByIndex(uint(idx))
2014-12-17 01:37:13 -08:00
// Commit vote?
if vote.Round < pol.Round {
voteDoc = SignBytes(&Vote{
2014-12-17 01:37:13 -08:00
Height: pol.Height, Round: vote.Round, Type: VoteTypeCommit,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
2014-12-17 01:37:13 -08:00
} else if vote.Round > pol.Round {
return Errorf("Invalid commit round %v for POL %v", vote.Round, pol)
}
2014-09-14 15:37:32 -07:00
// Validate
if _, seen := seenValidators[string(val.Address)]; seen {
2014-12-17 01:37:13 -08:00
return Errorf("Duplicate validator for vote %v for POL %v", vote, pol)
2014-09-14 15:37:32 -07:00
}
2014-12-17 01:37:13 -08:00
if !val.PubKey.VerifyBytes(voteDoc, vote.Signature) {
return Errorf("Invalid signature for vote %v for POL %v", vote, pol)
2014-09-14 15:37:32 -07:00
}
// Tally
seenValidators[string(val.Address)] = struct{}{}
2014-10-11 21:27:58 -07:00
talliedVotingPower += val.VotingPower
2014-09-14 15:37:32 -07:00
}
if talliedVotingPower > valSet.TotalVotingPower()*2/3 {
2014-09-14 15:37:32 -07:00
return nil
} else {
return Errorf("Invalid POL, insufficient voting power %v, needed %v",
talliedVotingPower, (valSet.TotalVotingPower()*2/3 + 1))
2014-09-14 15:37:32 -07:00
}
}
2014-10-18 01:42:33 -07:00
2014-12-23 01:35:54 -08:00
func (pol *POL) StringShort() string {
2014-10-18 01:42:33 -07:00
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
}
}