2020-12-02 17:37:11 +00:00
|
|
|
package ante_test
|
|
|
|
|
|
|
|
import (
|
2023-04-04 00:08:45 +00:00
|
|
|
"math/rand"
|
2022-05-06 18:41:58 +00:00
|
|
|
"os"
|
2020-12-02 17:37:11 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2020-12-02 17:37:11 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-05-06 18:41:58 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
|
|
|
authz "github.com/cosmos/cosmos-sdk/x/authz"
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2023-04-04 00:08:45 +00:00
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2020-12-02 17:37:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
tmdb "github.com/tendermint/tm-db"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
2024-05-01 05:53:58 +00:00
|
|
|
"github.com/0glabs/0g-chain/chaincfg"
|
2024-05-01 03:17:24 +00:00
|
|
|
bep3types "github.com/0glabs/0g-chain/x/bep3/types"
|
|
|
|
pricefeedtypes "github.com/0glabs/0g-chain/x/pricefeed/types"
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
|
|
|
|
2022-05-06 18:41:58 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2024-05-01 05:53:58 +00:00
|
|
|
chaincfg.SetSDKConfig()
|
2022-05-06 18:41:58 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppAnteHandler_AuthorizedMempool(t *testing.T) {
|
2020-12-02 17:37:11 +00:00
|
|
|
testPrivKeys, testAddresses := app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
unauthed := testAddresses[0:2]
|
2022-01-08 00:39:27 +00:00
|
|
|
unauthedKeys := testPrivKeys[0:2]
|
2020-12-02 17:37:11 +00:00
|
|
|
deputy := testAddresses[2]
|
|
|
|
deputyKey := testPrivKeys[2]
|
|
|
|
oracles := testAddresses[3:6]
|
|
|
|
oraclesKeys := testPrivKeys[3:6]
|
|
|
|
manual := testAddresses[6:]
|
|
|
|
manualKeys := testPrivKeys[6:]
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
encodingConfig := app.MakeEncodingConfig()
|
2022-04-21 20:16:28 +00:00
|
|
|
|
|
|
|
opts := app.DefaultOptions
|
|
|
|
opts.MempoolEnableAuth = true
|
|
|
|
opts.MempoolAuthAddresses = manual
|
|
|
|
|
2020-12-02 17:37:11 +00:00
|
|
|
tApp := app.TestApp{
|
2022-01-08 00:39:27 +00:00
|
|
|
App: *app.NewApp(
|
|
|
|
log.NewNopLogger(),
|
|
|
|
tmdb.NewMemDB(),
|
2024-05-01 05:53:58 +00:00
|
|
|
chaincfg.DefaultNodeHome,
|
2022-01-08 00:39:27 +00:00
|
|
|
nil,
|
|
|
|
encodingConfig,
|
2022-04-21 20:16:28 +00:00
|
|
|
opts,
|
2020-12-02 17:37:11 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
chainID := "kavatest_1-1"
|
2020-12-02 17:37:11 +00:00
|
|
|
tApp = tApp.InitializeFromGenesisStatesWithTimeAndChainID(
|
|
|
|
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
chainID,
|
2022-01-08 00:39:27 +00:00
|
|
|
app.NewFundedGenStateWithSameCoins(
|
|
|
|
tApp.AppCodec(),
|
2024-05-01 05:53:58 +00:00
|
|
|
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.DisplayDenom, 1e9)),
|
2020-12-02 17:37:11 +00:00
|
|
|
testAddresses,
|
|
|
|
),
|
2022-01-08 00:39:27 +00:00
|
|
|
newBep3GenStateMulti(tApp.AppCodec(), deputy),
|
|
|
|
newPricefeedGenStateMulti(tApp.AppCodec(), oracles),
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
address sdk.AccAddress
|
2022-01-08 00:39:27 +00:00
|
|
|
privKey cryptotypes.PrivKey
|
2020-12-02 17:37:11 +00:00
|
|
|
expectPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unauthorized",
|
|
|
|
address: unauthed[1],
|
2022-01-08 00:39:27 +00:00
|
|
|
privKey: unauthedKeys[1],
|
2020-12-02 17:37:11 +00:00
|
|
|
expectPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oracle",
|
|
|
|
address: oracles[1],
|
|
|
|
privKey: oraclesKeys[1],
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "deputy",
|
|
|
|
address: deputy,
|
|
|
|
privKey: deputyKey,
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manual",
|
|
|
|
address: manual[1],
|
|
|
|
privKey: manualKeys[1],
|
|
|
|
expectPass: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-04-04 00:08:45 +00:00
|
|
|
stdTx, err := helpers.GenSignedMockTx(
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano())),
|
2022-01-08 00:39:27 +00:00
|
|
|
encodingConfig.TxConfig,
|
2020-12-02 17:37:11 +00:00
|
|
|
[]sdk.Msg{
|
2022-01-08 00:39:27 +00:00
|
|
|
banktypes.NewMsgSend(
|
2020-12-02 17:37:11 +00:00
|
|
|
tc.address,
|
|
|
|
testAddresses[0],
|
2024-05-01 05:53:58 +00:00
|
|
|
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.DisplayDenom, 1_000_000)),
|
2020-12-02 17:37:11 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
sdk.NewCoins(), // no fee
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{0},
|
|
|
|
[]uint64{0}, // fixed sequence numbers will cause tests to fail sig verification if the same address is used twice
|
|
|
|
tc.privKey,
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
txBytes, err := encodingConfig.TxConfig.TxEncoder()(stdTx)
|
2020-12-02 17:37:11 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res := tApp.CheckTx(
|
|
|
|
abci.RequestCheckTx{
|
|
|
|
Tx: txBytes,
|
|
|
|
Type: abci.CheckTxType_New,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if tc.expectPass {
|
|
|
|
require.Zero(t, res.Code, res.Log)
|
|
|
|
} else {
|
|
|
|
require.NotZero(t, res.Code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func newPricefeedGenStateMulti(cdc codec.JSONCodec, oracles []sdk.AccAddress) app.GenesisState {
|
|
|
|
pfGenesis := pricefeedtypes.GenesisState{
|
|
|
|
Params: pricefeedtypes.Params{
|
|
|
|
Markets: []pricefeedtypes.Market{
|
2020-12-02 17:37:11 +00:00
|
|
|
{MarketID: "btc:usd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: oracles, Active: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return app.GenesisState{pricefeedtypes.ModuleName: cdc.MustMarshalJSON(&pfGenesis)}
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func newBep3GenStateMulti(cdc codec.JSONCodec, deputyAddress sdk.AccAddress) app.GenesisState {
|
|
|
|
bep3Genesis := bep3types.GenesisState{
|
|
|
|
Params: bep3types.Params{
|
|
|
|
AssetParams: bep3types.AssetParams{
|
|
|
|
bep3types.AssetParam{
|
2020-12-02 17:37:11 +00:00
|
|
|
Denom: "bnb",
|
|
|
|
CoinID: 714,
|
2022-01-08 00:39:27 +00:00
|
|
|
SupplyLimit: bep3types.SupplyLimit{
|
2023-04-05 23:21:59 +00:00
|
|
|
Limit: sdkmath.NewInt(350000000000000),
|
2020-12-02 17:37:11 +00:00
|
|
|
TimeLimited: false,
|
|
|
|
TimeBasedLimit: sdk.ZeroInt(),
|
|
|
|
TimePeriod: time.Hour,
|
|
|
|
},
|
|
|
|
Active: true,
|
|
|
|
DeputyAddress: deputyAddress,
|
2023-04-05 23:21:59 +00:00
|
|
|
FixedFee: sdkmath.NewInt(1000),
|
2020-12-02 17:37:11 +00:00
|
|
|
MinSwapAmount: sdk.OneInt(),
|
2023-04-05 23:21:59 +00:00
|
|
|
MaxSwapAmount: sdkmath.NewInt(1000000000000),
|
2022-01-08 00:39:27 +00:00
|
|
|
MinBlockLock: bep3types.DefaultMinBlockLock,
|
|
|
|
MaxBlockLock: bep3types.DefaultMaxBlockLock,
|
2020-12-02 17:37:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-08 00:39:27 +00:00
|
|
|
Supplies: bep3types.AssetSupplies{
|
|
|
|
bep3types.NewAssetSupply(
|
2020-12-02 17:37:11 +00:00
|
|
|
sdk.NewCoin("bnb", sdk.ZeroInt()),
|
|
|
|
sdk.NewCoin("bnb", sdk.ZeroInt()),
|
|
|
|
sdk.NewCoin("bnb", sdk.ZeroInt()),
|
|
|
|
sdk.NewCoin("bnb", sdk.ZeroInt()),
|
|
|
|
time.Duration(0),
|
|
|
|
),
|
|
|
|
},
|
2022-01-08 00:39:27 +00:00
|
|
|
PreviousBlockTime: bep3types.DefaultPreviousBlockTime,
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return app.GenesisState{bep3types.ModuleName: cdc.MustMarshalJSON(&bep3Genesis)}
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|
2022-05-06 18:41:58 +00:00
|
|
|
|
|
|
|
func TestAppAnteHandler_RejectMsgsInAuthz(t *testing.T) {
|
|
|
|
testPrivKeys, testAddresses := app.GeneratePrivKeyAddressPairs(10)
|
|
|
|
|
|
|
|
newMsgGrant := func(msgTypeUrl string) *authz.MsgGrant {
|
2023-04-04 00:08:45 +00:00
|
|
|
t := time.Date(9000, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
2022-05-06 18:41:58 +00:00
|
|
|
msg, err := authz.NewMsgGrant(
|
|
|
|
testAddresses[0],
|
|
|
|
testAddresses[1],
|
|
|
|
authz.NewGenericAuthorization(msgTypeUrl),
|
2023-04-04 00:08:45 +00:00
|
|
|
&t,
|
2022-05-06 18:41:58 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
chainID := "kavatest_1-1"
|
|
|
|
encodingConfig := app.MakeEncodingConfig()
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
msg sdk.Msg
|
|
|
|
expectedCode uint32
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "MsgEthereumTx is blocked",
|
|
|
|
msg: newMsgGrant(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{})),
|
|
|
|
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "MsgCreateVestingAccount is blocked",
|
|
|
|
msg: newMsgGrant(sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{})),
|
|
|
|
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
|
|
|
|
tApp = tApp.InitializeFromGenesisStatesWithTimeAndChainID(
|
|
|
|
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
chainID,
|
|
|
|
)
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
stdTx, err := helpers.GenSignedMockTx(
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano())),
|
2022-05-06 18:41:58 +00:00
|
|
|
encodingConfig.TxConfig,
|
|
|
|
[]sdk.Msg{tc.msg},
|
|
|
|
sdk.NewCoins(), // no fee
|
|
|
|
helpers.DefaultGenTxGas,
|
|
|
|
chainID,
|
|
|
|
[]uint64{0},
|
|
|
|
[]uint64{0},
|
|
|
|
testPrivKeys[0],
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
txBytes, err := encodingConfig.TxConfig.TxEncoder()(stdTx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resCheckTx := tApp.CheckTx(
|
|
|
|
abci.RequestCheckTx{
|
|
|
|
Tx: txBytes,
|
|
|
|
Type: abci.CheckTxType_New,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.Equal(t, resCheckTx.Code, tc.expectedCode, resCheckTx.Log)
|
|
|
|
|
|
|
|
resDeliverTx := tApp.DeliverTx(
|
|
|
|
abci.RequestDeliverTx{
|
|
|
|
Tx: txBytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
require.Equal(t, resDeliverTx.Code, tc.expectedCode, resDeliverTx.Log)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|