Skip to content

Commit

Permalink
[CORE-621] - Add unit tests to confirm that gas is accepted in usdc a…
Browse files Browse the repository at this point in the history
…nd native token (#508)
  • Loading branch information
clemire authored Oct 20, 2023
1 parent 11c9acb commit dcd6ecb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
78 changes: 78 additions & 0 deletions protocol/app/ante/gas_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package ante_test

import (
"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/dydxprotocol/v4-chain/protocol/cmd/dydxprotocold/cmd"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
assets "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"reflect"
"testing"

Expand Down Expand Up @@ -132,3 +139,74 @@ func TestValidateMsgType_FreeInfiniteGasDecorator(t *testing.T) {
})
}
}

func TestSubmitTxnWithGas(t *testing.T) {
tests := map[string]struct {
gasFee sdk.Coins
responseCode uint32
logMessage string
}{
"Success - 5 cents usdc gas fee": {
gasFee: constants.TestFeeCoins_5Cents,
responseCode: errors.SuccessABCICode,
},
"Success - 5 cents native token gas fee": {
gasFee: constants.TestFeeCoins_5Cents_NativeToken,
responseCode: errors.SuccessABCICode,
},
"Failure: 0 gas fee": {
gasFee: sdk.Coins{},
responseCode: sdkerrors.ErrInsufficientFee.ABCICode(),
logMessage: "insufficient fees; got: required: 25000000000000000adv4tnt," +
"25000ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5: insufficient fee",
},
"Failure: unsupported gas fee denom": {
gasFee: sdk.Coins{
// 1BTC, which is not supported as a gas fee denom, and should be plenty to cover gas.
sdk.NewCoin(constants.BtcUsd.Denom, sdkmath.NewInt(100_000_000)),
},
responseCode: sdkerrors.ErrInsufficientFee.ABCICode(),
logMessage: "insufficient fees; got: 100000000btc-denom required: 25000000000000000adv4tnt," +
"25000ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5: insufficient fee",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
msg := &bank.MsgSend{
FromAddress: constants.BobAccAddress.String(),
ToAddress: constants.AliceAccAddress.String(),
Amount: []sdk.Coin{
sdk.NewCoin(assets.AssetUsdc.Denom, sdkmath.NewInt(1)),
},
}

tApp := testapp.NewTestAppBuilder(t).
WithAppCreatorFn(
testapp.DefaultTestAppCreatorFn(map[string]interface{}{},
baseapp.SetMinGasPrices(cmd.MinGasPrice),
)).
Build()
ctx := tApp.InitChain()

msgSendCheckTx := testapp.MustMakeCheckTxWithPrivKeySupplier(
ctx,
tApp.App,
testapp.MustMakeCheckTxOptions{
AccAddressForSigning: constants.BobAccAddress.String(),
Gas: constants.TestGasLimit,
FeeAmt: tc.gasFee,
},
constants.GetPrivateKeyFromAddress,
msg,
)

checkTx := tApp.CheckTx(msgSendCheckTx)
// Sanity check that gas was used.
require.Greater(t, checkTx.GasUsed, int64(0))
require.Equal(t, tc.responseCode, checkTx.Code)
if tc.responseCode != errors.SuccessABCICode {
require.Equal(t, tc.logMessage, checkTx.Log)
}
})
}
}
17 changes: 11 additions & 6 deletions protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ type AdvanceToBlockOptions struct {
ValidateDeliverTxs ValidateDeliverTxsFn
}

// Create an instance of app.App with default settings, suitable for unit testing,
// with the option to override specific flags.
func DefaultTestApp(customFlags map[string]interface{}) *app.App {
// DefaultTestApp creates an instance of app.App with default settings, suitable for unit testing. The app will be
// initialized with any specified flags as overrides, and with any specified base app options.
func DefaultTestApp(customFlags map[string]interface{}, baseAppOptions ...func(*baseapp.BaseApp)) *app.App {
appOptions := appoptions.GetDefaultTestAppOptionsFromTempDirectory("", customFlags)
logger, ok := appOptions.Get(testlog.LoggerInstanceForTest).(log.Logger)
if !ok {
Expand All @@ -118,14 +118,19 @@ func DefaultTestApp(customFlags map[string]interface{}) *app.App {
nil,
true,
appOptions,
baseAppOptions...,
)
return dydxApp
}

// DefaultTestAppCreatorFn is a wrapper function around DefaultTestApp using the specified custom flags.
func DefaultTestAppCreatorFn(customFlags map[string]interface{}) AppCreatorFn {
// DefaultTestAppCreatorFn is a wrapper function around DefaultTestApp using the specified custom flags, and allowing
// for optional base app options.
func DefaultTestAppCreatorFn(
customFlags map[string]interface{},
baseAppOptions ...func(*baseapp.BaseApp),
) AppCreatorFn {
return func() *app.App {
return DefaultTestApp(customFlags)
return DefaultTestApp(customFlags, baseAppOptions...)
}
}

Expand Down
5 changes: 5 additions & 0 deletions protocol/testutil/constants/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
const (
// TestFee is the gas fee offered for test transactions.
TestFee = "50000" + asstypes.UusdcDenom // 5 cents
// TestFeeNativeTokens is the gas fee offered for test transactions specified in native tokens.
// Value is .05 of native token in adv4tnt denom.
TestFeeNativeTokens = "50000000000000000" + lib.DefaultBaseDenom
// TestGasLimit is the gas limit used for test transactions.
// It's set to a larger amount such that the transaction never runs out of gas.
TestGasLimit = 1_000_000
Expand All @@ -21,6 +24,8 @@ const (
var (
// TestFeeCoins_5Cents is the gas fee offered for test transactions.
TestFeeCoins_5Cents = lib.MustParseCoinsNormalized(TestFee)
// TestFeeCoins_5Cents_NativeToken is the gas fee offered for test transactions specified in native tokens.
TestFeeCoins_5Cents_NativeToken = lib.MustParseCoinsNormalized(TestFeeNativeTokens)
)

// BigNegMaxUint64 returns a `big.Int` that is set to -math.MaxUint64.
Expand Down

0 comments on commit dcd6ecb

Please sign in to comment.