Skip to content

Commit

Permalink
feat(node-builder): properly propagate the chainSpec to util cli comm…
Browse files Browse the repository at this point in the history
…ands (berachain#1088)

* bet

* bet

* bet

* x

* bet

* logger

* bet

* bet

* bet

* bet

* x

* bet
  • Loading branch information
Devon Bear authored May 16, 2024
1 parent 0fe1a78 commit 1df4c5e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 58 deletions.
5 changes: 4 additions & 1 deletion beacond/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

nodebuilder "github.com/berachain/beacon-kit/mod/node-builder"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/app"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/config/spec"
"go.uber.org/automaxprocs/maxprocs"
)

Expand All @@ -45,7 +46,9 @@ func run() error {
nb := nodebuilder.NewNodeBuilder[app.BeaconApp]().
WithAppName("beacond").
WithAppDescription("beacond is a beacon node for any beacon-kit chain").
WithDepInjectConfig(Config())
WithDepInjectConfig(Config()).
// TODO: Don't hardcode the default chain spec.
WithChainSpec(spec.LocalnetChainSpec())

return nb.RunNode()
}
Expand Down
8 changes: 6 additions & 2 deletions mod/node-builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
cmdlib "github.com/berachain/beacon-kit/mod/node-builder/pkg/commands"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/commands/utils/tos"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/components"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/config/spec"
"github.com/berachain/beacon-kit/mod/primitives"
depositdb "github.com/berachain/beacon-kit/mod/storage/pkg/deposit"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand All @@ -61,6 +61,9 @@ type NodeBuilder[T servertypes.Application] struct {
// Every node has some application it is running.
appInfo *AppInfo[T]

// chainSpec is the chain specification for the application.
chainSpec primitives.ChainSpec

// rootCmd is the root command for the application.
rootCmd *cobra.Command
}
Expand Down Expand Up @@ -100,7 +103,7 @@ func (nb *NodeBuilder[T]) BuildRootCmd() error {
depinject.Supply(
log.NewLogger(os.Stdout),
viper.GetViper(),
spec.LocalnetChainSpec(),
nb.chainSpec,
&depositdb.KVStore{},
),
depinject.Provide(
Expand Down Expand Up @@ -169,6 +172,7 @@ func (nb *NodeBuilder[T]) BuildRootCmd() error {
nb.rootCmd,
mm,
nb.AppCreator,
nb.chainSpec,
)

return autoCliOpts.EnhanceRootCommand(nb.rootCmd)
Expand Down
5 changes: 3 additions & 2 deletions mod/node-builder/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ func (nb *NodeBuilder[T]) AppCreator(
traceStore io.Writer,
appOpts servertypes.AppOptions,
) T {
bk := *app.NewBeaconKitApp(
app := *app.NewBeaconKitApp(
logger, db, traceStore, true,
appOpts,
nb.appInfo.DepInjectConfig,
nb.chainSpec,
server.DefaultBaseappOptions(appOpts)...,
)
return reflect.ValueOf(bk).Convert(
return reflect.ValueOf(app).Convert(
reflect.TypeOf((*T)(nil)).Elem()).Interface().(T)
}
9 changes: 9 additions & 0 deletions mod/node-builder/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package nodebuilder

import (
"cosmossdk.io/depinject"
"github.com/berachain/beacon-kit/mod/primitives"
)

// WithAppName sets the application name.
Expand All @@ -49,6 +50,14 @@ func (nb *NodeBuilder[T]) WithAppDescription(
return nb
}

// WithChainSpec sets the chain specification.
func (nb *NodeBuilder[T]) WithChainSpec(
spec primitives.ChainSpec,
) *NodeBuilder[T] {
nb.chainSpec = spec
return nb
}

// WithDepInjectConfig sets the dependency injection configuration.
func (nb *NodeBuilder[T]) WithDepInjectConfig(
config depinject.Config,
Expand Down
6 changes: 3 additions & 3 deletions mod/node-builder/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
datypes "github.com/berachain/beacon-kit/mod/da/pkg/types"
bkcomponents "github.com/berachain/beacon-kit/mod/node-builder/pkg/components"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/config/spec"
"github.com/berachain/beacon-kit/mod/primitives"
beaconkitruntime "github.com/berachain/beacon-kit/mod/runtime/pkg/runtime"
"github.com/berachain/beacon-kit/mod/state-transition/pkg/core/state"
"github.com/berachain/beacon-kit/mod/storage/pkg/deposit"
Expand Down Expand Up @@ -79,6 +79,7 @@ func NewBeaconKitApp(
loadLatest bool,
appOpts servertypes.AppOptions,
dCfg depinject.Config,
chainSpec primitives.ChainSpec,
baseAppOptions ...func(*baseapp.BaseApp),
) *BeaconApp {
app := &BeaconApp{}
Expand All @@ -100,8 +101,7 @@ func NewBeaconKitApp(
appOpts,
// supply the logger
logger,
// TODO: allow nodebuilder to inject.
spec.LocalnetChainSpec(),
chainSpec,
),
),
&appBuilder,
Expand Down
14 changes: 8 additions & 6 deletions mod/node-builder/pkg/commands/deposit/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/berachain/beacon-kit/mod/node-builder/pkg/commands/utils/parser"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/components"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/components/signer"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/config/spec"
"github.com/berachain/beacon-kit/mod/primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/constants"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/spf13/cobra"
Expand All @@ -45,7 +45,7 @@ import (
// NewValidateDeposit creates a new command for validating a deposit message.
//
//nolint:gomnd // lots of magic numbers
func NewCreateValidator() *cobra.Command {
func NewCreateValidator(chainSpec primitives.ChainSpec) *cobra.Command {
cmd := &cobra.Command{
Use: "create-validator",
Short: "Creates a validator deposit",
Expand All @@ -54,7 +54,7 @@ func NewCreateValidator() *cobra.Command {
amount, current version, and genesis validator root. If the broadcast
flag is set to true, a private key must be provided to sign the transaction.`,
Args: cobra.ExactArgs(4), //nolint:mnd // The number of arguments.
RunE: createValidatorCmd(),
RunE: createValidatorCmd(chainSpec),
}

cmd.Flags().BoolP(
Expand All @@ -80,7 +80,9 @@ func NewCreateValidator() *cobra.Command {
// for the geth client but something about the Deposit binding is not handling
// other execution layers correctly. Peep the commit history for what we had.
// 🤷‍♂️.
func createValidatorCmd() func(*cobra.Command, []string) error {
func createValidatorCmd(
chainSpec primitives.ChainSpec,
) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
var (
logger = log.NewLogger(os.Stdout)
Expand Down Expand Up @@ -115,7 +117,7 @@ func createValidatorCmd() func(*cobra.Command, []string) error {
// Create and sign the deposit message.
depositMsg, signature, err := types.CreateAndSignDepositMessage(
types.NewForkData(currentVersion, genesisValidatorRoot),
spec.LocalnetChainSpec().DomainTypeDeposit(),
chainSpec.DomainTypeDeposit(),
blsSigner,
credentials,
amount,
Expand All @@ -129,7 +131,7 @@ func createValidatorCmd() func(*cobra.Command, []string) error {
types.NewForkData(currentVersion, genesisValidatorRoot),
signature,
signer.BLSSigner{}.VerifySignature,
spec.LocalnetChainSpec().DomainTypeDeposit(),
chainSpec.DomainTypeDeposit(),
); err != nil {
return err
}
Expand Down
87 changes: 44 additions & 43 deletions mod/node-builder/pkg/commands/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import (
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/commands/utils/parser"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/components/signer"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/config/spec"
"github.com/berachain/beacon-kit/mod/primitives"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
)

// Commands creates a new command for deposit related actions.
func Commands() *cobra.Command {
func Commands(chainSpec primitives.ChainSpec) *cobra.Command {
cmd := &cobra.Command{
Use: "deposit",
Short: "deposit subcommands",
Expand All @@ -45,8 +45,8 @@ func Commands() *cobra.Command {
}

cmd.AddCommand(
NewValidateDeposit(),
NewCreateValidator(),
NewValidateDeposit(chainSpec),
NewCreateValidator(chainSpec),
)

return cmd
Expand All @@ -55,7 +55,7 @@ func Commands() *cobra.Command {
// NewValidateDeposit creates a new command for validating a deposit message.
//
//nolint:mnd // lots of magic numbers
func NewValidateDeposit() *cobra.Command {
func NewValidateDeposit(chainSpec primitives.ChainSpec) *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Short: "Validates a deposit message for creating a new validator",
Expand All @@ -65,59 +65,60 @@ func NewValidateDeposit() *cobra.Command {
withdrawal credentials, deposit amount, signature, current version,
and genesis validator root.`,
Args: cobra.ExactArgs(6),
RunE: validateDepositMessage,
RunE: validateDepositMessage(chainSpec),
}

return cmd
}

// validateDepositMessage validates a deposit message for creating a new
// validator.
func validateDepositMessage(
func validateDepositMessage(chainSpec primitives.ChainSpec) func(
_ *cobra.Command,
args []string,
) error {
pubkey, err := parser.ConvertPubkey(args[0])
if err != nil {
return err
}
return func(_ *cobra.Command, args []string) error {
pubkey, err := parser.ConvertPubkey(args[0])
if err != nil {
return err
}

credentials, err := parser.ConvertWithdrawalCredentials(args[1])
if err != nil {
return err
}
credentials, err := parser.ConvertWithdrawalCredentials(args[1])
if err != nil {
return err
}

amount, err := parser.ConvertAmount(args[2])
if err != nil {
return err
}
amount, err := parser.ConvertAmount(args[2])
if err != nil {
return err
}

signature, err := parser.ConvertSignature(args[3])
if err != nil {
return err
}
signature, err := parser.ConvertSignature(args[3])
if err != nil {
return err
}

currentVersion, err := parser.ConvertVersion(args[4])
if err != nil {
return err
}
currentVersion, err := parser.ConvertVersion(args[4])
if err != nil {
return err
}

genesisValidatorRoot, err := parser.ConvertGenesisValidatorRoot(args[5])
if err != nil {
return err
}
genesisValidatorRoot, err := parser.ConvertGenesisValidatorRoot(args[5])
if err != nil {
return err
}

depositMessage := types.DepositMessage{
Pubkey: pubkey,
Credentials: credentials,
Amount: amount,
}
depositMessage := types.DepositMessage{
Pubkey: pubkey,
Credentials: credentials,
Amount: amount,
}

return depositMessage.VerifyCreateValidator(
types.NewForkData(currentVersion, genesisValidatorRoot),
signature,
signer.BLSSigner{}.VerifySignature,
// TODO: needs to be configurable.
spec.LocalnetChainSpec().DomainTypeDeposit(),
)
return depositMessage.VerifyCreateValidator(
types.NewForkData(currentVersion, genesisValidatorRoot),
signature,
signer.BLSSigner{}.VerifySignature,
chainSpec.DomainTypeDeposit(),
)
}
}
4 changes: 3 additions & 1 deletion mod/node-builder/pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/berachain/beacon-kit/mod/node-builder/pkg/commands/genesis"
"github.com/berachain/beacon-kit/mod/node-builder/pkg/commands/jwt"
beaconconfig "github.com/berachain/beacon-kit/mod/node-builder/pkg/config"
"github.com/berachain/beacon-kit/mod/primitives"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/pruning"
"github.com/cosmos/cosmos-sdk/client/snapshot"
Expand All @@ -49,6 +50,7 @@ func DefaultRootCommandSetup[T servertypes.Application](
rootCmd *cobra.Command,
mm *module.Manager,
newApp servertypes.AppCreator[T],
chainSpec primitives.ChainSpec,
) {
// Add the ToS Flag to the root command.
beaconconfig.AddToSFlag(rootCmd)
Expand All @@ -73,7 +75,7 @@ func DefaultRootCommandSetup[T servertypes.Application](
genesis.AddPubkeyCmd(),
genesis.CollectValidatorsCmd(),
),
deposit.Commands(),
deposit.Commands(chainSpec),
// `jwt`
jwt.Commands(),
// `keys`
Expand Down

0 comments on commit 1df4c5e

Please sign in to comment.