Skip to content

Commit

Permalink
fix interchain test
Browse files Browse the repository at this point in the history
  • Loading branch information
phamminh0811 committed Jul 17, 2024
1 parent 2f779bf commit 8ecad7e
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 48 deletions.
20 changes: 11 additions & 9 deletions custom/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ func (h SDKMessageHandler) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddr
}

for _, sdkMsg := range sdkMsgs {
taxes := FilterMsgAndComputeTax(ctx, h.treasuryKeeper, sdkMsg)
if !taxes.IsZero() {
eventManager := sdk.NewEventManager()
if h.tax2gaskeeper.IsEnabled(ctx) {
taxes := FilterMsgAndComputeTax(ctx, h.treasuryKeeper, sdkMsg)
if !taxes.IsZero() {
eventManager := sdk.NewEventManager()

gas, err := tax2gasutils.ComputeGas(h.tax2gaskeeper.GetGasPrices(ctx), taxes)
if err != nil {
return nil, nil, err
}
ctx.GasMeter().ConsumeGas(gas, "tax gas")

gas, err := tax2gasutils.ComputeGas(h.tax2gaskeeper.GetGasPrices(ctx), taxes)
if err != nil {
return nil, nil, err
events = eventManager.Events()
}
ctx.GasMeter().ConsumeGas(gas, "tax gas")

events = eventManager.Events()
}

res, err := h.handleSdkMessage(ctx, contractAddr, sdkMsg)
Expand Down
2 changes: 2 additions & 0 deletions proto/terra/tax2gas/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ message Params {
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];

bool enabled = 2;
}

// GenesisState defines the tax2gas module's genesis state.
Expand Down
4 changes: 4 additions & 0 deletions tests/interchaintest/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func ModifyGenesis() func(ibc.ChainConfig, []byte) ([]byte, error) {
if err := dyno.Set(g, chainConfig.Denom, "app_state", "gov", "params", "min_deposit", 0, "denom"); err != nil {
return nil, fmt.Errorf("failed to set voting period in genesis json: %w", err)
}
// Disable tax2gas params to disable
if err := dyno.Set(g, false, "app_state", "tax2gas", "params", "enabled"); err != nil {
return nil, fmt.Errorf("failed to set tax2gas params in genesis json: %w", err)
}
// Modify signed blocks window
if err := dyno.Set(g, signedBlocksWindow, "app_state", "slashing", "params", "signed_blocks_window"); err != nil {
return nil, fmt.Errorf("failed to set signed blocks window in genesis json: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion x/tax2gas/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (fd FeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, nex
)

msgs := feeTx.GetMsgs()
if tax2gasutils.IsOracleTx(msgs) || simulate {
if tax2gasutils.IsOracleTx(msgs) || simulate || !fd.tax2gasKeeper.IsEnabled(ctx) {
return next(ctx, tx, simulate)
}

Expand Down
4 changes: 4 additions & 0 deletions x/tax2gas/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

func (k Keeper) IsEnabled(ctx sdk.Context) bool {
return k.GetParams(ctx).Enabled
}

func (k Keeper) GetGasPrices(ctx sdk.Context) sdk.DecCoins {
return k.GetParams(ctx).GasPrices.Sort()
}
4 changes: 2 additions & 2 deletions x/tax2gas/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

"github.com/classic-terra/core/v3/x/tax2gas/types"
Expand All @@ -24,7 +24,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {

func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if k.GetAuthority() != req.Authority {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
Expand Down
22 changes: 11 additions & 11 deletions x/tax2gas/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewTax2GasPostDecorator(accountKeeper ante.AccountKeeper, bankKeeper types.
}

// TODO: handle fail tx
func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, success bool, next sdk.PostHandler) (sdk.Context, error) {
func (tgd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, success bool, next sdk.PostHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
Expand All @@ -41,7 +41,7 @@ func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate b
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
}
msgs := feeTx.GetMsgs()
if tax2gasutils.IsOracleTx(msgs) || simulate {
if tax2gasutils.IsOracleTx(msgs) || simulate || !tgd.tax2gasKeeper.IsEnabled(ctx) {
return next(ctx, tx, simulate, success)
}

Expand All @@ -59,7 +59,7 @@ func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate b
return next(ctx, tx, simulate, success)
}

gasPrices := dd.tax2gasKeeper.GetGasPrices(ctx)
gasPrices := tgd.tax2gasKeeper.GetGasPrices(ctx)
found, paidDenomGasPrice := tax2gasutils.GetGasPriceByDenom(gasPrices, paidDenom)
if !found {
return ctx, types.ErrDenomNotFound
Expand All @@ -85,10 +85,10 @@ func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate b
// if feegranter set deduct fee from feegranter account.
// this works with only when feegrant enabled.
if feeGranter != nil {
if dd.feegrantKeeper == nil {
if tgd.feegrantKeeper == nil {
return ctx, sdkerrors.ErrInvalidRequest.Wrap("fee grants are not enabled")
} else if !feeGranter.Equals(feePayer) {
allowance, err := dd.feegrantKeeper.GetAllowance(ctx, feeGranter, feePayer)
allowance, err := tgd.feegrantKeeper.GetAllowance(ctx, feeGranter, feePayer)
if err != nil {
return ctx, errorsmod.Wrapf(err, "fee-grant not found with granter %s and grantee %s", feeGranter, feePayer)
}
Expand All @@ -102,12 +102,12 @@ func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate b
for _, feeRequired := range gasRemainingFees {
_, err := allowance.Accept(ctx, sdk.NewCoins(feeRequired), feeTx.GetMsgs())
if err == nil {
err = dd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, sdk.NewCoins(feeRequired), feeTx.GetMsgs())
err = tgd.feegrantKeeper.UseGrantedFees(ctx, feeGranter, feePayer, sdk.NewCoins(feeRequired), feeTx.GetMsgs())
if err != nil {
return ctx, errorsmod.Wrapf(err, "%s does not allow to pay fees for %s", feeGranter, feePayer)
}
feeGranter := dd.accountKeeper.GetAccount(ctx, feeGranter)
err = dd.bankKeeper.SendCoinsFromAccountToModule(ctx, feeGranter.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeRequired))
feeGranter := tgd.accountKeeper.GetAccount(ctx, feeGranter)
err = tgd.bankKeeper.SendCoinsFromAccountToModule(ctx, feeGranter.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeRequired))
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
Expand All @@ -119,23 +119,23 @@ func (dd Tax2gasPostDecorator) PostHandle(ctx sdk.Context, tx sdk.Tx, simulate b
}

for _, feeCoin := range feeCoins {
feePayer := dd.accountKeeper.GetAccount(ctx, feePayer)
feePayer := tgd.accountKeeper.GetAccount(ctx, feePayer)
found, gasPrice := tax2gasutils.GetGasPriceByDenom(gasPrices, feeCoin.Denom)
if !found {
continue
}
feeRequired := sdk.NewCoin(feeCoin.Denom, gasPrice.MulInt64(int64(gasRemaining)).Ceil().RoundInt())

if feeCoin.IsGTE(feeRequired) {
err := dd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeRequired))
err := tgd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeRequired))
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
gasRemaining = 0
break
}

err := dd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeCoin))
err := tgd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeCoin))
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
Expand Down
88 changes: 65 additions & 23 deletions x/tax2gas/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions x/tax2gas/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ func DefaultParams() Params {
sdk.NewDecCoinFromDec("umyr", sdk.NewDecWithPrec(3, 0)),
sdk.NewDecCoinFromDec("utwd", sdk.NewDecWithPrec(20, 0)),
),
Enabled: true,
}
}

// Validate validates params.
func (p Params) Validate() error {
if len(p.GasPrices) == 0 {
return fmt.Errorf("must provide at least 1 gas prices")
if p.Enabled {
if len(p.GasPrices) == 0 {
return fmt.Errorf("must provide at least 1 gas prices")
}
if !p.GasPrices.IsAllPositive() {
return fmt.Errorf("gas prices must be positive")
}
}
return nil
}

0 comments on commit 8ecad7e

Please sign in to comment.