chore!: remove params from slashing and auth (#17820)
This commit is contained in:
parent
5952ecf515
commit
79a64d07d0
@ -424,6 +424,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
* (signing) [#13701](https://github.com/cosmos/cosmos-sdk/pull/) Add `context.Context` as an argument `x/auth/signing.VerifySignature`.
|
||||
* (store) [#11825](https://github.com/cosmos/cosmos-sdk/pull/11825) Make extension snapshotter interface safer to use, renamed the util function `WriteExtensionItem` to `WriteExtensionPayload`.
|
||||
* (x/bank) [#17818](https://github.com/cosmos/cosmos-sdk/pull/17818) Remove params requirement from `NewAppModule`
|
||||
* (x/slashing & x/auth) [#17820](https://github.com/cosmos/cosmos-sdk/pull/17820) Remove params requirement from `NewAppModule`
|
||||
|
||||
### Client Breaking Changes
|
||||
|
||||
|
||||
@ -383,14 +383,14 @@ func NewSimApp(
|
||||
app.AccountKeeper, app.StakingKeeper, app,
|
||||
txConfig,
|
||||
),
|
||||
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
|
||||
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
|
||||
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
|
||||
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
|
||||
crisis.NewAppModule(app.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
|
||||
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
|
||||
gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
|
||||
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
|
||||
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
|
||||
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.interfaceRegistry),
|
||||
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
|
||||
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
|
||||
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
|
||||
@ -489,7 +489,7 @@ func NewSimApp(
|
||||
// NOTE: this is not required apps that don't use the simulator for fuzz testing
|
||||
// transactions
|
||||
overrideModules := map[string]module.AppModuleSimulation{
|
||||
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
|
||||
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
|
||||
}
|
||||
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)
|
||||
|
||||
|
||||
@ -244,7 +244,7 @@ func NewSimApp(
|
||||
// NOTE: this is not required apps that don't use the simulator for fuzz testing
|
||||
// transactions
|
||||
overrideModules := map[string]module.AppModuleSimulation{
|
||||
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
|
||||
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
|
||||
}
|
||||
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)
|
||||
|
||||
|
||||
@ -1,794 +0,0 @@
|
||||
package v2_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/header"
|
||||
"cosmossdk.io/depinject"
|
||||
"cosmossdk.io/log"
|
||||
sdkmath "cosmossdk.io/math"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4"
|
||||
authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
type mockSubspace struct {
|
||||
ps authtypes.Params
|
||||
}
|
||||
|
||||
func newMockSubspace(ps authtypes.Params) mockSubspace {
|
||||
return mockSubspace{ps: ps}
|
||||
}
|
||||
|
||||
func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps authexported.ParamSet) {
|
||||
*ps.(*authtypes.Params) = ms.ps
|
||||
}
|
||||
|
||||
func TestMigrateVestingAccounts(t *testing.T) {
|
||||
encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
|
||||
cdc := encCfg.Codec
|
||||
|
||||
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
|
||||
tKey := storetypes.NewTransientStoreKey("transient_test")
|
||||
ctx := testutil.DefaultContext(storeKey, tKey)
|
||||
storeService := runtime.NewKVStoreService(storeKey)
|
||||
|
||||
var (
|
||||
accountKeeper keeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
app, err := simtestutil.Setup(
|
||||
depinject.Configs(
|
||||
authtestutil.AppConfig,
|
||||
depinject.Supply(log.NewNopLogger()),
|
||||
),
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
legacySubspace := newMockSubspace(authtypes.DefaultParams())
|
||||
require.NoError(t, v4.Migrate(ctx, storeService, legacySubspace, cdc))
|
||||
|
||||
ctx = app.BaseApp.NewContext(false)
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Now()})
|
||||
err = stakingKeeper.Params.Set(ctx, stakingtypes.DefaultParams())
|
||||
require.NoError(t, err)
|
||||
lastAccNum := uint64(1000)
|
||||
createBaseAccount := func(addr sdk.AccAddress) *authtypes.BaseAccount {
|
||||
baseAccount := authtypes.NewBaseAccountWithAddress(addr)
|
||||
require.NoError(t, baseAccount.SetAccountNumber(atomic.AddUint64(&lastAccNum, 1)))
|
||||
return baseAccount
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
prepareFunc func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress)
|
||||
garbageFunc func(ctx sdk.Context, vesting exported.VestingAccount, accounKeeper keeper.AccountKeeper) error
|
||||
tokenAmount int64
|
||||
expVested int64
|
||||
expFree int64
|
||||
blockTime int64
|
||||
}{
|
||||
{
|
||||
"delayed vesting has vested, multiple delegations less than the total account balance",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(200)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
err = accountKeeper.Params.Set(ctx, authtypes.DefaultParams())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
0,
|
||||
300,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has vested, single delegations which exceed the vested amount",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(200)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
0,
|
||||
300,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has vested, multiple delegations which exceed the vested amount",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(200)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
0,
|
||||
300,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has not vested, single delegations which exceed the vested amount",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(200)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(1, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
200,
|
||||
100,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has not vested, multiple delegations which exceed the vested amount",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(200)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(1, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
200,
|
||||
100,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"not end time",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(1, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(100), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
300,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has not vested, single delegation greater than the total account balance",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(1, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
300,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"delayed vesting has vested, single delegation greater than the total account balance",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
0,
|
||||
300,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"continuous vesting, start time after blocktime",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
startTime := ctx.HeaderInfo().Time.AddDate(1, 0, 0).Unix()
|
||||
endTime := ctx.HeaderInfo().Time.AddDate(2, 0, 0).Unix()
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
300,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"continuous vesting, start time passed but not ended",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
startTime := ctx.HeaderInfo().Time.AddDate(-1, 0, 0).Unix()
|
||||
endTime := ctx.HeaderInfo().Time.AddDate(2, 0, 0).Unix()
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
200,
|
||||
100,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"continuous vesting, start time and endtime passed",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
startTime := ctx.HeaderInfo().Time.AddDate(-2, 0, 0).Unix()
|
||||
endTime := ctx.HeaderInfo().Time.AddDate(-1, 0, 0).Unix()
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
delayedAccount, err := types.NewContinuousVestingAccount(baseAccount, vestedCoins, startTime, endTime)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
0,
|
||||
300,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"periodic vesting account, yet to be vested, some rewards delegated",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(100)))
|
||||
|
||||
start := ctx.HeaderInfo().Time.Unix() + int64(time.Hour/time.Second)
|
||||
|
||||
periods := []types.Period{
|
||||
{
|
||||
Length: int64((24 * time.Hour) / time.Second),
|
||||
Amount: vestedCoins,
|
||||
},
|
||||
}
|
||||
|
||||
account, err := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, start, periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, account)
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(150), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
300,
|
||||
100,
|
||||
50,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"periodic vesting account, nothing has vested yet",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
/*
|
||||
Test case:
|
||||
- periodic vesting account starts at time 1601042400
|
||||
- account balance and original vesting: 3666666670000
|
||||
- nothing has vested, we put the block time slightly after start time
|
||||
- expected vested: original vesting amount
|
||||
- expected free: zero
|
||||
- we're delegating the full original vesting
|
||||
*/
|
||||
startTime := int64(1601042400)
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(3666666670000)))
|
||||
periods := []types.Period{
|
||||
{
|
||||
Length: 31536000,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(1833333335000))),
|
||||
},
|
||||
{
|
||||
Length: 15638400,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
{
|
||||
Length: 15897600,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
}
|
||||
|
||||
delayedAccount, err := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
// delegation of the original vesting
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(3666666670000), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
3666666670000,
|
||||
3666666670000,
|
||||
0,
|
||||
1601042400 + 1,
|
||||
},
|
||||
{
|
||||
"periodic vesting account, all has vested",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
/*
|
||||
Test case:
|
||||
- periodic vesting account starts at time 1601042400
|
||||
- account balance and original vesting: 3666666670000
|
||||
- all has vested, so we set the block time at initial time + sum of all periods times + 1 => 1601042400 + 31536000 + 15897600 + 15897600 + 1
|
||||
- expected vested: zero
|
||||
- expected free: original vesting amount
|
||||
- we're delegating the full original vesting
|
||||
*/
|
||||
startTime := int64(1601042400)
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(3666666670000)))
|
||||
periods := []types.Period{
|
||||
{
|
||||
Length: 31536000,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(1833333335000))),
|
||||
},
|
||||
{
|
||||
Length: 15638400,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
{
|
||||
Length: 15897600,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
}
|
||||
|
||||
delayedAccount, err := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Unix(1601042400+31536000+15897600+15897600+1, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
// delegation of the original vesting
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(3666666670000), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
3666666670000,
|
||||
0,
|
||||
3666666670000,
|
||||
1601042400 + 31536000 + 15897600 + 15897600 + 1,
|
||||
},
|
||||
{
|
||||
"periodic vesting account, first period has vested",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
/*
|
||||
Test case:
|
||||
- periodic vesting account starts at time 1601042400
|
||||
- account balance and original vesting: 3666666670000
|
||||
- first period have vested, so we set the block time at initial time + time of the first periods + 1 => 1601042400 + 31536000 + 1
|
||||
- expected vested: original vesting - first period amount
|
||||
- expected free: first period amount
|
||||
- we're delegating the full original vesting
|
||||
*/
|
||||
startTime := int64(1601042400)
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(3666666670000)))
|
||||
periods := []types.Period{
|
||||
{
|
||||
Length: 31536000,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(1833333335000))),
|
||||
},
|
||||
{
|
||||
Length: 15638400,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
{
|
||||
Length: 15897600,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
}
|
||||
|
||||
delayedAccount, err := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Unix(1601042400+31536000+1, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
// delegation of the original vesting
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(3666666670000), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
3666666670000,
|
||||
3666666670000 - 1833333335000,
|
||||
1833333335000,
|
||||
1601042400 + 31536000 + 1,
|
||||
},
|
||||
{
|
||||
"periodic vesting account, first 2 period has vested",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
/*
|
||||
Test case:
|
||||
- periodic vesting account starts at time 1601042400
|
||||
- account balance and original vesting: 3666666670000
|
||||
- first 2 periods have vested, so we set the block time at initial time + time of the two periods + 1 => 1601042400 + 31536000 + 15638400 + 1
|
||||
- expected vested: original vesting - (sum of the first two periods amounts)
|
||||
- expected free: sum of the first two periods
|
||||
- we're delegating the full original vesting
|
||||
*/
|
||||
startTime := int64(1601042400)
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(3666666670000)))
|
||||
periods := []types.Period{
|
||||
{
|
||||
Length: 31536000,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(1833333335000))),
|
||||
},
|
||||
{
|
||||
Length: 15638400,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
{
|
||||
Length: 15897600,
|
||||
Amount: sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(916666667500))),
|
||||
},
|
||||
}
|
||||
|
||||
delayedAccount, err := types.NewPeriodicVestingAccount(baseAccount, vestedCoins, startTime, periods)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Unix(1601042400+31536000+15638400+1, 0)})
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
// delegation of the original vesting
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(3666666670000), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
3666666670000,
|
||||
3666666670000 - 1833333335000 - 916666667500,
|
||||
1833333335000 + 916666667500,
|
||||
1601042400 + 31536000 + 15638400 + 1,
|
||||
},
|
||||
{
|
||||
"vesting account has unbonding delegations in place",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(10, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
|
||||
// delegation of the original vesting
|
||||
_, err = stakingKeeper.Delegate(ctx, delegatorAddr, sdkmath.NewInt(300), stakingtypes.Unbonded, validator, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: ctx.HeaderInfo().Time.AddDate(1, 0, 0)})
|
||||
|
||||
valAddr, err := sdk.ValAddressFromBech32(validator.OperatorAddress)
|
||||
require.NoError(t, err)
|
||||
|
||||
// un-delegation of the original vesting
|
||||
_, _, err = stakingKeeper.Undelegate(ctx, delegatorAddr, valAddr, sdkmath.LegacyNewDecFromInt(sdkmath.NewInt(300)))
|
||||
require.NoError(t, err)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
450,
|
||||
300,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"vesting account has never delegated anything",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(10, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
},
|
||||
cleartTrackingFields,
|
||||
450,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
{
|
||||
"vesting account has no delegation but dirty DelegatedFree and DelegatedVesting fields",
|
||||
func(ctx sdk.Context, validator stakingtypes.Validator, delegatorAddr sdk.AccAddress) {
|
||||
baseAccount := createBaseAccount(delegatorAddr)
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
vestedCoins := sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(300)))
|
||||
|
||||
delayedAccount, err := types.NewDelayedVestingAccount(baseAccount, vestedCoins, ctx.HeaderInfo().Time.AddDate(10, 0, 0).Unix())
|
||||
require.NoError(t, err)
|
||||
|
||||
accountKeeper.SetAccount(ctx, delayedAccount)
|
||||
},
|
||||
dirtyTrackingFields,
|
||||
450,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := accountKeeper.Params.Set(ctx, authtypes.DefaultParams())
|
||||
require.NoError(t, err)
|
||||
|
||||
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, sdkmath.NewInt(tc.tokenAmount))
|
||||
delegatorAddr := addrs[0]
|
||||
|
||||
_, valAddr := createValidator(t, ctx, bankKeeper, stakingKeeper, tc.tokenAmount*2)
|
||||
validator, err := stakingKeeper.GetValidator(ctx, valAddr)
|
||||
require.NoError(t, err)
|
||||
|
||||
tc.prepareFunc(ctx, validator, delegatorAddr)
|
||||
|
||||
if tc.blockTime != 0 {
|
||||
ctx = ctx.WithHeaderInfo(header.Info{Time: time.Unix(tc.blockTime, 0)})
|
||||
}
|
||||
|
||||
// We introduce the bug
|
||||
savedAccount := accountKeeper.GetAccount(ctx, delegatorAddr)
|
||||
vestingAccount, ok := savedAccount.(exported.VestingAccount)
|
||||
require.True(t, ok)
|
||||
require.NoError(t, tc.garbageFunc(ctx, vestingAccount, accountKeeper))
|
||||
|
||||
m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter(), legacySubspace)
|
||||
require.NoError(t, m.Migrate1to2(ctx))
|
||||
|
||||
var expVested sdk.Coins
|
||||
var expFree sdk.Coins
|
||||
|
||||
bondDenom, err := stakingKeeper.BondDenom(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
if tc.expVested != 0 {
|
||||
expVested = sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(tc.expVested)))
|
||||
}
|
||||
|
||||
if tc.expFree != 0 {
|
||||
expFree = sdk.NewCoins(sdk.NewCoin(bondDenom, sdkmath.NewInt(tc.expFree)))
|
||||
}
|
||||
|
||||
trackingCorrected(
|
||||
ctx,
|
||||
t,
|
||||
accountKeeper,
|
||||
savedAccount.GetAddress(),
|
||||
expVested,
|
||||
expFree,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func trackingCorrected(ctx sdk.Context, t *testing.T, ak keeper.AccountKeeper, addr sdk.AccAddress, expDelVesting, expDelFree sdk.Coins) { //nolint:thelper // false positive
|
||||
t.Helper()
|
||||
baseAccount := ak.GetAccount(ctx, addr)
|
||||
vDA, ok := baseAccount.(exported.VestingAccount)
|
||||
require.True(t, ok)
|
||||
|
||||
vestedOk := expDelVesting.Equal(vDA.GetDelegatedVesting())
|
||||
freeOk := expDelFree.Equal(vDA.GetDelegatedFree())
|
||||
require.True(t, vestedOk, vDA.GetDelegatedVesting().String())
|
||||
require.True(t, freeOk, vDA.GetDelegatedFree().String())
|
||||
}
|
||||
|
||||
func cleartTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, accountKeeper keeper.AccountKeeper) error {
|
||||
switch t := vesting.(type) {
|
||||
case *types.DelayedVestingAccount:
|
||||
t.DelegatedFree = nil
|
||||
t.DelegatedVesting = nil
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
case *types.ContinuousVestingAccount:
|
||||
t.DelegatedFree = nil
|
||||
t.DelegatedVesting = nil
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
case *types.PeriodicVestingAccount:
|
||||
t.DelegatedFree = nil
|
||||
t.DelegatedVesting = nil
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
default:
|
||||
return fmt.Errorf("expected vesting account, found %t", t)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func dirtyTrackingFields(ctx sdk.Context, vesting exported.VestingAccount, accountKeeper keeper.AccountKeeper) error {
|
||||
dirt := sdk.NewCoins(sdk.NewInt64Coin("stake", 42))
|
||||
|
||||
switch t := vesting.(type) {
|
||||
case *types.DelayedVestingAccount:
|
||||
t.DelegatedFree = dirt
|
||||
t.DelegatedVesting = dirt
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
case *types.ContinuousVestingAccount:
|
||||
t.DelegatedFree = dirt
|
||||
t.DelegatedVesting = dirt
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
case *types.PeriodicVestingAccount:
|
||||
t.DelegatedFree = dirt
|
||||
t.DelegatedVesting = dirt
|
||||
accountKeeper.SetAccount(ctx, t)
|
||||
default:
|
||||
return fmt.Errorf("expected vesting account, found %t", t)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createValidator(t *testing.T, ctx sdk.Context, bankKeeper bankkeeper.Keeper, stakingKeeper *stakingkeeper.Keeper, powers int64) (sdk.AccAddress, sdk.ValAddress) {
|
||||
t.Helper()
|
||||
valTokens := sdk.TokensFromConsensusPower(powers, sdk.DefaultPowerReduction)
|
||||
addrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 1, valTokens)
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
|
||||
pks := simtestutil.CreateTestPubKeys(1)
|
||||
|
||||
val1, err := stakingtypes.NewValidator(valAddrs[0].String(), pks[0], stakingtypes.Description{})
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, stakingKeeper.SetValidator(ctx, val1))
|
||||
require.NoError(t, stakingKeeper.SetValidatorByConsAddr(ctx, val1))
|
||||
require.NoError(t, stakingKeeper.SetNewValidatorByPowerIndex(ctx, val1))
|
||||
|
||||
_, err = stakingKeeper.Delegate(ctx, addrs[0], valTokens, stakingtypes.Unbonded, val1, true)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = stakingKeeper.EndBlocker(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
return addrs[0], valAddrs[0]
|
||||
}
|
||||
@ -99,7 +99,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
|
||||
log.NewNopLogger(),
|
||||
)
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
|
||||
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{
|
||||
|
||||
@ -106,7 +106,7 @@ func initFixture(tb testing.TB) *fixture {
|
||||
cdc, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), accountKeeper, bankKeeper, stakingKeeper, distrtypes.ModuleName, authority.String(),
|
||||
)
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
distrModule := distribution.NewAppModule(cdc, distrKeeper, accountKeeper, bankKeeper, stakingKeeper, nil)
|
||||
|
||||
@ -131,10 +131,10 @@ func initFixture(tb testing.TB) *fixture {
|
||||
router = router.AddRoute(evidencetypes.RouteEquivocation, testEquivocationHandler(evidenceKeeper))
|
||||
evidenceKeeper.SetRouter(router)
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, nil, cdc.InterfaceRegistry())
|
||||
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry())
|
||||
evidenceModule := evidence.NewAppModule(*evidenceKeeper)
|
||||
|
||||
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{
|
||||
|
||||
@ -123,7 +123,7 @@ func initFixture(tb testing.TB) *fixture {
|
||||
err = govKeeper.Params.Set(newCtx, v1.DefaultParams())
|
||||
assert.NilError(tb, err)
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
distrModule := distribution.NewAppModule(cdc, distrKeeper, accountKeeper, bankKeeper, stakingKeeper, nil)
|
||||
|
||||
@ -97,7 +97,7 @@ func initFixture(tb testing.TB) *fixture {
|
||||
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, nil, cdc.InterfaceRegistry())
|
||||
slashingModule := slashing.NewAppModule(cdc, slashingKeeper, accountKeeper, bankKeeper, stakingKeeper, cdc.InterfaceRegistry())
|
||||
|
||||
integrationApp := integration.NewIntegrationApp(newCtx, logger, keys, cdc, map[string]appmodule.AppModule{
|
||||
banktypes.ModuleName: bankModule,
|
||||
|
||||
@ -137,7 +137,7 @@ func initFixture(tb testing.TB) *fixture {
|
||||
|
||||
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[types.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
|
||||
|
||||
@ -108,7 +108,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture {
|
||||
|
||||
stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr))
|
||||
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(cdc, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper)
|
||||
stakingModule := staking.NewAppModule(cdc, stakingKeeper, accountKeeper, bankKeeper, nil)
|
||||
|
||||
|
||||
@ -50,7 +50,7 @@ func Example() {
|
||||
)
|
||||
|
||||
// subspace is nil because we don't test params (which is legacy anyway)
|
||||
authModule := auth.NewAppModule(encodingCfg.Codec, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(encodingCfg.Codec, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
|
||||
// here bankkeeper and staking keeper is nil because we are not testing them
|
||||
// subspace is nil because we don't test params (which is legacy anyway)
|
||||
@ -139,7 +139,7 @@ func Example_oneModule() {
|
||||
)
|
||||
|
||||
// subspace is nil because we don't test params (which is legacy anyway)
|
||||
authModule := auth.NewAppModule(encodingCfg.Codec, accountKeeper, authsims.RandomGenesisAccounts, nil)
|
||||
authModule := auth.NewAppModule(encodingCfg.Codec, accountKeeper, authsims.RandomGenesisAccounts)
|
||||
|
||||
// create the application and register all the modules from the previous step
|
||||
integrationApp := integration.NewIntegrationApp(
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
package exported
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ParamSet = paramtypes.ParamSet
|
||||
|
||||
// Subspace defines an interface that implements the legacy x/params Subspace
|
||||
// type.
|
||||
//
|
||||
// NOTE: This is used solely for migration of x/params managed parameters.
|
||||
Subspace interface {
|
||||
GetParamSet(ctx sdk.Context, ps ParamSet)
|
||||
}
|
||||
)
|
||||
@ -4,52 +4,30 @@ import (
|
||||
"github.com/cosmos/gogoproto/grpc"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
v2 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v2"
|
||||
v3 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v3"
|
||||
v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4"
|
||||
v5 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v5"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
// Migrator is a struct for handling in-place store migrations.
|
||||
type Migrator struct {
|
||||
keeper AccountKeeper
|
||||
queryServer grpc.Server
|
||||
legacySubspace exported.Subspace
|
||||
keeper AccountKeeper
|
||||
queryServer grpc.Server
|
||||
}
|
||||
|
||||
// NewMigrator returns a new Migrator.
|
||||
func NewMigrator(keeper AccountKeeper, queryServer grpc.Server, ss exported.Subspace) Migrator {
|
||||
return Migrator{keeper: keeper, queryServer: queryServer, legacySubspace: ss}
|
||||
func NewMigrator(keeper AccountKeeper, queryServer grpc.Server) Migrator {
|
||||
return Migrator{keeper: keeper, queryServer: queryServer}
|
||||
}
|
||||
|
||||
// Migrate1to2 migrates from version 1 to 2.
|
||||
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
||||
var iterErr error
|
||||
|
||||
m.keeper.IterateAccounts(ctx, func(account sdk.AccountI) (stop bool) {
|
||||
wb, err := v2.MigrateAccount(ctx, account, m.queryServer)
|
||||
if err != nil {
|
||||
iterErr = err
|
||||
return true
|
||||
}
|
||||
|
||||
if wb == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
m.keeper.SetAccount(ctx, wb)
|
||||
return false
|
||||
})
|
||||
|
||||
return iterErr
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate2to3 migrates from consensus version 2 to version 3. Specifically, for each account
|
||||
// we index the account's ID to their address.
|
||||
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
|
||||
return v3.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate3to4 migrates the x/auth module state from the consensus version 3 to
|
||||
@ -57,7 +35,7 @@ func (m Migrator) Migrate2to3(ctx sdk.Context) error {
|
||||
// and managed by the x/params modules and stores them directly into the x/auth
|
||||
// module state.
|
||||
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
|
||||
return v4.Migrate(ctx, m.keeper.storeService, m.legacySubspace, m.keeper.cdc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate4To5 migrates the x/auth module state from the consensus version 4 to 5.
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
package v1
|
||||
|
||||
const (
|
||||
ModuleName = "auth"
|
||||
AddrLen = 20
|
||||
)
|
||||
@ -1,314 +0,0 @@
|
||||
// Package v2 creates in-place store migrations for fixing tracking
|
||||
// delegations with vesting accounts.
|
||||
// ref: https://github.com/cosmos/cosmos-sdk/issues/8601
|
||||
// ref: https://github.com/cosmos/cosmos-sdk/issues/8812
|
||||
//
|
||||
// The migration script modifies x/auth state, hence lives in the `x/auth/legacy`
|
||||
// folder. However, it needs access to staking and bank state. To avoid
|
||||
// cyclic dependencies, we cannot import those 2 keepers in this file. To solve
|
||||
// this, we use the baseapp router to do inter-module querying, by importing
|
||||
// the `baseapp.QueryRouter grpc.Server`. This is really hacky.
|
||||
//
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
//
|
||||
// Proposals to refactor this file have been made in:
|
||||
// https://github.com/cosmos/cosmos-sdk/issues/9070
|
||||
// The preferred solution is to use inter-module communication (ADR-033), and
|
||||
// this file will be refactored to use ADR-033 once it's ready.
|
||||
package v2
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
"github.com/cosmos/gogoproto/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
|
||||
stakingv1beta1 "cosmossdk.io/api/cosmos/staking/v1beta1"
|
||||
"cosmossdk.io/math"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
|
||||
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
||||
)
|
||||
|
||||
const (
|
||||
delegatorDelegationPath = "/cosmos.staking.v1beta1.Query/DelegatorDelegations"
|
||||
stakingParamsPath = "/cosmos.staking.v1beta1.Query/Params"
|
||||
delegatorUnbondingDelegationsPath = "/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations"
|
||||
balancesPath = "/cosmos.bank.v1beta1.Query/AllBalances"
|
||||
)
|
||||
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func migrateVestingAccounts(ctx sdk.Context, account sdk.AccountI, queryServer grpc.Server) (sdk.AccountI, error) {
|
||||
bondDenom, err := getBondDenom(ctx, queryServer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
asVesting, ok := account.(exported.VestingAccount)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
addr := account.GetAddress().String()
|
||||
balance, err := getBalance(
|
||||
ctx,
|
||||
addr,
|
||||
queryServer,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
delegations, err := getDelegatorDelegationsSum(
|
||||
ctx,
|
||||
addr,
|
||||
queryServer,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unbondingDelegations, err := getDelegatorUnbondingDelegationsSum(
|
||||
ctx,
|
||||
addr,
|
||||
bondDenom,
|
||||
queryServer,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
delegations = delegations.Add(unbondingDelegations...)
|
||||
|
||||
asVesting, ok = resetVestingDelegatedBalances(asVesting)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// balance before any delegation includes balance of delegation
|
||||
for _, coin := range delegations {
|
||||
balance = balance.Add(coin)
|
||||
}
|
||||
|
||||
asVesting.TrackDelegation(ctx.HeaderInfo().Time, balance, delegations)
|
||||
|
||||
return asVesting.(sdk.AccountI), nil
|
||||
}
|
||||
|
||||
func resetVestingDelegatedBalances(evacct exported.VestingAccount) (exported.VestingAccount, bool) {
|
||||
// reset `DelegatedVesting` and `DelegatedFree` to zero
|
||||
df := sdk.NewCoins()
|
||||
dv := sdk.NewCoins()
|
||||
|
||||
switch vacct := evacct.(type) {
|
||||
case *vestingtypes.ContinuousVestingAccount:
|
||||
vacct.DelegatedVesting = dv
|
||||
vacct.DelegatedFree = df
|
||||
return vacct, true
|
||||
case *vestingtypes.DelayedVestingAccount:
|
||||
vacct.DelegatedVesting = dv
|
||||
vacct.DelegatedFree = df
|
||||
return vacct, true
|
||||
case *vestingtypes.PeriodicVestingAccount:
|
||||
vacct.DelegatedVesting = dv
|
||||
vacct.DelegatedFree = df
|
||||
return vacct, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func getDelegatorDelegationsSum(ctx sdk.Context, address string, queryServer grpc.Server) (sdk.Coins, error) {
|
||||
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
|
||||
}
|
||||
|
||||
queryFn := querier.Route(delegatorDelegationPath)
|
||||
|
||||
q := &stakingv1beta1.QueryDelegatorDelegationsRequest{
|
||||
DelegatorAddr: address,
|
||||
}
|
||||
|
||||
b, err := proto.Marshal(q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot marshal staking type query request, %w", err)
|
||||
}
|
||||
req := abci.RequestQuery{
|
||||
Data: b,
|
||||
Path: delegatorDelegationPath,
|
||||
}
|
||||
resp, err := queryFn(ctx, &req)
|
||||
if err != nil {
|
||||
e, ok := status.FromError(err)
|
||||
if ok && e.Code() == codes.NotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("staking query error, %w", err)
|
||||
}
|
||||
|
||||
balance := new(stakingv1beta1.QueryDelegatorDelegationsResponse)
|
||||
if err := proto.Unmarshal(resp.Value, balance); err != nil {
|
||||
return nil, fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
|
||||
}
|
||||
|
||||
res := sdk.NewCoins()
|
||||
for _, i := range balance.DelegationResponses {
|
||||
bal, err := strconv.Atoi(i.Balance.Amount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot convert balance amount to int, %w", err)
|
||||
}
|
||||
coin := sdk.NewCoin(i.Balance.Denom, math.NewInt(int64(bal)))
|
||||
res = res.Add(coin)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func getDelegatorUnbondingDelegationsSum(ctx sdk.Context, address, bondDenom string, queryServer grpc.Server) (sdk.Coins, error) {
|
||||
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
|
||||
}
|
||||
|
||||
queryFn := querier.Route(delegatorUnbondingDelegationsPath)
|
||||
|
||||
q := &stakingv1beta1.QueryDelegatorUnbondingDelegationsRequest{
|
||||
DelegatorAddr: address,
|
||||
}
|
||||
|
||||
b, err := proto.Marshal(q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot marshal staking type query request, %w", err)
|
||||
}
|
||||
req := abci.RequestQuery{
|
||||
Data: b,
|
||||
Path: delegatorUnbondingDelegationsPath,
|
||||
}
|
||||
resp, err := queryFn(ctx, &req)
|
||||
if err != nil && !errors.Is(err, sdkerrors.ErrNotFound) {
|
||||
e, ok := status.FromError(err)
|
||||
if ok && e.Code() == codes.NotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("staking query error, %w", err)
|
||||
}
|
||||
|
||||
balance := new(stakingv1beta1.QueryDelegatorUnbondingDelegationsResponse)
|
||||
if err := proto.Unmarshal(resp.Value, balance); err != nil {
|
||||
return nil, fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
|
||||
}
|
||||
|
||||
res := sdk.NewCoins()
|
||||
for _, i := range balance.UnbondingResponses {
|
||||
for _, r := range i.Entries {
|
||||
bal, err := strconv.Atoi(r.Balance)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to convert unbonding balance to int: %w", err)
|
||||
}
|
||||
res = res.Add(sdk.NewCoin(bondDenom, math.NewInt(int64(bal))))
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func getBalance(ctx sdk.Context, address string, queryServer grpc.Server) (sdk.Coins, error) {
|
||||
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
|
||||
}
|
||||
|
||||
queryFn := querier.Route(balancesPath)
|
||||
|
||||
q := &bankv1beta1.QueryAllBalancesRequest{
|
||||
Address: address,
|
||||
Pagination: nil,
|
||||
}
|
||||
b, err := proto.Marshal(q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot marshal bank type query request, %w", err)
|
||||
}
|
||||
|
||||
req := abci.RequestQuery{
|
||||
Data: b,
|
||||
Path: balancesPath,
|
||||
}
|
||||
resp, err := queryFn(ctx, &req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bank query error, %w", err)
|
||||
}
|
||||
balance := new(bankv1beta1.QueryAllBalancesResponse)
|
||||
if err := proto.Unmarshal(resp.Value, balance); err != nil {
|
||||
return nil, fmt.Errorf("unable to unmarshal bank balance response: %w", err)
|
||||
}
|
||||
coins := make(sdk.Coins, len(balance.Balances))
|
||||
for i, b := range balance.Balances {
|
||||
amount, err := strconv.Atoi(b.Amount)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot convert balance amount to int, %w", err)
|
||||
}
|
||||
coins[i] = sdk.NewCoin(b.Denom, math.NewInt(int64(amount)))
|
||||
}
|
||||
return coins, nil
|
||||
}
|
||||
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func getBondDenom(ctx sdk.Context, queryServer grpc.Server) (string, error) {
|
||||
querier, ok := queryServer.(*baseapp.GRPCQueryRouter)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unexpected type: %T wanted *baseapp.GRPCQueryRouter", queryServer)
|
||||
}
|
||||
|
||||
queryFn := querier.Route(stakingParamsPath)
|
||||
|
||||
q := &stakingv1beta1.QueryParamsRequest{}
|
||||
|
||||
b, err := proto.Marshal(q)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot marshal staking params query request, %w", err)
|
||||
}
|
||||
req := abci.RequestQuery{
|
||||
Data: b,
|
||||
Path: stakingParamsPath,
|
||||
}
|
||||
|
||||
resp, err := queryFn(ctx, &req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("staking query error, %w", err)
|
||||
}
|
||||
|
||||
params := new(stakingv1beta1.QueryParamsResponse)
|
||||
if err := proto.Unmarshal(resp.Value, params); err != nil {
|
||||
return "", fmt.Errorf("unable to unmarshal delegator query delegations: %w", err)
|
||||
}
|
||||
|
||||
return params.Params.BondDenom, nil
|
||||
}
|
||||
|
||||
// MigrateAccount migrates vesting account to make the DelegatedVesting and DelegatedFree fields correctly
|
||||
// track delegations.
|
||||
// References: https://github.com/cosmos/cosmos-sdk/issues/8601, https://github.com/cosmos/cosmos-sdk/issues/8812
|
||||
//
|
||||
// We use the baseapp.QueryRouter here to do inter-module state querying.
|
||||
// PLEASE DO NOT REPLICATE THIS PATTERN IN YOUR OWN APP.
|
||||
func MigrateAccount(ctx sdk.Context, account sdk.AccountI, queryServer grpc.Server) (sdk.AccountI, error) {
|
||||
return migrateVestingAccounts(ctx, account, queryServer)
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package v3
|
||||
|
||||
import (
|
||||
corestore "cosmossdk.io/core/store"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
func mapAccountAddressToAccountID(ctx sdk.Context, storeService corestore.KVStoreService, cdc codec.BinaryCodec) error {
|
||||
store := storeService.OpenKVStore(ctx)
|
||||
iterator, err := store.Iterator(types.AddressStoreKeyPrefix, storetypes.PrefixEndBytes(types.AddressStoreKeyPrefix))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer iterator.Close()
|
||||
for ; iterator.Valid(); iterator.Next() {
|
||||
var acc sdk.AccountI
|
||||
if err := cdc.UnmarshalInterface(iterator.Value(), &acc); err != nil {
|
||||
return err
|
||||
}
|
||||
err = store.Set(accountNumberStoreKey(acc.GetAccountNumber()), acc.GetAddress().Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MigrateStore performs in-place store migrations from v0.45 to v0.46. The
|
||||
// migration includes:
|
||||
// - Add an Account number as an index to get the account address
|
||||
func MigrateStore(ctx sdk.Context, storeService corestore.KVStoreService, cdc codec.BinaryCodec) error {
|
||||
return mapAccountAddressToAccountID(ctx, storeService, cdc)
|
||||
}
|
||||
|
||||
// accountNumberStoreKey turn an account number to key used to get the account address from account store
|
||||
// NOTE(tip): exists for legacy compatibility
|
||||
func accountNumberStoreKey(accountNumber uint64) []byte {
|
||||
return append(types.AccountNumberStoreKeyPrefix, sdk.Uint64ToBigEndian(accountNumber)...)
|
||||
}
|
||||
@ -1,111 +0,0 @@
|
||||
package v3_test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/depinject"
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4"
|
||||
authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
type mockSubspace struct {
|
||||
ps authtypes.Params
|
||||
}
|
||||
|
||||
func newMockSubspace(ps authtypes.Params) mockSubspace {
|
||||
return mockSubspace{ps: ps}
|
||||
}
|
||||
|
||||
func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps authexported.ParamSet) {
|
||||
*ps.(*authtypes.Params) = ms.ps
|
||||
}
|
||||
|
||||
// TestMigrateMapAccAddressToAccNumberKey test cases for state migration of map to accAddr to accNum
|
||||
func TestMigrateMapAccAddressToAccNumberKey(t *testing.T) {
|
||||
encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
|
||||
cdc := encCfg.Codec
|
||||
|
||||
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
|
||||
tKey := storetypes.NewTransientStoreKey("transient_test")
|
||||
ctx := testutil.DefaultContext(storeKey, tKey)
|
||||
storeService := runtime.NewKVStoreService(storeKey)
|
||||
|
||||
var accountKeeper keeper.AccountKeeper
|
||||
|
||||
app, err := simtestutil.Setup(
|
||||
depinject.Configs(
|
||||
authtestutil.AppConfig,
|
||||
depinject.Supply(log.NewNopLogger()),
|
||||
),
|
||||
&accountKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
legacySubspace := newMockSubspace(authtypes.DefaultParams())
|
||||
require.NoError(t, v4.Migrate(ctx, storeService, legacySubspace, cdc))
|
||||
|
||||
// new base account
|
||||
senderPrivKey := secp256k1.GenPrivKey()
|
||||
randAccNumber := uint64(rand.Intn(100000-10000) + 10000)
|
||||
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), randAccNumber, 0)
|
||||
|
||||
ctx = app.BaseApp.NewContextLegacy(false, cmtproto.Header{Time: time.Now()})
|
||||
|
||||
// migrator
|
||||
m := keeper.NewMigrator(accountKeeper, app.GRPCQueryRouter(), legacySubspace)
|
||||
// set the account to store with map acc addr to acc number
|
||||
require.NoError(t, m.V45SetAccount(ctx, acc))
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
doMigration bool
|
||||
accNum uint64
|
||||
}{
|
||||
{
|
||||
name: "without state migration",
|
||||
doMigration: false,
|
||||
accNum: acc.AccountNumber,
|
||||
},
|
||||
{
|
||||
name: "with state migration",
|
||||
doMigration: true,
|
||||
accNum: acc.AccountNumber,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.doMigration {
|
||||
require.NoError(t, m.Migrate2to3(ctx))
|
||||
}
|
||||
|
||||
// get the account address by acc id
|
||||
accAddr, err := accountKeeper.Accounts.Indexes.Number.MatchExact(ctx, tc.accNum)
|
||||
if tc.doMigration {
|
||||
require.Equal(t, accAddr.String(), acc.Address)
|
||||
} else {
|
||||
require.ErrorIs(t, err, collections.ErrNotFound)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
package v4
|
||||
|
||||
import (
|
||||
storetypes "cosmossdk.io/core/store"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
var ParamsKey = []byte{0x00}
|
||||
|
||||
// Migrate migrates the x/auth module state from the consensus version 3 to
|
||||
// version 4. Specifically, it takes the parameters that are currently stored
|
||||
// and managed by the x/params modules and stores them directly into the x/auth
|
||||
// module state.
|
||||
func Migrate(ctx sdk.Context, storeService storetypes.KVStoreService, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
|
||||
store := storeService.OpenKVStore(ctx)
|
||||
var currParams types.Params
|
||||
legacySubspace.GetParamSet(ctx, &currParams)
|
||||
|
||||
if err := currParams.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bz := cdc.MustMarshal(&currParams)
|
||||
return store.Set(ParamsKey, bz)
|
||||
}
|
||||
@ -1,50 +0,0 @@
|
||||
package v4_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
v4 "github.com/cosmos/cosmos-sdk/x/auth/migrations/v4"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
)
|
||||
|
||||
type mockSubspace struct {
|
||||
ps types.Params
|
||||
}
|
||||
|
||||
func newMockSubspace(ps types.Params) mockSubspace {
|
||||
return mockSubspace{ps: ps}
|
||||
}
|
||||
|
||||
func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
|
||||
*ps.(*types.Params) = ms.ps
|
||||
}
|
||||
|
||||
func TestMigrate(t *testing.T) {
|
||||
encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
|
||||
cdc := encCfg.Codec
|
||||
|
||||
storeKey := storetypes.NewKVStoreKey(v1.ModuleName)
|
||||
tKey := storetypes.NewTransientStoreKey("transient_test")
|
||||
ctx := testutil.DefaultContext(storeKey, tKey)
|
||||
storeService := runtime.NewKVStoreService(storeKey)
|
||||
|
||||
legacySubspace := newMockSubspace(types.DefaultParams())
|
||||
require.NoError(t, v4.Migrate(ctx, storeService, legacySubspace, cdc))
|
||||
|
||||
var res types.Params
|
||||
bz, err := storeService.OpenKVStore(ctx).Get(v4.ParamsKey)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, cdc.Unmarshal(bz, &res))
|
||||
require.Equal(t, legacySubspace.ps, res)
|
||||
}
|
||||
@ -19,7 +19,6 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/simulation"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
@ -89,9 +88,6 @@ type AppModule struct {
|
||||
|
||||
accountKeeper keeper.AccountKeeper
|
||||
randGenAccountsFn types.RandomGenesisAccountsFn
|
||||
|
||||
// legacySubspace is used solely for migration of x/params managed parameters
|
||||
legacySubspace exported.Subspace
|
||||
}
|
||||
|
||||
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
|
||||
@ -101,12 +97,11 @@ func (am AppModule) IsOnePerModuleType() {}
|
||||
func (am AppModule) IsAppModule() {}
|
||||
|
||||
// NewAppModule creates a new AppModule object
|
||||
func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAccountsFn types.RandomGenesisAccountsFn, ss exported.Subspace) AppModule {
|
||||
func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAccountsFn types.RandomGenesisAccountsFn) AppModule {
|
||||
return AppModule{
|
||||
AppModuleBasic: AppModuleBasic{ac: accountKeeper.AddressCodec()},
|
||||
accountKeeper: accountKeeper,
|
||||
randGenAccountsFn: randGenAccountsFn,
|
||||
legacySubspace: ss,
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,7 +111,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.accountKeeper))
|
||||
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.accountKeeper))
|
||||
|
||||
m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer(), am.legacySubspace)
|
||||
m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer())
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
|
||||
}
|
||||
@ -128,7 +123,6 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err))
|
||||
}
|
||||
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4To5); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5", types.ModuleName))
|
||||
}
|
||||
@ -194,9 +188,6 @@ type ModuleInputs struct {
|
||||
AddressCodec address.Codec
|
||||
RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"`
|
||||
AccountI func() sdk.AccountI `optional:"true"`
|
||||
|
||||
// LegacySubspace is used solely for migration of x/params managed parameters
|
||||
LegacySubspace exported.Subspace `optional:"true"`
|
||||
}
|
||||
|
||||
type ModuleOutputs struct {
|
||||
@ -232,7 +223,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
|
||||
}
|
||||
|
||||
k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth)
|
||||
m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace)
|
||||
m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn)
|
||||
|
||||
return ModuleOutputs{AccountKeeper: k, Module: m}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -20,6 +19,9 @@ const (
|
||||
|
||||
// QuerierRoute is the querier route for distribution
|
||||
QuerierRoute = ModuleName
|
||||
|
||||
addrLen = 20
|
||||
authModuleName = "auth"
|
||||
)
|
||||
|
||||
// Keys for distribution store
|
||||
@ -59,7 +61,7 @@ var (
|
||||
func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2)
|
||||
addr := key[1:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
return sdk.ValAddress(addr)
|
||||
}
|
||||
|
||||
@ -67,29 +69,29 @@ func GetValidatorOutstandingRewardsAddress(key []byte) (valAddr sdk.ValAddress)
|
||||
func GetDelegatorWithdrawInfoAddress(key []byte) (delAddr sdk.AccAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2)
|
||||
addr := key[1:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
return sdk.AccAddress(addr)
|
||||
}
|
||||
|
||||
// gets the addresses from a delegator starting info key
|
||||
func GetDelegatorStartingInfoAddresses(key []byte) (valAddr sdk.ValAddress, delAddr sdk.AccAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
|
||||
addr := key[1 : 1+v1auth.AddrLen]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyAtLeastLength(key, 2+addrLen)
|
||||
addr := key[1 : 1+addrLen]
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
valAddr = sdk.ValAddress(addr)
|
||||
addr = key[1+v1auth.AddrLen:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
addr = key[1+addrLen:]
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
delAddr = sdk.AccAddress(addr)
|
||||
return
|
||||
}
|
||||
|
||||
// gets the address & period from a validator's historical rewards key
|
||||
func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddress, period uint64) {
|
||||
kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
|
||||
addr := key[1 : 1+v1auth.AddrLen]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyAtLeastLength(key, 2+addrLen)
|
||||
addr := key[1 : 1+addrLen]
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
valAddr = sdk.ValAddress(addr)
|
||||
b := key[1+v1auth.AddrLen:]
|
||||
b := key[1+addrLen:]
|
||||
kv.AssertKeyLength(addr, 8)
|
||||
period = binary.LittleEndian.Uint64(b)
|
||||
return
|
||||
@ -99,7 +101,7 @@ func GetValidatorHistoricalRewardsAddressPeriod(key []byte) (valAddr sdk.ValAddr
|
||||
func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2)
|
||||
addr := key[1:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
return sdk.ValAddress(addr)
|
||||
}
|
||||
|
||||
@ -107,17 +109,17 @@ func GetValidatorCurrentRewardsAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
func GetValidatorAccumulatedCommissionAddress(key []byte) (valAddr sdk.ValAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2)
|
||||
addr := key[1:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
return sdk.ValAddress(addr)
|
||||
}
|
||||
|
||||
// gets the height from a validator's slash event key
|
||||
func GetValidatorSlashEventAddressHeight(key []byte) (valAddr sdk.ValAddress, height uint64) {
|
||||
kv.AssertKeyAtLeastLength(key, 2+v1auth.AddrLen)
|
||||
addr := key[1 : 1+v1auth.AddrLen]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
kv.AssertKeyAtLeastLength(key, 2+addrLen)
|
||||
addr := key[1 : 1+addrLen]
|
||||
kv.AssertKeyLength(addr, addrLen)
|
||||
valAddr = sdk.ValAddress(addr)
|
||||
startB := 1 + v1auth.AddrLen
|
||||
startB := 1 + addrLen
|
||||
kv.AssertKeyAtLeastLength(key, startB+9)
|
||||
b := key[startB : startB+8] // the next 8 bytes represent the height
|
||||
height = binary.BigEndian.Uint64(b)
|
||||
|
||||
@ -9,7 +9,6 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -24,6 +23,8 @@ const (
|
||||
|
||||
// QuerierRoute is the querier route for gov
|
||||
QuerierRoute = ModuleName
|
||||
|
||||
addrLen = 20
|
||||
)
|
||||
|
||||
// Keys for governance store
|
||||
@ -154,7 +155,7 @@ func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) {
|
||||
}
|
||||
|
||||
func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
|
||||
kv.AssertKeyLength(key[1:], 8+v1auth.AddrLen)
|
||||
kv.AssertKeyLength(key[1:], 8+addrLen)
|
||||
|
||||
kv.AssertKeyAtLeastLength(key, 10)
|
||||
proposalID = GetProposalIDFromBytes(key[1:9])
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
package exported
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||
)
|
||||
|
||||
type (
|
||||
ParamSet = paramtypes.ParamSet
|
||||
|
||||
// Subspace defines an interface that implements the legacy x/params Subspace
|
||||
// type.
|
||||
//
|
||||
// NOTE: This is used solely for migration of x/params managed parameters.
|
||||
Subspace interface {
|
||||
GetParamSet(ctx sdk.Context, ps ParamSet)
|
||||
}
|
||||
)
|
||||
@ -3,26 +3,22 @@ package keeper
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
|
||||
v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2"
|
||||
v3 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v3"
|
||||
v4 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v4"
|
||||
)
|
||||
|
||||
// Migrator is a struct for handling in-place store migrations.
|
||||
type Migrator struct {
|
||||
keeper Keeper
|
||||
legacySubspace exported.Subspace
|
||||
keeper Keeper
|
||||
}
|
||||
|
||||
// NewMigrator returns a new Migrator.
|
||||
func NewMigrator(keeper Keeper, ss exported.Subspace) Migrator {
|
||||
return Migrator{keeper: keeper, legacySubspace: ss}
|
||||
func NewMigrator(keeper Keeper) Migrator {
|
||||
return Migrator{keeper: keeper}
|
||||
}
|
||||
|
||||
// Migrate1to2 migrates from version 1 to 2.
|
||||
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
||||
return v2.MigrateStore(ctx, m.keeper.storeService)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate2to3 migrates the x/slashing module state from the consensus
|
||||
@ -30,8 +26,7 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
|
||||
// and managed by the x/params modules and stores them directly into the x/slashing
|
||||
// module state.
|
||||
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
|
||||
store := runtime.KVStoreAdapter(m.keeper.storeService.OpenKVStore(ctx))
|
||||
return v3.Migrate(ctx, store, m.legacySubspace, m.keeper.cdc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Migrate3to4 migrates the x/slashing module state from the consensus
|
||||
|
||||
@ -1,69 +0,0 @@
|
||||
// Package v040 is copy-pasted from:
|
||||
// https://github.com/cosmos/cosmos-sdk/blob/v0.41.0/x/slashing/types/keys.go
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
// ModuleName is the name of the module
|
||||
ModuleName = "slashing"
|
||||
|
||||
// StoreKey is the store key string for slashing
|
||||
StoreKey = ModuleName
|
||||
|
||||
// RouterKey is the message route for slashing
|
||||
RouterKey = ModuleName
|
||||
|
||||
// QuerierRoute is the querier route for slashing
|
||||
QuerierRoute = ModuleName
|
||||
)
|
||||
|
||||
// Keys for slashing store
|
||||
// Items are stored with the following key: values
|
||||
//
|
||||
// - 0x01<consAddress_Bytes>: ValidatorSigningInfo
|
||||
//
|
||||
// - 0x02<consAddress_Bytes><period_Bytes>: bool
|
||||
//
|
||||
// - 0x03<accAddr_Bytes>: crypto.PubKey
|
||||
var (
|
||||
ValidatorSigningInfoKeyPrefix = []byte{0x01} // Prefix for signing info
|
||||
ValidatorMissedBlockBitArrayKeyPrefix = []byte{0x02} // Prefix for missed block bit array
|
||||
AddrPubkeyRelationKeyPrefix = []byte{0x03} // Prefix for address-pubkey relation
|
||||
)
|
||||
|
||||
// ValidatorSigningInfoKey - stored by *Consensus* address (not operator address)
|
||||
func ValidatorSigningInfoKey(v sdk.ConsAddress) []byte {
|
||||
return append(ValidatorSigningInfoKeyPrefix, v.Bytes()...)
|
||||
}
|
||||
|
||||
// ValidatorSigningInfoAddress - extract the address from a validator signing info key
|
||||
func ValidatorSigningInfoAddress(key []byte) (v sdk.ConsAddress) {
|
||||
kv.AssertKeyAtLeastLength(key, 2)
|
||||
addr := key[1:]
|
||||
kv.AssertKeyLength(addr, v1auth.AddrLen)
|
||||
return sdk.ConsAddress(addr)
|
||||
}
|
||||
|
||||
// ValidatorMissedBlockBitArrayPrefixKey - stored by *Consensus* address (not operator address)
|
||||
func ValidatorMissedBlockBitArrayPrefixKey(v sdk.ConsAddress) []byte {
|
||||
return append(ValidatorMissedBlockBitArrayKeyPrefix, v.Bytes()...)
|
||||
}
|
||||
|
||||
// ValidatorMissedBlockBitArrayKey - stored by *Consensus* address (not operator address)
|
||||
func ValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, uint64(i))
|
||||
return append(ValidatorMissedBlockBitArrayPrefixKey(v), b...)
|
||||
}
|
||||
|
||||
// AddrPubkeyRelationKey gets pubkey relation key used to get the pubkey from the address
|
||||
func AddrPubkeyRelationKey(address []byte) []byte {
|
||||
return append(AddrPubkeyRelationKeyPrefix, address...)
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/address"
|
||||
)
|
||||
|
||||
var ValidatorMissedBlockBitArrayKeyPrefix = []byte{0x02}
|
||||
|
||||
func ValidatorMissedBlockBitArrayPrefixKey(v sdk.ConsAddress) []byte {
|
||||
return append(ValidatorMissedBlockBitArrayKeyPrefix, address.MustLengthPrefix(v.Bytes())...)
|
||||
}
|
||||
|
||||
func ValidatorMissedBlockBitArrayKey(v sdk.ConsAddress, i int64) []byte {
|
||||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, uint64(i))
|
||||
|
||||
return append(ValidatorMissedBlockBitArrayPrefixKey(v), b...)
|
||||
}
|
||||
@ -1,24 +0,0 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
storetypes "cosmossdk.io/core/store"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v1"
|
||||
v2staking "github.com/cosmos/cosmos-sdk/x/staking/migrations/v2"
|
||||
)
|
||||
|
||||
// MigrateStore performs in-place store migrations from v0.40 to v0.43. The
|
||||
// migration includes:
|
||||
//
|
||||
// - Change addresses to be length-prefixed.
|
||||
func MigrateStore(ctx context.Context, storeService storetypes.KVStoreService) error {
|
||||
store := runtime.KVStoreAdapter(storeService.OpenKVStore(ctx))
|
||||
v2staking.MigratePrefixAddress(store, v1.ValidatorSigningInfoKeyPrefix)
|
||||
v2staking.MigratePrefixAddressBytes(store, v1.ValidatorMissedBlockBitArrayKeyPrefix)
|
||||
v2staking.MigratePrefixAddress(store, v1.AddrPubkeyRelationKeyPrefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,82 +0,0 @@
|
||||
package v2_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/address"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v1"
|
||||
v2 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v2"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
)
|
||||
|
||||
func TestStoreMigration(t *testing.T) {
|
||||
slashingKey := storetypes.NewKVStoreKey("slashing")
|
||||
ctx := testutil.DefaultContext(slashingKey, storetypes.NewTransientStoreKey("transient_test"))
|
||||
storeService := runtime.NewKVStoreService(slashingKey)
|
||||
store := storeService.OpenKVStore(ctx)
|
||||
|
||||
_, _, addr1 := testdata.KeyTestPubAddr()
|
||||
consAddr := sdk.ConsAddress(addr1)
|
||||
// Use dummy value for all keys.
|
||||
value := []byte("foo")
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
oldKey []byte
|
||||
newKey []byte
|
||||
}{
|
||||
{
|
||||
"ValidatorSigningInfoKey",
|
||||
v1.ValidatorSigningInfoKey(consAddr),
|
||||
types.ValidatorSigningInfoKey(consAddr),
|
||||
},
|
||||
{
|
||||
"ValidatorMissedBlockBitArrayKey",
|
||||
v1.ValidatorMissedBlockBitArrayKey(consAddr, 2),
|
||||
v2.ValidatorMissedBlockBitArrayKey(consAddr, 2),
|
||||
},
|
||||
{
|
||||
"AddrPubkeyRelationKey",
|
||||
v1.AddrPubkeyRelationKey(consAddr),
|
||||
addrPubkeyRelationKey(consAddr),
|
||||
},
|
||||
}
|
||||
|
||||
// Set all the old keys to the store
|
||||
for _, tc := range testCases {
|
||||
err := store.Set(tc.oldKey, value)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Run migrations.
|
||||
err := v2.MigrateStore(ctx, storeService)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Make sure the new keys are set and old keys are deleted.
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if !bytes.Equal(tc.oldKey, tc.newKey) {
|
||||
v, err := store.Get(tc.oldKey)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, v)
|
||||
}
|
||||
v, err := store.Get(tc.newKey)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, value, v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func addrPubkeyRelationKey(addr []byte) []byte {
|
||||
return append(types.AddrPubkeyRelationKeyPrefix, address.MustLengthPrefix(addr)...)
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
package v3
|
||||
|
||||
import (
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
)
|
||||
|
||||
const (
|
||||
ModuleName = "slashing"
|
||||
)
|
||||
|
||||
var ParamsKey = []byte{0x00}
|
||||
|
||||
// Migrate migrates the x/slashing module state from the consensus version 2 to
|
||||
// version 3. Specifically, it takes the parameters that are currently stored
|
||||
// and managed by the x/params modules and stores them directly into the x/slashing
|
||||
// module state.
|
||||
func Migrate(ctx sdk.Context, store storetypes.KVStore, legacySubspace exported.Subspace, cdc codec.BinaryCodec) error {
|
||||
var currParams types.Params
|
||||
legacySubspace.GetParamSet(ctx, &currParams)
|
||||
|
||||
if err := currParams.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bz := cdc.MustMarshal(&currParams)
|
||||
store.Set(ParamsKey, bz)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package v3_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
|
||||
v3 "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v3"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
)
|
||||
|
||||
type mockSubspace struct {
|
||||
ps types.Params
|
||||
}
|
||||
|
||||
func newMockSubspace(ps types.Params) mockSubspace {
|
||||
return mockSubspace{ps: ps}
|
||||
}
|
||||
|
||||
func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
|
||||
*ps.(*types.Params) = ms.ps
|
||||
}
|
||||
|
||||
func TestMigrate(t *testing.T) {
|
||||
cdc := moduletestutil.MakeTestEncodingConfig(slashing.AppModuleBasic{}).Codec
|
||||
storeKey := storetypes.NewKVStoreKey(v3.ModuleName)
|
||||
tKey := storetypes.NewTransientStoreKey("transient_test")
|
||||
ctx := testutil.DefaultContext(storeKey, tKey)
|
||||
store := ctx.KVStore(storeKey)
|
||||
|
||||
legacySubspace := newMockSubspace(types.DefaultParams())
|
||||
require.NoError(t, v3.Migrate(ctx, store, legacySubspace, cdc))
|
||||
|
||||
var res types.Params
|
||||
bz := store.Get(v3.ParamsKey)
|
||||
require.NoError(t, cdc.Unmarshal(bz, &res))
|
||||
require.Equal(t, legacySubspace.ps, res)
|
||||
}
|
||||
@ -22,7 +22,6 @@ import (
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/exported"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
|
||||
"github.com/cosmos/cosmos-sdk/x/slashing/types"
|
||||
@ -100,9 +99,6 @@ type AppModule struct {
|
||||
accountKeeper types.AccountKeeper
|
||||
bankKeeper types.BankKeeper
|
||||
stakingKeeper types.StakingKeeper
|
||||
|
||||
// legacySubspace is used solely for migration of x/slashing managed parameters
|
||||
legacySubspace exported.Subspace
|
||||
}
|
||||
|
||||
// NewAppModule creates a new AppModule object
|
||||
@ -112,7 +108,6 @@ func NewAppModule(
|
||||
ak types.AccountKeeper,
|
||||
bk types.BankKeeper,
|
||||
sk types.StakingKeeper,
|
||||
ss exported.Subspace,
|
||||
registry cdctypes.InterfaceRegistry,
|
||||
) AppModule {
|
||||
return AppModule{
|
||||
@ -121,7 +116,6 @@ func NewAppModule(
|
||||
accountKeeper: ak,
|
||||
bankKeeper: bk,
|
||||
stakingKeeper: sk,
|
||||
legacySubspace: ss,
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,7 +130,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
||||
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
|
||||
|
||||
m := keeper.NewMigrator(am.keeper, am.legacySubspace)
|
||||
m := keeper.NewMigrator(am.keeper)
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err))
|
||||
}
|
||||
@ -144,7 +138,6 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err))
|
||||
}
|
||||
|
||||
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil {
|
||||
panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err))
|
||||
}
|
||||
@ -221,9 +214,6 @@ type ModuleInputs struct {
|
||||
AccountKeeper types.AccountKeeper
|
||||
BankKeeper types.BankKeeper
|
||||
StakingKeeper types.StakingKeeper
|
||||
|
||||
// LegacySubspace is used solely for migration of x/params managed parameters
|
||||
LegacySubspace exported.Subspace `optional:"true"`
|
||||
}
|
||||
|
||||
type ModuleOutputs struct {
|
||||
@ -242,7 +232,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
|
||||
}
|
||||
|
||||
k := keeper.NewKeeper(in.Cdc, in.LegacyAmino, in.StoreService, in.StakingKeeper, authority.String())
|
||||
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.LegacySubspace, in.Registry)
|
||||
m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.Registry)
|
||||
return ModuleOutputs{
|
||||
Keeper: k,
|
||||
Module: m,
|
||||
|
||||
@ -9,7 +9,6 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/kv"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
@ -30,6 +29,8 @@ const (
|
||||
// value by not adding the staking module to the application module manager's
|
||||
// SetOrderBeginBlockers.
|
||||
DefaultHistoricalEntries uint32 = 10000
|
||||
|
||||
addrLen = 20
|
||||
)
|
||||
|
||||
var (
|
||||
@ -90,7 +91,7 @@ func GetValidatorsByPowerIndexKey(validator types.Validator) []byte {
|
||||
powerBytesLen := len(powerBytes) // 8
|
||||
|
||||
// key is of format prefix || powerbytes || addrBytes
|
||||
key := make([]byte, 1+powerBytesLen+v1auth.AddrLen)
|
||||
key := make([]byte, 1+powerBytesLen+addrLen)
|
||||
|
||||
key[0] = ValidatorsByPowerIndexKey[0]
|
||||
copy(key[1:powerBytesLen+1], powerBytes)
|
||||
@ -117,11 +118,11 @@ func GetLastValidatorPowerKey(operator sdk.ValAddress) []byte {
|
||||
// GetREDKey returns a key prefix for indexing a redelegation from a delegator
|
||||
// and source validator to a destination validator.
|
||||
func GetREDKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.ValAddress) []byte {
|
||||
key := make([]byte, 1+v1auth.AddrLen*3)
|
||||
key := make([]byte, 1+addrLen*3)
|
||||
|
||||
copy(key[0:v1auth.AddrLen+1], GetREDsKey(delAddr.Bytes()))
|
||||
copy(key[v1auth.AddrLen+1:2*v1auth.AddrLen+1], valSrcAddr.Bytes())
|
||||
copy(key[2*v1auth.AddrLen+1:3*v1auth.AddrLen+1], valDstAddr.Bytes())
|
||||
copy(key[0:addrLen+1], GetREDsKey(delAddr.Bytes()))
|
||||
copy(key[addrLen+1:2*addrLen+1], valSrcAddr.Bytes())
|
||||
copy(key[2*addrLen+1:3*addrLen+1], valDstAddr.Bytes())
|
||||
|
||||
return key
|
||||
}
|
||||
@ -133,10 +134,10 @@ func GetREDByValSrcIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V
|
||||
offset := len(REDSFromValsSrcKey)
|
||||
|
||||
// key is of the form REDSFromValsSrcKey || delAddr || valDstAddr
|
||||
key := make([]byte, len(REDSFromValsSrcKey)+2*v1auth.AddrLen)
|
||||
key := make([]byte, len(REDSFromValsSrcKey)+2*addrLen)
|
||||
copy(key[0:offset], REDSFromValsSrcKey)
|
||||
copy(key[offset:offset+v1auth.AddrLen], delAddr.Bytes())
|
||||
copy(key[offset+v1auth.AddrLen:offset+2*v1auth.AddrLen], valDstAddr.Bytes())
|
||||
copy(key[offset:offset+addrLen], delAddr.Bytes())
|
||||
copy(key[offset+addrLen:offset+2*addrLen], valDstAddr.Bytes())
|
||||
|
||||
return key
|
||||
}
|
||||
@ -148,10 +149,10 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V
|
||||
offset := len(REDSToValsDstKey)
|
||||
|
||||
// key is of the form REDSToValsDstKey || delAddr || valSrcAddr
|
||||
key := make([]byte, len(REDSToValsDstKey)+2*v1auth.AddrLen)
|
||||
key := make([]byte, len(REDSToValsDstKey)+2*addrLen)
|
||||
copy(key[0:offset], REDSToValsDstKey)
|
||||
copy(key[offset:offset+v1auth.AddrLen], delAddr.Bytes())
|
||||
copy(key[offset+v1auth.AddrLen:offset+2*v1auth.AddrLen], valSrcAddr.Bytes())
|
||||
copy(key[offset:offset+addrLen], delAddr.Bytes())
|
||||
copy(key[offset+addrLen:offset+2*addrLen], valSrcAddr.Bytes())
|
||||
|
||||
return key
|
||||
}
|
||||
@ -159,11 +160,11 @@ func GetREDByValDstIndexKey(delAddr sdk.AccAddress, valSrcAddr, valDstAddr sdk.V
|
||||
// GetREDKeyFromValSrcIndexKey rearranges the ValSrcIndexKey to get the REDKey
|
||||
func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte {
|
||||
// note that first byte is prefix byte
|
||||
kv.AssertKeyLength(indexKey, 3*v1auth.AddrLen+1)
|
||||
kv.AssertKeyLength(indexKey, 3*addrLen+1)
|
||||
|
||||
valSrcAddr := indexKey[1 : v1auth.AddrLen+1]
|
||||
delAddr := indexKey[v1auth.AddrLen+1 : 2*v1auth.AddrLen+1]
|
||||
valDstAddr := indexKey[2*v1auth.AddrLen+1 : 3*v1auth.AddrLen+1]
|
||||
valSrcAddr := indexKey[1 : addrLen+1]
|
||||
delAddr := indexKey[addrLen+1 : 2*addrLen+1]
|
||||
valDstAddr := indexKey[2*addrLen+1 : 3*addrLen+1]
|
||||
|
||||
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
|
||||
}
|
||||
@ -171,11 +172,11 @@ func GetREDKeyFromValSrcIndexKey(indexKey []byte) []byte {
|
||||
// GetREDKeyFromValDstIndexKey rearranges the ValDstIndexKey to get the REDKey
|
||||
func GetREDKeyFromValDstIndexKey(indexKey []byte) []byte {
|
||||
// note that first byte is prefix byte
|
||||
kv.AssertKeyLength(indexKey, 3*v1auth.AddrLen+1)
|
||||
kv.AssertKeyLength(indexKey, 3*addrLen+1)
|
||||
|
||||
valDstAddr := indexKey[1 : v1auth.AddrLen+1]
|
||||
delAddr := indexKey[v1auth.AddrLen+1 : 2*v1auth.AddrLen+1]
|
||||
valSrcAddr := indexKey[2*v1auth.AddrLen+1 : 3*v1auth.AddrLen+1]
|
||||
valDstAddr := indexKey[1 : addrLen+1]
|
||||
delAddr := indexKey[addrLen+1 : 2*addrLen+1]
|
||||
valSrcAddr := indexKey[2*addrLen+1 : 3*addrLen+1]
|
||||
|
||||
return GetREDKey(delAddr, valSrcAddr, valDstAddr)
|
||||
}
|
||||
@ -259,11 +260,11 @@ func GetUBDByValIndexKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte
|
||||
func GetUBDKeyFromValIndexKey(indexKey []byte) []byte {
|
||||
kv.AssertKeyAtLeastLength(indexKey, 2)
|
||||
addrs := indexKey[1:] // remove prefix bytes
|
||||
kv.AssertKeyLength(addrs, 2*v1auth.AddrLen)
|
||||
kv.AssertKeyLength(addrs, 2*addrLen)
|
||||
|
||||
kv.AssertKeyAtLeastLength(addrs, v1auth.AddrLen+1)
|
||||
valAddr := addrs[:v1auth.AddrLen]
|
||||
delAddr := addrs[v1auth.AddrLen:]
|
||||
kv.AssertKeyAtLeastLength(addrs, addrLen+1)
|
||||
valAddr := addrs[:addrLen]
|
||||
delAddr := addrs[addrLen:]
|
||||
|
||||
return GetUBDKey(delAddr, valAddr)
|
||||
}
|
||||
|
||||
@ -5,7 +5,10 @@ import (
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/types/address"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
addrLen = 20
|
||||
)
|
||||
|
||||
// MigratePrefixAddress is a helper function that migrates all keys of format:
|
||||
@ -40,8 +43,8 @@ func MigratePrefixAddressBytes(store storetypes.KVStore, prefixBz []byte) {
|
||||
defer oldStoreIter.Close()
|
||||
|
||||
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
|
||||
addr := oldStoreIter.Key()[:v1auth.AddrLen]
|
||||
endBz := oldStoreIter.Key()[v1auth.AddrLen:]
|
||||
addr := oldStoreIter.Key()[:addrLen]
|
||||
endBz := oldStoreIter.Key()[addrLen:]
|
||||
newStoreKey := append(append(prefixBz, address.MustLengthPrefix(addr)...), endBz...)
|
||||
|
||||
// Set new key on store. Values don't change.
|
||||
@ -61,8 +64,8 @@ func MigratePrefixAddressAddress(store storetypes.KVStore, prefixBz []byte) {
|
||||
defer oldStoreIter.Close()
|
||||
|
||||
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
|
||||
addr1 := oldStoreIter.Key()[:v1auth.AddrLen]
|
||||
addr2 := oldStoreIter.Key()[v1auth.AddrLen:]
|
||||
addr1 := oldStoreIter.Key()[:addrLen]
|
||||
addr2 := oldStoreIter.Key()[addrLen:]
|
||||
newStoreKey := append(append(prefixBz, address.MustLengthPrefix(addr1)...), address.MustLengthPrefix(addr2)...)
|
||||
|
||||
// Set new key on store. Values don't change.
|
||||
|
||||
@ -6,7 +6,6 @@ import (
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/address"
|
||||
v1auth "github.com/cosmos/cosmos-sdk/x/auth/migrations/v1"
|
||||
v1 "github.com/cosmos/cosmos-sdk/x/staking/migrations/v1"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
@ -22,9 +21,9 @@ func migratePrefixAddressAddressAddress(store storetypes.KVStore, prefixBz []byt
|
||||
defer oldStoreIter.Close()
|
||||
|
||||
for ; oldStoreIter.Valid(); oldStoreIter.Next() {
|
||||
addr1 := oldStoreIter.Key()[:v1auth.AddrLen]
|
||||
addr2 := oldStoreIter.Key()[v1auth.AddrLen : 2*v1auth.AddrLen]
|
||||
addr3 := oldStoreIter.Key()[2*v1auth.AddrLen:]
|
||||
addr1 := oldStoreIter.Key()[:addrLen]
|
||||
addr2 := oldStoreIter.Key()[addrLen : 2*addrLen]
|
||||
addr3 := oldStoreIter.Key()[2*addrLen:]
|
||||
newStoreKey := append(append(append(
|
||||
prefixBz,
|
||||
address.MustLengthPrefix(addr1)...), address.MustLengthPrefix(addr2)...), address.MustLengthPrefix(addr3)...,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user