s/Validation/Commit/g

This commit is contained in:
Jae Kwon
2016-04-02 09:10:16 -07:00
parent daa76dcff0
commit f17c4c1d57
13 changed files with 206 additions and 234 deletions

View File

@@ -1,61 +0,0 @@
# `tendermint/block`
## Block
TODO: document
### Header
### Validation
### Data
## PartSet
PartSet is used to split a byteslice of data into parts (pieces) for transmission.
By splitting data into smaller parts and computing a Merkle root hash on the list,
you can verify that a part is legitimately part of the complete data, and the
part can be forwarded to other peers before all the parts are known. In short,
it's a fast way to propagate a large file over a gossip network.
PartSet was inspired by the LibSwift project.
Usage:
```Go
data := RandBytes(2 << 20) // Something large
partSet := NewPartSetFromData(data)
partSet.Total() // Total number of 4KB parts
partSet.Count() // Equal to the Total, since we already have all the parts
partSet.Hash() // The Merkle root hash
partSet.BitArray() // A BitArray of partSet.Total() 1's
header := partSet.Header() // Send this to the peer
header.Total // Total number of parts
header.Hash // The merkle root hash
// Now we'll reconstruct the data from the parts
partSet2 := NewPartSetFromHeader(header)
partSet2.Total() // Same total as partSet.Total()
partSet2.Count() // Zero, since this PartSet doesn't have any parts yet.
partSet2.Hash() // Same hash as in partSet.Hash()
partSet2.BitArray() // A BitArray of partSet.Total() 0's
// In a gossip network the parts would arrive in arbitrary order, perhaps
// in response to explicit requests for parts, or optimistically in response
// to the receiving peer's partSet.BitArray().
for !partSet2.IsComplete() {
part := receivePartFromGossipNetwork()
added, err := partSet2.AddPart(part)
if err != nil {
// A wrong part,
// the merkle trail does not hash to partSet2.Hash()
} else if !added {
// A duplicate part already received
}
}
data2, _ := ioutil.ReadAll(partSet2.GetReader())
bytes.Equal(data, data2) // true
```

View File

@@ -15,9 +15,9 @@ import (
const MaxBlockSize = 22020096 // 21MB TODO make it configurable
type Block struct {
*Header `json:"header"`
*Data `json:"data"`
LastValidation *Validation `json:"last_validation"`
*Header `json:"header"`
*Data `json:"data"`
LastCommit *Commit `json:"last_commit"`
}
// Basic validation that doesn't involve state data.
@@ -46,11 +46,11 @@ func (b *Block) ValidateBasic(chainID string, lastBlockHeight int, lastBlockHash
if !b.LastBlockParts.Equals(lastBlockParts) {
return errors.New(Fmt("Wrong Block.Header.LastBlockParts. Expected %v, got %v", lastBlockParts, b.LastBlockParts))
}
if !bytes.Equal(b.LastValidationHash, b.LastValidation.Hash()) {
return errors.New(Fmt("Wrong Block.Header.LastValidationHash. Expected %X, got %X", b.LastValidationHash, b.LastValidation.Hash()))
if !bytes.Equal(b.LastCommitHash, b.LastCommit.Hash()) {
return errors.New(Fmt("Wrong Block.Header.LastCommitHash. Expected %X, got %X", b.LastCommitHash, b.LastCommit.Hash()))
}
if b.Header.Height != 1 {
if err := b.LastValidation.ValidateBasic(); err != nil {
if err := b.LastCommit.ValidateBasic(); err != nil {
return err
}
}
@@ -65,8 +65,8 @@ func (b *Block) ValidateBasic(chainID string, lastBlockHeight int, lastBlockHash
}
func (b *Block) FillHeader() {
if b.LastValidationHash == nil {
b.LastValidationHash = b.LastValidation.Hash()
if b.LastCommitHash == nil {
b.LastCommitHash = b.LastCommit.Hash()
}
if b.DataHash == nil {
b.DataHash = b.Data.Hash()
@@ -76,7 +76,7 @@ func (b *Block) FillHeader() {
// Computes and returns the block hash.
// If the block is incomplete, block hash is nil for safety.
func (b *Block) Hash() []byte {
if b.Header == nil || b.Data == nil || b.LastValidation == nil {
if b.Header == nil || b.Data == nil || b.LastCommit == nil {
return nil
}
b.FillHeader()
@@ -115,7 +115,7 @@ func (b *Block) StringIndented(indent string) string {
%s}#%X`,
indent, b.Header.StringIndented(indent+" "),
indent, b.Data.StringIndented(indent+" "),
indent, b.LastValidation.StringIndented(indent+" "),
indent, b.LastCommit.StringIndented(indent+" "),
indent, b.Hash())
}
@@ -130,17 +130,17 @@ func (b *Block) StringShort() string {
//-----------------------------------------------------------------------------
type Header struct {
ChainID string `json:"chain_id"`
Height int `json:"height"`
Time time.Time `json:"time"`
Fees int64 `json:"fees"`
NumTxs int `json:"num_txs"`
LastBlockHash []byte `json:"last_block_hash"`
LastBlockParts PartSetHeader `json:"last_block_parts"`
LastValidationHash []byte `json:"last_validation_hash"`
DataHash []byte `json:"data_hash"`
ValidatorsHash []byte `json:"validators_hash"`
AppHash []byte `json:"app_hash"` // state merkle root of txs from the previous block
ChainID string `json:"chain_id"`
Height int `json:"height"`
Time time.Time `json:"time"`
Fees int64 `json:"fees"`
NumTxs int `json:"num_txs"`
LastBlockHash []byte `json:"last_block_hash"`
LastBlockParts PartSetHeader `json:"last_block_parts"`
LastCommitHash []byte `json:"last_commit_hash"`
DataHash []byte `json:"data_hash"`
ValidatorsHash []byte `json:"validators_hash"`
AppHash []byte `json:"app_hash"` // state merkle root of txs from the previous block
}
// NOTE: hash is nil if required fields are missing.
@@ -156,7 +156,7 @@ func (h *Header) Hash() []byte {
"NumTxs": h.NumTxs,
"LastBlock": h.LastBlockHash,
"LastBlockParts": h.LastBlockParts,
"LastValidation": h.LastValidationHash,
"LastCommit": h.LastCommitHash,
"Data": h.DataHash,
"Validators": h.ValidatorsHash,
"App": h.AppHash,
@@ -172,10 +172,10 @@ func (h *Header) StringIndented(indent string) string {
%s Height: %v
%s Time: %v
%s Fees: %v
%s NumTxs: %v
%s NumTxs: %v
%s LastBlock: %X
%s LastBlockParts: %v
%s LastValidation: %X
%s LastCommit: %X
%s Data: %X
%s Validators: %X
%s App: %X
@@ -187,7 +187,7 @@ func (h *Header) StringIndented(indent string) string {
indent, h.NumTxs,
indent, h.LastBlockHash,
indent, h.LastBlockParts,
indent, h.LastValidationHash,
indent, h.LastCommitHash,
indent, h.DataHash,
indent, h.ValidatorsHash,
indent, h.AppHash,
@@ -196,8 +196,8 @@ func (h *Header) StringIndented(indent string) string {
//-------------------------------------
// NOTE: Validation is empty for height 1, but never nil.
type Validation struct {
// NOTE: Commit is empty for height 1, but never nil.
type Commit struct {
// NOTE: The Precommits are in order of address to preserve the bonded ValidatorSet order.
// Any peer with a block can gossip precommits by index with a peer without recalculating the
// active ValidatorSet.
@@ -209,121 +209,121 @@ type Validation struct {
bitArray *BitArray
}
func (v *Validation) FirstPrecommit() *Vote {
if len(v.Precommits) == 0 {
func (commit *Commit) FirstPrecommit() *Vote {
if len(commit.Precommits) == 0 {
return nil
}
if v.firstPrecommit != nil {
return v.firstPrecommit
if commit.firstPrecommit != nil {
return commit.firstPrecommit
}
for _, precommit := range v.Precommits {
for _, precommit := range commit.Precommits {
if precommit != nil {
v.firstPrecommit = precommit
commit.firstPrecommit = precommit
return precommit
}
}
return nil
}
func (v *Validation) Height() int {
if len(v.Precommits) == 0 {
func (commit *Commit) Height() int {
if len(commit.Precommits) == 0 {
return 0
}
return v.FirstPrecommit().Height
return commit.FirstPrecommit().Height
}
func (v *Validation) Round() int {
if len(v.Precommits) == 0 {
func (commit *Commit) Round() int {
if len(commit.Precommits) == 0 {
return 0
}
return v.FirstPrecommit().Round
return commit.FirstPrecommit().Round
}
func (v *Validation) Type() byte {
func (commit *Commit) Type() byte {
return VoteTypePrecommit
}
func (v *Validation) Size() int {
if v == nil {
func (commit *Commit) Size() int {
if commit == nil {
return 0
}
return len(v.Precommits)
return len(commit.Precommits)
}
func (v *Validation) BitArray() *BitArray {
if v.bitArray == nil {
v.bitArray = NewBitArray(len(v.Precommits))
for i, precommit := range v.Precommits {
v.bitArray.SetIndex(i, precommit != nil)
func (commit *Commit) BitArray() *BitArray {
if commit.bitArray == nil {
commit.bitArray = NewBitArray(len(commit.Precommits))
for i, precommit := range commit.Precommits {
commit.bitArray.SetIndex(i, precommit != nil)
}
}
return v.bitArray
return commit.bitArray
}
func (v *Validation) GetByIndex(index int) *Vote {
return v.Precommits[index]
func (commit *Commit) GetByIndex(index int) *Vote {
return commit.Precommits[index]
}
func (v *Validation) IsCommit() bool {
if len(v.Precommits) == 0 {
func (commit *Commit) IsCommit() bool {
if len(commit.Precommits) == 0 {
return false
}
return true
}
func (v *Validation) ValidateBasic() error {
if len(v.Precommits) == 0 {
return errors.New("No precommits in validation")
func (commit *Commit) ValidateBasic() error {
if len(commit.Precommits) == 0 {
return errors.New("No precommits in commit")
}
height, round := v.Height(), v.Round()
for _, precommit := range v.Precommits {
height, round := commit.Height(), commit.Round()
for _, precommit := range commit.Precommits {
// It's OK for precommits to be missing.
if precommit == nil {
continue
}
// Ensure that all votes are precommits
if precommit.Type != VoteTypePrecommit {
return fmt.Errorf("Invalid validation vote. Expected precommit, got %v",
return fmt.Errorf("Invalid commit vote. Expected precommit, got %v",
precommit.Type)
}
// Ensure that all heights are the same
if precommit.Height != height {
return fmt.Errorf("Invalid validation precommit height. Expected %v, got %v",
return fmt.Errorf("Invalid commit precommit height. Expected %v, got %v",
height, precommit.Height)
}
// Ensure that all rounds are the same
if precommit.Round != round {
return fmt.Errorf("Invalid validation precommit round. Expected %v, got %v",
return fmt.Errorf("Invalid commit precommit round. Expected %v, got %v",
round, precommit.Round)
}
}
return nil
}
func (v *Validation) Hash() []byte {
if v.hash == nil {
bs := make([]interface{}, len(v.Precommits))
for i, precommit := range v.Precommits {
func (commit *Commit) Hash() []byte {
if commit.hash == nil {
bs := make([]interface{}, len(commit.Precommits))
for i, precommit := range commit.Precommits {
bs[i] = precommit
}
v.hash = merkle.SimpleHashFromBinaries(bs)
commit.hash = merkle.SimpleHashFromBinaries(bs)
}
return v.hash
return commit.hash
}
func (v *Validation) StringIndented(indent string) string {
if v == nil {
return "nil-Validation"
func (commit *Commit) StringIndented(indent string) string {
if commit == nil {
return "nil-Commit"
}
precommitStrings := make([]string, len(v.Precommits))
for i, precommit := range v.Precommits {
precommitStrings := make([]string, len(commit.Precommits))
for i, precommit := range commit.Precommits {
precommitStrings[i] = precommit.String()
}
return fmt.Sprintf(`Validation{
return fmt.Sprintf(`Commit{
%s Precommits: %v
%s}#%X`,
indent, strings.Join(precommitStrings, "\n"+indent+" "),
indent, v.hash)
indent, commit.hash)
}
//-----------------------------------------------------------------------------

View File

@@ -206,37 +206,37 @@ func (valSet *ValidatorSet) Iterate(fn func(index int, val *Validator) bool) {
}
// Verify that +2/3 of the set had signed the given signBytes
func (valSet *ValidatorSet) VerifyValidation(chainID string,
hash []byte, parts PartSetHeader, height int, v *Validation) error {
if valSet.Size() != len(v.Precommits) {
return fmt.Errorf("Invalid validation -- wrong set size: %v vs %v", valSet.Size(), len(v.Precommits))
func (valSet *ValidatorSet) VerifyCommit(chainID string,
hash []byte, parts PartSetHeader, height int, commit *Commit) error {
if valSet.Size() != len(commit.Precommits) {
return fmt.Errorf("Invalid commit -- wrong set size: %v vs %v", valSet.Size(), len(commit.Precommits))
}
if height != v.Height() {
return fmt.Errorf("Invalid validation -- wrong height: %v vs %v", height, v.Height())
if height != commit.Height() {
return fmt.Errorf("Invalid commit -- wrong height: %v vs %v", height, commit.Height())
}
talliedVotingPower := int64(0)
round := v.Round()
round := commit.Round()
for idx, precommit := range v.Precommits {
for idx, precommit := range commit.Precommits {
// may be nil if validator skipped.
if precommit == nil {
continue
}
if precommit.Height != height {
return fmt.Errorf("Invalid validation -- wrong height: %v vs %v", height, precommit.Height)
return fmt.Errorf("Invalid commit -- wrong height: %v vs %v", height, precommit.Height)
}
if precommit.Round != round {
return fmt.Errorf("Invalid validation -- wrong round: %v vs %v", round, precommit.Round)
return fmt.Errorf("Invalid commit -- wrong round: %v vs %v", round, precommit.Round)
}
if precommit.Type != VoteTypePrecommit {
return fmt.Errorf("Invalid validation -- not precommit @ index %v", idx)
return fmt.Errorf("Invalid commit -- not precommit @ index %v", idx)
}
_, val := valSet.GetByIndex(idx)
// Validate signature
precommitSignBytes := SignBytes(chainID, precommit)
if !val.PubKey.VerifyBytes(precommitSignBytes, precommit.Signature) {
return fmt.Errorf("Invalid validation -- invalid signature: %v", precommit)
return fmt.Errorf("Invalid commit -- invalid signature: %v", precommit)
}
if !bytes.Equal(precommit.BlockHash, hash) {
continue // Not an error, but doesn't count
@@ -251,7 +251,7 @@ func (valSet *ValidatorSet) VerifyValidation(chainID string,
if talliedVotingPower > valSet.TotalVotingPower()*2/3 {
return nil
} else {
return fmt.Errorf("Invalid validation -- insufficient voting power: got %v, needed %v",
return fmt.Errorf("Invalid commit -- insufficient voting power: got %v, needed %v",
talliedVotingPower, (valSet.TotalVotingPower()*2/3 + 1))
}
}

View File

@@ -71,9 +71,9 @@ func (vote *Vote) String() string {
}
//--------------------------------------------------------------------------------
// TODO: Move blocks/Validation to here?
// TODO: Move blocks/Commit to here?
// Common interface between *consensus.VoteSet and types.Validation
// Common interface between *consensus.VoteSet and types.Commit
type VoteSetReader interface {
Height() int
Round() int

View File

@@ -270,16 +270,16 @@ func (voteSet *VoteSet) StringShort() string {
}
//--------------------------------------------------------------------------------
// Validation
// Commit
func (voteSet *VoteSet) MakeValidation() *Validation {
func (voteSet *VoteSet) MakeCommit() *Commit {
if voteSet.type_ != VoteTypePrecommit {
PanicSanity("Cannot MakeValidation() unless VoteSet.Type is VoteTypePrecommit")
PanicSanity("Cannot MakeCommit() unless VoteSet.Type is VoteTypePrecommit")
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
if len(voteSet.maj23Hash) == 0 {
PanicSanity("Cannot MakeValidation() unless a blockhash has +2/3")
PanicSanity("Cannot MakeCommit() unless a blockhash has +2/3")
}
precommits := make([]*Vote, voteSet.valSet.Size())
voteSet.valSet.Iterate(func(valIndex int, val *Validator) bool {
@@ -296,7 +296,7 @@ func (voteSet *VoteSet) MakeValidation() *Validation {
precommits[valIndex] = vote
return false
})
return &Validation{
return &Commit{
Precommits: precommits,
}
}

View File

@@ -232,7 +232,7 @@ func TestBadVotes(t *testing.T) {
}
}
func TestMakeValidation(t *testing.T) {
func TestMakeCommit(t *testing.T) {
height, round := 1, 0
voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrecommit, 10, 1)
blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
@@ -245,8 +245,8 @@ func TestMakeValidation(t *testing.T) {
signAddVote(privValidators[i], vote, voteSet)
}
// MakeValidation should fail.
AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeValidation() })
// MakeCommit should fail.
AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeCommit() })
// 7th voted for some other block.
{
@@ -260,16 +260,16 @@ func TestMakeValidation(t *testing.T) {
signAddVote(privValidators[7], vote, voteSet)
}
validation := voteSet.MakeValidation()
commit := voteSet.MakeCommit()
// Validation should have 10 elements
if len(validation.Precommits) != 10 {
t.Errorf("Validation Precommits should have the same number of precommits as validators")
// Commit should have 10 elements
if len(commit.Precommits) != 10 {
t.Errorf("Commit Precommits should have the same number of precommits as validators")
}
// Ensure that Validation precommits are ordered.
if err := validation.ValidateBasic(); err != nil {
t.Errorf("Error in Validation.ValidateBasic(): %v", err)
// Ensure that Commit precommits are ordered.
if err := commit.ValidateBasic(); err != nil {
t.Errorf("Error in Commit.ValidateBasic(): %v", err)
}
}