Skip to content

Commit

Permalink
Remove block gas meter in occ (#407)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
This removes the block gas meter for occ, and will eventually be rebased
out with a corresponding change that will end up in main

## Testing performed to validate your change
loadtest cluster testing
  • Loading branch information
udpatil authored Jan 25, 2024
1 parent d766215 commit fb803dd
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 355 deletions.
28 changes: 4 additions & 24 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func (app *BaseApp) InitChain(ctx context.Context, req *abci.RequestInitChain) (
return
}

// add block gas meter for any genesis transactions (allow infinite gas)
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
app.prepareProposalState.ctx = app.prepareProposalState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
app.processProposalState.ctx = app.processProposalState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())

resp := app.initChainer(app.deliverState.ctx, *req)
app.initChainer(app.prepareProposalState.ctx, *req)
app.initChainer(app.processProposalState.ctx, *req)
Expand Down Expand Up @@ -1034,16 +1029,9 @@ func (app *BaseApp) ProcessProposal(ctx context.Context, req *abci.RequestProces
app.setProcessProposalHeader(header)
}

// add block gas meter
var gasMeter sdk.GasMeter
if maxGas := app.getMaximumBlockGas(app.processProposalState.ctx); maxGas > 0 {
gasMeter = sdk.NewGasMeter(maxGas)
} else {
gasMeter = sdk.NewInfiniteGasMeter()
}

// NOTE: header hash is not set in NewContext, so we manually set it here
app.prepareProcessProposalState(gasMeter, req.Hash)

app.prepareProcessProposalState(req.Hash)

defer func() {
if err := recover(); err != nil {
Expand Down Expand Up @@ -1116,22 +1104,14 @@ func (app *BaseApp) FinalizeBlock(ctx context.Context, req *abci.RequestFinalize
app.setDeliverStateHeader(header)
}

// add block gas meter
var gasMeter sdk.GasMeter
if maxGas := app.getMaximumBlockGas(app.deliverState.ctx); maxGas > 0 {
gasMeter = sdk.NewGasMeter(maxGas)
} else {
gasMeter = sdk.NewInfiniteGasMeter()
}

// NOTE: header hash is not set in NewContext, so we manually set it here

app.prepareDeliverState(gasMeter, req.Hash)
app.prepareDeliverState(req.Hash)

// we also set block gas meter to checkState in case the application needs to
// verify gas consumption during (Re)CheckTx
if app.checkState != nil {
app.checkState.SetContext(app.checkState.ctx.WithBlockGasMeter(gasMeter).WithHeaderHash(req.Hash))
app.checkState.SetContext(app.checkState.ctx.WithHeaderHash(req.Hash))
}

if app.finalizeBlocker != nil {
Expand Down
57 changes: 3 additions & 54 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ func (app *BaseApp) preparePrepareProposalState() {
}
}

func (app *BaseApp) prepareProcessProposalState(gasMeter sdk.GasMeter, headerHash []byte) {
app.processProposalState.SetContext(app.processProposalState.Context().WithBlockGasMeter(gasMeter).
func (app *BaseApp) prepareProcessProposalState(headerHash []byte) {
app.processProposalState.SetContext(app.processProposalState.Context().
WithHeaderHash(headerHash).
WithConsensusParams(app.GetConsensusParams(app.processProposalState.Context())))

Expand All @@ -623,9 +623,8 @@ func (app *BaseApp) prepareProcessProposalState(gasMeter sdk.GasMeter, headerHas
}
}

func (app *BaseApp) prepareDeliverState(gasMeter sdk.GasMeter, headerHash []byte) {
func (app *BaseApp) prepareDeliverState(headerHash []byte) {
app.deliverState.SetContext(app.deliverState.Context().
WithBlockGasMeter(gasMeter).
WithHeaderHash(headerHash).
WithConsensusParams(app.GetConsensusParams(app.deliverState.Context())))
}
Expand Down Expand Up @@ -724,27 +723,6 @@ func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *tmproto.ConsensusP
app.paramStore.Set(ctx, ParamStoreKeyABCIParams, cp.Abci)
}

// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
// if maximum block gas is less than negative one and returns zero if negative
// one.
func (app *BaseApp) getMaximumBlockGas(ctx sdk.Context) uint64 {
cp := app.GetConsensusParams(ctx)
if cp == nil || cp.Block == nil {
return 0
}

maxGas := cp.Block.MaxGas

// TODO::: This is a temporary fix, max gas causes non-deterministic behavior
// with parallel TX
switch {
case maxGas < -1:
panic(fmt.Sprintf("invalid maximum block gas: %d", maxGas))
default:
return 0
}
}

func (app *BaseApp) validateHeight(req abci.RequestBeginBlock) error {
if req.Header.Height < 1 {
return fmt.Errorf("invalid height: %d", req.Header.Height)
Expand Down Expand Up @@ -879,11 +857,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf

ms := ctx.MultiStore()

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
return gInfo, nil, nil, -1, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
}

defer func() {
if r := recover(); r != nil {
acltypes.SendAllSignalsForTx(ctx.TxCompletionChannels())
Expand All @@ -896,27 +869,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf
gInfo = sdk.GasInfo{GasWanted: gasWanted, GasUsed: ctx.GasMeter().GasConsumed()}
}()

blockGasConsumed := false
// consumeBlockGas makes sure block gas is consumed at most once. It must happen after
// tx processing, and must be execute even if tx processing fails. Hence we use trick with `defer`
consumeBlockGas := func() {
if !blockGasConsumed {
blockGasConsumed = true
ctx.BlockGasMeter().ConsumeGas(
ctx.GasMeter().GasConsumedToLimit(), "block gas meter",
)
}
}

// If BlockGasMeter() panics it will be caught by the above recover and will
// return an error - in any case BlockGasMeter will consume gas past the limit.
//
// NOTE: This must exist in a separate defer function for the above recovery
// to recover from this one.
if mode == runTxModeDeliver {
defer consumeBlockGas()
}

tx, err := app.txDecoder(txBytes)
if err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
Expand Down Expand Up @@ -1004,9 +956,6 @@ func (app *BaseApp) runTx(ctx sdk.Context, mode runTxMode, txBytes []byte) (gInf
result, err = app.runMsgs(runMsgCtx, msgs, mode)

if err == nil && mode == runTxModeDeliver {
// When block gas exceeds, it'll panic and won't commit the cached store.
consumeBlockGas()

msCache.Write()
}
// we do this since we will only be looking at result in DeliverTx
Expand Down
202 changes: 0 additions & 202 deletions baseapp/block_gas_test.go

This file was deleted.

1 change: 0 additions & 1 deletion baseapp/deliver_tx_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestDeliverTxBatch(t *testing.T) {
for blockN := 0; blockN < nBlocks; blockN++ {
header := tmproto.Header{Height: int64(blockN) + 1}
app.setDeliverState(header)
app.deliverState.ctx = app.deliverState.ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
app.BeginBlock(app.deliverState.ctx, abci.RequestBeginBlock{Header: header})

var requests []*sdk.DeliverTxEntry
Expand Down
Loading

0 comments on commit fb803dd

Please sign in to comment.