Skip to content

Commit 151d6c5

Browse files
authored
feat: add header hash to Context (cosmos#9390)
* baseapp, types: add header hash to * changelog
1 parent 61bd6cb commit 151d6c5

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ if input key is empty, or input data contains empty key.
126126

127127
### Improvements
128128

129+
* (baseapp, types) [#\9390](https://github.com/cosmos/cosmos-sdk/pull/9390) Add current block header hash to `Context`
129130
* (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata
130131
* (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
131132
* (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.

baseapp/abci.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
164164
// by InitChain. Context is now updated with Header information.
165165
app.deliverState.ctx = app.deliverState.ctx.
166166
WithBlockHeader(req.Header).
167-
WithBlockHeight(req.Header.Height)
167+
WithBlockHeight(req.Header.Height).
168+
WithHeaderHash(req.Hash)
168169
}
169170

170171
// add block gas meter

types/context.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/gogo/protobuf/proto"
88
abci "github.com/tendermint/tendermint/abci/types"
9+
tmbytes "github.com/tendermint/tendermint/libs/bytes"
910
"github.com/tendermint/tendermint/libs/log"
1011
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1112

@@ -25,6 +26,7 @@ type Context struct {
2526
ctx context.Context
2627
ms MultiStore
2728
header tmproto.Header
29+
headerHash tmbytes.HexBytes
2830
chainID string
2931
txBytes []byte
3032
logger log.Logger
@@ -63,6 +65,13 @@ func (c Context) BlockHeader() tmproto.Header {
6365
return *msg
6466
}
6567

68+
// HeaderHash returns a copy of the header hash obtained during abci.RequestBeginBlock
69+
func (c Context) HeaderHash() tmbytes.HexBytes {
70+
hash := make([]byte, len(c.headerHash))
71+
copy(hash, c.headerHash)
72+
return hash
73+
}
74+
6675
func (c Context) ConsensusParams() *abci.ConsensusParams {
6776
return proto.Clone(c.consParams).(*abci.ConsensusParams)
6877
}
@@ -104,6 +113,15 @@ func (c Context) WithBlockHeader(header tmproto.Header) Context {
104113
return c
105114
}
106115

116+
// WithHeaderHash returns a Context with an updated tendermint block header hash.
117+
func (c Context) WithHeaderHash(hash []byte) Context {
118+
temp := make([]byte, len(hash))
119+
copy(temp, hash)
120+
121+
c.headerHash = temp
122+
return c
123+
}
124+
107125
// WithBlockTime returns a Context with an updated tendermint block header time in UTC time
108126
func (c Context) WithBlockTime(newTime time.Time) Context {
109127
newHeader := c.BlockHeader()

types/context_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
9292
meter := types.NewGasMeter(10000)
9393
blockGasMeter := types.NewGasMeter(20000)
9494
minGasPrices := types.DecCoins{types.NewInt64DecCoin("feetoken", 1)}
95+
headerHash := []byte("headerHash")
9596

9697
ctx = types.NewContext(nil, header, ischeck, logger)
9798
s.Require().Equal(header, ctx.BlockHeader())
@@ -103,7 +104,8 @@ func (s *contextTestSuite) TestContextWithCustom() {
103104
WithVoteInfos(voteinfos).
104105
WithGasMeter(meter).
105106
WithMinGasPrices(minGasPrices).
106-
WithBlockGasMeter(blockGasMeter)
107+
WithBlockGasMeter(blockGasMeter).
108+
WithHeaderHash(headerHash)
107109
s.Require().Equal(height, ctx.BlockHeight())
108110
s.Require().Equal(chainid, ctx.ChainID())
109111
s.Require().Equal(ischeck, ctx.IsCheckTx())
@@ -113,6 +115,7 @@ func (s *contextTestSuite) TestContextWithCustom() {
113115
s.Require().Equal(meter, ctx.GasMeter())
114116
s.Require().Equal(minGasPrices, ctx.MinGasPrices())
115117
s.Require().Equal(blockGasMeter, ctx.BlockGasMeter())
118+
s.Require().Equal(headerHash, ctx.HeaderHash().Bytes())
116119
s.Require().False(ctx.WithIsCheckTx(false).IsCheckTx())
117120

118121
// test IsReCheckTx

0 commit comments

Comments
 (0)