diff --git a/docs/spec/blockchain/blockchain.md b/docs/spec/blockchain/blockchain.md index 89ab1b4f7..029b64fac 100644 --- a/docs/spec/blockchain/blockchain.md +++ b/docs/spec/blockchain/blockchain.md @@ -320,10 +320,10 @@ next validator sets Merkle root. ### ConsensusParamsHash ```go -block.ConsensusParamsHash == SimpleMerkleRoot(state.ConsensusParams) +block.ConsensusParamsHash == tmhash(amino(state.ConsensusParams)) ``` -Simple Merkle root of the consensus parameters. +Hash of the amino-encoded consensus parameters. ### AppHash diff --git a/types/params.go b/types/params.go index 129d47627..ed1e7963b 100644 --- a/types/params.go +++ b/types/params.go @@ -2,7 +2,7 @@ package types import ( abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/merkle" + "github.com/tendermint/tendermint/crypto/tmhash" cmn "github.com/tendermint/tendermint/libs/common" ) @@ -80,13 +80,18 @@ func (params *ConsensusParams) Validate() error { return nil } -// Hash returns a merkle hash of the parameters to store in the block header +// Hash returns a hash of the parameters to store in the block header +// No Merkle tree here, only three values are hashed here +// thus benefit from saving space < drawbacks from proofs' overhead +// Revisit this function if new fields are added to ConsensusParams func (params *ConsensusParams) Hash() []byte { - return merkle.SimpleHashFromMap(map[string][]byte{ - "block_size_max_bytes": cdcEncode(params.BlockSize.MaxBytes), - "block_size_max_gas": cdcEncode(params.BlockSize.MaxGas), - "evidence_params_max_age": cdcEncode(params.EvidenceParams.MaxAge), - }) + hasher := tmhash.New() + bz := cdcEncode(params) + if bz == nil { + panic("cannot fail to encode ConsensusParams") + } + hasher.Write(bz) + return hasher.Sum(nil) } // Update returns a copy of the params with updates from the non-zero fields of p2. diff --git a/types/params_test.go b/types/params_test.go index 888b678b4..2936e5a4e 100644 --- a/types/params_test.go +++ b/types/params_test.go @@ -53,6 +53,11 @@ func TestConsensusParamsHash(t *testing.T) { makeParams(4, 2, 3), makeParams(1, 4, 3), makeParams(1, 2, 4), + makeParams(2, 5, 7), + makeParams(1, 7, 6), + makeParams(9, 5, 4), + makeParams(7, 8, 9), + makeParams(4, 6, 5), } hashes := make([][]byte, len(params))