Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ issues:
- path: "^consensus-types/types/.*\\.go$"
linters:
- thelper
# The cosmos-sdk telemetry package is deprecated in favor of OpenTelemetry
# as of sdk v0.54, but is still the wired-up metrics backend here.
- text: "SA1019: telemetry\\."
linters:
- staticcheck
exclude-files:
- "pkg/cometbft/cli/.*\\.go"
- "pkg/cometbft/service/server/.*\\.go"
Expand Down
11 changes: 6 additions & 5 deletions beacon/blockchain/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (

func (s *Service) FinalizeBlock(
ctx sdk.Context,
req *cmtabci.FinalizeBlockRequest,
req *cmtabci.RequestFinalizeBlock,
) (transition.ValidatorUpdates, error) {
// STEP 1: Decode block and blobs.
signedBlk, blobs, err := s.ParseBeaconBlock(req)
Expand All @@ -66,7 +66,7 @@ func (s *Service) FinalizeBlock(
}

// STEP 2: Finalize sidecars first (block will check for sidecar availability).
if err = s.FinalizeSidecars(ctx, req.SyncingToHeight, blk, blobs); err != nil {
if err = s.FinalizeSidecars(ctx, req.Height, blk, blobs); err != nil {
return nil, fmt.Errorf("failed finalizing sidecars: %w", err)
}

Expand All @@ -90,10 +90,11 @@ func (s *Service) FinalizeSidecars(
blk *ctypes.BeaconBlock,
blobs datypes.BlobSidecars,
) error {
// SyncingToHeight is always the tip of the chain both during sync and when
// caught up. We don't need to process sidecars unless they are within DA period.
// syncingToHeight approximates the chain tip. The v0.40 fork ABCI has no
// SyncingToHeight, so callers pass the finalized height. We don't need to
// process sidecars unless they are within DA period.
//
//#nosec: G115 // SyncingToHeight will never be negative.
//#nosec: G115 // syncingToHeight will never be negative.
if s.chainSpec.WithinDAPeriod(blk.GetSlot(), math.Slot(syncingToHeight)) {
err := s.blobProcessor.ProcessSidecars(
s.storageBackend.AvailabilityStore(),
Expand Down
4 changes: 2 additions & 2 deletions beacon/blockchain/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type BlockchainI interface {
)
ProcessProposal(
sdk.Context,
*cmtabci.ProcessProposalRequest,
*cmtabci.RequestProcessProposal,
[]byte, // this node address
) (transition.ValidatorUpdates, error)
FinalizeSidecars(
Expand All @@ -157,7 +157,7 @@ type BlockchainI interface {
) error
FinalizeBlock(
sdk.Context,
*cmtabci.FinalizeBlockRequest,
*cmtabci.RequestFinalizeBlock,
) (transition.ValidatorUpdates, error)
PostFinalizeBlockOps(
sdk.Context,
Expand Down
7 changes: 4 additions & 3 deletions beacon/blockchain/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
"testing"
"time"

"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/log/v2"
"github.com/berachain/beacon-kit/beacon/blockchain"
bcmocks "github.com/berachain/beacon-kit/beacon/blockchain/mocks"
"github.com/berachain/beacon-kit/chain"
Expand All @@ -50,6 +49,8 @@ import (
"github.com/berachain/beacon-kit/state-transition/core/state"
"github.com/berachain/beacon-kit/storage/deposit"
statetransition "github.com/berachain/beacon-kit/testing/state-transition"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
storetypes "github.com/cosmos/cosmos-sdk/store/v2/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestOptimisticBlockBuildingVerifiedBlockStateChecks(t *testing.T) {
eng.EXPECT().NotifyForkchoiceUpdate(mock.Anything, mock.Anything).Return(dummyPayloadID, nil)

// BUILD A VALID BLOCK (without polluting state st)
sdkCtx := sdk.NewContext(cms.CacheMultiStore(), true, log.NewNopLogger())
sdkCtx := sdk.NewContext(cms.CacheMultiStore(), cmtproto.Header{}, true, log.NewNopLogger())
buildState := state.NewBeaconStateFromDB(
st.KVStore.WithContext(sdkCtx), cs, sdkCtx.Logger(), metrics.NewNoOpTelemetrySink(),
)
Expand Down
2 changes: 1 addition & 1 deletion beacon/blockchain/process_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const (
//nolint:funlen // abundantly commented
func (s *Service) ProcessProposal(
ctx sdk.Context,
req *cmtabci.ProcessProposalRequest,
req *cmtabci.RequestProcessProposal,
thisNodeAddress []byte,
) (transition.ValidatorUpdates, error) {
signedBlk, sidecars, err := s.ParseBeaconBlock(req)
Expand Down
3 changes: 2 additions & 1 deletion cli/commands/deposit/db_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/berachain/beacon-kit/primitives/version"
"github.com/berachain/beacon-kit/state-transition/core"
"github.com/berachain/beacon-kit/storage/db"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,7 +56,7 @@ func GetDBCheckCmd(appCreator servertypes.AppCreator) *cobra.Command {

// Setup the state to check.
ctx := sdk.NewContext(
app.CommitMultiStore().CacheMultiStore(), false, servercmtlog.WrapSDKLogger(logger),
app.CommitMultiStore().CacheMultiStore(), cmtproto.Header{}, false, servercmtlog.WrapSDKLogger(logger),
).WithContext(cmd.Context())
beaconState := app.StorageBackend().StateFromContext(ctx)
depositStore := app.StorageBackend().DepositStore()
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/genesis/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
servertypes "github.com/berachain/beacon-kit/cli/commands/server/types"
"github.com/berachain/beacon-kit/cli/context"
"github.com/berachain/beacon-kit/cli/utils/parser"
privvalinit "github.com/berachain/beacon-kit/cli/utils/privval"
"github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/node-core/components"
Expand All @@ -38,7 +39,6 @@ import (
"github.com/berachain/beacon-kit/primitives/math"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -96,7 +96,7 @@ func AddGenesisDeposit(
withdrawalAddress common.ExecutionAddress,
outputDocument string,
) error {
_, valPubKey, err := genutil.InitializeNodeValidatorFiles(
_, valPubKey, err := privvalinit.InitializeNodeValidatorFiles(
cometConfig, crypto.CometBLSType,
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cli/commands/initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/berachain/beacon-kit/chain"
clitypes "github.com/berachain/beacon-kit/cli/commands/server/types"
"github.com/berachain/beacon-kit/cli/context"
privvalinit "github.com/berachain/beacon-kit/cli/utils/privval"
cometbft "github.com/berachain/beacon-kit/consensus/cometbft/service"
"github.com/berachain/beacon-kit/errors"
"github.com/berachain/beacon-kit/primitives/crypto"
Expand Down Expand Up @@ -157,7 +158,7 @@ func InitCmd(creator clitypes.ChainSpecCreator, mm interface {
initHeight = 1
}

nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic, consensusKeyAlgo)
nodeID, _, err := privvalinit.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic, consensusKeyAlgo)
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package commands

import (
svrcmd "github.com/berachain/beacon-kit/cli/commands/server/cmd"
"github.com/berachain/beacon-kit/cli/config"
sdkclient "github.com/cosmos/cosmos-sdk/client"
sdkconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -59,13 +58,8 @@ func New(
return err
}

customClientTemplate, customClientConfig := config.InitClientConfig()
// Update the client context with the default custom config
clientCtx, err = sdkconfig.CreateClientConfig(
clientCtx,
customClientTemplate,
customClientConfig,
)
// Update the client context with the client config file
clientCtx, err = sdkconfig.ReadFromClientConfig(clientCtx)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/server/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"fmt"
"strings"

pruningtypes "cosmossdk.io/store/pruning/types"
"github.com/berachain/beacon-kit/cli/commands/server/types"
pruningtypes "github.com/cosmos/cosmos-sdk/store/v2/pruning/types"
"github.com/spf13/cast"
)

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
package server

import (
pruningtypes "cosmossdk.io/store/pruning/types"
types "github.com/berachain/beacon-kit/cli/commands/server/types"
clicontext "github.com/berachain/beacon-kit/cli/context"
"github.com/berachain/beacon-kit/storage/db"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
dbm "github.com/cosmos/cosmos-db"
pruningtypes "github.com/cosmos/cosmos-sdk/store/v2/pruning/types"
"github.com/spf13/cobra"
)

Expand Down
31 changes: 0 additions & 31 deletions cli/config/client.go

This file was deleted.

120 changes: 120 additions & 0 deletions cli/utils/privval/privval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2025, 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 privval initializes the CometBFT node key and consensus
// validator files with BLS key support. The cosmos-sdk genutil helper
// (InitializeNodeValidatorFilesFromMnemonicWithKeyType) is not used
// because its BLS path overwrites an existing priv_validator_key.json.
package privval

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/berachain/beacon-kit/primitives/crypto"
cmtcfg "github.com/cometbft/cometbft/config"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmtbls12381 "github.com/cometbft/cometbft/crypto/bls12381"
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/privval"
"github.com/cosmos/go-bip39"
)

// InitializeNodeValidatorFiles creates the node key and private validator
// files if they do not exist, and returns the node ID and validator pubkey.
func InitializeNodeValidatorFiles(
config *cmtcfg.Config, keyType string,
) (string, cmtcrypto.PubKey, error) {
return InitializeNodeValidatorFilesFromMnemonic(config, "", keyType)
}

// InitializeNodeValidatorFilesFromMnemonic is like
// InitializeNodeValidatorFiles but derives the ed25519 consensus key from
// the mnemonic when one is provided. BLS keys do not support mnemonics.
func InitializeNodeValidatorFilesFromMnemonic(
config *cmtcfg.Config, mnemonic, keyType string,
) (string, cmtcrypto.PubKey, error) {
if len(mnemonic) > 0 && !bip39.IsMnemonicValid(mnemonic) {
return "", nil, errors.New("invalid mnemonic")
}
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return "", nil, err
}
nodeID := string(nodeKey.ID())

pvKeyFile := config.PrivValidatorKeyFile()
if err = os.MkdirAll(filepath.Dir(pvKeyFile), 0o750); err != nil {
return "", nil, fmt.Errorf(
"could not create directory %q: %w", filepath.Dir(pvKeyFile), err,
)
}
pvStateFile := config.PrivValidatorStateFile()
if err = os.MkdirAll(filepath.Dir(pvStateFile), 0o750); err != nil {
return "", nil, fmt.Errorf(
"could not create directory %q: %w", filepath.Dir(pvStateFile), err,
)
}

privKey, err := genPrivKey(mnemonic, keyType)
if err != nil {
return "", nil, err
}
filePV := loadOrGenFilePV(privKey, pvKeyFile, pvStateFile)

pubKey, err := filePV.GetPubKey()
if err != nil {
return "", nil, err
}
return nodeID, pubKey, nil
}

func genPrivKey(mnemonic, keyType string) (cmtcrypto.PrivKey, error) {
switch keyType {
case crypto.CometBLSType:
if len(mnemonic) > 0 {
return nil, errors.New("BLS key type does not support mnemonic")
}
return cmtbls12381.GenPrivKey()
case "", cmted25519.KeyType:
if len(mnemonic) > 0 {
return cmted25519.GenPrivKeyFromSecret([]byte(mnemonic)), nil
}
return cmted25519.GenPrivKey(), nil
default:
return nil, fmt.Errorf("unsupported consensus key type %q", keyType)
}
}

// loadOrGenFilePV loads a FilePV from the given file paths or generates a
// new one from privKey and saves it.
func loadOrGenFilePV(
privKey cmtcrypto.PrivKey, keyFilePath, stateFilePath string,
) *privval.FilePV {
if _, err := os.Stat(keyFilePath); err == nil {
return privval.LoadFilePV(keyFilePath, stateFilePath)
}
pv := privval.NewFilePV(privKey, keyFilePath, stateFilePath)
pv.Save()
return pv
}
2 changes: 1 addition & 1 deletion config/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package config
import (
"fmt"

pruningtypes "cosmossdk.io/store/pruning/types"
pruningtypes "github.com/cosmos/cosmos-sdk/store/v2/pruning/types"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/spf13/viper"
)
Expand Down
7 changes: 4 additions & 3 deletions consensus-types/types/signed_beacon_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ func TestSignedBeaconBlock_SignBeaconBlock(t *testing.T) {
t.Parallel()
runForAllSupportedVersions(t, func(t *testing.T, v common.Version) {
// Generate a new bls key signer
filePV, err := privval.GenFilePV(
privKey, err := generatePrivKey()
require.NoError(t, err)
filePV := privval.NewFilePV(
privKey,
"signed_beacon_block_test_filepv_key",
"signed_beacon_block_test_filepv_state",
generatePrivKey,
)
require.NoError(t, err)
blsSigner := signer.BLSSigner{PrivValidator: filePV}

// Generate real signed beacon block
Expand Down
Loading
Loading