Skip to content

Commit

Permalink
fix:exceed block max gas
Browse files Browse the repository at this point in the history
  • Loading branch information
phamminh0811 committed Sep 10, 2024
1 parent 8bd7278 commit c3e596a
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 72 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (app *TerraApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIC

// RegisterTxService implements the Application.RegisterTxService method.
func (app *TerraApp) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.interfaceRegistry)
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.BaseApp.SimulateSpecial, app.interfaceRegistry)
customauthtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.TreasuryKeeper, app.Tax2gasKeeper)
}

Expand Down
56 changes: 51 additions & 5 deletions custom/auth/client/utils/feeutils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package utils

import (
"context"
"fmt"

gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/spf13/pflag"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

Expand All @@ -32,19 +38,30 @@ type ComputeReqParams struct {
func ComputeFeesWithCmd(
clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk.Msg,
) (*legacytx.StdFee, error) {
txf, err := tx.NewFactoryCLI(clientCtx, flagSet)
txf, err := clienttx.NewFactoryCLI(clientCtx, flagSet)
if err != nil {
return nil, err
}

gasStr, _ := flagSet.GetString(flags.FlagGas)
switch gasStr {
case flags.GasFlagAuto:
// skip
case "":
txf = txf.WithGas(0)
txf = txf.WithSimulateAndExecute(true)
default:
return nil, fmt.Errorf("current version can not support specific gas as it will cause exceed block max gas, please use --fees flag")
}

gas := txf.Gas()
if txf.SimulateAndExecute() {
txf, err := prepareFactory(clientCtx, txf)
if err != nil {
return nil, err
}

_, adj, err := tx.CalculateGas(clientCtx, txf, msgs...)
_, adj, err := CalculateGas(clientCtx, txf, msgs...)
if err != nil {
return nil, err
}
Expand All @@ -59,7 +76,7 @@ func ComputeFeesWithCmd(

if !gasPrices.IsZero() {
glDec := sdk.NewDec(int64(gas))
adjustment := sdk.NewDecWithPrec(int64(txf.GasAdjustment())*100, 2)
adjustment := sdk.NewDecWithPrec(int64(txf.GasAdjustment()*100), 2)

if adjustment.LT(sdk.OneDec()) {
adjustment = sdk.OneDec()
Expand All @@ -82,11 +99,40 @@ func ComputeFeesWithCmd(
}, nil
}

// CalculateGas simulates the execution of a transaction and returns the
// simulation response obtained by the query and the adjusted gas amount.
func CalculateGas(
clientCtx gogogrpc.ClientConn, txf clienttx.Factory, msgs ...sdk.Msg,
) (*tx.SimulateResponse, uint64, error) {
txBytes, err := txf.BuildSimTx(msgs...)
if err != nil {
return nil, 0, err
}

txSvcClient := tx.NewServiceClient(clientCtx)
simSpecialRes, err := txSvcClient.SimulateSpecial(context.Background(), &tx.SimulateSpecialRequest{
TxBytes: txBytes,
})
if err != nil {
return nil, 0, err
}
simRes, err := txSvcClient.Simulate(context.Background(), &tx.SimulateRequest{
TxBytes: txBytes,
})
if err != nil {
return nil, 0, err
}

taxGas := simSpecialRes.GasInfo.GasUsed - simRes.GasInfo.GasUsed
actualGas := uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed))
return simRes, actualGas + taxGas, nil
}

// prepareFactory ensures the account defined by ctx.GetFromAddress() exists and
// if the account number and/or the account sequence number are zero (not set),
// they will be queried for and set on the provided Factory. A new Factory with
// the updated fields will be returned.
func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) {
func prepareFactory(clientCtx client.Context, txf clienttx.Factory) (clienttx.Factory, error) {
from := clientCtx.GetFromAddress()

if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions custom/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func (h SDKMessageHandler) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddr
return nil, nil, err
}
ctx.TaxGasMeter().ConsumeGas(taxGas, "tax gas")
if ctx.IsSpecialSimulate() {
ctx.CacheTaxGasMeter().ConsumeGas(taxGas, "tax gas")
}

events = eventManager.Events()
}
Expand Down
44 changes: 22 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.20
go 1.21

toolchain go1.22.1

module github.com/classic-terra/core/v3

Expand Down Expand Up @@ -26,8 +28,8 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014
google.golang.org/grpc v1.62.1
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117
google.golang.org/grpc v1.66.1
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -75,16 +77,15 @@ require (
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
pgregory.net/rapid v1.1.0 // indirect
)

require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.24.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
Expand All @@ -99,7 +100,7 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.9 // indirect
Expand Down Expand Up @@ -127,7 +128,7 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
Expand Down Expand Up @@ -174,14 +175,14 @@ require (
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/client_model v0.6.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.16.0
github.com/subosito/gotenv v1.4.2 // indirect
Expand All @@ -192,19 +193,18 @@ require (
github.com/zondax/hid v0.9.2 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/api v0.162.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
Expand All @@ -229,7 +229,7 @@ replace (
// use cometbft
github.com/cometbft/cometbft => github.com/classic-terra/cometbft v0.37.4-terra1
github.com/cometbft/cometbft-db => github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-sdk => github.com/classic-terra/cosmos-sdk v0.47.10-terra.1.0.20240731055430-cf7f52e8ee42
github.com/cosmos/cosmos-sdk => github.com/classic-terra/cosmos-sdk v0.47.10-terra.1.0.20240910225558-386d57c2a1ee
github.com/cosmos/ibc-go/v7 => github.com/classic-terra/ibc-go/v7 v7.4.0-terra
github.com/cosmos/ledger-cosmos-go => github.com/terra-money/ledger-terra-go v0.11.2
// replace goleveldb to optimized one
Expand Down
Loading

0 comments on commit c3e596a

Please sign in to comment.