Skip to content

Commit

Permalink
Merge pull request #58 from xpladev/refactor/ante-fee
Browse files Browse the repository at this point in the history
refactor: ante fee
  • Loading branch information
JoowonYun committed Mar 8, 2023
2 parents b27a58d + 55c8d94 commit aab9b82
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 108 deletions.
4 changes: 2 additions & 2 deletions ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func newCosmosAnteHandler(opts HandlerOptions) sdk.AnteHandler {
wasmkeeper.NewLimitSimulationGasDecorator(opts.WasmConfig.SimulationGasLimit),
wasmkeeper.NewCountTXDecorator(opts.TxCounterStoreKey),
authante.NewRejectExtensionOptionsDecorator(),
NewMempoolFeeDecorator(opts.BypassMinFeeMsgTypes, opts.AccountKeeper, opts.XATPKeeper, opts.WasmConfig.SmartQueryGasLimit),
NewMempoolFeeDecorator(opts.BypassMinFeeMsgTypes, opts.AccountKeeper, opts.XATPKeeper),

authante.NewValidateBasicDecorator(),
authante.NewTxTimeoutHeightDecorator(),
authante.NewValidateMemoDecorator(opts.AccountKeeper),
authante.NewConsumeGasForTxSizeDecorator(opts.AccountKeeper),
NewDeductFeeDecorator(opts.AccountKeeper, opts.BankKeeper, opts.FeegrantKeeper, opts.XATPKeeper, opts.MinGasPrices, opts.WasmConfig.SmartQueryGasLimit),
NewDeductFeeDecorator(opts.AccountKeeper, opts.BankKeeper, opts.FeegrantKeeper, opts.XATPKeeper),
// SetPubKeyDecorator must be called before all signature verification decorators
authante.NewSetPubKeyDecorator(opts.AccountKeeper),
authante.NewValidateSigCountDecorator(opts.AccountKeeper),
Expand Down
172 changes: 67 additions & 105 deletions ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ante

import (
"fmt"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -12,8 +11,6 @@ import (
xplatypes "github.com/xpladev/xpla/types"

xatpkeeper "github.com/xpladev/xpla/x/xatp/keeper"

authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
)

const maxBypassMinFeeMsgGasUsage = uint64(200_000)
Expand All @@ -30,15 +27,13 @@ type MempoolFeeDecorator struct {
BypassMinFeeMsgTypes []string
ak authante.AccountKeeper
xatpKeeper xatpkeeper.Keeper
smartQueryGasLimit uint64
}

func NewMempoolFeeDecorator(bypassMsgTypes []string, ak authante.AccountKeeper, ck xatpkeeper.Keeper, sqgl uint64) MempoolFeeDecorator {
func NewMempoolFeeDecorator(bypassMsgTypes []string, ak authante.AccountKeeper, ck xatpkeeper.Keeper) MempoolFeeDecorator {
return MempoolFeeDecorator{
BypassMinFeeMsgTypes: bypassMsgTypes,
ak: ak,
xatpKeeper: ck,
smartQueryGasLimit: sqgl,
}
}

Expand All @@ -59,35 +54,28 @@ func (mfd MempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
if ctx.IsCheckTx() && !simulate && !(mfd.bypassMinFeeMsgs(msgs) && gas <= uint64(len(msgs))*maxBypassMinFeeMsgGasUsage) {
minGasPrices := ctx.MinGasPrices()

var decimal int64
var cw20Decimal *big.Int

ctx = ctx.WithGasMeter(sdk.NewGasMeter(sdk.Gas(mfd.smartQueryGasLimit)))

if !minGasPrices.IsZero() {
var defaultGasPrice sdk.DecCoin
for _, minGasPrice := range minGasPrices {
if minGasPrice.Denom == xplatypes.DefaultDenom {
defaultGasPrice = minGasPrice
decimal = int64(len(defaultGasPrice.Amount.RoundInt().String()))
cw20Decimal = new(big.Int).Exp(big.NewInt(10), big.NewInt(decimal), nil)
break
payer := mfd.xatpKeeper.GetPayer(ctx)
if payer != "" {
var defaultGasPrice sdk.DecCoin
for _, minGasPrice := range minGasPrices {
if minGasPrice.Denom == xplatypes.DefaultDenom {
defaultGasPrice = minGasPrice
break
}
}
}

for _, fee := range feeCoins {
xatp, found := mfd.xatpKeeper.GetXatp(ctx, fee.Denom)
if found {
ratioDec, err := mfd.xatpKeeper.GetFeeInfoFromXATP(ctx, xatp.Denom)
if err != nil {
return ctx, err
}
for _, fee := range feeCoins {

xatp, found := mfd.xatpKeeper.GetXatp(ctx, fee.Denom)
if found {
ratioDec, err := mfd.xatpKeeper.GetFeeInfoFromXATP(ctx, xatp.Denom)
if err != nil {
return ctx, err
}

minGasPrices = minGasPrices.Add(
sdk.DecCoin{
Denom: xatp.Denom,
Amount: defaultGasPrice.Amount.Mul(ratioDec),
})
minGasPrices = minGasPrices.Add(sdk.NewDecCoinFromDec(xatp.Denom, defaultGasPrice.Amount.Mul(ratioDec)))
}
}
}

Expand All @@ -101,7 +89,11 @@ func (mfd MempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b
fee = gp.Amount.Mul(glDec)

if gp.Denom != xplatypes.DefaultDenom {
fee = sdk.NewDecFromBigInt(new(big.Int).Div(fee.Ceil().RoundInt().BigInt(), cw20Decimal))
xatp, found := mfd.xatpKeeper.GetXatp(ctx, gp.Denom)
if found {
decimalDiff := sdk.NewDecWithPrec(1, sdk.Precision-int64(xatp.Decimals))
fee = fee.Ceil().Mul(decimalDiff)
}
}

requiredFees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
Expand Down Expand Up @@ -132,20 +124,17 @@ type DeductFeeDecorator struct {
bankKeeper types.BankKeeper
feegrantKeeper authante.FeegrantKeeper

xatpKeeper xatpkeeper.Keeper
MinGasPrices string
smartQueryGasLimit uint64
xatpKeeper xatpkeeper.Keeper
MinGasPrices string
}

func NewDeductFeeDecorator(ak authante.AccountKeeper, bk types.BankKeeper, fk authante.FeegrantKeeper, xk xatpkeeper.Keeper, minGasPrices string, sqgl uint64) DeductFeeDecorator {
func NewDeductFeeDecorator(ak authante.AccountKeeper, bk types.BankKeeper, fk authante.FeegrantKeeper, xk xatpkeeper.Keeper) DeductFeeDecorator {
return DeductFeeDecorator{
ak: ak,
bankKeeper: bk,
feegrantKeeper: fk,

xatpKeeper: xk,
MinGasPrices: minGasPrices,
smartQueryGasLimit: sqgl,
xatpKeeper: xk,
}
}

Expand Down Expand Up @@ -186,85 +175,58 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "fee payer address: %s does not exist", deductFeesFrom)
}

var isXpla bool = false
for _, coin := range fee {
denom := coin.Denom

if denom == xplatypes.DefaultDenom {
isXpla = true
}
}

// deduct the fees
if !feeTx.GetFee().IsZero() {
nativeFees := sdk.Coins{}
xatpFees := sdk.Coins{}

if isXpla {
err = DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc, feeTx.GetFee())
if err != nil {
return ctx, err
}

} else {
xatpPayer := dfd.xatpKeeper.GetPayer(ctx)

ctx = ctx.WithGasMeter(sdk.NewGasMeter(sdk.Gas(dfd.smartQueryGasLimit)))
for _, coin := range fee {

denom := coin.Denom

ratioDec, err := dfd.xatpKeeper.GetFeeInfoFromXATP(ctx, denom)
if err != nil {
return ctx, err
}

var xplaDecimal sdk.Dec
if denom != xplatypes.DefaultDenom {

minGasPrices, err := sdk.ParseDecCoins(dfd.MinGasPrices)
if err != nil {
return ctx, err
}

var decimal int64
for _, gp := range minGasPrices {

if gp.Denom == xplatypes.DefaultDenom {
decimal = int64(len(gp.Amount.RoundInt().String()))
}
}

xplaDecimal = sdk.NewDecFromBigInt(new(big.Int).Mul(big.NewInt(1), new(big.Int).Exp(big.NewInt(10), big.NewInt(decimal), nil)))
for _, fee := range feeTx.GetFee() {
if xatpPayer == "" {
break
}

err = dfd.xatpKeeper.PayXATP(ctx, deductFeesFrom, denom, coin.Amount.String())
if err != nil {
return ctx, err
}
}
xatp, found := dfd.xatpKeeper.GetXatp(ctx, fee.Denom)
if !found {
nativeFees = nativeFees.Add(fee)
continue
}

xatpPayer := dfd.xatpKeeper.GetPayer(ctx)
XatpPayerAcc, err := sdk.AccAddressFromBech32(xatpPayer)
if err != nil {
return ctx, err
}
err := dfd.xatpKeeper.PayXATP(ctx, deductFeesFrom, xatp.Denom, fee.Amount.String())
if err != nil {
return ctx, err
}

accExists := dfd.ak.(authkeeper.AccountKeeper).HasAccount(ctx, XatpPayerAcc)
if !accExists {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "XATP payer account: %s not found", XatpPayerAcc)
}
ratioDec, err := dfd.xatpKeeper.GetFeeInfoFromXATP(ctx, xatp.Denom)
if err != nil {
return ctx, err
}

deductFeesFromAcc0 := dfd.ak.GetAccount(ctx, XatpPayerAcc)
feeAmount := sdk.NewDecFromIntWithPrec(fee.Amount, int64(xatp.Decimals))
defaultFeeAmountDec := feeAmount.Quo(ratioDec)
xatpFees = xatpFees.Add(sdk.NewCoin(xplatypes.DefaultDenom, defaultFeeAmountDec.TruncateInt()))
}

xplaValue := sdk.NewDecFromInt(coin.Amount).Quo(ratioDec)
xplaValue = xplaValue.Mul(xplaDecimal)
xplaFee := sdk.NewCoins(sdk.NewCoin(xplatypes.DefaultDenom, xplaValue.Ceil().RoundInt()))
if !nativeFees.Empty() {
err = DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc, nativeFees)
if err != nil {
return ctx, err
}
}

err = DeductFees(dfd.bankKeeper, ctx, deductFeesFromAcc0, xplaFee)
if err != nil {
return ctx, err
}
if !xatpFees.Empty() {
xatpPayerAcc, err := sdk.AccAddressFromBech32(xatpPayer)
if err != nil {
return ctx, err
}

deductFeeAccount := dfd.ak.GetAccount(ctx, xatpPayerAcc)
err = DeductFees(dfd.bankKeeper, ctx, deductFeeAccount, xatpFees)
if err != nil {
return ctx, err
}
}

}

events := sdk.Events{sdk.NewEvent(sdk.EventTypeTx,
Expand Down
2 changes: 1 addition & 1 deletion ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *IntegrationTestSuite) TestMempoolFeeDecorator() {
sdk.MsgTypeURL(&ibcchanneltypes.MsgRecvPacket{}),
sdk.MsgTypeURL(&ibcchanneltypes.MsgAcknowledgement{}),
sdk.MsgTypeURL(&ibcclienttypes.MsgUpdateClient{}),
}, s.app.AccountKeeper, s.app.XATPKeeper, 3_000_000)
}, s.app.AccountKeeper, s.app.XATPKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)
priv1, _, addr1 := testdata.KeyTestPubAddr()

Expand Down

0 comments on commit aab9b82

Please sign in to comment.