From f4ddc273ef0bc7551f0508705b7671d1162d22da Mon Sep 17 00:00:00 2001 From: beer-1 Date: Thu, 24 Oct 2024 15:03:13 +0900 Subject: [PATCH] initialize staking at genesis --- x/bank/keeper/common_test.go | 2 +- x/distribution/keeper/common_test.go | 2 +- x/gov/keeper/common_test.go | 2 +- x/ibc-hooks/keeper/common_test.go | 2 +- x/ibc-hooks/move-hooks/common_test.go | 2 +- x/move/keeper/common_test.go | 2 +- x/move/keeper/genesis.go | 18 +++++++++++- x/move/keeper/staking_test.go | 42 +++++++++++++++++++++++++++ x/mstaking/keeper/common_test.go | 2 +- x/reward/keeper/common_test.go | 2 +- x/slashing/keeper/common_test.go | 2 +- 11 files changed, 68 insertions(+), 10 deletions(-) diff --git a/x/bank/keeper/common_test.go b/x/bank/keeper/common_test.go index 3ba17571..d3cef32a 100644 --- a/x/bank/keeper/common_test.go +++ b/x/bank/keeper/common_test.go @@ -394,7 +394,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/distribution/keeper/common_test.go b/x/distribution/keeper/common_test.go index 253bcfa1..79a9bf11 100644 --- a/x/distribution/keeper/common_test.go +++ b/x/distribution/keeper/common_test.go @@ -378,7 +378,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/gov/keeper/common_test.go b/x/gov/keeper/common_test.go index 40b9425b..e5c3e1ed 100644 --- a/x/gov/keeper/common_test.go +++ b/x/gov/keeper/common_test.go @@ -406,7 +406,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/ibc-hooks/keeper/common_test.go b/x/ibc-hooks/keeper/common_test.go index 6f7959af..20197b28 100644 --- a/x/ibc-hooks/keeper/common_test.go +++ b/x/ibc-hooks/keeper/common_test.go @@ -396,7 +396,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/ibc-hooks/move-hooks/common_test.go b/x/ibc-hooks/move-hooks/common_test.go index a6e8a52e..31922153 100644 --- a/x/ibc-hooks/move-hooks/common_test.go +++ b/x/ibc-hooks/move-hooks/common_test.go @@ -309,7 +309,7 @@ func _createTestInput( if initialize { stdlib, err := vmprecom.ReadStdlib() require.NoError(t, err) - require.NoError(t, moveKeeper.Initialize(ctx, stdlib, moveParams.AllowedPublishers)) + require.NoError(t, moveKeeper.Initialize(ctx, stdlib, moveParams.AllowedPublishers, bondDenom)) } faucet := NewTestFaucet(t, ctx, bankKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/move/keeper/common_test.go b/x/move/keeper/common_test.go index eac95a0e..b0d587f6 100644 --- a/x/move/keeper/common_test.go +++ b/x/move/keeper/common_test.go @@ -419,7 +419,7 @@ func _createTestInput( // append test module moduleBytes = append(moduleBytes, basicCoinModule) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/move/keeper/genesis.go b/x/move/keeper/genesis.go index 4ae2cab7..ad856db4 100644 --- a/x/move/keeper/genesis.go +++ b/x/move/keeper/genesis.go @@ -15,6 +15,7 @@ func (k Keeper) Initialize( ctx context.Context, moduleBytes [][]byte, allowedPublishers []string, + baseDenom string, ) error { sdkCtx := sdk.UnwrapSDKContext(ctx) ctx = sdkCtx.WithTxBytes(make([]byte, 32)) @@ -53,6 +54,21 @@ func (k Keeper) Initialize( if err != nil { return err } + if err = k.handleExecuteResponse(sdkCtx, sdkCtx.GasMeter(), execRes); err != nil { + return err + } + + // if staking keeper is available, initialize move staking module. + if k.StakingKeeper != nil { + if err := k.moveBankKeeper.InitializeCoin(ctx, baseDenom); err != nil { + return err + } + + // initialize move staking module if staking keeper is available + if err := k.InitializeStaking(ctx, baseDenom); err != nil { + return err + } + } return k.handleExecuteResponse(sdkCtx, sdkCtx.GasMeter(), execRes) } @@ -74,7 +90,7 @@ func (k Keeper) InitGenesis(ctx context.Context, moduleNames []string, genState } if len(genState.GetModules()) == 0 { - if err := k.Initialize(ctx, genState.GetStdlibs(), params.AllowedPublishers); err != nil { + if err := k.Initialize(ctx, genState.GetStdlibs(), params.AllowedPublishers, params.BaseDenom); err != nil { return err } } diff --git a/x/move/keeper/staking_test.go b/x/move/keeper/staking_test.go index c72d179b..ba21d845 100644 --- a/x/move/keeper/staking_test.go +++ b/x/move/keeper/staking_test.go @@ -233,6 +233,48 @@ func TestInstantUnbondFromBondedValidator(t *testing.T) { require.True(t, val.IsUnbonding()) } +func TestApplyStakingDeltas_BaseDenom(t *testing.T) { + ctx, input := createDefaultTestInput(t) + + // add second BondDenom to staking keeper + input.StakingKeeper.SetBondDenoms(ctx, []string{bondDenom}) + + // initialize staking + valAddr := createValidatorWithBalance(t, ctx, input, 100_000_000, 100_000) + + // mint not possible for lp coin, so transfer from the 0x2 + _, _, addr := keyPubAddr() + input.Faucet.Mint(ctx, addr, sdk.NewCoin(bondDenom, math.NewInt(100_000_000))) + + // delegate coins via move staking module + valAddrArg, err := vmtypes.SerializeString(valAddr.String()) + require.NoError(t, err) + + amountArg, err := vmtypes.SerializeUint64(50_000_000) + require.NoError(t, err) + + metadataAddr, err := types.MetadataAddressFromDenom(bondDenom) + require.NoError(t, err) + + vmAddr, err := vmtypes.NewAccountAddressFromBytes(addr) + require.NoError(t, err) + err = input.MoveKeeper.ExecuteEntryFunction( + ctx, + vmAddr, + vmtypes.StdAddress, + types.MoveModuleNameStaking, + types.FunctionNameStakingDelegateScript, + []vmtypes.TypeTag{}, + [][]byte{metadataAddr[:], valAddrArg, amountArg}, + ) + require.NoError(t, err) + + delModuleAddr := types.GetDelegatorModuleAddress(valAddr) + delegation, err := input.StakingKeeper.GetDelegation(ctx, delModuleAddr, valAddr) + require.NoError(t, err) + require.Equal(t, delegation.Shares, sdk.NewDecCoins(sdk.NewDecCoin(bondDenom, math.NewInt(50_000_000)))) +} + func TestApplyStakingDeltas(t *testing.T) { ctx, input := createDefaultTestInput(t) diff --git a/x/mstaking/keeper/common_test.go b/x/mstaking/keeper/common_test.go index 04bb912c..23fb5858 100644 --- a/x/mstaking/keeper/common_test.go +++ b/x/mstaking/keeper/common_test.go @@ -420,7 +420,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/reward/keeper/common_test.go b/x/reward/keeper/common_test.go index c0fff4ad..16a055aa 100644 --- a/x/reward/keeper/common_test.go +++ b/x/reward/keeper/common_test.go @@ -375,7 +375,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...) diff --git a/x/slashing/keeper/common_test.go b/x/slashing/keeper/common_test.go index f3be7c9c..fae63447 100644 --- a/x/slashing/keeper/common_test.go +++ b/x/slashing/keeper/common_test.go @@ -389,7 +389,7 @@ func _createTestInput( moduleBytes, err := precompile.ReadStdlib() require.NoError(t, err) - err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers) + err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.AllowedPublishers, bondDenom) require.NoError(t, err) faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...)