Skip to content

Commit

Permalink
Merge branch 'minh/tax2gas' of github-expertdicer:classic-terra/core …
Browse files Browse the repository at this point in the history
…into minh/tax2gas
  • Loading branch information
expertdicer committed Jul 17, 2024
2 parents b7aa915 + 8ecad7e commit d66c1f4
Show file tree
Hide file tree
Showing 18 changed files with 144 additions and 88 deletions.
2 changes: 1 addition & 1 deletion app/upgrades/v8_1/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v8_1
package v81

import (
"github.com/classic-terra/core/v3/app/upgrades"
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/v8_1/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v8_1
package v81

import (
"github.com/classic-terra/core/v3/app/keepers"
Expand Down
17 changes: 8 additions & 9 deletions custom/auth/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ package post
import (
dyncommkeeper "github.com/classic-terra/core/v3/x/dyncomm/keeper"
dyncommpost "github.com/classic-terra/core/v3/x/dyncomm/post"
tax2gasKeeper "github.com/classic-terra/core/v3/x/tax2gas/keeper"
tax2gasPost "github.com/classic-terra/core/v3/x/tax2gas/post"
"github.com/classic-terra/core/v3/x/tax2gas/types"
tax2gasTypes "github.com/classic-terra/core/v3/x/tax2gas/types"
tax2gaskeeper "github.com/classic-terra/core/v3/x/tax2gas/keeper"
tax2gaspost "github.com/classic-terra/core/v3/x/tax2gas/post"
tax2gastypes "github.com/classic-terra/core/v3/x/tax2gas/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
AccountKeeper ante.AccountKeeper
BankKeeper types.BankKeeper
FeegrantKeeper types.FeegrantKeeper
BankKeeper tax2gastypes.BankKeeper
FeegrantKeeper tax2gastypes.FeegrantKeeper
DyncommKeeper dyncommkeeper.Keeper
TreasuryKeeper tax2gasTypes.TreasuryKeeper
Tax2Gaskeeper tax2gasKeeper.Keeper
TreasuryKeeper tax2gastypes.TreasuryKeeper
Tax2Gaskeeper tax2gaskeeper.Keeper
}

// NewPostHandler returns an PostHandler that checks and set target
// commission rate for msg create validator and msg edit validator
func NewPostHandler(options HandlerOptions) (sdk.PostHandler, error) {
return sdk.ChainPostDecorators(
dyncommpost.NewDyncommPostDecorator(options.DyncommKeeper),
tax2gasPost.NewTax2GasPostDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TreasuryKeeper, options.Tax2Gaskeeper),
tax2gaspost.NewTax2GasPostDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TreasuryKeeper, options.Tax2Gaskeeper),
), nil
}
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(ctx, 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
Binary file removed forwarder.wasm
Binary file not shown.
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
6 changes: 3 additions & 3 deletions scripts/run-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ $BINARY keys add $KEY1 --keyring-backend $KEYRING --home $HOME_DIR
$BINARY keys add $KEY2 --keyring-backend $KEYRING --home $HOME_DIR

# Allocate genesis accounts (cosmos formatted addresses)
$BINARY add-genesis-account $KEY "1000000000000${DENOM},1000000000000uusd" --keyring-backend $KEYRING --home $HOME_DIR
$BINARY add-genesis-account $KEY1 "1000000000000${DENOM},1000000000000uusd" --keyring-backend $KEYRING --home $HOME_DIR
$BINARY add-genesis-account $KEY2 "1000000000000${DENOM},1000000000000uusd" --keyring-backend $KEYRING --home $HOME_DIR
$BINARY add-genesis-account $KEY "1000000000000${DENOM}" --keyring-backend $KEYRING --home $HOME_DIR
$BINARY add-genesis-account $KEY1 "1000000000000${DENOM}" --keyring-backend $KEYRING --home $HOME_DIR
$BINARY add-genesis-account $KEY2 "1000000000000${DENOM}" --keyring-backend $KEYRING --home $HOME_DIR

update_test_genesis '.app_state["mint"]["params"]["mint_denom"]="'$DENOM'"'
update_test_genesis '.app_state["gov"]["deposit_params"]["min_deposit"]=[{"denom":"'$DENOM'","amount": "1000000"}]'
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
9 changes: 4 additions & 5 deletions x/tax2gas/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ 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)
}

// Compute taxes based on consumed gas
gasPrices := fd.tax2gasKeeper.GetGasPrices(ctx)
gasConsumed := ctx.GasMeter().GasConsumed()
gasConsumedFees, err := tax2gasutils.ComputeFeesOnGasConsumed(ctx, tx, gasPrices, gasConsumed)
gasConsumedFees, err := tax2gasutils.ComputeFeesOnGasConsumed(tx, gasPrices, gasConsumed)
if err != nil {
return ctx, err
}

// Compute taxes based on sent amount
taxes := tax2gasutils.FilterMsgAndComputeTax(ctx, fd.treasuryKeeper, msgs...)
// Convert taxes to gas
taxGas, err := tax2gasutils.ComputeGas(ctx, gasPrices, taxes)
taxGas, err := tax2gasutils.ComputeGas(gasPrices, taxes)
if err != nil {
return ctx, err
}
Expand Down Expand Up @@ -193,9 +193,8 @@ func (fd FeeDecorator) tryDeductFee(ctx sdk.Context, feeTx sdk.FeeTx, taxes sdk.

// As there is only 1 element
return foundCoins.Denoms()[0], nil
} else {
return "", fmt.Errorf("can't find coin that matches. Expected %s, wanted %s", feeCoins, taxes)
}
return "", fmt.Errorf("can't find coin that matches. Expected %s, wanted %s", feeCoins, taxes)
}

// DeductFees deducts fees from the given account.
Expand Down
1 change: 0 additions & 1 deletion x/tax2gas/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,4 @@ func (s *AnteTestSuite) TestDeductFeeDecorator() {
}
})
}

}
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
6 changes: 3 additions & 3 deletions x/tax2gas/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
}

// ValidateGenesis performs genesis state validation for the tax2gas module.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
var genState types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
Expand Down Expand Up @@ -87,7 +87,7 @@ func NewAppModule(cdc codec.Codec, tax2gasKeeper keeper.Keeper) AppModule {
}
}

func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {
}

// QuerierRoute returns the tax2gas module's querier route name.
Expand All @@ -114,7 +114,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock performs TODO.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

Expand Down
36 changes: 18 additions & 18 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,15 +85,15 @@ 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)
}

gasRemainingFees, err := tax2gasutils.ComputeFeesOnGasConsumed(ctx, tx, gasPrices, gasRemaining)
gasRemainingFees, err := tax2gasutils.ComputeFeesOnGasConsumed(tx, gasPrices, gasRemaining)
if err != nil {
return ctx, err
}
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,28 +119,28 @@ 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
} else {
err := dd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeCoin))
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
feeRemaining := sdk.NewDecCoinFromCoin(feeRequired.Sub(feeCoin))
gasRemaining = uint64(feeRemaining.Amount.Quo(gasPrice).Ceil().RoundInt64())
}

err := tgd.bankKeeper.SendCoinsFromAccountToModule(ctx, feePayer.GetAddress(), authtypes.FeeCollectorName, sdk.NewCoins(feeCoin))
if err != nil {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
feeRemaining := sdk.NewDecCoinFromCoin(feeRequired.Sub(feeCoin))
gasRemaining = uint64(feeRemaining.Amount.Quo(gasPrice).Ceil().RoundInt64())
}
if gasRemaining > 0 {
return ctx, errorsmod.Wrapf(sdkerrors.ErrInsufficientFee, "fees are not enough to pay for gas")
Expand Down
2 changes: 1 addition & 1 deletion x/tax2gas/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package types

import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/feegrant"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/feegrant"
)

// TreasuryKeeper for tax charging & recording
Expand Down
Loading

0 comments on commit d66c1f4

Please sign in to comment.