Skip to content

Commit

Permalink
feat: add header hash to Context (#9390)
Browse files Browse the repository at this point in the history
* baseapp, types: add header hash to

* changelog

(cherry picked from commit 151d6c5)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
fedekunze authored and mergify-bot committed Jun 17, 2021
1 parent 35e9e7a commit e2dd75a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#9383](https://github.com/cosmos/cosmos-sdk/pull/9383) New CLI command `query ibc-transfer escrow-address <port> <channel id>` to get the escrow address for a channel; can be used to then query balance of escrowed tokens


<<<<<<< HEAD
## [v0.42.5](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.42.5) - 2021-05-18
=======
### Improvements

* (baseapp, types) [#\9390](https://github.com/cosmos/cosmos-sdk/pull/9390) Add current block header hash to `Context`
* (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata
* (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
* (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.
* (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) Add tracking module versions as per ADR-041
* (types) [\#8962](https://github.com/cosmos/cosmos-sdk/issues/8962) Add `Abs()` method to `sdk.Int`.
* (x/bank) [\#8950](https://github.com/cosmos/cosmos-sdk/pull/8950) Improve efficiency on supply updates.
* (store) [\#8012](https://github.com/cosmos/cosmos-sdk/pull/8012) Implementation of ADR-038 WriteListener and listen.KVStore
* (makefile) [\#7933](https://github.com/cosmos/cosmos-sdk/issues/7933) Use Docker to generate swagger files.
>>>>>>> 151d6c5e9 (feat: add header hash to `Context` (#9390))
### Bug Fixes

Expand Down
3 changes: 2 additions & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
// by InitChain. Context is now updated with Header information.
app.deliverState.ctx = app.deliverState.ctx.
WithBlockHeader(req.Header).
WithBlockHeight(req.Header.Height)
WithBlockHeight(req.Header.Height).
WithHeaderHash(req.Hash)
}

// add block gas meter
Expand Down
18 changes: 18 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

Expand All @@ -25,6 +26,7 @@ type Context struct {
ctx context.Context
ms MultiStore
header tmproto.Header
headerHash tmbytes.HexBytes
chainID string
txBytes []byte
logger log.Logger
Expand Down Expand Up @@ -63,6 +65,13 @@ func (c Context) BlockHeader() tmproto.Header {
return *msg
}

// HeaderHash returns a copy of the header hash obtained during abci.RequestBeginBlock
func (c Context) HeaderHash() tmbytes.HexBytes {
hash := make([]byte, len(c.headerHash))
copy(hash, c.headerHash)
return hash
}

func (c Context) ConsensusParams() *abci.ConsensusParams {
return proto.Clone(c.consParams).(*abci.ConsensusParams)
}
Expand Down Expand Up @@ -104,6 +113,15 @@ func (c Context) WithBlockHeader(header tmproto.Header) Context {
return c
}

// WithHeaderHash returns a Context with an updated tendermint block header hash.
func (c Context) WithHeaderHash(hash []byte) Context {
temp := make([]byte, len(hash))
copy(temp, hash)

c.headerHash = temp
return c
}

// WithBlockTime returns a Context with an updated tendermint block header time in UTC time
func (c Context) WithBlockTime(newTime time.Time) Context {
newHeader := c.BlockHeader()
Expand Down
5 changes: 4 additions & 1 deletion types/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
meter := types.NewGasMeter(10000)
blockGasMeter := types.NewGasMeter(20000)
minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)}
headerHash := []byte("headerHash")

ctx = types.NewContext(nil, header, ischeck, logger)
s.Require().Equal(header, ctx.BlockHeader())
Expand All @@ -114,7 +115,8 @@ func (s *contextTestSuite) TestContextWithCustom() {
WithVoteInfos(voteinfos).
WithGasMeter(meter).
WithMinGasPrices(minGasPrices).
WithBlockGasMeter(blockGasMeter)
WithBlockGasMeter(blockGasMeter).
WithHeaderHash(headerHash)
s.Require().Equal(height, ctx.BlockHeight())
s.Require().Equal(chainid, ctx.ChainID())
s.Require().Equal(ischeck, ctx.IsCheckTx())
Expand All @@ -124,6 +126,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
s.Require().Equal(meter, ctx.GasMeter())
s.Require().Equal(minGasPrices, ctx.MinGasPrices())
s.Require().Equal(blockGasMeter, ctx.BlockGasMeter())
s.Require().Equal(headerHash, ctx.HeaderHash().Bytes())
s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx())

// test IsReCheckTx
Expand Down

0 comments on commit e2dd75a

Please sign in to comment.