From 3b7b0aa6cac13b0905d7fb9a77d9061325aa5ee9 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 13 Jul 2023 11:08:27 +0200 Subject: [PATCH] feat(staking)!: add consensus and validator address codec in staking (#16959) Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> (cherry picked from commit e0be2b80fc1b019cacdcd503f82287ac7b282954) # Conflicts: # client/v2/go.mod # client/v2/go.sum # simapp/go.mod # simapp/go.sum # tests/go.mod # tests/go.sum # x/staking/keeper/keeper_test.go --- CHANGELOG.md | 3 +- client/v2/autocli/app.go | 4 +- client/v2/go.mod | 8 ++- client/v2/go.sum | 14 +++-- go.mod | 2 +- go.sum | 4 +- simapp/app.go | 4 +- simapp/app_config.go | 10 +++- simapp/app_test.go | 18 ++++-- simapp/app_v2.go | 18 ++++-- simapp/go.mod | 5 ++ simapp/go.sum | 5 ++ tests/go.mod | 5 ++ tests/go.sum | 5 ++ .../bank/keeper/deterministic_test.go | 1 - .../distribution/keeper/msg_server_test.go | 3 +- .../evidence/keeper/infraction_test.go | 3 +- tests/integration/gov/keeper/keeper_test.go | 3 +- .../slashing/keeper/keeper_test.go | 3 +- .../integration/staking/keeper/common_test.go | 3 +- .../staking/keeper/determinstic_test.go | 3 +- testutil/integration/example_test.go | 2 - x/auth/ante/testutil_test.go | 2 +- x/auth/keeper/deterministic_test.go | 3 - x/auth/keeper/keeper.go | 34 ++++-------- x/auth/keeper/keeper_test.go | 1 - x/auth/module.go | 29 +++------- x/auth/types/depinject_alias.go | 6 -- x/auth/vesting/msg_server_test.go | 1 - x/auth/vesting/types/vesting_account_test.go | 1 - x/group/migrations/v2/migrate_test.go | 2 +- x/staking/keeper/keeper.go | 45 +++++++++++---- x/staking/keeper/keeper_test.go | 7 ++- x/staking/keeper/val_state_change.go | 4 +- x/staking/module.go | 55 +++++++++++++++++-- x/staking/testutil/expected_keepers_mocks.go | 14 ----- x/staking/types/depinject_alias.go | 11 ++++ x/staking/types/expected_keepers.go | 1 - 38 files changed, 209 insertions(+), 133 deletions(-) delete mode 100644 x/auth/types/depinject_alias.go create mode 100644 x/staking/types/depinject_alias.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 61efd2c23732..75e436ebfe3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* (staking) [#16959](https://github.com/cosmos/cosmos-sdk/pull/16959) Add validator and consensus address codec as staking keeper arguments. * (types) [#16272](https://github.com/cosmos/cosmos-sdk/pull/16272) From now the `FeeGranter` in the `FeeTx` interface takes the byte type instead of string. * (testutil) [#16899](https://github.com/cosmos/cosmos-sdk/pull/16899) The *cli testutil* `QueryBalancesExec` has been removed. Use the gRPC or REST query instead. * (x/auth) [#16650](https://github.com/cosmos/cosmos-sdk/pull/16650) The *cli testutil* `QueryAccountExec` has been removed. Use the gRPC or REST query instead. @@ -95,7 +96,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/staking) [#16324](https://github.com/cosmos/cosmos-sdk/pull/16324) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error`. Notable changes: * `Validator` method now returns `types.ErrNoValidatorFound` instead of `nil` when not found. -* (x/auth) [#16621](https://github.com/cosmos/cosmos-sdk/pull/16621), [#16768](https://github.com/cosmos/cosmos-sdk/pull/16768) Pass address codecs to auth new keeper constructor. +* (x/auth) [#16621](https://github.com/cosmos/cosmos-sdk/pull/16621) Pass address codec to auth new keeper constructor. * (x/auth/vesting) [#16741](https://github.com/cosmos/cosmos-sdk/pull/16741) Vesting account constructor now return an error with the result of their validate function. ## [v0.50.0-alpha.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.0-alpha.0) - 2023-06-07 diff --git a/client/v2/autocli/app.go b/client/v2/autocli/app.go index 83f78443e335..e53ce06048a9 100644 --- a/client/v2/autocli/app.go +++ b/client/v2/autocli/app.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) // AppOptions are autocli options for an app. These options can be built via depinject based on an app config. Ex: @@ -39,7 +39,7 @@ type AppOptions struct { // AddressCodec is the address codec to use for the app. AddressCodec address.Codec - ValidatorAddressCodec authtypes.ValidatorAddressCodec + ValidatorAddressCodec stakingtypes.ValidatorAddressCodec } // EnhanceRootCommand enhances the provided root command with autocli AppOptions, diff --git a/client/v2/go.mod b/client/v2/go.mod index 5a9bc97d777c..65e836a9e989 100644 --- a/client/v2/go.mod +++ b/client/v2/go.mod @@ -3,12 +3,16 @@ module cosmossdk.io/client/v2 go 1.20 require ( - cosmossdk.io/api v0.5.0 + cosmossdk.io/api v0.6.0 cosmossdk.io/core v0.9.0 cosmossdk.io/depinject v1.0.0-alpha.3 github.com/cockroachdb/errors v1.10.0 github.com/cosmos/cosmos-proto v1.0.0-beta.3 +<<<<<<< HEAD github.com/cosmos/cosmos-sdk v0.50.0-alpha.1 +======= + github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90 +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) github.com/cosmos/gogoproto v1.4.10 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 @@ -22,7 +26,7 @@ require ( require ( cosmossdk.io/collections v0.3.0 // indirect cosmossdk.io/errors v1.0.0 // indirect - cosmossdk.io/log v1.1.0 // indirect + cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca // indirect cosmossdk.io/math v1.0.1 // indirect cosmossdk.io/store v1.0.0-alpha.1 // indirect cosmossdk.io/x/tx v0.8.0 // indirect diff --git a/client/v2/go.sum b/client/v2/go.sum index 7cd47cc531a6..2076a4989a2b 100644 --- a/client/v2/go.sum +++ b/client/v2/go.sum @@ -35,8 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= -cosmossdk.io/api v0.5.0 h1:C0gzb5N3qYE0VEUlXleuilv7Z/7/MHhdOKkgn2Ugfnc= -cosmossdk.io/api v0.5.0/go.mod h1:5Jc00Q4tSTcrZXvvE3J/ybvZK6DopLcp3ThK3X703QE= +cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM= +cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M= cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08= cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg= cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic= @@ -45,8 +45,8 @@ cosmossdk.io/depinject v1.0.0-alpha.3 h1:6evFIgj//Y3w09bqOUOzEpFj5tsxBqdc5CfkO7z cosmossdk.io/depinject v1.0.0-alpha.3/go.mod h1:eRbcdQ7MRpIPEM5YUJh8k97nxHpYbc3sMUnEtt8HPWU= cosmossdk.io/errors v1.0.0 h1:nxF07lmlBbB8NKQhtJ+sJm6ef5uV1XkvPXG2bUntb04= cosmossdk.io/errors v1.0.0/go.mod h1:+hJZLuhdDE0pYN8HkOrVNwrIOYvUGnn6+4fjnJs/oV0= -cosmossdk.io/log v1.1.0 h1:v0ogPHYeTzPcBTcPR1A3j1hkei4pZama8kz8LKlCMv0= -cosmossdk.io/log v1.1.0/go.mod h1:6zjroETlcDs+mm62gd8Ig7mZ+N+fVOZS91V17H+M4N4= +cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca h1:msenprh2BLLRwNT7zN56TbBHOGk/7ARQckXHxXyvjoQ= +cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca/go.mod h1:PkIAKXZvaxrTRc++z53XMRvFk8AcGGWYHcMIPzVYX9c= cosmossdk.io/math v1.0.1 h1:Qx3ifyOPaMLNH/89WeZFH268yCvU4xEcnPLu3sJqPPg= cosmossdk.io/math v1.0.1/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k= cosmossdk.io/store v1.0.0-alpha.1 h1:/151XxAgm0tiKuYrtJzMG61lf6enpPuP+D/hIN8cRjQ= @@ -98,6 +98,7 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5MS5JVb4c= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= @@ -160,8 +161,13 @@ github.com/cosmos/cosmos-db v1.0.0 h1:EVcQZ+qYag7W6uorBKFPvX6gRjw6Uq2hIh4hCWjuQ0 github.com/cosmos/cosmos-db v1.0.0/go.mod h1:iBvi1TtqaedwLdcrZVYRSSCb6eSy61NLj4UNmdIgs0U= github.com/cosmos/cosmos-proto v1.0.0-beta.3 h1:VitvZ1lPORTVxkmF2fAp3IiA61xVwArQYKXTdEcpW6o= github.com/cosmos/cosmos-proto v1.0.0-beta.3/go.mod h1:t8IASdLaAq+bbHbjq4p960BvcTqtwuAxid3b/2rOD6I= +<<<<<<< HEAD github.com/cosmos/cosmos-sdk v0.50.0-alpha.1 h1:7ERS+ZW1tTly/DLfUwjMB6h80wtlNGBcFjI2Eb9eZIU= github.com/cosmos/cosmos-sdk v0.50.0-alpha.1/go.mod h1:xee+suLR+WgtzSQBr0jjXQsvb1FIteMfnEzbr2Lk+4w= +======= +github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90 h1:DBxe7gSu1/UPsSqsimostzeis6P732bBTbxG511s/WA= +github.com/cosmos/cosmos-sdk v0.46.0-beta2.0.20230712155822-3b269baa9c90/go.mod h1:L/QMvN62eJFdeaf5AlQPI3g4dSwwsdPA5YCnMbxCvjc= +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY= github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw= github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE= diff --git a/go.mod b/go.mod index 7fdfff2e169b..6489708d5885 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ go 1.20 module github.com/cosmos/cosmos-sdk require ( - cosmossdk.io/api v0.5.0 + cosmossdk.io/api v0.6.0 cosmossdk.io/collections v0.3.0 cosmossdk.io/core v0.9.0 cosmossdk.io/depinject v1.0.0-alpha.3 diff --git a/go.sum b/go.sum index 644bd61c8e09..bfd8495370b6 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= -cosmossdk.io/api v0.5.0 h1:C0gzb5N3qYE0VEUlXleuilv7Z/7/MHhdOKkgn2Ugfnc= -cosmossdk.io/api v0.5.0/go.mod h1:5Jc00Q4tSTcrZXvvE3J/ybvZK6DopLcp3ThK3X703QE= +cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM= +cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M= cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08= cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg= cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic= diff --git a/simapp/app.go b/simapp/app.go index 2754dc24adde..e6d76583f6ed 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -280,7 +280,7 @@ func NewSimApp( bApp.SetParamStore(app.ConsensusParamsKeeper.ParamsStore) // add keepers - app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec(sdk.Bech32MainPrefix), authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec(sdk.Bech32MainPrefix), sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String()) app.BankKeeper = bankkeeper.NewBaseKeeper( appCodec, @@ -291,7 +291,7 @@ func NewSimApp( logger, ) app.StakingKeeper = stakingkeeper.NewKeeper( - appCodec, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), app.AccountKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), + appCodec, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), app.AccountKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(), authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr), authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr), ) app.MintKeeper = mintkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), app.StakingKeeper, app.AccountKeeper, app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String()) diff --git a/simapp/app_config.go b/simapp/app_config.go index 852d39bd0e9f..402ece7cc7a7 100644 --- a/simapp/app_config.go +++ b/simapp/app_config.go @@ -168,7 +168,6 @@ var ( Name: authtypes.ModuleName, Config: appconfig.WrapAny(&authmodulev1.Module{ Bech32Prefix: "cosmos", - Bech32PrefixValidator: "cosmosvaloper", ModuleAccountPermissions: moduleAccPerms, // By default modules authority is the governance module. This is configurable with the following: // Authority: "group", // A custom module authority can be set using a module name @@ -186,8 +185,13 @@ var ( }), }, { - Name: stakingtypes.ModuleName, - Config: appconfig.WrapAny(&stakingmodulev1.Module{}), + Name: stakingtypes.ModuleName, + Config: appconfig.WrapAny(&stakingmodulev1.Module{ + // NOTE: specifying a prefix is only necessary when using bech32 addresses + // If not specfied, the auth Bech32Prefix appended with "valoper" and "valcons" is used by default + Bech32PrefixValidator: "cosmosvaloper", + Bech32PrefixConsensus: "cosmosvalcons", + }), }, { Name: slashingtypes.ModuleName, diff --git a/simapp/app_test.go b/simapp/app_test.go index 21b478bc01d7..5d1c87898186 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -28,7 +28,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/msgservice" "github.com/cosmos/cosmos-sdk/x/auth" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/vesting" authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module" "github.com/cosmos/cosmos-sdk/x/bank" @@ -42,6 +41,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/x/slashing" "github.com/cosmos/cosmos-sdk/x/staking" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func TestSimAppExportAndBlockedAddrs(t *testing.T) { @@ -315,14 +315,15 @@ func (c customAddressCodec) BytesToString(bz []byte) (string, error) { func TestAddressCodecFactory(t *testing.T) { var addrCodec address.Codec - var valAddressCodec authtypes.ValidatorAddressCodec + var valAddressCodec stakingtypes.ValidatorAddressCodec + var consAddressCodec stakingtypes.ConsensusAddressCodec err := depinject.Inject( depinject.Configs( network.MinimumAppConfig(), depinject.Supply(log.NewNopLogger()), ), - &addrCodec, &valAddressCodec) + &addrCodec, &valAddressCodec, &consAddressCodec) require.NoError(t, err) require.NotNil(t, addrCodec) _, ok := addrCodec.(customAddressCodec) @@ -330,6 +331,9 @@ func TestAddressCodecFactory(t *testing.T) { require.NotNil(t, valAddressCodec) _, ok = valAddressCodec.(customAddressCodec) require.False(t, ok) + require.NotNil(t, consAddressCodec) + _, ok = consAddressCodec.(customAddressCodec) + require.False(t, ok) // Set the address codec to the custom one err = depinject.Inject( @@ -338,10 +342,11 @@ func TestAddressCodecFactory(t *testing.T) { depinject.Supply( log.NewNopLogger(), func() address.Codec { return customAddressCodec{} }, - func() authtypes.ValidatorAddressCodec { return customAddressCodec{} }, + func() stakingtypes.ValidatorAddressCodec { return customAddressCodec{} }, + func() stakingtypes.ConsensusAddressCodec { return customAddressCodec{} }, ), ), - &addrCodec, &valAddressCodec) + &addrCodec, &valAddressCodec, &consAddressCodec) require.NoError(t, err) require.NotNil(t, addrCodec) _, ok = addrCodec.(customAddressCodec) @@ -349,4 +354,7 @@ func TestAddressCodecFactory(t *testing.T) { require.NotNil(t, valAddressCodec) _, ok = valAddressCodec.(customAddressCodec) require.True(t, ok) + require.NotNil(t, consAddressCodec) + _, ok = consAddressCodec.(customAddressCodec) + require.True(t, ok) } diff --git a/simapp/app_v2.go b/simapp/app_v2.go index 4a1e3321a801..35f2fba69d57 100644 --- a/simapp/app_v2.go +++ b/simapp/app_v2.go @@ -128,20 +128,28 @@ func NewSimApp( // add it below. By default the auth module uses simulation.RandomGenesisAccounts. // // authtypes.RandomGenesisAccountsFn(simulation.RandomGenesisAccounts), - + // // For providing a custom a base account type add it below. // By default the auth module uses authtypes.ProtoBaseAccount(). // // func() sdk.AccountI { return authtypes.ProtoBaseAccount() }, - + // // For providing a different address codec, add it below. // By default the auth module uses a Bech32 address codec, // with the prefix defined in the auth module configuration. - // NOTE, if a custom address codec is provided, a custom validator - // address codec must be provided as well. // // func() address.Codec { return <- custom address codec type -> } - // func() auth.ValidatorAddressCodec { return <- custom validator address codec type -> } + + // + // STAKING + // + // For provinding a different validator and consensus address codec, add it below. + // By default the staking module uses the bech32 prefix provided in the auth config, + // and appends "valoper" and "valcons" for validator and consensus addresses respectively. + // When providing a custom address codec in auth, a custom codec must be provided here as well. + // + // func() stakingtypes.ValidatorAddressCodec { return <- custom validator address codec type -> } + // func() stakingtypes.ConsensusAddressCodec { return <- custom consensus address codec type -> } // // MINT diff --git a/simapp/go.mod b/simapp/go.mod index c398114f2b54..219a4f3434d9 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -3,8 +3,13 @@ module cosmossdk.io/simapp go 1.20 require ( +<<<<<<< HEAD cosmossdk.io/api v0.5.0 cosmossdk.io/client/v2 v2.0.0-20230706173432-bc5eb31751de +======= + cosmossdk.io/api v0.6.0 + cosmossdk.io/client/v2 v2.0.0-20230630094428-02b760776860 +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) cosmossdk.io/core v0.9.0 cosmossdk.io/depinject v1.0.0-alpha.3 cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca diff --git a/simapp/go.sum b/simapp/go.sum index e6042e633e67..dfe07864136e 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -187,10 +187,15 @@ cloud.google.com/go/webrisk v1.4.0/go.mod h1:Hn8X6Zr+ziE2aNd8SliSDWpEnSS1u4R9+xX cloud.google.com/go/webrisk v1.5.0/go.mod h1:iPG6fr52Tv7sGk0H6qUFzmL3HHZev1htXuWDEEsqMTg= cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1Vwf+KmJENM0= cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M= +<<<<<<< HEAD cosmossdk.io/api v0.5.0 h1:C0gzb5N3qYE0VEUlXleuilv7Z/7/MHhdOKkgn2Ugfnc= cosmossdk.io/api v0.5.0/go.mod h1:5Jc00Q4tSTcrZXvvE3J/ybvZK6DopLcp3ThK3X703QE= cosmossdk.io/client/v2 v2.0.0-20230706173432-bc5eb31751de h1:RVE6LX+OPeQhz9kgzAcrC7Zot34leCc1S6d6Zhp7t3c= cosmossdk.io/client/v2 v2.0.0-20230706173432-bc5eb31751de/go.mod h1:fVvL7eswXH3VmJVr96BYjovTXXriU6OjUPktjXxOmNU= +======= +cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM= +cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M= +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08= cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg= cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic= diff --git a/tests/go.mod b/tests/go.mod index 81a1718663d4..f265cf318fd2 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -3,8 +3,13 @@ module github.com/cosmos/cosmos-sdk/tests go 1.20 require ( +<<<<<<< HEAD cosmossdk.io/api v0.5.0 cosmossdk.io/collections v0.3.0 // indirect +======= + cosmossdk.io/api v0.6.0 + cosmossdk.io/collections v0.3.0 +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) cosmossdk.io/core v0.9.0 cosmossdk.io/depinject v1.0.0-alpha.3 cosmossdk.io/errors v1.0.0 diff --git a/tests/go.sum b/tests/go.sum index fd97204e04e6..9c4bb940a283 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -187,10 +187,15 @@ cloud.google.com/go/webrisk v1.4.0/go.mod h1:Hn8X6Zr+ziE2aNd8SliSDWpEnSS1u4R9+xX cloud.google.com/go/webrisk v1.5.0/go.mod h1:iPG6fr52Tv7sGk0H6qUFzmL3HHZev1htXuWDEEsqMTg= cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1Vwf+KmJENM0= cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M= +<<<<<<< HEAD cosmossdk.io/api v0.5.0 h1:C0gzb5N3qYE0VEUlXleuilv7Z/7/MHhdOKkgn2Ugfnc= cosmossdk.io/api v0.5.0/go.mod h1:5Jc00Q4tSTcrZXvvE3J/ybvZK6DopLcp3ThK3X703QE= cosmossdk.io/client/v2 v2.0.0-20230706173432-bc5eb31751de h1:RVE6LX+OPeQhz9kgzAcrC7Zot34leCc1S6d6Zhp7t3c= cosmossdk.io/client/v2 v2.0.0-20230706173432-bc5eb31751de/go.mod h1:fVvL7eswXH3VmJVr96BYjovTXXriU6OjUPktjXxOmNU= +======= +cosmossdk.io/api v0.6.0 h1:AEpx/sRZaQEusARZqAxbg20zRXnS6e1F+uoal39trOM= +cosmossdk.io/api v0.6.0/go.mod h1:kJFAEMLN57y0viszHDPLMmieF0471o5QAwwApa+270M= +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) cosmossdk.io/collections v0.3.0 h1:v0eEqLBxebAV+t+Ahwf9tSJOu95HVLINwROXx2TTZ08= cosmossdk.io/collections v0.3.0/go.mod h1:CHE1+niUElL9ikCpevRZcp0yqQ4TU0TrEEGirN0mvIg= cosmossdk.io/core v0.9.0 h1:30ScAOHDIUOCg1DKAwqkho9wuQJnu7GUrMcg0XLioic= diff --git a/tests/integration/bank/keeper/deterministic_test.go b/tests/integration/bank/keeper/deterministic_test.go index 2b5a62381745..86bce291117c 100644 --- a/tests/integration/bank/keeper/deterministic_test.go +++ b/tests/integration/bank/keeper/deterministic_test.go @@ -83,7 +83,6 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index 80ad789be64f..1260dc1b0ad7 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -82,7 +82,6 @@ func initFixture(t testing.TB) *fixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -99,7 +98,7 @@ func initFixture(t testing.TB) *fixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String()) + stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr)) distrKeeper := distrkeeper.NewKeeper( cdc, runtime.NewKVStoreService(keys[distrtypes.StoreKey]), accountKeeper, bankKeeper, stakingKeeper, distrtypes.ModuleName, authority.String(), diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index 8aaad0bae527..0c17797754bf 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -104,7 +104,6 @@ func initFixture(t testing.TB) *fixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -121,7 +120,7 @@ func initFixture(t testing.TB) *fixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String()) + stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr)) slashingKeeper := slashingkeeper.NewKeeper(cdc, codec.NewLegacyAmino(), runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), stakingKeeper, authority.String()) diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index e3e4a54dc958..4e385e8f0d70 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -75,7 +75,6 @@ func initFixture(t testing.TB) *fixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -92,7 +91,7 @@ func initFixture(t testing.TB) *fixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String()) + stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr)) // set default staking params stakingKeeper.SetParams(newCtx, stakingtypes.DefaultParams()) diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index 46ebdeae52b7..d43c07f1df85 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -75,7 +75,6 @@ func initFixture(t testing.TB) *fixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -92,7 +91,7 @@ func initFixture(t testing.TB) *fixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String()) + stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String(), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), addresscodec.NewBech32Codec(sdk.Bech32PrefixConsAddr)) slashingKeeper := slashingkeeper.NewKeeper(cdc, &codec.LegacyAmino{}, runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), stakingKeeper, authority.String()) diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index 9f244b4cf858..f4958b1b86da 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -117,7 +117,6 @@ func initFixture(t testing.TB) *fixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -134,7 +133,7 @@ func initFixture(t testing.TB) *fixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[types.StoreKey]), accountKeeper, bankKeeper, authority.String()) + 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) bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index 81bd86370910..d9fbf630a250 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -90,7 +90,6 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { authtypes.ProtoBaseAccount, maccPerms, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), - addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authority.String(), ) @@ -107,7 +106,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { log.NewNopLogger(), ) - stakingKeeper := stakingkeeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[stakingtypes.StoreKey]), accountKeeper, bankKeeper, authority.String()) + 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) bankModule := bank.NewAppModule(cdc, bankKeeper, accountKeeper, nil) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 83712071b72f..b395324c1917 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -46,7 +46,6 @@ func Example() { authtypes.ProtoBaseAccount, map[string][]string{minttypes.ModuleName: {authtypes.Minter}}, addresscodec.NewBech32Codec("cosmos"), - addresscodec.NewBech32Codec("cosmosvaloper"), "cosmos", authority, ) @@ -136,7 +135,6 @@ func Example_oneModule() { authtypes.ProtoBaseAccount, map[string][]string{minttypes.ModuleName: {authtypes.Minter}}, addresscodec.NewBech32Codec("cosmos"), - addresscodec.NewBech32Codec("cosmosvaloper"), "cosmos", authority, ) diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index 3110ba9056cb..a1f5762fdf92 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -78,7 +78,7 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite { } suite.accountKeeper = keeper.NewAccountKeeper( - suite.encCfg.Codec, runtime.NewKVStoreService(key), types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), authcodec.NewBech32Codec("cosmosvaloper"), + suite.encCfg.Codec, runtime.NewKVStoreService(key), types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, types.NewModuleAddress("gov").String(), ) suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) diff --git a/x/auth/keeper/deterministic_test.go b/x/auth/keeper/deterministic_test.go index d172ead1eaeb..630fa173fd4c 100644 --- a/x/auth/keeper/deterministic_test.go +++ b/x/auth/keeper/deterministic_test.go @@ -74,7 +74,6 @@ func (suite *DeterministicTestSuite) SetupTest() { types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", types.NewModuleAddress("gov").String(), ) @@ -295,7 +294,6 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccounts() { types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", types.NewModuleAddress("gov").String(), ) @@ -343,7 +341,6 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccountByName() { types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", types.NewModuleAddress("gov").String(), ) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 1d2e688fc24f..393ed99907d3 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -56,9 +56,6 @@ type AccountKeeperI interface { // AddressCodec returns the account address codec. AddressCodec() address.Codec - - // ValidatorAddressCodec returns the validator address codec. - ValidatorAddressCodec() address.Codec } func NewAccountIndexes(sb *collections.SchemaBuilder) AccountsIndexes { @@ -86,8 +83,7 @@ func (a AccountsIndexes) IndexesList() []collections.Index[sdk.AccAddress, sdk.A // AccountKeeper encodes/decodes accounts using the go-amino (binary) // encoding/decoding library. type AccountKeeper struct { - addressCodec address.Codec - validatorAddressCodec address.Codec + addressCodec address.Codec storeService store.KVStoreService cdc codec.BinaryCodec @@ -118,7 +114,7 @@ var _ AccountKeeperI = &AccountKeeper{} // may use auth.Keeper to access the accounts permissions map. func NewAccountKeeper( cdc codec.BinaryCodec, storeService store.KVStoreService, proto func() sdk.AccountI, - maccPerms map[string][]string, ac, vac address.Codec, bech32Prefix, authority string, + maccPerms map[string][]string, ac address.Codec, bech32Prefix, authority string, ) AccountKeeper { permAddrs := make(map[string]types.PermissionsForAddress) for name, perms := range maccPerms { @@ -128,17 +124,16 @@ func NewAccountKeeper( sb := collections.NewSchemaBuilder(storeService) ak := AccountKeeper{ - addressCodec: ac, - validatorAddressCodec: vac, - bech32Prefix: bech32Prefix, - storeService: storeService, - proto: proto, - cdc: cdc, - permAddrs: permAddrs, - authority: authority, - Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), - AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"), - Accounts: collections.NewIndexedMap(sb, types.AddressStoreKeyPrefix, "accounts", sdk.AccAddressKey, codec.CollInterfaceValue[sdk.AccountI](cdc), NewAccountIndexes(sb)), + addressCodec: ac, + bech32Prefix: bech32Prefix, + storeService: storeService, + proto: proto, + cdc: cdc, + permAddrs: permAddrs, + authority: authority, + Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)), + AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"), + Accounts: collections.NewIndexedMap(sb, types.AddressStoreKeyPrefix, "accounts", sdk.AccAddressKey, codec.CollInterfaceValue[sdk.AccountI](cdc), NewAccountIndexes(sb)), } schema, err := sb.Build() if err != nil { @@ -159,11 +154,6 @@ func (ak AccountKeeper) AddressCodec() address.Codec { return ak.addressCodec } -// ValidatorAddressCodec returns the x/auth validator address codec. -func (ak AccountKeeper) ValidatorAddressCodec() address.Codec { - return ak.validatorAddressCodec -} - // Logger returns a module-specific logger. func (ak AccountKeeper) Logger(ctx context.Context) log.Logger { return sdk.UnwrapSDKContext(ctx).Logger().With("module", "x/"+types.ModuleName) diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 431adb9a6aff..8eff83d1dddb 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -66,7 +66,6 @@ func (suite *KeeperTestSuite) SetupTest() { types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", types.NewModuleAddress("gov").String(), ) diff --git a/x/auth/module.go b/x/auth/module.go index 171d8a3b5b4d..130f1d90d124 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -195,28 +195,22 @@ func init() { type AddressCodecInputs struct { depinject.In - Config *modulev1.Module - AddressCodecFactory func() address.Codec `optional:"true"` - ValidatorAddressCodecFactory func() types.ValidatorAddressCodec `optional:"true"` + Config *modulev1.Module + AddressCodecFactory func() address.Codec `optional:"true"` } // ProvideAddressCodec provides an address.Codec to the container for any // modules that want to do address string <> bytes conversion. -func ProvideAddressCodec(in AddressCodecInputs) (address.Codec, types.ValidatorAddressCodec) { - if in.AddressCodecFactory != nil && in.ValidatorAddressCodecFactory != nil { - return in.AddressCodecFactory(), in.ValidatorAddressCodecFactory() +func ProvideAddressCodec(in AddressCodecInputs) address.Codec { + if in.AddressCodecFactory != nil { + return in.AddressCodecFactory() } - if (in.AddressCodecFactory != nil && in.ValidatorAddressCodecFactory == nil) || - (in.AddressCodecFactory == nil && in.ValidatorAddressCodecFactory != nil) { - panic("either both or none of AddressCodecFactory and ValidatorAddressCodecFactory must be provided") + if in.Config.Bech32Prefix == "" { + panic("bech32 prefix cannot be empty") } - if in.Config.Bech32PrefixValidator == "" { - in.Config.Bech32PrefixValidator = fmt.Sprintf("%svaloper", in.Config.Bech32Prefix) - } - - return authcodec.NewBech32Codec(in.Config.Bech32Prefix), authcodec.NewBech32Codec(in.Config.Bech32PrefixValidator) + return authcodec.NewBech32Codec(in.Config.Bech32Prefix) } type ModuleInputs struct { @@ -227,7 +221,6 @@ type ModuleInputs struct { Cdc codec.Codec AddressCodec address.Codec - ValidatorAddressCodec types.ValidatorAddressCodec RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"` AccountI func() sdk.AccountI `optional:"true"` @@ -262,11 +255,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.AccountI = types.ProtoBaseAccount } - k := keeper.NewAccountKeeper( - in.Cdc, in.StoreService, in.AccountI, - maccPerms, in.AddressCodec, in.ValidatorAddressCodec, - in.Config.Bech32Prefix, authority.String(), - ) + k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, authority.String()) m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace) return ModuleOutputs{AccountKeeper: k, Module: m} diff --git a/x/auth/types/depinject_alias.go b/x/auth/types/depinject_alias.go deleted file mode 100644 index e77630eda3ca..000000000000 --- a/x/auth/types/depinject_alias.go +++ /dev/null @@ -1,6 +0,0 @@ -package types - -import "cosmossdk.io/core/address" - -// ValidatorAddressCodec is an alias for address.Codec for validator addresses. -type ValidatorAddressCodec address.Codec diff --git a/x/auth/vesting/msg_server_test.go b/x/auth/vesting/msg_server_test.go index 2b45cfeedfab..7ca30a1a7f91 100644 --- a/x/auth/vesting/msg_server_test.go +++ b/x/auth/vesting/msg_server_test.go @@ -59,7 +59,6 @@ func (s *VestingTestSuite) SetupTest() { authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", authtypes.NewModuleAddress("gov").String(), ) diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index 928b767ea170..74cdfe59c5a5 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -60,7 +60,6 @@ func (s *VestingAccountTestSuite) SetupTest() { authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), - authcodec.NewBech32Codec("cosmosvaloper"), "cosmos", authtypes.NewModuleAddress("gov").String(), ) diff --git a/x/group/migrations/v2/migrate_test.go b/x/group/migrations/v2/migrate_test.go index d494ff64e5a0..66810076dd37 100644 --- a/x/group/migrations/v2/migrate_test.go +++ b/x/group/migrations/v2/migrate_test.go @@ -79,7 +79,7 @@ func createGroupPolicies(ctx sdk.Context, storeKey storetypes.StoreKey, cdc code // createOldPolicyAccount re-creates the group policy account using a module account func createOldPolicyAccount(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec, policies []sdk.AccAddress) ([]*authtypes.ModuleAccount, group.AccountKeeper) { - accountKeeper := authkeeper.NewAccountKeeper(cdc, runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), authtypes.ProtoBaseAccount, nil, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), addresscodec.NewBech32Codec(sdk.Bech32PrefixValAddr), sdk.Bech32MainPrefix, authorityAddr.String()) + accountKeeper := authkeeper.NewAccountKeeper(cdc, runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), authtypes.ProtoBaseAccount, nil, addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), sdk.Bech32MainPrefix, authorityAddr.String()) oldPolicyAccounts := make([]*authtypes.ModuleAccount, len(policies)) for i, policyAddr := range policies { diff --git a/x/staking/keeper/keeper.go b/x/staking/keeper/keeper.go index 35c4804916f3..45948f72224c 100644 --- a/x/staking/keeper/keeper.go +++ b/x/staking/keeper/keeper.go @@ -6,6 +6,7 @@ import ( abci "github.com/cometbft/cometbft/abci/types" + addresscodec "cosmossdk.io/core/address" storetypes "cosmossdk.io/core/store" "cosmossdk.io/log" "cosmossdk.io/math" @@ -23,12 +24,14 @@ var _ types.DelegationSet = Keeper{} // Keeper of the x/staking store type Keeper struct { - storeService storetypes.KVStoreService - cdc codec.BinaryCodec - authKeeper types.AccountKeeper - bankKeeper types.BankKeeper - hooks types.StakingHooks - authority string + storeService storetypes.KVStoreService + cdc codec.BinaryCodec + authKeeper types.AccountKeeper + bankKeeper types.BankKeeper + hooks types.StakingHooks + authority string + validatorAddressCodec addresscodec.Codec + consensusAddressCodec addresscodec.Codec } // NewKeeper creates a new staking Keeper instance @@ -38,6 +41,8 @@ func NewKeeper( ak types.AccountKeeper, bk types.BankKeeper, authority string, + validatorAddressCodec addresscodec.Codec, + consensusAddressCodec addresscodec.Codec, ) *Keeper { // ensure bonded and not bonded module accounts are set if addr := ak.GetModuleAddress(types.BondedPoolName); addr == nil { @@ -53,13 +58,19 @@ func NewKeeper( panic("authority is not a valid acc address") } + if validatorAddressCodec == nil || consensusAddressCodec == nil { + panic("validator and/or consensus address codec are nil") + } + return &Keeper{ - storeService: storeService, - cdc: cdc, - authKeeper: ak, - bankKeeper: bk, - hooks: nil, - authority: authority, + storeService: storeService, + cdc: cdc, + authKeeper: ak, + bankKeeper: bk, + hooks: nil, + authority: authority, + validatorAddressCodec: validatorAddressCodec, + consensusAddressCodec: consensusAddressCodec, } } @@ -125,6 +136,16 @@ func (k Keeper) GetAuthority() string { return k.authority } +// ValidatorAddressCodec returns the app validator address codec. +func (k Keeper) ValidatorAddressCodec() addresscodec.Codec { + return k.validatorAddressCodec +} + +// ConsensusAddressCodec returns the app consensus address codec. +func (k Keeper) ConsensusAddressCodec() addresscodec.Codec { + return k.consensusAddressCodec +} + // SetValidatorUpdates sets the ABCI validator power updates for the current block. func (k Keeper) SetValidatorUpdates(ctx context.Context, valUpdates []abci.ValidatorUpdate) error { store := k.storeService.OpenKVStore(ctx) diff --git a/x/staking/keeper/keeper_test.go b/x/staking/keeper/keeper_test.go index ad931be545a6..a2047f45e1ff 100644 --- a/x/staking/keeper/keeper_test.go +++ b/x/staking/keeper/keeper_test.go @@ -55,7 +55,6 @@ func (s *KeeperTestSuite) SetupTest() { accountKeeper.EXPECT().GetModuleAddress(stakingtypes.BondedPoolName).Return(bondedAcc.GetAddress()) accountKeeper.EXPECT().GetModuleAddress(stakingtypes.NotBondedPoolName).Return(notBondedAcc.GetAddress()) accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes() - accountKeeper.EXPECT().ValidatorAddressCodec().Return(address.NewBech32Codec("cosmosvaloper")).AnyTimes() bankKeeper := stakingtestutil.NewMockBankKeeper(ctrl) @@ -64,7 +63,13 @@ func (s *KeeperTestSuite) SetupTest() { storeService, accountKeeper, bankKeeper, +<<<<<<< HEAD authtypes.NewModuleAddress(govtypes.ModuleName).String(), +======= + authtypes.NewModuleAddress(stakingtypes.GovModuleName).String(), + address.NewBech32Codec("cosmosvaloper"), + address.NewBech32Codec("cosmosvalcons"), +>>>>>>> e0be2b80f (feat(staking)!: add consensus and validator address codec in staking (#16959)) ) require.NoError(keeper.SetParams(ctx, stakingtypes.DefaultParams())) diff --git a/x/staking/keeper/val_state_change.go b/x/staking/keeper/val_state_change.go index a1d331d1b4d7..2df861a636c2 100644 --- a/x/staking/keeper/val_state_change.go +++ b/x/staking/keeper/val_state_change.go @@ -188,7 +188,7 @@ func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) (updates } // fetch the old power bytes - valAddrStr, err := k.authKeeper.ValidatorAddressCodec().BytesToString(valAddr) + valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr) if err != nil { return nil, err } @@ -456,7 +456,7 @@ func (k Keeper) getLastValidatorsByAddr(ctx context.Context) (validatorsByAddr, for ; iterator.Valid(); iterator.Next() { // extract the validator address from the key (prefix is 1-byte, addrLen is 1-byte) valAddr := types.AddressFromLastValidatorPowerKey(iterator.Key()) - valAddrStr, err := k.authKeeper.ValidatorAddressCodec().BytesToString(valAddr) + valAddrStr, err := k.validatorAddressCodec.BytesToString(valAddr) if err != nil { return nil, err } diff --git a/x/staking/module.go b/x/staking/module.go index 886d9fa45e2e..109c6209f17f 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/cobra" "golang.org/x/exp/maps" + authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1" modulev1 "cosmossdk.io/api/cosmos/staking/module/v1" "cosmossdk.io/core/appmodule" "cosmossdk.io/core/store" @@ -22,6 +23,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" @@ -198,18 +200,59 @@ func init() { appmodule.Register( &modulev1.Module{}, appmodule.Provide(ProvideModule), + appmodule.Provide(ProvideAddressCodec), appmodule.Invoke(InvokeSetStakingHooks), ) } +type AddressCodecInputs struct { + depinject.In + + AccountKeeper types.AccountKeeper + Config *modulev1.Module + AuthConfig *authmodulev1.Module + ValidatorAddressCodecFactory func() types.ValidatorAddressCodec `optional:"true"` + ConsensusAddressCodecFactory func() types.ConsensusAddressCodec `optional:"true"` +} + +// ProvideAddressCodec provides an address.Codec to the container for any +// modules that want to do address string <> bytes conversion. +func ProvideAddressCodec(in AddressCodecInputs) (types.ValidatorAddressCodec, types.ConsensusAddressCodec) { + if in.ValidatorAddressCodecFactory != nil && in.ConsensusAddressCodecFactory != nil { + return in.ValidatorAddressCodecFactory(), in.ConsensusAddressCodecFactory() + } + + if in.ValidatorAddressCodecFactory != nil || in.ConsensusAddressCodecFactory != nil { + panic("either both or none of validator and consensus address codecs must be provided") + } + + if in.AuthConfig.Bech32Prefix != "" { + if in.Config.Bech32PrefixValidator == "" { + in.Config.Bech32PrefixValidator = fmt.Sprintf("%svaloper", in.AuthConfig.Bech32Prefix) + } + + if in.Config.Bech32PrefixConsensus == "" { + in.Config.Bech32PrefixConsensus = fmt.Sprintf("%svalcons", in.AuthConfig.Bech32Prefix) + } + } + + if in.Config.Bech32PrefixValidator == "" || in.Config.Bech32PrefixConsensus == "" { + panic("bech32 prefixes for validator and/or consensus addresses must be provided (cannot fallback to auth bech32 prefixes)") + } + + return authcodec.NewBech32Codec(in.Config.Bech32PrefixValidator), authcodec.NewBech32Codec(in.Config.Bech32PrefixConsensus) +} + type ModuleInputs struct { depinject.In - Config *modulev1.Module - AccountKeeper types.AccountKeeper - BankKeeper types.BankKeeper - Cdc codec.Codec - StoreService store.KVStoreService + Config *modulev1.Module + ValidatorAddressCodec types.ValidatorAddressCodec + ConsensusAddressCodec types.ConsensusAddressCodec + AccountKeeper types.AccountKeeper + BankKeeper types.BankKeeper + Cdc codec.Codec + StoreService store.KVStoreService // LegacySubspace is used solely for migration of x/params managed parameters LegacySubspace exported.Subspace `optional:"true"` @@ -236,6 +279,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.AccountKeeper, in.BankKeeper, authority.String(), + in.ValidatorAddressCodec, + in.ConsensusAddressCodec, ) m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.LegacySubspace) return ModuleOutputs{StakingKeeper: k, Module: m} diff --git a/x/staking/testutil/expected_keepers_mocks.go b/x/staking/testutil/expected_keepers_mocks.go index dd180db4e380..e66027edd2e7 100644 --- a/x/staking/testutil/expected_keepers_mocks.go +++ b/x/staking/testutil/expected_keepers_mocks.go @@ -169,20 +169,6 @@ func (mr *MockAccountKeeperMockRecorder) SetModuleAccount(arg0, arg1 interface{} return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetModuleAccount", reflect.TypeOf((*MockAccountKeeper)(nil).SetModuleAccount), arg0, arg1) } -// ValidatorAddressCodec mocks base method. -func (m *MockAccountKeeper) ValidatorAddressCodec() address.Codec { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ValidatorAddressCodec") - ret0, _ := ret[0].(address.Codec) - return ret0 -} - -// ValidatorAddressCodec indicates an expected call of ValidatorAddressCodec. -func (mr *MockAccountKeeperMockRecorder) ValidatorAddressCodec() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidatorAddressCodec", reflect.TypeOf((*MockAccountKeeper)(nil).ValidatorAddressCodec)) -} - // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller diff --git a/x/staking/types/depinject_alias.go b/x/staking/types/depinject_alias.go new file mode 100644 index 000000000000..531712a8fa55 --- /dev/null +++ b/x/staking/types/depinject_alias.go @@ -0,0 +1,11 @@ +package types + +import "cosmossdk.io/core/address" + +type ( + // ValidatorAddressCodec is an alias for address.Codec for validator addresses. + ValidatorAddressCodec address.Codec + + // ConsensusAddressCodec is an alias for address.Codec for validator consensus addresses. + ConsensusAddressCodec address.Codec +) diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index 1b146c03d8cd..9ab221103179 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -18,7 +18,6 @@ type DistributionKeeper interface { // AccountKeeper defines the expected account keeper (noalias) type AccountKeeper interface { AddressCodec() address.Codec - ValidatorAddressCodec() address.Codec IterateAccounts(ctx context.Context, process func(sdk.AccountI) (stop bool)) GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI // only used for simulation