feat(staking)!: add consensus and validator address codec in staking (#16959)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
This commit is contained in:
Julien Robert
2023-07-13 09:08:27 +00:00
committed by GitHub
co-authored by Facundo Medica
parent b1ac5768f7
commit e0be2b80fc
38 changed files with 185 additions and 145 deletions
+50 -5
View File
@@ -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"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
@@ -197,18 +199,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"`
@@ -235,6 +278,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}