Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore(node-core): cleanup + prep for server v2 #1561

Merged
merged 4 commits into from
Jun 22, 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
24 changes: 10 additions & 14 deletions beacond/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ func run() error {
// Build the node using the node-core.
nb := nodebuilder.New(
// Set the DepInject Configuration to the Default.
nodebuilder.WithDepInjectConfig[types.NodeI](
nodebuilder.WithDepInjectConfig[types.Node](
nodebuilder.DefaultDepInjectConfig()),
// Set the Runtime Components to the Default.
nodebuilder.WithComponents[types.NodeI](
nodebuilder.WithComponents[types.Node](
nodecomponents.DefaultComponentsWithStandardTypes(),
),
)

// Build the root command using the builder
cb := clibuilder.New(
// Set the Name to the Default.
clibuilder.WithName[types.NodeI](nodebuilder.DefaultAppName),
clibuilder.WithName[types.Node](nodebuilder.DefaultAppName),
// Set the Description to the Default.
clibuilder.WithDescription[types.NodeI](nodebuilder.DefaultDescription),
clibuilder.WithDescription[types.Node](nodebuilder.DefaultDescription),
// Set the DepInject Configuration to the Default.
clibuilder.WithDepInjectConfig[types.NodeI](
clibuilder.WithDepInjectConfig[types.Node](
nodebuilder.DefaultDepInjectConfig(),
),
// Set the Runtime Components to the Default.
clibuilder.WithComponents[types.NodeI](
clibuilder.WithComponents[types.Node](
append(
clicomponents.DefaultClientComponents(),
// TODO: remove these, and eventually pull cfg and chainspec
Expand All @@ -73,21 +73,17 @@ func run() error {
nodecomponents.ProvideChainSpec,
),
),
clibuilder.SupplyModuleDeps[types.NodeI](
clibuilder.SupplyModuleDeps[types.Node](
beacon.SupplyModuleDependencies(),
),
// Set the Run Handler to the Default.
clibuilder.WithRunHandler[types.NodeI](
clibuilder.WithRunHandler[types.Node](
server.InterceptConfigsPreRunHandler,
),
// Set the AppCreator to the NodeBuilder AppCreator.
clibuilder.WithAppCreator[types.NodeI](nb.AppCreator),
// Set the NodeBuilderFunc to the NodeBuilder Build.
clibuilder.WithNodeBuilderFunc[types.Node](nb.Build),
)

// we never have to call nb.build() because this function is passed
// to the cli through the clibuilder.WithAppCreator option, eventually to be
// called by the cosmos-sdk

cmd, err := cb.Build()
if err != nil {
return err
Expand Down
13 changes: 6 additions & 7 deletions mod/cli/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ type CLIBuilder[T servertypes.Application] struct {
// components is a list of component providers for depinject.
components []any
// suppliers is a list of suppliers for depinject.
suppliers []any
suppliers []any
// runHandler is a function to set up run handlers for the command.
runHandler runHandler
// appCreator is a function that builds the Node, eventually called by the
// cosmos-sdk.
// nodeBuilderFunc is a function that builds the Node,
// eventually called by the cosmos-sdk.
// TODO: CLI should not know about the AppCreator
appCreator servertypes.AppCreator[T]
nodeBuilderFunc servertypes.AppCreator[T]
// rootCmdSetup is a function that sets up the root command.
rootCmdSetup rootCmdSetup[T]
// logger is the logger
// logger log.Logger
}

// New returns a new CLIBuilder with the given options.
Expand Down Expand Up @@ -119,7 +118,7 @@ func (cb *CLIBuilder[T]) Build() (*cmdlib.Root, error) {
cmdlib.DefaultRootCommandSetup(
rootCmd,
mm,
cb.appCreator,
cb.nodeBuilderFunc,
chainSpec,
)

Expand Down
8 changes: 4 additions & 4 deletions mod/cli/pkg/builder/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func WithDefaultRootCommandSetup[T servertypes.Application]() Opt[T] {
}
}

// WithAppCreator sets the cosmos app creator for the CLIBuilder.
func WithAppCreator[T servertypes.Application](
appCreator servertypes.AppCreator[T],
// WithNodeBuilderFunc sets the cosmos app creator for the CLIBuilder.
func WithNodeBuilderFunc[T servertypes.Application](
nodeBuilderFunc servertypes.AppCreator[T],
) Opt[T] {
return func(cb *CLIBuilder[T]) {
cb.appCreator = appCreator
cb.nodeBuilderFunc = nodeBuilderFunc
}
}
7 changes: 4 additions & 3 deletions mod/cli/pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
package commands

import (
"github.com/berachain/beacon-kit/mod/cli/pkg/config"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
sdkconfig "github.com/cosmos/cosmos-sdk/client/config"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,9 +58,9 @@ func New(name string,
return err
}

customClientTemplate, customClientConfig := InitClientConfig()
customClientTemplate, customClientConfig := config.InitClientConfig()
// Update the client context with the default custom config
clientCtx, err = config.CreateClientConfig(
clientCtx, err = sdkconfig.CreateClientConfig(
clientCtx,
customClientTemplate,
customClientConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/berachain/beacon-kit/mod/cli/pkg/commands/jwt"
"github.com/berachain/beacon-kit/mod/cli/pkg/flags"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/pruning"
"github.com/cosmos/cosmos-sdk/client/snapshot"
Expand Down Expand Up @@ -84,11 +83,3 @@ func DefaultRootCommandSetup[T servertypes.Application](
version.NewVersionCommand(),
)
}

// InitClientConfig sets up the default client configuration, allowing for
// overrides.
func InitClientConfig() (string, interface{}) {
clientCfg := config.DefaultConfig()
clientCfg.KeyringBackend = "test"
return config.DefaultClientConfigTemplate, clientCfg
}
8 changes: 4 additions & 4 deletions mod/cli/pkg/components/client_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"path/filepath"

"cosmossdk.io/core/address"
"github.com/berachain/beacon-kit/mod/cli/pkg/commands"
"github.com/berachain/beacon-kit/mod/cli/pkg/config"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
sdkconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
)
Expand Down Expand Up @@ -66,8 +66,8 @@ func ProvideClientContext(

// Read the config to overwrite the default values with the values from the
// config file
customClientTemplate, customClientConfig := commands.InitClientConfig()
clientCtx, err = config.ReadDefaultValuesFromDefaultClientConfig(
customClientTemplate, customClientConfig := config.InitClientConfig()
clientCtx, err = sdkconfig.ReadDefaultValuesFromDefaultClientConfig(
clientCtx,
customClientTemplate,
customClientConfig,
Expand Down
31 changes: 31 additions & 0 deletions mod/cli/pkg/config/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: BUSL-1.1
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was your plan? would a client folder make more sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh no pref i just moved this cause i hate utils.go as a concept and its like just a convenience func for cosmos client config

// 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 config

import "github.com/cosmos/cosmos-sdk/client/config"

// InitClientConfig sets up the default client configuration, allowing for
// overrides.
func InitClientConfig() (string, interface{}) {
clientCfg := config.DefaultConfig()
clientCfg.KeyringBackend = "test"
return config.DefaultClientConfigTemplate, clientCfg
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import (

// This file contains Options that extend our default baseapp options to be
// called by cosmos when building the app.
// TODO: refactor into consensus_options for serverv2 migration.

// WithCometParamStore sets the param store to the comet consensus engine.
func WithCometParamStore(
chainSpec common.ChainSpec) func(bApp *baseapp.BaseApp) {
chainSpec common.ChainSpec,
) func(bApp *baseapp.BaseApp) {
return func(bApp *baseapp.BaseApp) {
bApp.SetParamStore(comet.NewConsensusParamsStore(chainSpec))
}
Expand All @@ -58,7 +60,8 @@ func WithProcessProposal(

// WithPreBlocker sets the pre-blocker to the baseapp.
func WithPreBlocker(
preBlocker sdk.PreBlocker) func(bApp *baseapp.BaseApp) {
preBlocker sdk.PreBlocker,
) func(bApp *baseapp.BaseApp) {
return func(bApp *baseapp.BaseApp) {
bApp.SetPreBlocker(preBlocker)
}
Expand Down
81 changes: 79 additions & 2 deletions mod/node-core/pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@
package builder

import (
"context"
"io"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/berachain/beacon-kit/mod/node-core/pkg/app"
"github.com/berachain/beacon-kit/mod/node-core/pkg/components"
"github.com/berachain/beacon-kit/mod/node-core/pkg/node"
"github.com/berachain/beacon-kit/mod/node-core/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/runtime/pkg/service"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
)

// TODO: #Make nodebuilder build a node. Currently this is just a builder for
// the AppCreator function, which is eventually called by cosmos to build a
// node.
type NodeBuilder[NodeT types.NodeI] struct {
type NodeBuilder[NodeT types.Node] struct {
node NodeT
// depinjectCfg holds is an extendable config container used by the
// depinject framework.
Expand All @@ -39,7 +51,7 @@ type NodeBuilder[NodeT types.NodeI] struct {
}

// New returns a new NodeBuilder.
func New[NodeT types.NodeI](opts ...Opt[NodeT]) *NodeBuilder[NodeT] {
func New[NodeT types.Node](opts ...Opt[NodeT]) *NodeBuilder[NodeT] {
nb := &NodeBuilder[NodeT]{
node: node.New[NodeT](),
}
Expand All @@ -48,3 +60,68 @@ func New[NodeT types.NodeI](opts ...Opt[NodeT]) *NodeBuilder[NodeT] {
}
return nb
}

// Build uses the node builder options and runtime parameters to
// build a new instance of the node.
// It is necessary to adhere to the types.AppCreator[T] interface.
func (nb *NodeBuilder[NodeT]) Build(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
appOpts servertypes.AppOptions,
) NodeT {
// Check for goleveldb cause bad project.
if appOpts.Get("app-db-backend") == "goleveldb" {
panic("goleveldb is not supported")
}

// variables to hold the components needed to set up BeaconApp
var (
chainSpec common.ChainSpec
appBuilder *runtime.AppBuilder
abciMiddleware *components.ABCIMiddleware
serviceRegistry *service.Registry
)

// build all node components using depinject
if err := depinject.Inject(
depinject.Configs(
nb.depInjectCfg,
depinject.Provide(
nb.components...,
),
depinject.Supply(
appOpts,
logger,
),
),
&appBuilder,
&chainSpec,
&abciMiddleware,
&serviceRegistry,
); err != nil {
panic(err)
}

// set the application to a new BeaconApp with necessary ABCI handlers
nb.node.RegisterApp(
app.NewBeaconKitApp(
db, traceStore, true, appBuilder,
append(
server.DefaultBaseappOptions(appOpts),
WithCometParamStore(chainSpec),
WithPrepareProposal(abciMiddleware.PrepareProposal),
WithProcessProposal(abciMiddleware.ProcessProposal),
WithPreBlocker(abciMiddleware.PreBlock),
)...,
),
)
nb.node.SetServiceRegistry(serviceRegistry)

// TODO: put this in some post node creation hook/listener.
if err := nb.node.Start(context.Background()); err != nil {
logger.Error("failed to start node", "err", err)
panic(err)
}
return nb.node
}
Loading
Loading