Skip to content

Commit

Permalink
- add adjustment calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
StrathCole committed Sep 23, 2024
1 parent e859a81 commit 7611bb6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
50 changes: 45 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
tmos "github.com/cometbft/cometbft/libs/os"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
nodeservice "github.com/cosmos/cosmos-sdk/client/grpc/node"
Expand Down Expand Up @@ -128,14 +129,53 @@ type TerraApp struct {
func (app *TerraApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
res := app.BaseApp.CheckTx(req)

// the ctx here is just for the logger, so it might be okay to remove it
// and the logging in the event parsing
ctx := app.NewContext(true, tmproto.Header{})

maxGas := app.BaseApp.GetConsensusParams(ctx).Block.MaxGas
// fetch consumed tax gas from events
taxGas := sdkmath.ZeroInt()
for _, event := range res.Events {
if event.Type == "tax2gas" {
for _, attr := range event.Attributes {
if string(attr.Key) == "tax_gas" {

Check failure on line 141 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary conversion (unconvert)
value, ok := sdkmath.NewIntFromString(string(attr.Value))

Check failure on line 142 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary conversion (unconvert)
if !ok {
ctx.Logger().Error("failed to parse tax gas from events", "value", string(attr.Value))

Check failure on line 144 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary conversion (unconvert)
continue
}

taxGas = taxGas.Add(value)
}
}
}
}

gasWanted := uint64(res.GasWanted)
gasUsed := uint64(res.GasUsed)
subTaxGas := taxGas.Uint64()

// check how many times the gas used fits into the gas wanted
// if it doesn't fit, we need to adjust the gas wanted
multiple := sdkmath.LegacyNewDec(res.GasWanted).Quo(sdkmath.LegacyNewDec(res.GasUsed).Add(taxGas.ToLegacyDec()))

// we have a multiplier, so we know approximately how often tax gas can be subtracted
// we should assume everything >= 0.9 should be subtracted. Using >= 1 would have issues with approximations
if multiple.GTE(sdkmath.LegacyNewDecWithPrec(9, 1)) {
// adjust gas wanted by the tax gas
subTaxGas = taxGas.ToLegacyDec().Mul(multiple).TruncateInt().Uint64()
}

if gasWanted > subTaxGas {
gasWanted -= subTaxGas
}

//maxGas := app.BaseApp.GetConsensusParams(ctx).Block.MaxGas

Check failure on line 173 in app/app.go

View workflow job for this annotation

GitHub Actions / golangci-lint

commentFormatting: put a space between `//` and comment text (gocritic)
//fmt.Println("GasWanted", res.GasWanted, "GasUsed", res.GasUsed, "TaxGas", taxGas, "MaxGas", maxGas, "Multiple", multiple, "GasWantedAdjusted", gasWanted)

// Adjust GasWanted if necessary
if uint64(res.GasWanted) > uint64(maxGas) {
// Set GasWanted to maxGasWanted to satisfy the mempool's check
res.GasWanted = maxGas
// if the gas wanted is still higher than the gas used, we can adjust the gas wanted
if gasWanted >= gasUsed {
res.GasWanted = int64(gasWanted)
}

return res
Expand Down
3 changes: 3 additions & 0 deletions x/tax2gas/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate
}
}

// add the consumed tax gas to the transaction logs
ctx.EventManager().EmitEvent(sdk.NewEvent("tax2gas", sdk.NewAttribute("tax_gas", taxGas.String())))

// Deduct the gas consumed amount spent on ante handler
totalGasRemaining := sdkmath.NewInt(int64(totalGasConsumed - anteConsumedGas)).Add(taxGas)

Expand Down

0 comments on commit 7611bb6

Please sign in to comment.