Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: export wired registries for reuse by integrators #417

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package cmd

import (
"os"

"cosmossdk.io/client/v2/autocli"
clientv2keyring "cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
pfm "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward"
pfmtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
soloclient "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
tmclient "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/noble-assets/noble/v8"
)

var (
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address.
Bech32PrefixAccAddr = "noble"
// Bech32PrefixAccPub defines the Bech32 prefix of an account's public key.
Bech32PrefixAccPub = Bech32PrefixAccAddr + "pub"
// Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address.
Bech32PrefixValAddr = Bech32PrefixAccAddr + "valoper"
// Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key.
Bech32PrefixValPub = Bech32PrefixAccAddr + "valoperpub"
// Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address.
Bech32PrefixConsAddr = Bech32PrefixAccAddr + "valcons"
// Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key.
Bech32PrefixConsPub = Bech32PrefixAccAddr + "valconspub"

txConfigOpts tx.ConfigOptions
autoCliOpts autocli.AppOptions
ModuleBasicManager module.BasicManager
ClientCtx client.Context
)

func Initialize() {
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub)
cfg.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub)
cfg.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub)
cfg.Seal()

if err := depinject.Inject(
depinject.Configs(noble.AppConfig(),
depinject.Supply(
log.NewNopLogger(),
),
johnletey marked this conversation as resolved.
Show resolved Hide resolved
depinject.Provide(
ProvideClientContext,
ProvideKeyring,
),
),
&txConfigOpts,
&autoCliOpts,
&ModuleBasicManager,
&ClientCtx,
); err != nil {
panic(err)
johnletey marked this conversation as resolved.
Show resolved Hide resolved
}

// Since the IBC modules don't support dependency injection, we need to
// manually register the modules on the client side.
// This needs to be removed after IBC supports App Wiring.
modules := map[string]appmodule.AppModule{
capabilitytypes.ModuleName: capability.AppModule{},
ibcexported.ModuleName: ibc.AppModule{},
icatypes.ModuleName: ica.AppModule{},
pfmtypes.ModuleName: pfm.AppModule{},
transfertypes.ModuleName: transfer.AppModule{},
tmclient.ModuleName: tmclient.AppModule{},
soloclient.ModuleName: soloclient.AppModule{},
}
for name, mod := range modules {
ModuleBasicManager[name] = module.CoreAppModuleBasicAdaptor(name, mod)
ModuleBasicManager[name].RegisterInterfaces(ClientCtx.InterfaceRegistry)
autoCliOpts.Modules[name] = mod
}
}

func ProvideClientContext(
appCodec codec.Codec,
interfaceRegistry codectypes.InterfaceRegistry,
txConfig client.TxConfig,
legacyAmino *codec.LegacyAmino,
) client.Context {
clientCtx := client.Context{}.
WithCodec(appCodec).
WithInterfaceRegistry(interfaceRegistry).
WithTxConfig(txConfig).
WithLegacyAmino(legacyAmino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(noble.DefaultNodeHome).
WithViper("") // env variable prefix

// Read the config again to overwrite the default values with the values from the config file
clientCtx, _ = config.ReadFromClientConfig(clientCtx)
johnletey marked this conversation as resolved.
Show resolved Hide resolved

return clientCtx
}

func ProvideKeyring(clientCtx client.Context, addressCodec address.Codec) (clientv2keyring.Keyring, error) {
kb, err := client.NewKeyringFromBackend(clientCtx, clientCtx.Keyring.Backend())
if err != nil {
return nil, err
}

return keyring.NewAutoCLIKeyring(kb)
}
36 changes: 0 additions & 36 deletions cmd/nobled/main.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,15 @@
// Copyright 2024 NASD Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/noble-assets/noble/v8"
"github.com/noble-assets/noble/v8/cmd"
)

var (
// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address.
Bech32PrefixAccAddr = "noble"
// Bech32PrefixAccPub defines the Bech32 prefix of an account's public key.
Bech32PrefixAccPub = Bech32PrefixAccAddr + "pub"
// Bech32PrefixValAddr defines the Bech32 prefix of a validator's operator address.
Bech32PrefixValAddr = Bech32PrefixAccAddr + "valoper"
// Bech32PrefixValPub defines the Bech32 prefix of a validator's operator public key.
Bech32PrefixValPub = Bech32PrefixAccAddr + "valoperpub"
// Bech32PrefixConsAddr defines the Bech32 prefix of a consensus node address.
Bech32PrefixConsAddr = Bech32PrefixAccAddr + "valcons"
// Bech32PrefixConsPub defines the Bech32 prefix of a consensus node public key.
Bech32PrefixConsPub = Bech32PrefixAccAddr + "valconspub"
)

func main() {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(Bech32PrefixAccAddr, Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(Bech32PrefixValAddr, Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(Bech32PrefixConsAddr, Bech32PrefixConsPub)
config.Seal()

rootCmd := cmd.NewRootCmd()
if err := svrcmd.Execute(rootCmd, "", noble.DefaultNodeHome); err != nil {
fmt.Fprintln(rootCmd.OutOrStderr(), err)
Expand Down
107 changes: 5 additions & 102 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,21 @@
package cmd

import (
"os"

"cosmossdk.io/client/v2/autocli"
clientv2keyring "cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
"github.com/cosmos/cosmos-sdk/x/auth/types"
pfm "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward"
pfmtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8/packetforward/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ica "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
soloclient "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
tmclient "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
"github.com/noble-assets/noble/v8"
"github.com/spf13/cobra"
)

// NewRootCmd creates a new root command for nobled. It is called once in the main function.
func NewRootCmd() *cobra.Command {
var (
txConfigOpts tx.ConfigOptions
autoCliOpts autocli.AppOptions
moduleBasicManager module.BasicManager
clientCtx client.Context
)

if err := depinject.Inject(
depinject.Configs(noble.AppConfig(),
depinject.Supply(
log.NewNopLogger(),
),
depinject.Provide(
ProvideClientContext,
ProvideKeyring,
),
),
&txConfigOpts,
&autoCliOpts,
&moduleBasicManager,
&clientCtx,
); err != nil {
panic(err)
}
Initialize()
johnletey marked this conversation as resolved.
Show resolved Hide resolved

rootCmd := &cobra.Command{
Use: "nobled",
Expand All @@ -86,8 +38,8 @@ func NewRootCmd() *cobra.Command {
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

clientCtx = clientCtx.WithCmdContext(cmd.Context())
clientCtx, err := client.ReadPersistentCommandFlags(clientCtx, cmd.Flags())
ClientCtx = ClientCtx.WithCmdContext(cmd.Context())
clientCtx, err := client.ReadPersistentCommandFlags(ClientCtx, cmd.Flags())
johnletey marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down Expand Up @@ -116,67 +68,18 @@ func NewRootCmd() *cobra.Command {

// overwrite the minimum gas price from the app configuration
srvCfg := serverconfig.DefaultConfig()
srvCfg.MinGasPrices = "0uusdc"
srvCfg.MinGasPrices = "0uusdc,0ausdy,0ueure"
johnletey marked this conversation as resolved.
Show resolved Hide resolved
srvCfg.API.Enable = true

return server.InterceptConfigsPreRunHandler(cmd, serverconfig.DefaultConfigTemplate, srvCfg, cmtcfg.DefaultConfig())
},
}

// Since the IBC modules don't support dependency injection, we need to
// manually register the modules on the client side.
// This needs to be removed after IBC supports App Wiring.
modules := map[string]appmodule.AppModule{
capabilitytypes.ModuleName: capability.AppModule{},
ibcexported.ModuleName: ibc.AppModule{},
icatypes.ModuleName: ica.AppModule{},
pfmtypes.ModuleName: pfm.AppModule{},
transfertypes.ModuleName: transfer.AppModule{},
tmclient.ModuleName: tmclient.AppModule{},
soloclient.ModuleName: soloclient.AppModule{},
}
for name, mod := range modules {
moduleBasicManager[name] = module.CoreAppModuleBasicAdaptor(name, mod)
moduleBasicManager[name].RegisterInterfaces(clientCtx.InterfaceRegistry)
autoCliOpts.Modules[name] = mod
}

initRootCmd(rootCmd, clientCtx.TxConfig, moduleBasicManager)
initRootCmd(rootCmd, ClientCtx.TxConfig, ModuleBasicManager)
johnletey marked this conversation as resolved.
Show resolved Hide resolved

if err := autoCliOpts.EnhanceRootCommand(rootCmd); err != nil {
panic(err)
}

return rootCmd
}

func ProvideClientContext(
appCodec codec.Codec,
interfaceRegistry codectypes.InterfaceRegistry,
txConfig client.TxConfig,
legacyAmino *codec.LegacyAmino,
) client.Context {
clientCtx := client.Context{}.
WithCodec(appCodec).
WithInterfaceRegistry(interfaceRegistry).
WithTxConfig(txConfig).
WithLegacyAmino(legacyAmino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(noble.DefaultNodeHome).
WithViper("") // env variable prefix

// Read the config again to overwrite the default values with the values from the config file
clientCtx, _ = config.ReadFromClientConfig(clientCtx)

return clientCtx
}

func ProvideKeyring(clientCtx client.Context, addressCodec address.Codec) (clientv2keyring.Keyring, error) {
kb, err := client.NewKeyringFromBackend(clientCtx, clientCtx.Keyring.Backend())
if err != nil {
return nil, err
}

return keyring.NewAutoCLIKeyring(kb)
}