Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tests refactoring #793

Merged
merged 15 commits into from
Dec 20, 2024
27 changes: 16 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

v502 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.2"
v504 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.4"
v505 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.5"
dynamicfeestypes "github.com/neutron-org/neutron/v5/x/dynamicfees/types"

"github.com/skip-mev/feemarket/x/feemarket"
Expand Down Expand Up @@ -232,6 +233,7 @@ var (
v500.Upgrade,
v502.Upgrade,
v504.Upgrade,
v505.Upgrade,
}

// DefaultNodeHome default home directories for the application daemon
Expand Down Expand Up @@ -623,6 +625,17 @@ func New(
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)

tokenFactoryKeeper := tokenfactorykeeper.NewKeeper(
appCodec,
app.keys[tokenfactorytypes.StoreKey],
maccPerms,
app.AccountKeeper,
&app.BankKeeper,
&app.WasmKeeper,
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)
app.TokenFactoryKeeper = &tokenFactoryKeeper

app.WireICS20PreWasmKeeper(appCodec)
app.PFMModule = packetforward.NewAppModule(app.PFMKeeper, app.GetSubspace(pfmtypes.ModuleName))

Expand Down Expand Up @@ -686,17 +699,6 @@ func New(
app.ConsumerKeeper = *app.ConsumerKeeper.SetHooks(app.SlashingKeeper.Hooks())
consumerModule := ccvconsumer.NewAppModule(app.ConsumerKeeper, app.GetSubspace(ccvconsumertypes.ModuleName))

tokenFactoryKeeper := tokenfactorykeeper.NewKeeper(
appCodec,
app.keys[tokenfactorytypes.StoreKey],
maccPerms,
app.AccountKeeper,
&app.BankKeeper,
&app.WasmKeeper,
authtypes.NewModuleAddress(adminmoduletypes.ModuleName).String(),
)
app.TokenFactoryKeeper = &tokenFactoryKeeper

app.BankKeeper.BaseSendKeeper = app.BankKeeper.BaseSendKeeper.SetHooks(
banktypes.NewMultiBankHooks(
app.TokenFactoryKeeper.Hooks(),
Expand Down Expand Up @@ -1373,6 +1375,8 @@ func (app *App) setupUpgradeHandlers() {
DynamicfeesKeeper: app.DynamicFeesKeeper,
DexKeeper: &app.DexKeeper,
IbcRateLimitKeeper: app.RateLimitingICS4Wrapper.IbcratelimitKeeper,
ChannelKeeper: &app.IBCKeeper.ChannelKeeper,
TransferKeeper: app.TransferKeeper.Keeper,
GlobalFeeSubspace: app.GetSubspace(globalfee.ModuleName),
CcvConsumerSubspace: app.GetSubspace(ccvconsumertypes.ModuleName),
},
Expand Down Expand Up @@ -1722,6 +1726,7 @@ func (app *App) WireICS20PreWasmKeeper(
transferSudo.NewIBCModule(
app.TransferKeeper,
contractmanager.NewSudoLimitWrapper(app.ContractManagerKeeper, &app.WasmKeeper),
app.TokenFactoryKeeper,
),
app.PFMKeeper,
0,
Expand Down
2 changes: 2 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
channelkeeper "github.com/cosmos/ibc-go/v8/modules/core/04-channel/keeper"
ccvconsumerkeeper "github.com/cosmos/interchain-security/v5/x/ccv/consumer/keeper"
auctionkeeper "github.com/skip-mev/block-sdk/v2/x/auction/keeper"
feemarketkeeper "github.com/skip-mev/feemarket/x/feemarket/keeper"
Expand Down Expand Up @@ -69,6 +70,7 @@ type UpgradeKeepers struct {
DynamicfeesKeeper *dynamicfeeskeeper.Keeper
DexKeeper *dexkeeper.Keeper
IbcRateLimitKeeper *ibcratelimitkeeper.Keeper
ChannelKeeper *channelkeeper.Keeper
// subspaces
GlobalFeeSubspace paramtypes.Subspace
CcvConsumerSubspace paramtypes.Subspace
Expand Down
18 changes: 18 additions & 0 deletions app/upgrades/v5.0.5/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v505

import (
storetypes "cosmossdk.io/store/types"

"github.com/neutron-org/neutron/v5/app/upgrades"
)

const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v5.0.5"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: storetypes.StoreUpgrades{},
}
45 changes: 45 additions & 0 deletions app/upgrades/v5.0.5/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v505

import (
"context"
"fmt"

transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/neutron-org/neutron/v5/app/upgrades"
)

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *upgrades.UpgradeKeepers,
_ upgrades.StoreKeys,
_ codec.Codec,
) upgradetypes.UpgradeHandler {
return func(c context.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
ctx := sdk.UnwrapSDKContext(c)

ctx.Logger().Info("Starting module migrations...")

vm, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return vm, err
}

transferChannels := keepers.ChannelKeeper.GetAllChannelsWithPortPrefix(ctx, keepers.TransferKeeper.GetPort(ctx))
for _, channel := range transferChannels {
escrowAddress := transfertypes.GetEscrowAddress(channel.PortId, channel.ChannelId)
ctx.Logger().Info("Saving escrow address", "port_id", channel.PortId, "channel_id",
channel.ChannelId, "address", escrowAddress.String())
keepers.TokenFactoryKeeper.StoreEscrowAddress(ctx, escrowAddress)
}

ctx.Logger().Info(fmt.Sprintf("Migration {%s} applied", UpgradeName))
return vm, nil
}
}
52 changes: 52 additions & 0 deletions app/upgrades/v5.0.5/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v505_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

v505 "github.com/neutron-org/neutron/v5/app/upgrades/v5.0.5"
"github.com/neutron-org/neutron/v5/testutil"
)

type UpgradeTestSuite struct {
testutil.IBCConnectionTestSuite
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

func (suite *UpgradeTestSuite) SetupTest() {
suite.IBCConnectionTestSuite.SetupTest()
}

func (suite *UpgradeTestSuite) TestUpgrade() {
app := suite.GetNeutronZoneApp(suite.ChainA)
ctx := suite.ChainA.GetContext().WithChainID("neutron-1")
t := suite.T()

upgrade := upgradetypes.Plan{
Name: v505.UpgradeName,
Info: "some text here",
Height: 100,
}

var escrowAddresses []sdk.AccAddress
transferChannels := app.IBCKeeper.ChannelKeeper.GetAllChannelsWithPortPrefix(ctx, app.TransferKeeper.GetPort(ctx))
for _, channel := range transferChannels {
escrowAddresses = append(escrowAddresses, transfertypes.GetEscrowAddress(channel.PortId, channel.ChannelId))
}
require.Greater(t, len(escrowAddresses), 0)
require.NoError(t, app.UpgradeKeeper.ApplyUpgrade(ctx, upgrade))

for _, escrowAddress := range escrowAddresses {
require.True(t, app.TokenFactoryKeeper.IsEscrowAddress(ctx, escrowAddress))
}
require.False(t, app.TokenFactoryKeeper.IsEscrowAddress(ctx, []byte{1, 2, 3, 4, 5}))
}
1 change: 0 additions & 1 deletion testutil/mocks/contractmanager/types/expected_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion testutil/mocks/interchainqueries/keeper/verify.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion testutil/mocks/interchaintxs/types/expected_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion testutil/mocks/transfer/types/expected_keepers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions x/tokenfactory/keeper/bankactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string) error {
return status.Errorf(codes.Internal, "minting to module accounts is forbidden")
}

if k.IsEscrowAddress(ctx, mintToAcc) {
return status.Errorf(codes.Internal, "minting to IBC escrow accounts is forbidden")
}

err = k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(amount))
if err != nil {
return err
Expand All @@ -50,6 +54,10 @@ func (k Keeper) burnFrom(ctx sdk.Context, amount sdk.Coin, burnFrom string) erro
return status.Errorf(codes.Internal, "burning from module accounts is forbidden")
}

if k.IsEscrowAddress(ctx, burnFromAcc) {
return status.Errorf(codes.Internal, "burning from IBC escrow accounts is forbidden")
}

err = k.bankKeeper.SendCoinsFromAccountToModule(ctx,
burnFromAcc,
types.ModuleName,
Expand Down Expand Up @@ -86,6 +94,14 @@ func (k Keeper) forceTransfer(ctx sdk.Context, amount sdk.Coin, fromAddr, toAddr
return status.Errorf(codes.Internal, "force transfer to module accounts is forbidden")
}

if k.IsEscrowAddress(ctx, transferFromAcc) {
return status.Errorf(codes.Internal, "force transfer from IBC escrow accounts is forbidden")
}

if k.IsEscrowAddress(ctx, transferToAcc) {
return status.Errorf(codes.Internal, "force transfer to IBC escrow accounts is forbidden")
}

return k.bankKeeper.SendCoins(ctx, transferFromAcc, transferToAcc, sdk.NewCoins(amount))
}

Expand Down
21 changes: 21 additions & 0 deletions x/tokenfactory/keeper/escrow_addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper

import (
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/neutron-org/neutron/v5/x/tokenfactory/types"
)

// StoreEscrowAddress sets the total set of params.
func (k Keeper) StoreEscrowAddress(ctx sdk.Context, address sdk.AccAddress) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.EscrowAddressKey)

prefixStore.Set(address.Bytes(), []byte{0})
}

func (k Keeper) IsEscrowAddress(ctx sdk.Context, address sdk.AccAddress) bool {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.EscrowAddressKey)
bz := prefixStore.Get(address.Bytes())

return len(bz) != 0
}
1 change: 0 additions & 1 deletion x/tokenfactory/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/neutron-org/neutron/v5/x/tokenfactory/types"
)

Expand Down
2 changes: 2 additions & 0 deletions x/tokenfactory/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
const (
KeySeparator = "|"
prefixParamsKey = iota + 1
prefixEscrowAddressKey
)

var (
Expand All @@ -34,6 +35,7 @@ var (
AdminPrefixKey = "admin"
BeforeSendHookAddressPrefixKey = "beforesendhook"
ParamsKey = []byte{prefixParamsKey}
EscrowAddressKey = []byte{prefixEscrowAddressKey}
)

// GetDenomPrefixStore returns the store prefix where all the data associated with a specific denom
Expand Down
7 changes: 5 additions & 2 deletions x/transfer/ibc_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func TestHandleAcknowledgement(t *testing.T) {
feeKeeper := mock_types.NewMockFeeRefunderKeeper(ctrl)
chanKeeper := mock_types.NewMockChannelKeeper(ctrl)
authKeeper := mock_types.NewMockAccountKeeper(ctrl)
tokenfactoryKeeper := mock_types.NewMockTokenfactoryKeeper(ctrl)

// required to initialize keeper
authKeeper.EXPECT().GetModuleAddress(transfertypes.ModuleName).Return([]byte("address"))
txKeeper, infCtx, _ := testkeeper.TransferKeeper(t, wmKeeper, feeKeeper, chanKeeper, authKeeper)
txModule := transfer.NewIBCModule(*txKeeper, wmKeeper)
txModule := transfer.NewIBCModule(*txKeeper, wmKeeper, tokenfactoryKeeper)
ctx := infCtx.WithGasMeter(types2.NewGasMeter(1_000_000_000_000))

resACK := channeltypes.Acknowledgement{
Expand Down Expand Up @@ -114,10 +116,11 @@ func TestHandleTimeout(t *testing.T) {
feeKeeper := mock_types.NewMockFeeRefunderKeeper(ctrl)
chanKeeper := mock_types.NewMockChannelKeeper(ctrl)
authKeeper := mock_types.NewMockAccountKeeper(ctrl)
tokenfactoryKeeper := mock_types.NewMockTokenfactoryKeeper(ctrl)
// required to initialize keeper
authKeeper.EXPECT().GetModuleAddress(transfertypes.ModuleName).Return([]byte("address"))
txKeeper, infCtx, _ := testkeeper.TransferKeeper(t, wmKeeper, feeKeeper, chanKeeper, authKeeper)
txModule := transfer.NewIBCModule(*txKeeper, wmKeeper)
txModule := transfer.NewIBCModule(*txKeeper, wmKeeper, tokenfactoryKeeper)
ctx := infCtx.WithGasMeter(types2.NewGasMeter(1_000_000_000_000))
contractAddress := sdk.MustAccAddressFromBech32(testutil.TestOwnerAddress)
relayerBech32 := "neutron1fxudpred77a0grgh69u0j7y84yks5ev4n5050z45kecz792jnd6scqu98z"
Expand Down
1 change: 0 additions & 1 deletion x/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down
Loading
Loading