Skip to content

Commit

Permalink
refactor(core): move amino registrar and drop legacy package (#21531)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Sep 5, 2024
1 parent 30688b0 commit a57b254
Show file tree
Hide file tree
Showing 59 changed files with 234 additions and 276 deletions.
4 changes: 2 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,11 @@ The signature of the extension interface `HasRegisterInterfaces` has been change
+func (AppModule) RegisterInterfaces(registry registry.InterfaceRegistrar) {
```

The signature of the extension interface `HasAminoCodec` has been changed to accept a `cosmossdk.io/core/legacy.Amino` instead of a `codec.LegacyAmino`. Modules should update their `HasAminoCodec` implementation to accept a `cosmossdk.io/core/legacy.Amino` interface.
The signature of the extension interface `HasAminoCodec` has been changed to accept a `cosmossdk.io/core/registry.AminoRegistrar` instead of a `codec.LegacyAmino`. Modules should update their `HasAminoCodec` implementation to accept a `cosmossdk.io/core/registry.AminoRegistrar` interface.

```diff
-func (AppModule) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
+func (AppModule) RegisterLegacyAminoCodec(cdc legacy.Amino) {
+func (AppModule) RegisterLegacyAminoCodec(registrar registry.AminoRegistrar) {
```

##### Simulation
Expand Down
12 changes: 6 additions & 6 deletions codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
cmttypes "github.com/cometbft/cometbft/types"
"github.com/tendermint/go-amino"

"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"

"github.com/cosmos/cosmos-sdk/codec/types"
)
Expand All @@ -25,17 +25,17 @@ func (cdc *LegacyAmino) Seal() {
cdc.Amino.Seal()
}

var _ legacy.Amino = &LegacyAmino{}
var _ registry.AminoRegistrar = &LegacyAmino{}

func NewLegacyAmino() *LegacyAmino {
return &LegacyAmino{amino.NewCodec()}
}

// RegisterEvidences registers CometBFT evidence types with the provided Amino
// codec.
func RegisterEvidences(cdc legacy.Amino) {
cdc.RegisterInterface((*cmttypes.Evidence)(nil), nil)
cdc.RegisterConcrete(&cmttypes.DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence")
func RegisterEvidences(registrar registry.AminoRegistrar) {
registrar.RegisterInterface((*cmttypes.Evidence)(nil), nil)
registrar.RegisterConcrete(&cmttypes.DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence")
}

// MarshalJSONIndent provides a utility for indented JSON encoding of an object
Expand Down Expand Up @@ -179,7 +179,7 @@ func (*LegacyAmino) UnpackAny(*types.Any, interface{}) error {
return errors.New("AminoCodec can't handle unpack protobuf Any's")
}

func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *legacy.InterfaceOptions) {
func (cdc *LegacyAmino) RegisterInterface(ptr interface{}, iopts *registry.AminoInterfaceOptions) {
if iopts == nil {
cdc.Amino.RegisterInterface(ptr, nil)
} else {
Expand Down
3 changes: 1 addition & 2 deletions codec/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
stakingmodulev1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/depinject"
"cosmossdk.io/x/tx/signing"
Expand Down Expand Up @@ -45,7 +44,7 @@ func ProvideInterfaceRegistry(
return interfaceRegistry, interfaceRegistry, nil
}

func ProvideLegacyAmino() legacy.Amino {
func ProvideLegacyAmino() registry.AminoRegistrar {
return NewLegacyAmino()
}

Expand Down
6 changes: 3 additions & 3 deletions codec/legacy/amino_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package legacy
import (
"fmt"

"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// RegisterAminoMsg first checks that the msgName is <40 chars
// (else this would break ledger nano signing: https://github.com/cosmos/cosmos-sdk/issues/10870),
// then registers the concrete msg type with amino.
func RegisterAminoMsg(cdc legacy.Amino, msg sdk.Msg, msgName string) {
func RegisterAminoMsg(registrar registry.AminoRegistrar, msg sdk.Msg, msgName string) {
if len(msgName) > 39 {
panic(fmt.Errorf("msg name %s is too long to be registered with amino", msgName))
}
cdc.RegisterConcrete(msg, msgName)
registrar.RegisterConcrete(msg, msgName)
}
4 changes: 2 additions & 2 deletions core/appmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
)

// AppModule is a tag interface for app module implementations to use as a basis
Expand Down Expand Up @@ -68,5 +68,5 @@ type HasPrecommit interface {
// HasAminoCodec is an extension interface that module must implement to support JSON encoding and decoding of its types
// through amino. This is used in genesis & the CLI client.
type HasAminoCodec interface {
RegisterLegacyAminoCodec(legacy.Amino)
RegisterLegacyAminoCodec(registry.AminoRegistrar)
}
16 changes: 0 additions & 16 deletions core/legacy/amino.go

This file was deleted.

16 changes: 16 additions & 0 deletions core/registry/amino.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package registry

// AminoRegistrar is an interface that allow to register concrete types and interfaces with the Amino codec.
type AminoRegistrar interface {
// RegisterInterface registers an interface and its concrete type with the Amino codec.
RegisterInterface(interfacePtr any, iopts *AminoInterfaceOptions)

// RegisterConcrete registers a concrete type with the Amino codec.
RegisterConcrete(cdcType interface{}, name string)
}

// AminoInterfaceOptions defines options for registering an interface with the Amino codec.
type AminoInterfaceOptions struct {
Priority []string // Disamb priority.
AlwaysDisambiguate bool // If true, include disamb for all types.
}
File renamed without changes.
27 changes: 13 additions & 14 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package codec
import (
"github.com/cometbft/cometbft/crypto/sr25519"

"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"

bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand All @@ -14,24 +14,23 @@ import (

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc legacy.Amino) {
cdc.RegisterInterface((*cryptotypes.PubKey)(nil), nil)
cdc.RegisterConcrete(sr25519.PubKey{},
func RegisterCrypto(registrar registry.AminoRegistrar) {
registrar.RegisterInterface((*cryptotypes.PubKey)(nil), nil)
registrar.RegisterConcrete(sr25519.PubKey{},
sr25519.PubKeyName)
cdc.RegisterConcrete(&ed25519.PubKey{},
registrar.RegisterConcrete(&ed25519.PubKey{},
ed25519.PubKeyName)
cdc.RegisterConcrete(&secp256k1.PubKey{},
registrar.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName)
cdc.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName)
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName)
registrar.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute)

cdc.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
cdc.RegisterConcrete(sr25519.PrivKey{},
registrar.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
registrar.RegisterConcrete(sr25519.PrivKey{},
sr25519.PrivKeyName)
cdc.RegisterConcrete(&ed25519.PrivKey{},
registrar.RegisterConcrete(&ed25519.PrivKey{},
ed25519.PrivKeyName)
cdc.RegisterConcrete(&secp256k1.PrivKey{},
registrar.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName)
cdc.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName)
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName)
}
2 changes: 1 addition & 1 deletion docs/build/building-modules/01-module-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The usage of extension interfaces allows modules to define only the functionalit
https://github.com/cosmos/cosmos-sdk/blob/eee5e21e1c8d0995b6d4f83b7f55ec0b58d27ba7/core/appmodule/module.go#L74-L78
```

* `RegisterLegacyAminoCodec(*codec.LegacyAmino)`: Registers the `amino` codec for the module, which is used to marshal and unmarshal structs to/from `[]byte` in order to persist them in the module's `KVStore`.
* `RegisterLegacyAminoCodec(registry.AminoRegistrar)`: Registers the `amino` codec for the module, which is used to marshal and unmarshal structs to/from `[]byte` in order to persist them in the module's `KVStore`.

### `HasRegisterInterfaces`

Expand Down
4 changes: 2 additions & 2 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

Expand Down Expand Up @@ -50,7 +50,7 @@ type App struct {
storeKeys []storetypes.StoreKey
interfaceRegistry codectypes.InterfaceRegistry
cdc codec.Codec
amino legacy.Amino
amino registry.AminoRegistrar
baseAppOptions []BaseAppOption
msgServiceRouter *baseapp.MsgServiceRouter
grpcQueryRouter *baseapp.GRPCQueryRouter
Expand Down
6 changes: 3 additions & 3 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
Expand Down Expand Up @@ -112,7 +112,7 @@ func init() {

func ProvideApp(
interfaceRegistry codectypes.InterfaceRegistry,
amino legacy.Amino,
amino registry.AminoRegistrar,
protoCodec *codec.ProtoCodec,
) (
*AppBuilder,
Expand Down Expand Up @@ -159,7 +159,7 @@ type AppInputs struct {
ModuleManager *module.Manager
BaseAppOptions []BaseAppOption
InterfaceRegistry codectypes.InterfaceRegistry
LegacyAmino legacy.Amino
LegacyAmino registry.AminoRegistrar
AppOptions servertypes.AppOptions `optional:"true"` // can be nil in client wiring
}

Expand Down
3 changes: 1 addition & 2 deletions runtime/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
gogoproto "github.com/cosmos/gogoproto/proto"

runtimev2 "cosmossdk.io/api/cosmos/app/runtime/v2"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
Expand Down Expand Up @@ -41,7 +40,7 @@ type App[T transaction.Tx] struct {
// modules configuration
storeKeys []string
interfaceRegistrar registry.InterfaceRegistrar
amino legacy.Amino
amino registry.AminoRegistrar
moduleManager *MM[T]

// GRPCMethodsToMessageMap maps gRPC method name to a function that decodes the request
Expand Down
5 changes: 2 additions & 3 deletions runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
cosmosmsg "cosmossdk.io/api/cosmos/msg/v1"
"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
"cosmossdk.io/log"
Expand Down Expand Up @@ -85,10 +84,10 @@ func (m *MM[T]) Modules() map[string]appmodulev2.AppModule {
}

// RegisterLegacyAminoCodec registers all module codecs
func (m *MM[T]) RegisterLegacyAminoCodec(cdc legacy.Amino) {
func (m *MM[T]) RegisterLegacyAminoCodec(registrar registry.AminoRegistrar) {
for _, b := range m.modules {
if mod, ok := b.(appmodule.HasAminoCodec); ok {
mod.RegisterLegacyAminoCodec(cdc)
mod.RegisterLegacyAminoCodec(registrar)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
Expand Down Expand Up @@ -107,7 +106,7 @@ func init() {

func ProvideAppBuilder[T transaction.Tx](
interfaceRegistrar registry.InterfaceRegistrar,
amino legacy.Amino,
amino registry.AminoRegistrar,
) (
*AppBuilder[T],
*stf.MsgRouterBuilder,
Expand Down Expand Up @@ -146,7 +145,7 @@ type AppInputs struct {
AppBuilder *AppBuilder[transaction.Tx]
ModuleManager *MM[transaction.Tx]
InterfaceRegistrar registry.InterfaceRegistrar
LegacyAmino legacy.Amino
LegacyAmino registry.AminoRegistrar
Logger log.Logger
Viper *viper.Viper `optional:"true"` // can be nil in client wiring
}
Expand Down
4 changes: 2 additions & 2 deletions simapp/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
clienthelpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
Expand Down Expand Up @@ -65,7 +65,7 @@ var (
// capabilities aren't needed for testing.
type SimApp struct {
*runtime.App
legacyAmino legacy.Amino
legacyAmino registry.AminoRegistrar
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry
Expand Down
4 changes: 2 additions & 2 deletions simapp/simd/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
stakingv1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/address"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/simapp"
Expand Down Expand Up @@ -100,7 +100,7 @@ func ProvideClientContext(
appCodec codec.Codec,
interfaceRegistry codectypes.InterfaceRegistry,
txConfigOpts tx.ConfigOptions,
legacyAmino legacy.Amino,
legacyAmino registry.AminoRegistrar,
addressCodec address.Codec,
validatorAddressCodec address.ValidatorAddressCodec,
consensusAddressCodec address.ConsensusAddressCodec,
Expand Down
4 changes: 2 additions & 2 deletions simapp/v2/app_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"

clienthelpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
Expand Down Expand Up @@ -47,7 +47,7 @@ var DefaultNodeHome string
// capabilities aren't needed for testing.
type SimApp[T transaction.Tx] struct {
*runtime.App[T]
legacyAmino legacy.Amino
legacyAmino registry.AminoRegistrar
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry
Expand Down
6 changes: 3 additions & 3 deletions simapp/v2/simdv2/cmd/root_di.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/address"
"cosmossdk.io/core/legacy"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
Expand Down Expand Up @@ -99,7 +99,7 @@ func ProvideClientContext(
appCodec codec.Codec,
interfaceRegistry codectypes.InterfaceRegistry,
txConfigOpts tx.ConfigOptions,
legacyAmino legacy.Amino,
legacyAmino registry.AminoRegistrar,
addressCodec address.Codec,
validatorAddressCodec address.ValidatorAddressCodec,
consensusAddressCodec address.ConsensusAddressCodec,
Expand All @@ -108,7 +108,7 @@ func ProvideClientContext(

amino, ok := legacyAmino.(*codec.LegacyAmino)
if !ok {
panic("legacy.Amino must be an *codec.LegacyAmino instance for legacy ClientContext")
panic("registry.AminoRegistrar must be an *codec.LegacyAmino instance for legacy ClientContext")
}

clientCtx := client.Context{}.
Expand Down
Loading

0 comments on commit a57b254

Please sign in to comment.