From e4469098bc783f4bfc6dc747710289d0f799a1a4 Mon Sep 17 00:00:00 2001 From: ocnc-two Date: Sun, 25 Aug 2024 13:56:07 +0300 Subject: [PATCH] client --- mod/cli/go.mod | 2 +- mod/cli/pkg/commands/genesis/collect.go | 4 +- mod/cli/pkg/commands/genesis/deposit.go | 6 +- mod/cli/pkg/commands/genesis/payload.go | 4 +- mod/cli/pkg/commands/server/rollback.go | 8 +-- mod/cli/pkg/commands/server/start.go | 8 +-- mod/cli/pkg/config/server.go | 9 ++- mod/cli/pkg/context/cmd.go | 69 ++++++++++++++++++++++ mod/cli/pkg/context/keys.go | 31 ++++++++++ mod/consensus/go.mod | 1 + mod/consensus/go.sum | 2 + mod/consensus/pkg/cometbft/cli/commands.go | 16 ++--- 12 files changed, 131 insertions(+), 29 deletions(-) create mode 100644 mod/cli/pkg/context/cmd.go create mode 100644 mod/cli/pkg/context/keys.go diff --git a/mod/cli/go.mod b/mod/cli/go.mod index a13f211756..72f1532495 100644 --- a/mod/cli/go.mod +++ b/mod/cli/go.mod @@ -21,7 +21,6 @@ replace ( ) require ( - cosmossdk.io/core v1.0.0 cosmossdk.io/depinject v1.0.0 github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240821053614-036c5d2945f0 @@ -42,6 +41,7 @@ require ( ) require ( + cosmossdk.io/core v1.0.0 // indirect cosmossdk.io/errors/v2 v2.0.0-20240731132947-df72853b3ca5 // indirect cosmossdk.io/log v1.4.1 // indirect cosmossdk.io/schema v0.1.1 // indirect diff --git a/mod/cli/pkg/commands/genesis/collect.go b/mod/cli/pkg/commands/genesis/collect.go index e7b9b62841..d11b4b13ae 100644 --- a/mod/cli/pkg/commands/genesis/collect.go +++ b/mod/cli/pkg/commands/genesis/collect.go @@ -25,10 +25,10 @@ import ( "path/filepath" "strings" + "github.com/berachain/beacon-kit/mod/cli/pkg/context" "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/x/genutil" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/spf13/afero" @@ -42,7 +42,7 @@ func CollectGenesisDepositsCmd() *cobra.Command { Use: "collect-premined-deposits", Short: "adds a validator to the genesis file", RunE: func(cmd *cobra.Command, _ []string) error { - config := client.GetConfigFromCmd(cmd) + config := context.GetConfigFromCmd(cmd) appGenesis, err := genutiltypes.AppGenesisFromFile( config.GenesisFile(), diff --git a/mod/cli/pkg/commands/genesis/deposit.go b/mod/cli/pkg/commands/genesis/deposit.go index 0373fc6ff6..bef7984ca7 100644 --- a/mod/cli/pkg/commands/genesis/deposit.go +++ b/mod/cli/pkg/commands/genesis/deposit.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" + "github.com/berachain/beacon-kit/mod/cli/pkg/context" "github.com/berachain/beacon-kit/mod/cli/pkg/utils/parser" "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" "github.com/berachain/beacon-kit/mod/errors" @@ -35,7 +36,6 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/primitives/pkg/version" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/x/genutil" "github.com/spf13/afero" @@ -49,7 +49,7 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command { Use: "add-premined-deposit", Short: "adds a validator to the genesis file", RunE: func(cmd *cobra.Command, _ []string) error { - config := client.GetConfigFromCmd(cmd) + config := context.GetConfigFromCmd(cmd) _, valPubKey, err := genutil.InitializeNodeValidatorFiles( config, crypto.CometBLSType, @@ -69,7 +69,7 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command { // Get the BLS signer. blsSigner, err := components.ProvideBlsSigner( components.BlsSignerInput{ - AppOpts: client.GetViperFromCmd(cmd), + AppOpts: context.GetViperFromCmd(cmd), }, ) if err != nil { diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index 2e2fb5d7ac..deccc019ad 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -23,6 +23,7 @@ package genesis import ( "unsafe" + "github.com/berachain/beacon-kit/mod/cli/pkg/context" "github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives" "github.com/berachain/beacon-kit/mod/errors" @@ -32,7 +33,6 @@ import ( "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/json" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" "github.com/berachain/beacon-kit/mod/primitives/pkg/version" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/x/genutil" genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" "github.com/spf13/afero" @@ -65,7 +65,7 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { nil, ).ExecutionPayload - config := client.GetConfigFromCmd(cmd) + config := context.GetConfigFromCmd(cmd) appGenesis, err := genutiltypes.AppGenesisFromFile( config.GenesisFile(), diff --git a/mod/cli/pkg/commands/server/rollback.go b/mod/cli/pkg/commands/server/rollback.go index e721ffaf6a..d79cc31d57 100644 --- a/mod/cli/pkg/commands/server/rollback.go +++ b/mod/cli/pkg/commands/server/rollback.go @@ -26,11 +26,11 @@ import ( "cosmossdk.io/store" types "github.com/berachain/beacon-kit/mod/cli/pkg/commands/server/types" + clicontext "github.com/berachain/beacon-kit/mod/cli/pkg/context" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/storage/pkg/db" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/client" "github.com/spf13/cobra" ) @@ -60,9 +60,9 @@ restarting CometBFT the transactions in block n will be re-executed against the application. `, RunE: func(cmd *cobra.Command, _ []string) error { - v := client.GetViperFromCmd(cmd) - logger := client.GetLoggerFromCmd(cmd).Impl().(LoggerT) - cfg := client.GetConfigFromCmd(cmd) + v := clicontext.GetViperFromCmd(cmd) + logger := clicontext.GetLoggerFromCmd[LoggerT](cmd) + cfg := clicontext.GetConfigFromCmd(cmd) db, err := db.OpenDB(cfg.RootDir, dbm.PebbleDBBackend) if err != nil { diff --git a/mod/cli/pkg/commands/server/start.go b/mod/cli/pkg/commands/server/start.go index 111198fddf..134d7f246f 100644 --- a/mod/cli/pkg/commands/server/start.go +++ b/mod/cli/pkg/commands/server/start.go @@ -26,11 +26,11 @@ import ( pruningtypes "cosmossdk.io/store/pruning/types" types "github.com/berachain/beacon-kit/mod/cli/pkg/commands/server/types" + clicontext "github.com/berachain/beacon-kit/mod/cli/pkg/context" "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/storage/pkg/db" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/client" "github.com/spf13/cobra" ) @@ -105,9 +105,9 @@ custom: allow pruning options to be manually specified through 'pruning-keep-rec `, RunE: func(cmd *cobra.Command, _ []string) error { - logger := client.GetLoggerFromCmd(cmd).Impl().(LoggerT) - cfg := client.GetConfigFromCmd(cmd) - v := client.GetViperFromCmd(cmd) + logger := clicontext.GetLoggerFromCmd[LoggerT](cmd) + cfg := clicontext.GetConfigFromCmd(cmd) + v := clicontext.GetViperFromCmd(cmd) _, err := GetPruningOptionsFromFlags(v) if err != nil { return err diff --git a/mod/cli/pkg/config/server.go b/mod/cli/pkg/config/server.go index ad516fc4f7..41ea31aba4 100644 --- a/mod/cli/pkg/config/server.go +++ b/mod/cli/pkg/config/server.go @@ -28,12 +28,11 @@ import ( "path/filepath" "strings" - corectx "cosmossdk.io/core/context" + clicontext "github.com/berachain/beacon-kit/mod/cli/pkg/context" sdklog "github.com/berachain/beacon-kit/mod/consensus/pkg/cometbft/service/log" "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/log" cmtcfg "github.com/cometbft/cometbft/config" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -60,7 +59,7 @@ func SetupCommand[ } if err := handleConfigs( - client.GetViperFromCmd(cmd), + clicontext.GetViperFromCmd(cmd), appTemplate, appConfig, cmtConfig, ); err != nil { return err @@ -93,9 +92,9 @@ func InitializeCmd[ } ctx := cmd.Context() - ctx = context.WithValue(ctx, corectx.ViperContextKey, viper) + ctx = context.WithValue(ctx, clicontext.ViperContextKey, viper) ctx = context.WithValue( - ctx, corectx.LoggerContextKey, sdklog.WrapSDKLogger(logger), + ctx, clicontext.LoggerContextKey, sdklog.WrapSDKLogger(logger), ) cmd.SetContext(ctx) diff --git a/mod/cli/pkg/context/cmd.go b/mod/cli/pkg/context/cmd.go new file mode 100644 index 0000000000..c002e8e808 --- /dev/null +++ b/mod/cli/pkg/context/cmd.go @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package context + +import ( + "github.com/berachain/beacon-kit/mod/log" + "github.com/berachain/beacon-kit/mod/log/pkg/phuslu" + cmtcfg "github.com/cometbft/cometbft/config" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func GetViperFromCmd(cmd *cobra.Command) *viper.Viper { + value := cmd.Context().Value(ViperContextKey) + v, ok := value.(*viper.Viper) + if !ok { + return viper.New() + } + return v +} + +func GetLoggerFromCmd[ + LoggerT log.AdvancedLogger[LoggerT], +](cmd *cobra.Command) LoggerT { + v := cmd.Context().Value(LoggerContextKey) + logger, ok := v.(LoggerT) + if !ok { + return any(phuslu.NewLogger(cmd.OutOrStdout(), nil)).(LoggerT) + } + return logger +} + +func GetConfigFromCmd(cmd *cobra.Command) *cmtcfg.Config { + v := cmd.Context().Value(ViperContextKey) + viper, ok := v.(*viper.Viper) + if !ok { + return cmtcfg.DefaultConfig() + } + return GetConfigFromViper(viper) +} + +func GetConfigFromViper(v *viper.Viper) *cmtcfg.Config { + conf := cmtcfg.DefaultConfig() + err := v.Unmarshal(conf) + rootDir := v.GetString(flags.FlagHome) + if err != nil { + return cmtcfg.DefaultConfig().SetRoot(rootDir) + } + return conf.SetRoot(rootDir) +} diff --git a/mod/cli/pkg/context/keys.go b/mod/cli/pkg/context/keys.go new file mode 100644 index 0000000000..8b996289f9 --- /dev/null +++ b/mod/cli/pkg/context/keys.go @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package context + +type ( + loggerContextKey struct{} + viperContextKey struct{} +) + +var ( + LoggerContextKey loggerContextKey + ViperContextKey viperContextKey +) diff --git a/mod/consensus/go.mod b/mod/consensus/go.mod index 2e73e80766..bbe9e30617 100644 --- a/mod/consensus/go.mod +++ b/mod/consensus/go.mod @@ -166,6 +166,7 @@ require ( github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/petermattis/goid v0.0.0-20240607163614-bb94eb51e7a7 // indirect + github.com/phuslu/log v1.0.110 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.20.1 // indirect diff --git a/mod/consensus/go.sum b/mod/consensus/go.sum index ae9d253953..88950479c4 100644 --- a/mod/consensus/go.sum +++ b/mod/consensus/go.sum @@ -481,6 +481,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o= github.com/petermattis/goid v0.0.0-20240607163614-bb94eb51e7a7 h1:CtBLeckhC0zAXgp5V8uR30CNYH0JgCJoxCg5+6i2zQk= github.com/petermattis/goid v0.0.0-20240607163614-bb94eb51e7a7/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4= +github.com/phuslu/log v1.0.110 h1:9WQnpL1/CBi3IwZaVadYnI/i0bgobTvit2ayXIgSg4c= +github.com/phuslu/log v1.0.110/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= diff --git a/mod/consensus/pkg/cometbft/cli/commands.go b/mod/consensus/pkg/cometbft/cli/commands.go index 7e62bc166d..2173b08df2 100644 --- a/mod/consensus/pkg/cometbft/cli/commands.go +++ b/mod/consensus/pkg/cometbft/cli/commands.go @@ -25,9 +25,9 @@ import ( "cosmossdk.io/store" types "github.com/berachain/beacon-kit/mod/cli/pkg/commands/server/types" - "github.com/berachain/beacon-kit/mod/log" - + clicontext "github.com/berachain/beacon-kit/mod/cli/pkg/context" service "github.com/berachain/beacon-kit/mod/consensus/pkg/cometbft/service" + "github.com/berachain/beacon-kit/mod/log" "github.com/berachain/beacon-kit/mod/storage/pkg/db" cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands" cmtcfg "github.com/cometbft/cometbft/config" @@ -124,7 +124,7 @@ func ShowNodeIDCmd() *cobra.Command { Use: "show-node-id", Short: "Show this node's ID", RunE: func(cmd *cobra.Command, args []string) error { - cfg := client.GetConfigFromCmd(cmd) + cfg := clicontext.GetConfigFromCmd(cmd) nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile()) if err != nil { return err @@ -142,7 +142,7 @@ func ShowValidatorCmd() *cobra.Command { Use: "show-validator", Short: "Show this node's CometBFT validator info", RunE: func(cmd *cobra.Command, args []string) error { - cfg := client.GetConfigFromCmd(cmd) + cfg := clicontext.GetConfigFromCmd(cmd) privValidator := pvm.LoadFilePV( cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), @@ -177,7 +177,7 @@ func ShowAddressCmd() *cobra.Command { Use: "show-address", Short: "Shows this node's CometBFT validator consensus address", RunE: func(cmd *cobra.Command, args []string) error { - cfg := client.GetConfigFromCmd(cmd) + cfg := clicontext.GetConfigFromCmd(cmd) privValidator := pvm.LoadFilePV( cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), @@ -232,9 +232,9 @@ func BootstrapStateCmd[T interface { Short: "Bootstrap CometBFT state at an arbitrary block height using a light client", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - logger := client.GetLoggerFromCmd(cmd).Impl().(LoggerT) - cfg := client.GetConfigFromCmd(cmd) - v := client.GetViperFromCmd(cmd) + logger := clicontext.GetLoggerFromCmd[LoggerT](cmd) + cfg := clicontext.GetConfigFromCmd(cmd) + v := clicontext.GetViperFromCmd(cmd) height, err := cmd.Flags().GetInt64("height") if err != nil {