Skip to content

Commit

Permalink
Problem: block gas used not set in context
Browse files Browse the repository at this point in the history
Solution:
- fix the way context is updated

block gas wanted

Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang committed Apr 3, 2024
1 parent 1c80a47 commit e41cfba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (bank) [#237](https://github.com/crypto-org-chain/cosmos-sdk/pull/237) Support virtual accounts in sending coins.
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API in `Context` to use new CoW branched cache store.
* [#248](https://github.com/crypto-org-chain/cosmos-sdk/pull/248) Init btree store lazily to save allocations when no content insert into it.
* [#252](https://github.com/crypto-org-chain/cosmos-sdk/pull/252) Add `BlockGasWanted` to `Context` to support feemarket module.

## [Unreleased-Upstream]

Expand Down
14 changes: 11 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -796,13 +796,21 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
app.finalizeBlockState.ms = app.finalizeBlockState.ms.SetTracingContext(nil).(storetypes.CacheMultiStore)
}

var blockGasUsed uint64
var (
blockGasUsed uint64
blockGasWanted uint64
)
for _, res := range txResults {
blockGasUsed += uint64(res.GasUsed)
blockGasWanted += uint64(res.GasWanted)
}
sdkCtx := app.finalizeBlockState.Context().WithBlockGasUsed(blockGasUsed)
app.finalizeBlockState.SetContext(
app.finalizeBlockState.Context().
WithBlockGasUsed(blockGasUsed).
WithBlockGasWanted(blockGasWanted),
)

endBlock, err := app.endBlock(sdkCtx)
endBlock, err := app.endBlock(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ func (app *BaseApp) deliverTxWithMultiStore(tx []byte, txIndex int, txMultiStore

// endBlock is an application-defined function that is called after transactions
// have been processed in FinalizeBlock.
func (app *BaseApp) endBlock(ctx context.Context) (sdk.EndBlock, error) {
func (app *BaseApp) endBlock(_ context.Context) (sdk.EndBlock, error) {
var endblock sdk.EndBlock

if app.endBlocker != nil {
Expand Down
8 changes: 8 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Context struct {
txCount int
// sum the gas used by all the transactions in the current block, only accessible by end blocker
blockGasUsed uint64
// sum the gas wanted by all the transactions in the current block, only accessible by end blocker
blockGasWanted uint64
}

// Proposed rename, not done to avoid API breakage
Expand Down Expand Up @@ -105,6 +107,7 @@ func (c Context) TxIndex() int { return c.txInd
func (c Context) MsgIndex() int { return c.msgIndex }
func (c Context) TxCount() int { return c.txCount }
func (c Context) BlockGasUsed() uint64 { return c.blockGasUsed }
func (c Context) BlockGasWanted() uint64 { return c.blockGasWanted }

// clone the header before returning
func (c Context) BlockHeader() cmtproto.Header {
Expand Down Expand Up @@ -346,6 +349,11 @@ func (c Context) WithBlockGasUsed(gasUsed uint64) Context {
return c
}

func (c Context) WithBlockGasWanted(gasWanted uint64) Context {
c.blockGasWanted = gasWanted
return c
}

// TODO: remove???
func (c Context) IsZero() bool {
return c.ms == nil
Expand Down

0 comments on commit e41cfba

Please sign in to comment.