Skip to content

Commit

Permalink
chore(cli): Enforce valid withdrawal address set for `add-premined-de…
Browse files Browse the repository at this point in the history
…posit` & `create-validator` (berachain#2174)
  • Loading branch information
calbera authored Dec 6, 2024
1 parent 6e83dfe commit e7b49ab
Show file tree
Hide file tree
Showing 26 changed files with 730 additions and 666 deletions.
9 changes: 6 additions & 3 deletions cli/commands/deposit/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func NewCreateValidator[
cmd := &cobra.Command{
Use: "create-validator",
Short: "Creates a validator deposit",
Long: `Creates a validator deposit with the necessary credentials. The
arguments are expected in the order of withdrawal credentials, deposit
Long: `Creates a validator deposit with the necessary credentials. The
arguments are expected in the order of withdrawal address, deposit
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.
Expand Down Expand Up @@ -80,10 +80,13 @@ func createValidatorCmd[
return err
}

credentials, err := parser.ConvertWithdrawalCredentials(args[0])
withdrawalAddress, err := parser.ConvertWithdrawalAddress(args[0])
if err != nil {
return err
}
credentials := types.NewCredentialsFromExecutionAddress(
withdrawalAddress,
)

amount, err := parser.ConvertAmount(args[1])
if err != nil {
Expand Down
32 changes: 13 additions & 19 deletions cli/commands/genesis/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ import (

// AddGenesisDepositCmd - returns the cobra command to
// add a premined deposit to the genesis file.
//

func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command {
cmd := &cobra.Command{
Use: "add-premined-deposit",
Short: "adds a validator to the genesis file",
RunE: func(cmd *cobra.Command, _ []string) error {
Long: `Adds a validator to the genesis file with the necessary
credentials. The arguments are expected in the order of the deposit
amount and withdrawal address.`,
Args: cobra.ExactArgs(2), //nolint:mnd // The number of arguments.
RunE: func(cmd *cobra.Command, args []string) error {
config := context.GetConfigFromCmd(cmd)

_, valPubKey, err := genutil.InitializeNodeValidatorFiles(
Expand All @@ -61,11 +67,6 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command {
)
}

var (
depositAmountString string
depositAmount math.Gwei
)

// Get the BLS signer.
blsSigner, err := components.ProvideBlsSigner(
components.BlsSignerInput{
Expand All @@ -77,12 +78,8 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command {
}

// Get the deposit amount.
depositAmountString, err = cmd.Flags().GetString(depositAmountFlag)
if err != nil {
return err
}

depositAmount, err = parser.ConvertAmount(depositAmountString)
var depositAmount math.Gwei
depositAmount, err = parser.ConvertAmount(args[0])
if err != nil {
return err
}
Expand All @@ -92,14 +89,14 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command {
version.Deneb,
)

// Get the withdrawal address.
withdrawalAddress := common.NewExecutionAddressFromHex(args[1])

depositMsg, signature, err := types.CreateAndSignDepositMessage(
types.NewForkData(currentVersion, common.Root{}),
cs.DomainTypeDeposit(),
blsSigner,
// TODO: configurable.
types.NewCredentialsFromExecutionAddress(
common.ExecutionAddress{},
),
types.NewCredentialsFromExecutionAddress(withdrawalAddress),
depositAmount,
)
if err != nil {
Expand Down Expand Up @@ -141,9 +138,6 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command {
},
}

cmd.Flags().
String(depositAmountFlag, defaultDepositAmount, depositAmountFlagMsg)

return cmd
}

Expand Down
27 changes: 0 additions & 27 deletions cli/commands/genesis/flags.go

This file was deleted.

19 changes: 19 additions & 0 deletions cli/utils/parser/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package parser

import (
"fmt"
"math/big"

"github.com/berachain/beacon-kit/consensus-types/types"
Expand All @@ -45,6 +46,24 @@ func ConvertPubkey(pubkey string) (crypto.BLSPubkey, error) {
return crypto.BLSPubkey(pubkeyBytes), nil
}

// ConvertWithdrawalAddress converts a string to a withdrawal address.
func ConvertWithdrawalAddress(address string) (common.ExecutionAddress, error) {
// Wrap the call in a recover to handle potential panics from invalid
// addresses.
var (
addr common.ExecutionAddress
err error
)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("invalid withdrawal address: %v", r)
}
}()

addr = common.NewExecutionAddressFromHex(address)
return addr, err
}

// ConvertWithdrawalCredentials converts a string to a withdrawal credentials.
func ConvertWithdrawalCredentials(credentials string) (
types.WithdrawalCredentials,
Expand Down
2 changes: 1 addition & 1 deletion cmd/beacond/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func DefaultComponents() []any {
*AvailabilityStore, *BeaconBlock, *BeaconBlockBody,
*BeaconBlockHeader, *BlobSidecars, *Logger,
],
components.ProvideBeaconDepositContract[
components.ProvideDepositContract[
*Deposit, *ExecutionPayload, *ExecutionPayloadHeader,
],
components.ProvideBlockStore[
Expand Down
2 changes: 1 addition & 1 deletion cmd/beacond/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type (
Deposit = types.Deposit

// DepositContract is a type alias for the deposit contract.
DepositContract = deposit.WrappedBeaconDepositContract[
DepositContract = deposit.WrappedDepositContract[
*Deposit,
WithdrawalCredentials,
]
Expand Down
8 changes: 4 additions & 4 deletions consensus/cometbft/service/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,16 @@ func (s *Service[LoggerT]) internalFinalizeBlock(
//
// NOTE: Not all raw transactions may adhere to the sdk.Tx interface, e.g.
// vote extensions, so skip those.
txResults := make([]*cmtabci.ExecTxResult, 0, len(req.Txs))
for range req.Txs {
txResults := make([]*cmtabci.ExecTxResult, len(req.Txs))
for i := range req.Txs {
//nolint:mnd // its okay for now.
txResults = append(txResults, &cmtabci.ExecTxResult{
txResults[i] = &cmtabci.ExecTxResult{
Codespace: "sdk",
Code: 2,
Log: "skip decoding",
GasWanted: 0,
GasUsed: 0,
})
}
}

finalizeBlock, err := s.Middleware.FinalizeBlock(
Expand Down
2 changes: 1 addition & 1 deletion contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ forge-lint-fix:

forge-lint:
@echo "--> Running forge lint"
@cd $(CONTRACTS_DIR) && forge fmt --check
@cd $(CONTRACTS_DIR) && forge fmt --check
Loading

0 comments on commit e7b49ab

Please sign in to comment.