0g-chain/x/issuance/keeper/issuance_test.go

806 lines
23 KiB
Go
Raw Normal View History

package keeper_test
import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
tmprototypes "github.com/cometbft/cometbft/proto/tendermint/types"
Update cosmos-sdk to v0.47.7 (#1811) * Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 22:54:10 +00:00
"github.com/cometbft/cometbft/crypto"
tmtime "github.com/cometbft/cometbft/types/time"
2024-08-02 11:32:08 +00:00
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/x/issuance/keeper"
"github.com/0glabs/0g-chain/x/issuance/types"
)
// Test suite used for all keeper tests
type KeeperTestSuite struct {
suite.Suite
tApp app.TestApp
keeper keeper.Keeper
ctx sdk.Context
addrs []string
modAccount sdk.AccAddress
}
// The default state used by each test
func (suite *KeeperTestSuite) SetupTest() {
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, tmprototypes.Header{Height: 1, Time: tmtime.Now()})
tApp.InitializeFromGenesisStates()
_, addrs := app.GeneratePrivKeyAddressPairs(5)
var strAddrs []string
for _, addr := range addrs {
acc := tApp.GetAccountKeeper().NewAccountWithAddress(ctx, addr)
tApp.GetAccountKeeper().SetAccount(ctx, acc)
strAddrs = append(strAddrs, addr.String())
}
keeper := tApp.GetIssuanceKeeper()
2024-08-02 11:32:08 +00:00
modAccount, err := sdk.AccAddressFromBech32("0g1ffv7nhd3z6sych2qpqkk03ec6hzkmufyhp5hf8")
suite.Require().NoError(err)
suite.tApp = tApp
suite.ctx = ctx
suite.keeper = keeper
suite.addrs = strAddrs
suite.modAccount = modAccount
}
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authtypes.AccountI {
ak := suite.tApp.GetAccountKeeper()
return ak.GetAccount(suite.ctx, addr)
}
func (suite *KeeperTestSuite) getBalance(addr sdk.AccAddress, denom string) sdk.Coin {
bk := suite.tApp.GetBankKeeper()
return bk.GetBalance(suite.ctx, addr, denom)
}
func (suite *KeeperTestSuite) getModuleAccount(name string) authtypes.ModuleAccountI {
sk := suite.tApp.GetAccountKeeper()
return sk.GetModuleAccount(suite.ctx, name)
}
func (suite *KeeperTestSuite) TestGetSetParams() {
params := suite.keeper.GetParams(suite.ctx)
suite.Require().Equal(types.Params{Assets: []types.Asset(nil)}, params)
asset := types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0)))
params = types.NewParams([]types.Asset{asset})
suite.keeper.SetParams(suite.ctx, params)
newParams := suite.keeper.GetParams(suite.ctx)
suite.Require().Equal(params, newParams)
}
func (suite *KeeperTestSuite) TestIssueTokens() {
type args struct {
assets []types.Asset
sender string
tokens sdk.Coin
receiver string
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid issuance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[2],
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"non-owner issuance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[2],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[3],
},
errArgs{
expectPass: false,
contains: "account not authorized",
},
},
{
"invalid denom",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("othertoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[2],
},
errArgs{
expectPass: false,
contains: "no asset with input denom found",
},
},
{
"issue to blocked address",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[1],
},
errArgs{
expectPass: false,
contains: "account is blocked",
},
},
2024-08-02 11:32:08 +00:00
// TODO: need fix
// {
// "issue to module account",
// args{
// assets: []types.Asset{
// types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
// },
// sender: suite.addrs[0],
// tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
// receiver: suite.modAccount.String(),
// },
// errArgs{
// expectPass: false,
// contains: "cannot issue tokens to module account",
// },
// },
{
"paused issuance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, true, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[1],
},
errArgs{
expectPass: false,
contains: "asset is paused",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
receiver, _ := sdk.AccAddressFromBech32(tc.args.receiver)
err := suite.keeper.IssueTokens(suite.ctx, tc.args.tokens, sender, receiver)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
suite.Require().Equal(sdk.NewCoins(tc.args.tokens), sdk.NewCoins(suite.getBalance(receiver, tc.args.tokens.Denom)))
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestIssueTokensRateLimited() {
type args struct {
assets []types.Asset
supplies []types.AssetSupply
sender string
tokens sdk.Coin
receiver string
blockTime time.Time
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid issuance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
},
supplies: []types.AssetSupply{
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
receiver: suite.addrs[2],
blockTime: suite.ctx.BlockTime().Add(time.Hour),
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"over-limit issuance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
},
supplies: []types.AssetSupply{
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
},
sender: suite.addrs[0],
tokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(10000000001)),
receiver: suite.addrs[2],
blockTime: suite.ctx.BlockTime().Add(time.Hour),
},
errArgs{
expectPass: false,
contains: "asset supply over limit",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
for _, supply := range tc.args.supplies {
suite.keeper.SetAssetSupply(suite.ctx, supply, supply.GetDenom())
}
suite.ctx = suite.ctx.WithBlockTime(tc.args.blockTime)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
receiver, _ := sdk.AccAddressFromBech32(tc.args.receiver)
err := suite.keeper.IssueTokens(suite.ctx, tc.args.tokens, sender, receiver)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
suite.Require().Equal(sdk.NewCoins(tc.args.tokens), sdk.NewCoins(suite.getBalance(receiver, tc.args.tokens.Denom)))
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestRedeemTokens() {
type args struct {
assets []types.Asset
sender string
initialTokens sdk.Coin
redeemTokens sdk.Coin
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid redemption",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
initialTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
redeemTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"invalid denom redemption",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
initialTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
redeemTokens: sdk.NewCoin("othertoken", sdkmath.NewInt(100000)),
},
errArgs{
expectPass: false,
contains: "",
},
},
{
"non-owner redemption",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[2],
initialTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
redeemTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
},
errArgs{
expectPass: false,
contains: "account not authorized",
},
},
{
"paused redemption",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, true, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
initialTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
redeemTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
},
errArgs{
expectPass: false,
contains: "asset is paused",
},
},
{
"redeem amount greater than balance",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
initialTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
redeemTokens: sdk.NewCoin("usdtoken", sdkmath.NewInt(200000)),
},
errArgs{
expectPass: false,
contains: "insufficient funds",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
sk := suite.tApp.GetBankKeeper()
err := sk.MintCoins(suite.ctx, types.ModuleAccountName, sdk.NewCoins(tc.args.initialTokens))
suite.Require().NoError(err)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
err = sk.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleAccountName, sender, sdk.NewCoins(tc.args.initialTokens))
suite.Require().NoError(err)
err = suite.keeper.RedeemTokens(suite.ctx, tc.args.redeemTokens, sender)
if tc.errArgs.expectPass {
suite.Require().NoError(err)
initialSupply := sdk.NewCoins(tc.args.redeemTokens)
moduleAccount := suite.getModuleAccount(types.ModuleAccountName)
feat: upgrade to Cosmos v0.46 (#1477) * Update cosmos-sdk to v0.45.10-kava * Add RegisterNodeService to app * Update cosmos proto files * Update cosmos proto files * Use tagged v0.45.10-kava-v0.19-0.21 cosmos version * update x/auth/legacy to x/auth/migrations * Delete rest packages and registration * Remove rest from proposal handlers * Remove legacy types referencing removed sdk types * Remove legacy tx broadcast handler * Update incentive staking hooks to return error * Remove grpc replace directive, use new grpc version * Fix storetypes import * Update tally_handler with updated gov types * Delete legacy types * Use new gov default config * Update RegisterTendermintService params Signed-off-by: drklee3 <derrick@dlee.dev> * Replace sdk.StoreKey with storetypes.StoreKey * Replace sdk.Int#ToDec with sdk.NewDecFromInt * Replace sdk.NewUintFromBigInt with sdkmath.NewUintFromBigInt Signed-off-by: drklee3 <derrick@dlee.dev> * Update most intances of govtypes to govv1beta1 * Unpack coin slice for Coins#Sub and Coins#SafeSub Signed-off-by: drklee3 <derrick@dlee.dev> * Update committee gov codec registration Signed-off-by: drklee3 <derrick@dlee.dev> * Update migrate utils period_vesting Coins#Sub Signed-off-by: drklee3 <derrick@dlee.dev> * Update Coin#Sub in community proposal handler Signed-off-by: drklee3 <derrick@dlee.dev> * Update Coin#Sub, FundModuleAccount/FundAccount in banktestutil Signed-off-by: drklee3 <derrick@dlee.dev> * Update community, earn, kavadist proposal gov registration * Update evm cli client EthSecp256k1Type check * AccAddressFromHex to AccAddressFromHexUnsafe * Add mint DefaultInflationCalculationFn to earn test * Update use of removed staking.NewHandler * Rename FlagIAVLFastNode -> FlagDisableIAVLFastNode * cmd: Update new snapshot app option Signed-off-by: drklee3 <derrick@dlee.dev> * cmd: Add tendermint default config, use cosmos rpc status command Signed-off-by: drklee3 <derrick@dlee.dev> * Update ethermint import path github.com/tharsis/ethermint -> github.com/evmos/ethermint * Upgrade ibc-go to v6 * Update proto dependencies Signed-off-by: drklee3 <derrick@dlee.dev> * Update Tally handler test with new gov types * Update helpers.GenTx -> helpers.GenSignedMockTx * Update evmkeeper.NewKeeper params Signed-off-by: drklee3 <derrick@dlee.dev> * Update ante authz, tests * Add feemarket transient key, pass subspaces to evm/feemarket keepers * Update new ante decorators * Add new addModuleInitFlags to server commands * Pass codec to keyring.New in genaccounts * Pass codec to client keys add * Add SendCoins to evmutil bank_keeper * Use github.com/cosmos/iavl@v0.19.5 * Add ante HandlerOptions * Add unimplemented SendCoins to evmutil bank keeper Ethermint x/evm does not use this method * Update init-new-chain script to disable post-london blocks * Modify test genesis states to append 1 validator * Update tally handler test to use string values * Prevent querying balance for empty sdk.AccAddress in auction bidding test * Set default bond denom to ukava * Remove overwritten bank genesis total supply in committee proposal test Signed-off-by: drklee3 <derrick@dlee.dev> * Use ukava for testing staked balance * Disable minting in community proposal handler test Previously stake denom is used, which resulted in 0 minted coins * Update hard APYToSPY test expected value Increased iterations in sdk.ApproxRoot, updated closer to real value * Fix NewDecCoinsFromCoins bug in incentive collectDerivativeStakingRewards * Allow bkava earn incentive test values to match within small margin for rounding Signed-off-by: drklee3 <derrick@dlee.dev> * Update invalid denom in issuance message coin validation Colons are now valid in denoms Signed-off-by: drklee3 <derrick@dlee.dev> * Remove genesis validator in incentive delegation tests * Update pricefeed market test for invalid denom Signed-off-by: drklee3 <derrick@dlee.dev> * Update incentive delegator rewards test without genesis validator Signed-off-by: drklee3 <derrick@dlee.dev> * Add validator to export test * Clear bank state in minting tests Signed-off-by: drklee3 <derrick@dlee.dev> * Remove validator for no stake tally test Signed-off-by: drklee3 <derrick@dlee.dev> * Clear incentive state before InitGenesis in incentive genesis export test * Update swagger Signed-off-by: drklee3 <derrick@dlee.dev> * Update ethermint version to match replaced version * Remove legacy swagger * Add NewEthEmitEventDecorator * Remove redundant func for AddModuleInitFlags * Remove unused addBankBalanceForAddress func * Add SetIAVLLazyLoading option to app cmd * Use legacy.RegisterAminoMsg for committee msg concrete registration * Remove unnecessary Amino field * Add evm_util bankkeeper SendCoins comment * Update test method ResetBankState to DeleteGenesisValidatorCoins to be more clear * Validate incentive params.RewardsPerSecond to be non-zero * Validate swap pools to disallow colons in token denoms * Register all legacy amino types on gov modulecdc * Remove redundant Comittee interface registration * Pin goleveldb to v1.0.1-0.20210819022825-2ae1ddf74ef7 Causes failed to load state at height errors * Update ethermint to new pinned version with minGasPrices parse error fix * Update cosmos fork dependcy commit to include reverted account constructor patch * Update Cosmos v0.46.11 and cometbft v0.34.27 * Bump minimum go version to 1.19 * Update tendermint proto * Update internal testnet genesis * Move NewCanTransferDecorator before NewEthGasConsumeDecorator * Add hard borrow store tests (#1514) * add store tests for Borrow type * refactor Deposit tests to match * Fix old bep3 tests (#1515) * Update Ethermint to 1b17445 to fix duplicate proto registration * Add custom status command to use snake_case and stdout * Add SetInflation helper * Reduce ambiguity with evm CanSignEthTx error * Remove init genesis validator claim in test * Add disabled evmante.NewMinGasPriceDecorator with x/feemarket note * chore: use tagged versions for Cosmos and Ethermint forks * update kvtool & increase wait for ibc transfer test --------- Signed-off-by: drklee3 <derrick@dlee.dev> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Robert Pirtle <astropirtle@gmail.com>
2023-04-04 00:08:45 +00:00
suite.Require().Equal(sdk.NewCoins(initialSupply.Sub(tc.args.redeemTokens)...), sdk.NewCoins(suite.getBalance(moduleAccount.GetAddress(), tc.args.redeemTokens.Denom)))
} else {
suite.Require().Error(err)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestBlockAddress() {
type args struct {
assets []types.Asset
sender string
blockedAddr string
denom string
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid block",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: suite.addrs[1],
denom: "usdtoken",
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"unblockable token",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, false, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: suite.addrs[1],
denom: "usdtoken",
},
errArgs{
expectPass: false,
contains: "asset does not support block/unblock functionality",
},
},
{
"non-owner block",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[2],
blockedAddr: suite.addrs[1],
denom: "usdtoken",
},
errArgs{
expectPass: false,
contains: "account not authorized",
},
},
{
"invalid denom block",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: suite.addrs[1],
denom: "othertoken",
},
errArgs{
expectPass: false,
contains: "no asset with input denom found",
},
},
{
"block non-existing account",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: sdk.AccAddress(crypto.AddressHash([]byte("RandomAddr"))).String(),
denom: "usdtoken",
},
errArgs{
expectPass: false,
contains: "cannot block account that does not exist in state",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
blockedAddr, _ := sdk.AccAddressFromBech32(tc.args.blockedAddr)
err := suite.keeper.BlockAddress(suite.ctx, tc.args.denom, sender, blockedAddr)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
asset, found := suite.keeper.GetAsset(suite.ctx, tc.args.denom)
blocked := false
suite.Require().True(found)
for _, blockedAddr := range asset.BlockedAddresses {
if blockedAddr == tc.args.blockedAddr {
blocked = true
}
}
suite.Require().True(blocked)
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestUnblockAddress() {
type args struct {
assets []types.Asset
sender string
blockedAddr string
denom string
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid unblock",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: suite.addrs[1],
denom: "usdtoken",
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"non-owner unblock",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[2],
blockedAddr: suite.addrs[1],
denom: "usdtoken",
},
errArgs{
expectPass: false,
contains: "account not authorized",
},
},
{
"invalid denom block",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
blockedAddr: suite.addrs[1],
denom: "othertoken",
},
errArgs{
expectPass: false,
contains: "no asset with input denom found",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
blockedAddr, _ := sdk.AccAddressFromBech32(tc.args.blockedAddr)
err := suite.keeper.UnblockAddress(suite.ctx, tc.args.denom, sender, blockedAddr)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
asset, found := suite.keeper.GetAsset(suite.ctx, tc.args.denom)
blocked := false
suite.Require().True(found)
for _, blockedAddr := range asset.BlockedAddresses {
if blockedAddr == tc.args.blockedAddr {
blocked = true
}
}
suite.Require().False(blocked)
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestChangePauseStatus() {
type args struct {
assets []types.Asset
sender string
startStatus bool
endStatus bool
denom string
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid pause",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
startStatus: false,
endStatus: true,
denom: "usdtoken",
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"valid unpause",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, true, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
startStatus: true,
endStatus: false,
denom: "usdtoken",
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"non-owner pause",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[2],
startStatus: false,
endStatus: true,
denom: "usdtoken",
},
errArgs{
expectPass: false,
contains: "account not authorized",
},
},
{
"invalid denom pause",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
sender: suite.addrs[0],
startStatus: true,
endStatus: false,
denom: "othertoken",
},
errArgs{
expectPass: false,
contains: "no asset with input denom found",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
sender, _ := sdk.AccAddressFromBech32(tc.args.sender)
err := suite.keeper.SetPauseStatus(suite.ctx, sender, tc.args.denom, tc.args.endStatus)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
asset, found := suite.keeper.GetAsset(suite.ctx, tc.args.denom)
suite.Require().True(found)
suite.Require().Equal(tc.args.endStatus, asset.Paused)
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestSeizeCoinsFromBlockedAddress() {
type args struct {
assets []types.Asset
initialCoins sdk.Coin
blockedAddrs []string
denom string
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid seize",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
initialCoins: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000000)),
denom: "usdtoken",
blockedAddrs: []string{suite.addrs[1], suite.addrs[2]},
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"invalid denom seize",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{}, false, true, types.NewRateLimit(false, sdk.ZeroInt(), time.Duration(0))),
},
initialCoins: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000000)),
denom: "othertoken",
blockedAddrs: []string{suite.addrs[1], suite.addrs[2]},
},
errArgs{
expectPass: false,
contains: "no asset with input denom found",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
assetsWithBlockedAddrs := []types.Asset{}
for _, asset := range tc.args.assets {
asset.BlockedAddresses = tc.args.blockedAddrs
assetsWithBlockedAddrs = append(assetsWithBlockedAddrs, asset)
}
params := types.NewParams(assetsWithBlockedAddrs)
suite.keeper.SetParams(suite.ctx, params)
sk := suite.tApp.GetBankKeeper()
for _, addrStr := range tc.args.blockedAddrs {
addr, _ := sdk.AccAddressFromBech32(addrStr)
err := sk.MintCoins(suite.ctx, types.ModuleAccountName, sdk.NewCoins(tc.args.initialCoins))
suite.Require().NoError(err)
err = sk.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleAccountName, addr, sdk.NewCoins(tc.args.initialCoins))
}
err := suite.keeper.SeizeCoinsFromBlockedAddresses(suite.ctx, tc.args.denom)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
asset, found := suite.keeper.GetAsset(suite.ctx, tc.args.denom)
suite.Require().True(found)
owner, _ := sdk.AccAddressFromBech32(asset.Owner)
ownerCoinAmount := tc.args.initialCoins.Amount.Mul(sdkmath.NewInt(int64(len(tc.args.blockedAddrs))))
suite.Require().Equal(sdk.NewCoins(sdk.NewCoin(tc.args.denom, ownerCoinAmount)), sdk.NewCoins(suite.getBalance(owner, tc.args.initialCoins.Denom)))
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}