Add ConsensusHash to header

This commit is contained in:
Ethan Frey
2017-12-14 10:28:04 +01:00
committed by Ethan Buchman
parent 56cada6a0c
commit d151e36ea8
7 changed files with 33 additions and 14 deletions

View File

@ -25,7 +25,7 @@ type Block struct {
// TODO: Add version information to the Block struct.
func MakeBlock(height int64, chainID string, txs []Tx,
totalTxs int64, commit *Commit,
prevBlockID BlockID, valHash, appHash []byte,
prevBlockID BlockID, valHash, appHash, consensusHash []byte,
partSize int) (*Block, *PartSet) {
newTxs := int64(len(txs))
@ -39,6 +39,7 @@ func MakeBlock(height int64, chainID string, txs []Tx,
LastBlockID: prevBlockID,
ValidatorsHash: valHash,
AppHash: appHash, // state merkle root of txs from the previous block.
ConsensusHash: consensusHash,
},
LastCommit: commit,
Data: &Data{
@ -52,7 +53,7 @@ func MakeBlock(height int64, chainID string, txs []Tx,
// ValidateBasic performs basic validation that doesn't involve state data.
func (b *Block) ValidateBasic(chainID string, lastBlockHeight int64,
lastBlockTotalTx int64, lastBlockID BlockID,
lastBlockTime time.Time, appHash []byte) error {
lastBlockTime time.Time, appHash, consensusHash []byte) error {
if b.ChainID != chainID {
return errors.New(cmn.Fmt("Wrong Block.Header.ChainID. Expected %v, got %v", chainID, b.ChainID))
@ -91,6 +92,13 @@ func (b *Block) ValidateBasic(chainID string, lastBlockHeight int64,
if !bytes.Equal(b.AppHash, appHash) {
return errors.New(cmn.Fmt("Wrong Block.Header.AppHash. Expected %X, got %v", appHash, b.AppHash))
}
// TODO: make testing easier
if len(consensusHash) == 0 {
panic("food")
}
if !bytes.Equal(b.ConsensusHash, consensusHash) {
return errors.New(cmn.Fmt("Wrong Block.Header.ConsensusHash. Expected %X, got %v", consensusHash, b.ConsensusHash))
}
// NOTE: the AppHash and ValidatorsHash are validated later.
return nil
}
@ -178,6 +186,7 @@ type Header struct {
DataHash data.Bytes `json:"data_hash"` // transactions
ValidatorsHash data.Bytes `json:"validators_hash"` // validators for the current block
AppHash data.Bytes `json:"app_hash"` // state after txs from the previous block
ConsensusHash data.Bytes `json:"consensus_hash"` // consensus params for current block
}
// Hash returns the hash of the header.
@ -197,6 +206,7 @@ func (h *Header) Hash() data.Bytes {
"Data": h.DataHash,
"Validators": h.ValidatorsHash,
"App": h.AppHash,
"Consensus": h.ConsensusHash,
})
}
@ -216,6 +226,7 @@ func (h *Header) StringIndented(indent string) string {
%s Data: %v
%s Validators: %v
%s App: %v
%s Conensus: %v
%s}#%v`,
indent, h.ChainID,
indent, h.Height,
@ -227,6 +238,7 @@ func (h *Header) StringIndented(indent string) string {
indent, h.DataHash,
indent, h.ValidatorsHash,
indent, h.AppHash,
indent, h.ConsensusHash,
indent, h.Hash())
}

View File

@ -13,6 +13,7 @@ func TestValidateBlock(t *testing.T) {
lastID := makeBlockID()
valHash := []byte("val")
appHash := []byte("app")
consensusHash := []byte("consensus-params")
h := int64(3)
voteSet, _, vals := randVoteSet(h-1, 1, VoteTypePrecommit,
@ -21,33 +22,36 @@ func TestValidateBlock(t *testing.T) {
require.NoError(t, err)
block, _ := MakeBlock(h, "hello", txs, 10, commit,
lastID, valHash, appHash, 2)
lastID, valHash, appHash, consensusHash, 2)
require.NotNil(t, block)
// proper block must pass
err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash)
err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash, consensusHash)
require.NoError(t, err)
// wrong chain fails
err = block.ValidateBasic("other", h-1, 10, lastID, block.Time, appHash)
err = block.ValidateBasic("other", h-1, 10, lastID, block.Time, appHash, consensusHash)
require.Error(t, err)
// wrong height fails
err = block.ValidateBasic("hello", h+4, 10, lastID, block.Time, appHash)
err = block.ValidateBasic("hello", h+4, 10, lastID, block.Time, appHash, consensusHash)
require.Error(t, err)
// wrong total tx fails
err = block.ValidateBasic("hello", h-1, 15, lastID, block.Time, appHash)
err = block.ValidateBasic("hello", h-1, 15, lastID, block.Time, appHash, consensusHash)
require.Error(t, err)
// wrong blockid fails
err = block.ValidateBasic("hello", h-1, 10, makeBlockID(), block.Time, appHash)
err = block.ValidateBasic("hello", h-1, 10, makeBlockID(), block.Time, appHash, consensusHash)
require.Error(t, err)
// wrong app hash fails
err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, []byte("bad-hash"))
err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, []byte("bad-hash"), consensusHash)
require.Error(t, err)
// wrong consensus hash fails
err = block.ValidateBasic("hello", h-1, 10, lastID, block.Time, appHash, []byte("wrong-params"))
require.Error(t, err)
}
func makeBlockID() BlockID {