From 4b7ace0ba6da6c3902322a830a826808b4505ea1 Mon Sep 17 00:00:00 2001 From: Manav Darji Date: Wed, 26 Mar 2025 11:13:43 +0530 Subject: [PATCH] node, internal/cli: add pruner options back, fix db creation in tests --- internal/cli/snapshot_test.go | 31 ++++++------------------------- node/database.go | 6 +++++- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/internal/cli/snapshot_test.go b/internal/cli/snapshot_test.go index 0e7e83ebbf..11a4d0ee42 100644 --- a/internal/cli/snapshot_test.go +++ b/internal/cli/snapshot_test.go @@ -65,11 +65,11 @@ func testOfflineBlockPruneWithAmountReserved(t *testing.T, amountReserved uint64 oldAncientPath := filepath.Join(chaindbPath, "ancient") newAncientPath := filepath.Join(chaindbPath, "ancient_back") - _, _, blockList, receiptsList, externTdList, startBlockNumber, _ := BlockchainCreator(t, chaindbPath, oldAncientPath, amountReserved) - node := startEthService(t, chaindbPath) defer node.Close() + _, _, blockList, receiptsList, externTdList, startBlockNumber, _ := BlockchainCreator(t, node, chaindbPath, oldAncientPath, amountReserved) + // Initialize a block pruner for pruning, only remain amountReserved blocks backward. testBlockPruner := pruner.NewBlockPruner(node, oldAncientPath, newAncientPath, amountReserved) dbHandles, err := server.MakeDatabaseHandles(0) @@ -78,17 +78,7 @@ func testOfflineBlockPruneWithAmountReserved(t *testing.T, amountReserved uint64 err = testBlockPruner.BlockPruneBackup(chaindbPath, 512, dbHandles, "", false, false) require.NoError(t, err, "failed to backup block") - dbBack, err := rawdb.Open(rawdb.OpenOptions{ - Type: node.Config().DBEngine, - Directory: chaindbPath, - AncientsDirectory: newAncientPath, - Namespace: "", - Cache: 0, - Handles: 0, - ReadOnly: false, - DisableFreeze: true, - IsLastOffset: false, - }) + dbBack, err := node.OpenDatabaseWithFreezer(chaindbPath, 0, 0, newAncientPath, "", false, true, false) require.NoError(t, err, "failed to create db with ancient backend") defer dbBack.Close() @@ -138,20 +128,11 @@ func testOfflineBlockPruneWithAmountReserved(t *testing.T, amountReserved uint64 require.NoError(t, err, "failed to replace ancientDb") } -func BlockchainCreator(t *testing.T, chaindbPath, AncientPath string, blockRemain uint64) (ethdb.Database, []*types.Block, []*types.Block, []types.Receipts, []*big.Int, uint64, *core.BlockChain) { +func BlockchainCreator(t *testing.T, node *node.Node, chaindbPath, AncientPath string, blockRemain uint64) (ethdb.Database, []*types.Block, []*types.Block, []types.Receipts, []*big.Int, uint64, *core.BlockChain) { t.Helper() - // Create a database with ancient freezer - db, err := rawdb.Open(rawdb.OpenOptions{ - Directory: chaindbPath, - AncientsDirectory: AncientPath, - Namespace: "", - Cache: 0, - Handles: 0, - ReadOnly: false, - DisableFreeze: false, - IsLastOffset: false, - }) + // Create a database with ancient freezer. + db, err := node.OpenDatabaseWithFreezer(chaindbPath, 0, 0, AncientPath, "", false, false, false) require.NoError(t, err, "failed to create db with ancient backend") defer db.Close() diff --git a/node/database.go b/node/database.go index e3ccb91066..8289f99186 100644 --- a/node/database.go +++ b/node/database.go @@ -36,6 +36,10 @@ type openOptions struct { Cache int // the capacity(in megabytes) of the data caching Handles int // number of files to be open simultaneously ReadOnly bool + + // Ancient pruner related fields + DisableFreeze bool + IsLastOffset bool } // openDatabase opens both a disk-based key-value database such as leveldb or pebble, but also @@ -51,7 +55,7 @@ func openDatabase(o openOptions) (ethdb.Database, error) { if len(o.AncientsDirectory) == 0 { return kvdb, nil } - frdb, err := rawdb.NewDatabaseWithFreezer(kvdb, o.AncientsDirectory, o.Namespace, o.ReadOnly) + frdb, err := rawdb.NewDatabaseWithFreezer(kvdb, o.AncientsDirectory, o.Namespace, o.ReadOnly, o.DisableFreeze, o.IsLastOffset) if err != nil { kvdb.Close() return nil, err