From 7611bb6f55aaa99aad8c896c880cfe09612c637d Mon Sep 17 00:00:00 2001 From: StrathCole Date: Mon, 23 Sep 2024 09:57:15 +0200 Subject: [PATCH 1/4] - add adjustment calculation --- app/app.go | 50 +++++++++++++++++++++++++++++++++++++----- x/tax2gas/post/post.go | 3 +++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/app/app.go b/app/app.go index 279c24ef..73e451ac 100644 --- a/app/app.go +++ b/app/app.go @@ -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" @@ -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" { + value, ok := sdkmath.NewIntFromString(string(attr.Value)) + if !ok { + ctx.Logger().Error("failed to parse tax gas from events", "value", string(attr.Value)) + 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 + //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 diff --git a/x/tax2gas/post/post.go b/x/tax2gas/post/post.go index 6e9bc8e4..66019e73 100644 --- a/x/tax2gas/post/post.go +++ b/x/tax2gas/post/post.go @@ -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) From 6135512ab5ce34a01e4ede4f7cefd933ca97eb13 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Mon, 23 Sep 2024 10:10:19 +0200 Subject: [PATCH 2/4] - lint --- app/app.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/app.go b/app/app.go index 73e451ac..198e46b2 100644 --- a/app/app.go +++ b/app/app.go @@ -138,10 +138,10 @@ func (app *TerraApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { for _, event := range res.Events { if event.Type == "tax2gas" { for _, attr := range event.Attributes { - if string(attr.Key) == "tax_gas" { - value, ok := sdkmath.NewIntFromString(string(attr.Value)) + if attr.Key == "tax_gas" { + value, ok := sdkmath.NewIntFromString(attr.Value) if !ok { - ctx.Logger().Error("failed to parse tax gas from events", "value", string(attr.Value)) + ctx.Logger().Error("failed to parse tax gas from events", "value", attr.Value) continue } @@ -170,8 +170,8 @@ func (app *TerraApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { gasWanted -= subTaxGas } - //maxGas := app.BaseApp.GetConsensusParams(ctx).Block.MaxGas - //fmt.Println("GasWanted", res.GasWanted, "GasUsed", res.GasUsed, "TaxGas", taxGas, "MaxGas", maxGas, "Multiple", multiple, "GasWantedAdjusted", gasWanted) + // maxGas := app.BaseApp.GetConsensusParams(ctx).Block.MaxGas + // fmt.Println("GasWanted", res.GasWanted, "GasUsed", res.GasUsed, "TaxGas", taxGas, "MaxGas", maxGas, "Multiple", multiple, "GasWantedAdjusted", gasWanted) // if the gas wanted is still higher than the gas used, we can adjust the gas wanted if gasWanted >= gasUsed { From d84cc3dffe392e7e91750358ba8aded5688e011a Mon Sep 17 00:00:00 2001 From: StrathCole Date: Thu, 10 Oct 2024 16:17:45 +0200 Subject: [PATCH 3/4] - early exit in checkTx if applicable --- app/app.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/app.go b/app/app.go index 198e46b2..3bcf1d4b 100644 --- a/app/app.go +++ b/app/app.go @@ -151,6 +151,10 @@ func (app *TerraApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { } } + if taxGas.IsZero() { + return res + } + gasWanted := uint64(res.GasWanted) gasUsed := uint64(res.GasUsed) subTaxGas := taxGas.Uint64() From 0b28fd726123d48c62a8dbded506026228d3d0e0 Mon Sep 17 00:00:00 2001 From: StrathCole Date: Thu, 10 Oct 2024 16:20:54 +0200 Subject: [PATCH 4/4] - add logging output (temporarily) --- app/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 3bcf1d4b..21375a22 100644 --- a/app/app.go +++ b/app/app.go @@ -175,7 +175,7 @@ func (app *TerraApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx { } // maxGas := app.BaseApp.GetConsensusParams(ctx).Block.MaxGas - // fmt.Println("GasWanted", res.GasWanted, "GasUsed", res.GasUsed, "TaxGas", taxGas, "MaxGas", maxGas, "Multiple", multiple, "GasWantedAdjusted", gasWanted) + ctx.Logger().Info("CheckTx", "GasWanted", res.GasWanted, "GasUsed", res.GasUsed, "TaxGas", taxGas, "GasWantedAdjusted", gasWanted, "Multiple", multiple) // if the gas wanted is still higher than the gas used, we can adjust the gas wanted if gasWanted >= gasUsed {