Skip to content

Commit

Permalink
cleanup and wip - 401 unauth
Browse files Browse the repository at this point in the history
  • Loading branch information
rezbera committed Feb 5, 2025
1 parent f2a16b4 commit 60bf98f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
1 change: 1 addition & 0 deletions consensus/cometbft/service/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (s *Service) initChain(

s.finalizeBlockState = s.resetState(ctx)

//nolint:contextcheck // ctx already passed via resetState
resValidators, err := s.initChainer(
s.finalizeBlockState.Context(),
req.AppStateBytes,
Expand Down
79 changes: 44 additions & 35 deletions testing/injected-consensus/injected-consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/berachain/beacon-kit/beacon/blockchain"
"github.com/berachain/beacon-kit/cli/commands/genesis"
"github.com/berachain/beacon-kit/cli/commands/initialize"
servertypes "github.com/berachain/beacon-kit/cli/commands/server/types"
"github.com/berachain/beacon-kit/cli/flags"
beaconkitconfig "github.com/berachain/beacon-kit/config"
"github.com/berachain/beacon-kit/config/spec"
Expand All @@ -45,6 +44,7 @@ import (
nodetypes "github.com/berachain/beacon-kit/node-core/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/math"
"github.com/berachain/beacon-kit/primitives/net/url"
"github.com/berachain/beacon-kit/storage/db"
cmtcfg "github.com/cometbft/cometbft/config"
dbm "github.com/cosmos/cosmos-db"
Expand Down Expand Up @@ -111,29 +111,23 @@ type TestNode struct {
Node nodetypes.Node
CometService *cometbft.Service
BlockchainService *blockchain.Service
CometConfig *cmtcfg.Config
Homedir string
Context context.Context
CancelFunc context.CancelFunc
}

// createConfiguration creates the BeaconKit configuration and the CometBFT configuration.
func createConfiguration(t *testing.T, tempHomeDir string) (
*beaconkitconfig.Config,
*cmtcfg.Config,
) {
func createCometConfig(t *testing.T, tempHomeDir string) *cmtcfg.Config {
t.Helper()
cmtCfg := cometbft.DefaultConfig()
cmtCfg.RootDir = tempHomeDir
// Forces Comet to Create it
// cmtCfg.NodeKey = "node_key.json"
beaconCfg := beaconkitconfig.DefaultConfig()
return beaconCfg, cmtCfg
return cmtCfg
}

func createBeaconKitConfig(t *testing.T) *beaconkitconfig.Config {
return beaconkitconfig.DefaultConfig()
}

// getAppOptions returns the Application Options we need to set for the Node Builder.
// Ideally we can avoid having to set the flags like this and just directly modify a config type.
func getAppOptions(t *testing.T, beaconKitConfig *beaconkitconfig.Config, tempHomeDir string) servertypes.AppOptions {
func getAppOptions(t *testing.T, beaconKitConfig *beaconkitconfig.Config, tempHomeDir string) *viper.Viper {
t.Helper()
appOpts := viper.New()
// Execution Client Config
Expand Down Expand Up @@ -196,12 +190,16 @@ func initCommand(t *testing.T, tempHomeDir string) {
require.NoError(t, err)
}

func StartGeth(t *testing.T, tempHomeDir string) {
// 1. Create pool
func StartGeth(t *testing.T, tempHomeDir string) (*dockertest.Pool, *dockertest.Resource, string) {
// Create pool
pool, err := dockertest.NewPool("")
require.NoError(t, err)

// 2. Pull the Geth image (if not present). This can speed up future runs.
// uses pool to try to connect to Docker
err = pool.Client.Ping()
require.NoErrorf(t, err, "Could not connect to Docker: %s", err)

// Pull the Geth image (if not present). This can speed up future runs.
err = pool.Client.PullImage(
docker.PullImageOptions{
Repository: "ethereum/client-go",
Expand Down Expand Up @@ -262,26 +260,18 @@ func StartGeth(t *testing.T, tempHomeDir string) {
return nil
})
require.NoError(t, err)
return pool, resource, authRPC
}

// NewTestNode Uses the mainnet chainspec.
func NewTestNode(t *testing.T) *TestNode {
t.Helper()

ctx, cancelFunc := context.WithCancel(context.Background())
logger := phuslu.NewLogger(os.Stdout, nil)

tempHomeDir := t.TempDir()
func InitializeHomeDir(t *testing.T, tempHomeDir string) *cmtcfg.Config {
t.Logf("tempHomeDir=%s", tempHomeDir)
beaconKitConfig, cometConfig := createConfiguration(t, tempHomeDir)
cometConfig := createCometConfig(t, tempHomeDir)

chainSpec, err := spec.TestnetChainSpec()
require.NoError(t, err)

t.Setenv(components.ChainSpecTypeEnvVar, components.TestnetChainSpecType)

appOpts := getAppOptions(t, beaconKitConfig, tempHomeDir)

// Same as `beacond init`
initCommand(t, tempHomeDir)

Expand All @@ -304,20 +294,43 @@ func NewTestNode(t *testing.T) *TestNode {

err = genesis.AddExecutionPayload(chainSpec, path.Join(tempHomeDir, "eth-genesis.json"), cometConfig)
require.NoError(t, err)
return cometConfig
}

type TestNodeInput struct {
TempHomeDir string
CometConfig *cmtcfg.Config
AuthRPCURLStr string
Logger *phuslu.Logger
}

// NewTestNode Uses the mainnet chainspec.
func NewTestNode(
t *testing.T,
input TestNodeInput,
) *TestNode {
t.Helper()

beaconKitConfig := createBeaconKitConfig(t)
authRPCURL, err := url.NewFromRaw(input.AuthRPCURLStr)
require.NoError(t, err)
beaconKitConfig.GetEngine().RPCDialURL = authRPCURL

appOpts := getAppOptions(t, beaconKitConfig, input.TempHomeDir)

// Create a database
database, err := db.OpenDB(tempHomeDir, dbm.PebbleDBBackend)
database, err := db.OpenDB(input.TempHomeDir, dbm.PebbleDBBackend)
require.NoError(t, err)

// Build a node
nb := nodebuilder.New(
nodebuilder.WithComponents[nodetypes.Node](DefaultComponents(t)),
)
node := nb.Build(
logger,
input.Logger,
database,
os.Stdout, // or some other writer
cometConfig,
input.CometConfig,
appOpts,
)

Expand All @@ -336,9 +349,5 @@ func NewTestNode(t *testing.T) *TestNode {
Node: node,
CometService: cometService,
BlockchainService: blockchainService,
CometConfig: cometConfig,
Homedir: tempHomeDir,
Context: ctx,
CancelFunc: cancelFunc,
}
}
42 changes: 37 additions & 5 deletions testing/injected-consensus/invalid_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,58 @@
package injectedconsensus_test

import (
"context"
"os"
"testing"
"time"

"github.com/berachain/beacon-kit/log/phuslu"
injectedconsensus "github.com/berachain/beacon-kit/testing/injected-consensus"

Check failure on line 30 in testing/injected-consensus/invalid_block_test.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/berachain/beacon-kit/testing/injected-consensus (-: # github.com/berachain/beacon-kit/testing/injected-consensus
"github.com/ory/dockertest"
"github.com/stretchr/testify/suite"
)

type InjectedConsensus struct {
suite.Suite
testNode *injectedconsensus.TestNode
ctx context.Context
cancelFunc context.CancelFunc
testNode *injectedconsensus.TestNode

// Geth dockertest handles for closing
poolHandle *dockertest.Pool
gethHandle *dockertest.Resource
}

func (s *InjectedConsensus) SetupTest() {
s.testNode = injectedconsensus.NewTestNode(s.T())
injectedconsensus.StartGeth(s.T(), s.testNode.Homedir)
ctx, cancelFunc := context.WithCancel(context.Background())
s.ctx = ctx
s.cancelFunc = cancelFunc

tempHomeDir := s.T().TempDir()
// Initialize home directory
cometConfig := injectedconsensus.InitializeHomeDir(s.T(), tempHomeDir)

// Start the Geth node, needs to be done first as we need the auth rpc
poolHandle, gethHandle, authRPC := injectedconsensus.StartGeth(s.T(), tempHomeDir)
s.poolHandle = poolHandle
s.gethHandle = gethHandle

// Build the Beacon node once we have the auth rpc url
logger := phuslu.NewLogger(os.Stdout, nil)
testNode := injectedconsensus.NewTestNode(s.T(),
injectedconsensus.TestNodeInput{
TempHomeDir: tempHomeDir,
CometConfig: cometConfig,
AuthRPCURLStr: authRPC,
Logger: logger,
})
s.testNode = testNode
}

func (s *InjectedConsensus) TearDownTest() {
// Ensure teardown runs no matter what
s.testNode.CancelFunc()
s.cancelFunc()
s.gethHandle.Close()
}

// func (s *InjectedConsensus) TestInitChainRequestsInvalidChainID() {
Expand All @@ -54,7 +86,7 @@ func (s *InjectedConsensus) TearDownTest() {
// TestProcessProposalRequestInvalidBlock tests the scenario where a peer sends us a block with an invalid timestamp.
func (s *InjectedConsensus) TestProcessProposalRequestInvalidBlock() {
go func() {
if err := s.testNode.Node.Start(s.testNode.Context); err != nil {
if err := s.testNode.Node.Start(s.ctx); err != nil {
s.T().Error(err)
}
}()
Expand Down

0 comments on commit 60bf98f

Please sign in to comment.