Skip to content

Commit

Permalink
client
Browse files Browse the repository at this point in the history
  • Loading branch information
ocnc2 committed Aug 25, 2024
1 parent 7043612 commit e446909
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 29 deletions.
2 changes: 1 addition & 1 deletion mod/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mod/cli/pkg/commands/genesis/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions mod/cli/pkg/commands/genesis/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions mod/cli/pkg/commands/genesis/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions mod/cli/pkg/commands/server/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions mod/cli/pkg/commands/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions mod/cli/pkg/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -60,7 +59,7 @@ func SetupCommand[
}

if err := handleConfigs(
client.GetViperFromCmd(cmd),
clicontext.GetViperFromCmd(cmd),
appTemplate, appConfig, cmtConfig,
); err != nil {
return err
Expand Down Expand Up @@ -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)

Expand Down
69 changes: 69 additions & 0 deletions mod/cli/pkg/context/cmd.go
Original file line number Diff line number Diff line change
@@ -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)
}
31 changes: 31 additions & 0 deletions mod/cli/pkg/context/keys.go
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions mod/consensus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mod/consensus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 8 additions & 8 deletions mod/consensus/pkg/cometbft/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e446909

Please sign in to comment.