From f06ff8e70a3fd40ebe05cc1357b6dc96cb7b1dd8 Mon Sep 17 00:00:00 2001 From: Diego Ximenes Date: Fri, 24 May 2024 20:40:12 -0300 Subject: [PATCH] Test for openInitializeChainDb --- cmd/nitro/init_test.go | 89 +++++++++++++++++++++++++++++++++ system_tests/common_test.go | 27 ++-------- system_tests/das_test.go | 3 +- system_tests/program_test.go | 2 +- util/testhelpers/stackconfig.go | 23 +++++++++ 5 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 cmd/nitro/init_test.go create mode 100644 util/testhelpers/stackconfig.go diff --git a/cmd/nitro/init_test.go b/cmd/nitro/init_test.go new file mode 100644 index 00000000000..7144d2a1480 --- /dev/null +++ b/cmd/nitro/init_test.go @@ -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") + } +} diff --git a/system_tests/common_test.go b/system_tests/common_test.go index 4e82f9f870c..172890ad578 100644 --- a/system_tests/common_test.go +++ b/system_tests/common_test.go @@ -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 } @@ -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. @@ -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") @@ -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) @@ -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) diff --git a/system_tests/das_test.go b/system_tests/das_test.go index a2e49d7d2da..0e380146797 100644 --- a/system_tests/das_test.go +++ b/system_tests/das_test.go @@ -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" ) @@ -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) diff --git a/system_tests/program_test.go b/system_tests/program_test.go index 902833bfa6c..614fbc54b2b 100644 --- a/system_tests/program_test.go +++ b/system_tests/program_test.go @@ -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) diff --git a/util/testhelpers/stackconfig.go b/util/testhelpers/stackconfig.go new file mode 100644 index 00000000000..a15e6f94a96 --- /dev/null +++ b/util/testhelpers/stackconfig.go @@ -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 +}