Skip to content

Commit

Permalink
Test for openInitializeChainDb
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoximenes committed May 24, 2024
1 parent abe368b commit f06ff8e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 24 deletions.
89 changes: 89 additions & 0 deletions cmd/nitro/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package main

import (
"context"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/node"
"github.com/offchainlabs/nitro/arbnode"
"github.com/offchainlabs/nitro/cmd/chaininfo"
"github.com/offchainlabs/nitro/execution/gethexec"
"github.com/offchainlabs/nitro/util/testhelpers"
)

func TestOpenInitializeChainDbIncompatibleStateScheme(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

stackConfig := testhelpers.CreateStackConfigForTest(t.TempDir())
stack, err := node.New(stackConfig)
defer stack.Close()
Require(t, err)

nodeConfig := NodeConfigDefault
nodeConfig.Execution.Caching.StateScheme = rawdb.PathScheme
nodeConfig.Chain.ID = 42161
nodeConfig.Node = *arbnode.ConfigDefaultL2Test()
// force InitializeArbosInDatabase to be tested
nodeConfig.Init.DevInit = true
nodeConfig.Init.DevInitAddress = "0x3f1Eae7D46d88F08fc2F8ed27FCb2AB183EB2d0E"

l1Client := ethclient.NewClient(stack.Attach())

// opening for the first time doesn't error
chainDb, blockchain, err := openInitializeChainDb(
ctx,
stack,
&nodeConfig,
new(big.Int).SetUint64(nodeConfig.Chain.ID),
gethexec.DefaultCacheConfigFor(stack, &nodeConfig.Execution.Caching),
&nodeConfig.Persistent,
l1Client,
chaininfo.RollupAddresses{},
)
Require(t, err)
blockchain.Stop()
err = chainDb.Close()
Require(t, err)

// opening for the second time doesn't error
chainDb, blockchain, err = openInitializeChainDb(
ctx,
stack,
&nodeConfig,
new(big.Int).SetUint64(nodeConfig.Chain.ID),
gethexec.DefaultCacheConfigFor(stack, &nodeConfig.Execution.Caching),
&nodeConfig.Persistent,
l1Client,
chaininfo.RollupAddresses{},
)
Require(t, err)
blockchain.Stop()
err = chainDb.Close()
Require(t, err)

// opening with a different state scheme errors
nodeConfig.Execution.Caching.StateScheme = rawdb.HashScheme
_, _, err = openInitializeChainDb(
ctx,
stack,
&nodeConfig,
new(big.Int).SetUint64(nodeConfig.Chain.ID),
gethexec.DefaultCacheConfigFor(stack, &nodeConfig.Execution.Caching),
&nodeConfig.Persistent,
l1Client,
chaininfo.RollupAddresses{},
)
if !strings.Contains(err.Error(), "incompatible state scheme, stored: path, provided: hash") {
t.Fatalf("Failed to detect incompatible state scheme")
}
}
27 changes: 5 additions & 22 deletions system_tests/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func (b *NodeBuilder) DefaultConfig(t *testing.T, withL1 bool) *NodeBuilder {
b.L1Info = NewL1TestInfo(t)
b.L2Info = NewArbTestInfo(t, b.chainConfig.ChainID)
b.dataDir = t.TempDir()
b.l1StackConfig = createStackConfigForTest(b.dataDir)
b.l2StackConfig = createStackConfigForTest(b.dataDir)
b.l1StackConfig = testhelpers.CreateStackConfigForTest(b.dataDir)
b.l2StackConfig = testhelpers.CreateStackConfigForTest(b.dataDir)
b.execConfig = gethexec.ConfigDefaultTest()
return b
}
Expand Down Expand Up @@ -505,23 +505,6 @@ func createTestL1BlockChain(t *testing.T, l1info info) (info, *ethclient.Client,
return createTestL1BlockChainWithConfig(t, l1info, nil)
}

func createStackConfigForTest(dataDir string) *node.Config {
stackConf := node.DefaultConfig
stackConf.DataDir = dataDir
stackConf.UseLightweightKDF = true
stackConf.WSPort = 0
stackConf.WSModules = append(stackConf.WSModules, "eth", "debug")
stackConf.HTTPPort = 0
stackConf.HTTPHost = ""
stackConf.HTTPModules = append(stackConf.HTTPModules, "eth", "debug")
stackConf.P2P.NoDiscovery = true
stackConf.P2P.NoDial = true
stackConf.P2P.ListenAddr = ""
stackConf.P2P.NAT = nil
stackConf.DBEngine = "leveldb" // TODO Try pebble again in future once iterator race condition issues are fixed
return &stackConf
}

func createRedisGroup(ctx context.Context, t *testing.T, streamName string, client redis.UniversalClient) {
t.Helper()
// Stream name and group name are the same.
Expand Down Expand Up @@ -633,7 +616,7 @@ func createTestL1BlockChainWithConfig(t *testing.T, l1info info, stackConfig *no
l1info = NewL1TestInfo(t)
}
if stackConfig == nil {
stackConfig = createStackConfigForTest(t.TempDir())
stackConfig = testhelpers.CreateStackConfigForTest(t.TempDir())
}
l1info.GenerateAccount("Faucet")

Expand Down Expand Up @@ -769,7 +752,7 @@ func createL2BlockChainWithStackConfig(
var stack *node.Node
var err error
if stackConfig == nil {
stackConfig = createStackConfigForTest(dataDir)
stackConfig = testhelpers.CreateStackConfigForTest(dataDir)
}
stack, err = node.New(stackConfig)
Require(t, err)
Expand Down Expand Up @@ -976,7 +959,7 @@ func Create2ndNodeWithConfig(
l1client := ethclient.NewClient(l1rpcClient)

if stackConfig == nil {
stackConfig = createStackConfigForTest(t.TempDir())
stackConfig = testhelpers.CreateStackConfigForTest(t.TempDir())
}
l2stack, err := node.New(stackConfig)
Require(t, err)
Expand Down
3 changes: 2 additions & 1 deletion system_tests/das_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/headerreader"
"github.com/offchainlabs/nitro/util/signature"
"github.com/offchainlabs/nitro/util/testhelpers"
"golang.org/x/exp/slog"
)

Expand Down Expand Up @@ -175,7 +176,7 @@ func TestDASRekey(t *testing.T) {

// Restart the node on the new keyset against the new DAS server running on the same disk as the first with new keys

stackConfig := createStackConfigForTest(nodeDir)
stackConfig := testhelpers.CreateStackConfigForTest(nodeDir)
l2stackA, err := node.New(stackConfig)
Require(t, err)

Expand Down
2 changes: 1 addition & 1 deletion system_tests/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ func TestWasmRecreate(t *testing.T) {
Require(t, err)

testDir := t.TempDir()
nodeBStack := createStackConfigForTest(testDir)
nodeBStack := testhelpers.CreateStackConfigForTest(testDir)
nodeB, cleanupB := builder.Build2ndNode(t, &SecondNodeParams{stackConfig: nodeBStack})

_, err = EnsureTxSucceeded(ctx, nodeB.Client, storeTx)
Expand Down
23 changes: 23 additions & 0 deletions util/testhelpers/stackconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package testhelpers

import "github.com/ethereum/go-ethereum/node"

func CreateStackConfigForTest(dataDir string) *node.Config {
stackConf := node.DefaultConfig
stackConf.DataDir = dataDir
stackConf.UseLightweightKDF = true
stackConf.WSPort = 0
stackConf.WSModules = append(stackConf.WSModules, "eth", "debug")
stackConf.HTTPPort = 0
stackConf.HTTPHost = ""
stackConf.HTTPModules = append(stackConf.HTTPModules, "eth", "debug")
stackConf.P2P.NoDiscovery = true
stackConf.P2P.NoDial = true
stackConf.P2P.ListenAddr = ""
stackConf.P2P.NAT = nil
stackConf.DBEngine = "leveldb" // TODO Try pebble again in future once iterator race condition issues are fixed
return &stackConf
}

0 comments on commit f06ff8e

Please sign in to comment.