Skip to content

Commit

Permalink
feat: add precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurist-85 committed Aug 16, 2024
1 parent 3058d8f commit 80352d7
Show file tree
Hide file tree
Showing 423 changed files with 53,295 additions and 3,589 deletions.
2 changes: 1 addition & 1 deletion app/ante/cosmos/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
// checkDisabledMsgs iterates through the msgs and returns an error if it finds any unauthorized msgs.
//
// When searchOnlyInAuthzMsgs is enabled, only authz MsgGrant and MsgExec are blocked, if they contain unauthorized msg types.
// Otherwise any msg matching the disabled types are blocked, regardless of being in an authz msg or not.
// Otherwise, any msg matching the disabled types are blocked, regardless of being in an authz msg or not.
//
// This method is recursive as MsgExec's can wrap other MsgExecs. The check for nested messages is performed up to the
// maxNestedMsgs threshold. If there are more than that limit, it returns an error
Expand Down
3 changes: 2 additions & 1 deletion app/ante/cosmos/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/require"

"cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -415,7 +416,7 @@ func (suite *AnteTestSuite) TestRejectMsgsInAuthz() {
)

if tc.isEIP712 {
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdk.NewInt(20))
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, math.NewInt(20))
fees := sdk.NewCoins(coinAmount)
cosmosTxArgs := utiltx.CosmosTxArgs{
TxCfg: suite.clientCtx.TxConfig,
Expand Down
3 changes: 2 additions & 1 deletion app/ante/cosmos/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
Expand Down Expand Up @@ -187,7 +188,7 @@ func checkFeeCoinsAgainstMinGasPrices(ctx sdk.Context, feeCoins sdk.Coins, gas u

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
glDec := sdk.NewDec(int64(gas)) //#nosec G701 -- gosec warning about integer overflow is not relevant here
glDec := sdkmath.LegacyNewDec(int64(gas)) //#nosec G701 -- gosec warning about integer overflow is not relevant here
for i, gp := range minGasPrices {
fee := gp.Amount.Mul(glDec)
requiredFees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt())
Expand Down
98 changes: 38 additions & 60 deletions app/ante/cosmos/fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cosmos_test

import (
"fmt"
"time"

"cosmossdk.io/math"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/feegrant"
Expand All @@ -16,37 +14,39 @@ import (
"github.com/haqq-network/haqq/utils"
)

type deductFeeDecoratorTestCase struct {
name string
balance math.Int
rewards []math.Int
gas uint64
gasPrice *math.Int
feeGranter sdk.AccAddress
checkTx bool
simulate bool
expPass bool
errContains string
postCheck func()
malleate func()
}

func (suite *AnteTestSuite) TestDeductFeeDecorator() {
var (
dfd cosmosante.DeductFeeDecorator
// General setup
addr, priv = testutiltx.NewAccAddressAndKey()
// fee granter
fgAddr, _ = testutiltx.NewAccAddressAndKey()
initBalance = sdk.NewInt(1e18)
initBalance = math.NewInt(1e18)
lowGasPrice = math.NewInt(1)
zero = sdk.ZeroInt()
zero = math.ZeroInt()
)

// Testcase definitions
testcases := []struct {
name string
balance math.Int
rewards math.Int
gas uint64
gasPrice *math.Int
feeGranter sdk.AccAddress
checkTx bool
simulate bool
expPass bool
errContains string
postCheck func()
malleate func()
}{
testcases := []deductFeeDecoratorTestCase{
{
name: "pass - sufficient balance to pay fees",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 0,
checkTx: false,
simulate: true,
Expand All @@ -56,7 +56,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "fail - zero gas limit in check tx mode",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 0,
checkTx: true,
simulate: false,
Expand All @@ -66,7 +66,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "fail - checkTx - insufficient funds and no staking rewards",
balance: zero,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
checkTx: true,
simulate: false,
Expand All @@ -86,7 +86,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "pass - insufficient funds but sufficient staking rewards",
balance: zero,
rewards: initBalance,
rewards: []math.Int{initBalance},
gas: 10_000_000,
checkTx: false,
simulate: false,
Expand All @@ -108,8 +108,8 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
},
{
name: "fail - insufficient funds and insufficient staking rewards",
balance: sdk.NewInt(1e5),
rewards: sdk.NewInt(1e5),
balance: math.NewInt(1e5),
rewards: []math.Int{math.NewInt(1e5)},
gas: 10_000_000,
checkTx: false,
simulate: false,
Expand All @@ -118,21 +118,21 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
postCheck: func() {
// the balance should not have changed
balance := suite.app.BankKeeper.GetBalance(suite.ctx, addr, utils.BaseDenom)
suite.Require().Equal(sdk.NewInt(1e5), balance.Amount, "expected balance to be unchanged")
suite.Require().Equal(math.NewInt(1e5), balance.Amount, "expected balance to be unchanged")

// the rewards should not have changed
rewards, err := testutil.GetTotalDelegationRewards(suite.ctx, suite.app.DistrKeeper, addr)
suite.Require().NoError(err, "failed to get total delegation rewards")
suite.Require().Equal(
sdk.NewDecCoins(sdk.NewDecCoin(utils.BaseDenom, sdk.NewInt(1e5))),
sdk.NewDecCoins(sdk.NewDecCoin(utils.BaseDenom, math.NewInt(1e5))),
rewards,
"expected rewards to be unchanged")
},
},
{
name: "fail - sufficient balance to pay fees but provided fees < required fees",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
gasPrice: &lowGasPrice,
checkTx: true,
Expand All @@ -142,15 +142,15 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
malleate: func() {
suite.ctx = suite.ctx.WithMinGasPrices(
sdk.NewDecCoins(
sdk.NewDecCoin(utils.BaseDenom, sdk.NewInt(10_000)),
sdk.NewDecCoin(utils.BaseDenom, math.NewInt(10_000)),
),
)
},
},
{
name: "success - sufficient balance to pay fees & min gas prices is zero",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
gasPrice: &lowGasPrice,
checkTx: true,
Expand All @@ -168,7 +168,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "success - sufficient balance to pay fees (fees > required fees)",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
checkTx: true,
simulate: false,
Expand All @@ -177,15 +177,15 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
malleate: func() {
suite.ctx = suite.ctx.WithMinGasPrices(
sdk.NewDecCoins(
sdk.NewDecCoin(utils.BaseDenom, sdk.NewInt(100)),
sdk.NewDecCoin(utils.BaseDenom, math.NewInt(100)),
),
)
},
},
{
name: "success - zero fees",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 100,
gasPrice: &zero,
checkTx: true,
Expand All @@ -208,7 +208,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "fail - with not authorized fee granter",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
feeGranter: fgAddr,
checkTx: true,
Expand All @@ -219,7 +219,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "success - with authorized fee granter",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
feeGranter: fgAddr,
checkTx: true,
Expand Down Expand Up @@ -248,7 +248,7 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
{
name: "fail - authorized fee granter but no feegrant keeper on decorator",
balance: initBalance,
rewards: zero,
rewards: []math.Int{zero},
gas: 10_000_000,
feeGranter: fgAddr,
checkTx: true,
Expand Down Expand Up @@ -279,32 +279,10 @@ func (suite *AnteTestSuite) TestDeductFeeDecorator() {
// Test execution
for _, tc := range testcases {
suite.Run(tc.name, func() {
var args testutiltx.CosmosTxArgs
suite.SetupTest()

// Create a new DeductFeeDecorator
dfd = cosmosante.NewDeductFeeDecorator(
suite.app.AccountKeeper, suite.app.BankKeeper, suite.app.DistrKeeper, suite.app.FeeGrantKeeper, suite.app.StakingKeeper, nil,
)

// prepare the testcase
var err error
suite.ctx, err = testutil.PrepareAccountsForDelegationRewards(suite.T(), suite.ctx, suite.app, addr, tc.balance, tc.rewards)
suite.Require().NoError(err, "failed to prepare accounts for delegation rewards")
suite.ctx, err = testutil.Commit(suite.ctx, suite.app, time.Second*0, nil)
suite.Require().NoError(err)

// Create an arbitrary message for testing purposes
msg := sdktestutil.NewTestMsg(addr)

// Set up the transaction arguments
args := testutiltx.CosmosTxArgs{
TxCfg: suite.clientCtx.TxConfig,
Priv: priv,
Gas: tc.gas,
GasPrice: tc.gasPrice,
FeeGranter: tc.feeGranter,
Msgs: []sdk.Msg{msg},
}
// make the setup for the test case
dfd, args = suite.setupDeductFeeDecoratorTestCase(addr, priv, tc)

if tc.malleate != nil {
tc.malleate()
Expand Down
20 changes: 16 additions & 4 deletions app/ante/cosmos/min_price.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cosmos

import (
"fmt"
"math/big"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"golang.org/x/exp/slices"

evmante "github.com/haqq-network/haqq/app/ante/evm"
)
Expand Down Expand Up @@ -34,27 +37,36 @@ func (mpd MinGasPriceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate

minGasPrice := mpd.feesKeeper.GetParams(ctx).MinGasPrice

feeCoins := feeTx.GetFee()
evmParams := mpd.evmKeeper.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()

// only allow user to pass in aISLM and stake native token as transaction fees
// allow use stake native tokens for fees is just for unit tests to pass
validFees := len(feeCoins) == 0 || (len(feeCoins) == 1 && slices.Contains([]string{evmDenom, sdk.DefaultBondDenom}, feeCoins.GetDenomByIndex(0)))
if !validFees && !simulate {
return ctx, fmt.Errorf("expected only use native token %s for fee, but got %s", evmDenom, feeCoins.String())
}

// Short-circuit if min gas price is 0 or if simulating
if minGasPrice.IsZero() || simulate {
return next(ctx, tx, simulate)
}
evmParams := mpd.evmKeeper.GetParams(ctx)
evmDenom := evmParams.GetEvmDenom()

minGasPrices := sdk.DecCoins{
{
Denom: evmDenom,
Amount: minGasPrice,
},
}

feeCoins := feeTx.GetFee()
gas := feeTx.GetGas()

requiredFees := make(sdk.Coins, 0)

// Determine the required fees by multiplying each required minimum gas
// price by the gas limit, where fee = ceil(minGasPrice * gasLimit).
gasLimit := sdk.NewDecFromBigInt(new(big.Int).SetUint64(gas))
gasLimit := math.LegacyNewDecFromBigInt(new(big.Int).SetUint64(gas))

for _, gp := range minGasPrices {
fee := gp.Amount.Mul(gasLimit).Ceil().RoundInt()
Expand Down
Loading

0 comments on commit 80352d7

Please sign in to comment.