Skip to content

Commit

Permalink
[R4R] Unmerklize ConsensusParams.Hash() (#2609)
Browse files Browse the repository at this point in the history
* Hash() uses tmhash instead of merkle.SimpleHashFromMap

* marshal whole struct

* update comments

* update docs
  • Loading branch information
mossid authored and ebuchman committed Oct 15, 2018
1 parent 2646202 commit 4ab7dcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/spec/blockchain/blockchain.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 4ab7dcf

Please sign in to comment.