Skip to content

Commit

Permalink
Problem: block gas used not set in context (cosmos#252)
Browse files Browse the repository at this point in the history
Solution:
- fix the way context is updated
  • Loading branch information
yihuang authored and mmsqe committed Dec 19, 2024
1 parent 60405c5 commit 4917793
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,20 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Finaliz
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)
endBlock, err := app.endBlock(sdkCtx)
app.finalizeBlockState.SetContext(
app.finalizeBlockState.Context().
WithBlockGasUsed(blockGasUsed).
WithBlockGasWanted(blockGasWanted),
)
endBlock, err := app.endBlock(ctx)
if err != nil {
return nil, err
}
Expand Down
8 changes: 8 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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 @@ -104,6 +106,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 }

// BlockHeader returns the header by value.
func (c Context) BlockHeader() cmtproto.Header {
Expand Down Expand Up @@ -354,6 +357,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 4917793

Please sign in to comment.