[R4R] Unmerklize ConsensusParams.Hash() (#2609)

* Hash() uses tmhash instead of merkle.SimpleHashFromMap

* marshal whole struct

* update comments

* update docs
This commit is contained in:
Joon
2018-10-16 05:31:27 +09:00
committed by Ethan Buchman
parent 26462025bc
commit 4ab7dcf3ac
3 changed files with 19 additions and 9 deletions

View File

@ -320,10 +320,10 @@ next validator sets Merkle root.
### ConsensusParamsHash ### ConsensusParamsHash
```go ```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 ### AppHash

View File

@ -2,7 +2,7 @@ package types
import ( import (
abci "github.com/tendermint/tendermint/abci/types" 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" cmn "github.com/tendermint/tendermint/libs/common"
) )
@ -80,13 +80,18 @@ func (params *ConsensusParams) Validate() error {
return nil 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 { func (params *ConsensusParams) Hash() []byte {
return merkle.SimpleHashFromMap(map[string][]byte{ hasher := tmhash.New()
"block_size_max_bytes": cdcEncode(params.BlockSize.MaxBytes), bz := cdcEncode(params)
"block_size_max_gas": cdcEncode(params.BlockSize.MaxGas), if bz == nil {
"evidence_params_max_age": cdcEncode(params.EvidenceParams.MaxAge), 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. // Update returns a copy of the params with updates from the non-zero fields of p2.

View File

@ -53,6 +53,11 @@ func TestConsensusParamsHash(t *testing.T) {
makeParams(4, 2, 3), makeParams(4, 2, 3),
makeParams(1, 4, 3), makeParams(1, 4, 3),
makeParams(1, 2, 4), 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)) hashes := make([][]byte, len(params))