tendermint/consensus/pol.go

102 lines
3.1 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
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
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 {
2015-05-01 17:26:49 -07:00
Round uint `json:"round"`
Signature account.SignatureEd25519 `json:"signature"`
}
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 {
2015-05-01 17:26:49 -07:00
Height uint `json:"height"`
Round uint `json:"round"`
BlockHash []byte `json:"block_hash"` // Could be nil, which makes this a proof of unlock.
BlockParts types.PartSetHeader `json:"block_parts"` // When BlockHash is nil, this is zero.
Votes []POLVoteSignature `json:"votes"` // 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 *sm.ValidatorSet) error {
if uint(len(pol.Votes)) != valSet.Size() {
2015-04-15 21:49:14 -07:00
return fmt.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 := account.SignBytes(&types.Vote{
Height: pol.Height, Round: pol.Round, Type: types.VoteTypePrevote,
2014-10-30 03:32:09 -07:00
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 = account.SignBytes(&types.Vote{
Height: pol.Height, Round: vote.Round, Type: types.VoteTypeCommit,
BlockHash: pol.BlockHash,
BlockParts: pol.BlockParts,
})
2014-12-17 01:37:13 -08:00
} else if vote.Round > pol.Round {
2015-04-15 21:49:14 -07:00
return fmt.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 {
2015-04-15 21:49:14 -07:00
return fmt.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) {
2015-04-15 21:49:14 -07:00
return fmt.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 {
2015-04-15 21:49:14 -07:00
return fmt.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
}
}
func (pol *POL) MakePartSet() *types.PartSet {
return types.NewPartSetFromData(binary.BinaryBytes(pol))
}