From 151d6c5e97e87d97f0f5872c35b43fc0a821f101 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Wed, 26 May 2021 04:36:42 -0400 Subject: [PATCH] feat: add header hash to `Context` (#9390) * baseapp, types: add header hash to * changelog --- CHANGELOG.md | 1 + baseapp/abci.go | 3 ++- types/context.go | 18 ++++++++++++++++++ types/context_test.go | 5 ++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40ce65063d97..9fea6ffe6d74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,7 @@ if input key is empty, or input data contains empty key. ### 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. diff --git a/baseapp/abci.go b/baseapp/abci.go index 399fc42efe58..a5cff07c5a93 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -164,7 +164,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 diff --git a/types/context.go b/types/context.go index 6d326fb73f19..35030dad9a96 100644 --- a/types/context.go +++ b/types/context.go @@ -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" @@ -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 @@ -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) } @@ -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() diff --git a/types/context_test.go b/types/context_test.go index 4af5d8390f2f..9533a52ee2e3 100644 --- a/types/context_test.go +++ b/types/context_test.go @@ -92,6 +92,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()) @@ -103,7 +104,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()) @@ -113,6 +115,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