[R4R] Unmerklize ConsensusParams.Hash() (#2609)
* Hash() uses tmhash instead of merkle.SimpleHashFromMap * marshal whole struct * update comments * update docs
This commit is contained in:
parent
26462025bc
commit
4ab7dcf3ac
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue