Skip to content

Commit

Permalink
feat(clibuilder): custom context interceptor (berachain#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocnc2 authored Jun 28, 2024
1 parent b938c1e commit d12edf7
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mod/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/pflag v1.0.5
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.12 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
Expand Down
7 changes: 3 additions & 4 deletions mod/cli/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"cosmossdk.io/depinject"
"cosmossdk.io/log"
cmdlib "github.com/berachain/beacon-kit/mod/cli/pkg/commands"
"github.com/berachain/beacon-kit/mod/cli/pkg/utils/context"
"github.com/berachain/beacon-kit/mod/node-core/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
cmtcfg "github.com/cometbft/cometbft/config"
Expand Down Expand Up @@ -147,14 +148,12 @@ func (cb *CLIBuilder[T]) InterceptConfigsPreRunHandler(
cmd *cobra.Command, logger log.Logger, customAppConfigTemplate string,
customAppConfig interface{}, cmtConfig *cmtcfg.Config,
) error {
serverCtx, err := server.InterceptConfigsAndCreateContext(
cmd, customAppConfigTemplate, customAppConfig, cmtConfig)
serverCtx, err := context.InterceptConfigsAndCreateContext(
cmd, customAppConfigTemplate, customAppConfig, cmtConfig, logger)
if err != nil {
return err
}

serverCtx.Logger = logger

// set server context
return server.SetCmdServerContext(cmd, serverCtx)
}
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 @@ -26,10 +26,10 @@ import (
"path/filepath"
"strings"

"github.com/berachain/beacon-kit/mod/cli/pkg/utils/context"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/genesis"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/errors"
"github.com/cosmos/cosmos-sdk/server"
"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 {
serverCtx := server.GetServerContextFromCmd(cmd)
serverCtx := context.GetServerContextFromCmd(cmd)
config := serverCtx.Config

appGenesis, err := genutiltypes.AppGenesisFromFile(
Expand Down
4 changes: 2 additions & 2 deletions mod/cli/pkg/commands/genesis/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"path/filepath"

"cosmossdk.io/depinject"
"github.com/berachain/beacon-kit/mod/cli/pkg/utils/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 @@ -37,7 +38,6 @@ import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand All @@ -50,7 +50,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 {
serverCtx := server.GetServerContextFromCmd(cmd)
serverCtx := context.GetServerContextFromCmd(cmd)
config := serverCtx.Config

_, valPubKey, err := genutil.InitializeNodeValidatorFiles(
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 @@ -25,6 +25,7 @@ import (
"encoding/json"
"unsafe"

serverContext "github.com/berachain/beacon-kit/mod/cli/pkg/utils/context"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/genesis"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
Expand All @@ -34,7 +35,6 @@ import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
ethengineprimitives "github.com/ethereum/go-ethereum/beacon/engine"
Expand Down Expand Up @@ -70,7 +70,7 @@ func AddExecutionPayloadCmd() *cobra.Command {
nil,
).ExecutionPayload

serverCtx := server.GetServerContextFromCmd(cmd)
serverCtx := serverContext.GetServerContextFromCmd(cmd)
config := serverCtx.Config

appGenesis, err := genutiltypes.AppGenesisFromFile(
Expand Down
41 changes: 41 additions & 0 deletions mod/cli/pkg/utils/context/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 (
"cosmossdk.io/log"
"github.com/berachain/beacon-kit/mod/log/pkg/noop"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
)

// GetServerContextFromCmd returns a Context from a command or an empty Context
// if it has not been set.
func GetServerContextFromCmd(cmd *cobra.Command) *server.Context {
if v := cmd.Context().Value(server.ServerContextKey); v != nil {
serverCtxPtr, _ := v.(*server.Context)
return serverCtxPtr
}

return newDefaultContextWithLogger(
&noop.Logger[any, log.Logger]{},
)
}
Loading

0 comments on commit d12edf7

Please sign in to comment.