Skip to content

Commit

Permalink
move balances from axelarnet module account to nexus
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-sammy committed Sep 26, 2024
1 parent 104d584 commit f93dbcd
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 59 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ func initAppModules(keepers *KeeperCache, bApp *bam.BaseApp, encodingConfig axel
GetKeeper[stakingkeeper.Keeper](keepers),
GetKeeper[axelarnetKeeper.Keeper](keepers),
GetKeeper[rewardKeeper.Keeper](keepers),
GetKeeper[bankkeeper.BaseKeeper](keepers),
GetKeeper[authkeeper.AccountKeeper](keepers),
),
evm.NewAppModule(
GetKeeper[evmKeeper.BaseKeeper](keepers),
Expand Down
2 changes: 2 additions & 0 deletions x/axelarnet/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ type BankKeeper interface {
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
BlockedAddr(addr sdk.AccAddress) bool
SpendableBalance(ctx sdk.Context, address sdk.AccAddress, denom string) sdk.Coin
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
}

// IBCTransferKeeper provides functionality to manage IBC transfers
Expand Down
112 changes: 112 additions & 0 deletions x/axelarnet/types/mock/expected_keepers.go

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

11 changes: 6 additions & 5 deletions x/nexus/keeper/lockable_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/axelarnetwork/axelar-core/testutils/fake"
"github.com/axelarnetwork/axelar-core/testutils/rand"
axelarnet "github.com/axelarnetwork/axelar-core/x/axelarnet/exported"
Expand All @@ -13,11 +19,6 @@ import (
"github.com/axelarnetwork/axelar-core/x/nexus/types/mock"
"github.com/axelarnetwork/utils/funcs"
. "github.com/axelarnetwork/utils/test"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

func TestLockableCoin(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions x/nexus/keeper/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

axelarnettypes "github.com/axelarnetwork/axelar-core/x/axelarnet/types"
"github.com/axelarnetwork/axelar-core/x/nexus/types"
)

// Migrate6to7 returns the handler that performs in-place store migrations
func Migrate6to7(k Keeper) func(ctx sdk.Context) error {
// Migrate7to8 returns the handler that performs in-place store migrations
func Migrate7to8(_ Keeper, bank types.BankKeeper, account types.AccountKeeper) func(ctx sdk.Context) error {
return func(ctx sdk.Context) error {
addModuleParamGateway(ctx, k)
addModuleParamEndBlockerLimit(ctx, k)
if err := sendCoinsFromAxelarnetToNexus(ctx, bank, account); err != nil {
return err

Check warning on line 14 in x/nexus/keeper/migrate.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/keeper/migrate.go#L14

Added line #L14 was not covered by tests
}

return nil
}
}

func addModuleParamGateway(ctx sdk.Context, k Keeper) {
k.params.Set(ctx, types.KeyGateway, types.DefaultParams().Gateway)
}
func sendCoinsFromAxelarnetToNexus(ctx sdk.Context, bank types.BankKeeper, account types.AccountKeeper) error {
balances := bank.GetAllBalances(ctx, account.GetModuleAddress(axelarnettypes.ModuleName))

func addModuleParamEndBlockerLimit(ctx sdk.Context, k Keeper) {
k.params.Set(ctx, types.KeyEndBlockerLimit, types.DefaultParams().EndBlockerLimit)
return bank.SendCoinsFromModuleToModule(ctx, axelarnettypes.ModuleName, types.ModuleName, balances)
}
79 changes: 37 additions & 42 deletions x/nexus/keeper/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,50 @@ import (

"github.com/axelarnetwork/axelar-core/app"
"github.com/axelarnetwork/axelar-core/testutils/fake"
"github.com/axelarnetwork/axelar-core/testutils/rand"
axelarnettypes "github.com/axelarnetwork/axelar-core/x/axelarnet/types"
"github.com/axelarnetwork/axelar-core/x/nexus/keeper"
"github.com/axelarnetwork/axelar-core/x/nexus/types"
. "github.com/axelarnetwork/utils/test"
"github.com/axelarnetwork/axelar-core/x/nexus/types/mock"
)

func TestMigrate6to7(t *testing.T) {
func TestMigrate7to8(t *testing.T) {
encCfg := app.MakeEncodingConfig()
subspace := params.NewSubspace(encCfg.Codec, encCfg.Amino, sdk.NewKVStoreKey("nexusKey"), sdk.NewKVStoreKey("tNexusKey"), "nexus")
k := keeper.NewKeeper(encCfg.Codec, sdk.NewKVStoreKey("nexus"), subspace)
ctx := sdk.NewContext(fake.NewMultiStore(), tmproto.Header{}, false, log.TestingLogger())

Given("subspace is setup with params before migration", func() {
subspace.Set(ctx, types.KeyChainActivationThreshold, types.DefaultParams().ChainActivationThreshold)
subspace.Set(ctx, types.KeyChainMaintainerMissingVoteThreshold, types.DefaultParams().ChainMaintainerMissingVoteThreshold)
subspace.Set(ctx, types.KeyChainMaintainerIncorrectVoteThreshold, types.DefaultParams().ChainMaintainerIncorrectVoteThreshold)
subspace.Set(ctx, types.KeyChainMaintainerCheckWindow, types.DefaultParams().ChainMaintainerCheckWindow)
}).
When("", func() {}).
Then("the migration should add the new param with the default value", func(t *testing.T) {
actualGateway := sdk.AccAddress{}
actualEndBlockerLimit := uint64(0)

assert.PanicsWithError(t, "UnmarshalJSON cannot decode empty bytes", func() {
subspace.Get(ctx, types.KeyGateway, &actualGateway)
})
assert.PanicsWithError(t, "UnmarshalJSON cannot decode empty bytes", func() {
subspace.Get(ctx, types.KeyEndBlockerLimit, &actualEndBlockerLimit)
})
assert.PanicsWithError(t, "UnmarshalJSON cannot decode empty bytes", func() {
k.GetParams(ctx)
})

keeper.Migrate6to7(k)(ctx)

assert.NotPanics(t, func() {
subspace.Get(ctx, types.KeyGateway, &actualGateway)
})
assert.NotPanics(t, func() {
subspace.Get(ctx, types.KeyEndBlockerLimit, &actualEndBlockerLimit)
})
assert.NotPanics(t, func() {
k.GetParams(ctx)
})

assert.Equal(t, types.DefaultParams().Gateway, actualGateway)
assert.Equal(t, types.DefaultParams().Gateway, k.GetParams(ctx).Gateway)
assert.Equal(t, types.DefaultParams().EndBlockerLimit, actualEndBlockerLimit)
assert.Equal(t, types.DefaultParams().EndBlockerLimit, k.GetParams(ctx).EndBlockerLimit)
}).
Run(t)

t.Run("sendCoinsFromAxelarnetToNexus", func(t *testing.T) {
bank := &mock.BankKeeperMock{}
account := &mock.AccountKeeperMock{}
balances := sdk.NewCoins(rand.Coin(), rand.Coin(), rand.Coin())
axelarnetModuleAccount := rand.AccAddr()
nexusModuleAccount := rand.AccAddr()

account.GetModuleAddressFunc = func(name string) sdk.AccAddress {
switch name {
case axelarnettypes.ModuleName:
return axelarnetModuleAccount
case types.ModuleName:
return nexusModuleAccount
default:
return sdk.AccAddress{}
}
}
bank.GetAllBalancesFunc = func(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
if addr.Equals(axelarnetModuleAccount) {
return balances
}
return sdk.NewCoins()
}
bank.SendCoinsFromModuleToModuleFunc = func(ctx sdk.Context, sender, recipient string, coins sdk.Coins) error { return nil }

err := keeper.Migrate7to8(k, bank, account)(ctx)

assert.NoError(t, err)
assert.Len(t, bank.SendCoinsFromModuleToModuleCalls(), 1)
assert.Equal(t, axelarnettypes.ModuleName, bank.SendCoinsFromModuleToModuleCalls()[0].SenderModule)
assert.Equal(t, types.ModuleName, bank.SendCoinsFromModuleToModuleCalls()[0].RecipientModule)
assert.Equal(t, balances, bank.SendCoinsFromModuleToModuleCalls()[0].Amt)
})
}
10 changes: 7 additions & 3 deletions x/nexus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ type AppModule struct {
staking types.StakingKeeper
axelarnet types.AxelarnetKeeper
reward types.RewardKeeper
bank types.BankKeeper
account types.AccountKeeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(k keeper.Keeper, snapshotter types.Snapshotter, slashing types.SlashingKeeper, staking types.StakingKeeper, axelarnet types.AxelarnetKeeper, reward types.RewardKeeper) AppModule {
func NewAppModule(k keeper.Keeper, snapshotter types.Snapshotter, slashing types.SlashingKeeper, staking types.StakingKeeper, axelarnet types.AxelarnetKeeper, reward types.RewardKeeper, bank types.BankKeeper, account types.AccountKeeper) AppModule {

Check warning on line 97 in x/nexus/module.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/module.go#L97

Added line #L97 was not covered by tests
return AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: k,
Expand All @@ -101,6 +103,8 @@ func NewAppModule(k keeper.Keeper, snapshotter types.Snapshotter, slashing types
staking: staking,
axelarnet: axelarnet,
reward: reward,
bank: bank,
account: account,

Check warning on line 107 in x/nexus/module.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/module.go#L106-L107

Added lines #L106 - L107 were not covered by tests
}
}

Expand All @@ -116,7 +120,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServiceServer(grpc.ServerWithSDKErrors{Server: cfg.MsgServer(), Err: types.ErrNexus, Logger: am.keeper.Logger}, msgServer)
types.RegisterQueryServiceServer(cfg.QueryServer(), keeper.NewGRPCQuerier(am.keeper, am.axelarnet))

err := cfg.RegisterMigration(types.ModuleName, 6, keeper.Migrate6to7(am.keeper))
err := cfg.RegisterMigration(types.ModuleName, 7, keeper.Migrate7to8(am.keeper, am.bank, am.account))

Check warning on line 123 in x/nexus/module.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/module.go#L123

Added line #L123 was not covered by tests
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -167,4 +171,4 @@ func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 7 }
func (AppModule) ConsensusVersion() uint64 { return 8 }

Check warning on line 174 in x/nexus/module.go

View check run for this annotation

Codecov / codecov/patch

x/nexus/module.go#L174

Added line #L174 was not covered by tests
2 changes: 2 additions & 0 deletions x/nexus/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ type BankKeeper interface {
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
}
Loading

0 comments on commit f93dbcd

Please sign in to comment.