diff --git a/consensus/clique/snapshot_test.go b/consensus/clique/snapshot_test.go index 1ab469954b4fa74d3a7dc09c91e37d4074f13334..30c67f8829c13ea002470f15b33b469ef088d71b 100644 --- a/consensus/clique/snapshot_test.go +++ b/consensus/clique/snapshot_test.go @@ -507,5 +507,6 @@ func TestClique(t *testing.T) { t.Errorf("test %d, signer %d: signer mismatch: have %x, want %x", i, j, result[j], signers[j]) } } + db.Close() } } diff --git a/core/bench_test.go b/core/bench_test.go index 035826301c84aad849f81dcd59f363d8e459ad77..18fb74283f000d2d565ea598fc70fa86c673c394 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -154,6 +154,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) { var db ethdb.Database if !disk { db = ethdb.NewMemDatabase() + defer db.Close() } else { dir, err := ioutil.TempDir("", "eth-core-bench") if err != nil { diff --git a/core/block_validator_test.go b/core/block_validator_test.go index 78d8721b53f94804c29e52cb75a5670defc11f1b..76600f1ebe505ad896e77d478620725c82aee5b4 100644 --- a/core/block_validator_test.go +++ b/core/block_validator_test.go @@ -89,8 +89,9 @@ func TestHeaderConcurrentVerification32(t *testing.T) { testHeaderConcurrentVeri func testHeaderConcurrentVerification(t *testing.T, threads int) { // Create a simple chain to verify + testdb := ethdb.NewMemDatabase() + defer testdb.Close() var ( - testdb = ethdb.NewMemDatabase() gspec = &Genesis{Config: params.TestChainConfig} genesis = gspec.MustCommit(testdb) blocks, _ = GenerateChain(context.Background(), params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 8, nil) @@ -161,8 +162,10 @@ func TestHeaderConcurrentAbortion32(t *testing.T) { testHeaderConcurrentAbortion func testHeaderConcurrentAbortion(t *testing.T, threads int) { // Create a simple chain to verify + testdb := ethdb.NewMemDatabase() + defer testdb.Close() + var ( - testdb = ethdb.NewMemDatabase() gspec = &Genesis{Config: params.TestChainConfig} genesis = gspec.MustCommit(testdb) blocks, _ = GenerateChain(context.Background(), params.TestChainConfig, genesis, ethash.NewFaker(), testdb, 1024, nil) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d026fbc9b631ad4bdd4566ef2a2daec97c150699..ed9ea4b41aed9ba88c849652e233cc646d500094 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -23,6 +23,7 @@ import ( "math/big" "math/rand" "os" + "strconv" "sync" "testing" "time" @@ -49,27 +50,13 @@ import ( var ( canonicalSeed = 1 forkSeed = 2 - - // TODO [Issue 144] run tests with BadgerDB as well - useBadgerDB = false ) // newCanonical creates a chain database, and injects a deterministic canonical // chain. Depending on the full flag, if creates either a full block chain or a // header only chain. func newCanonical(engine consensus.Engine, n int, full bool) (context.Context, ethdb.Database, *BlockChain, error) { - var db ethdb.Database - - if useBadgerDB { - var err error - db, err = ethdb.NewEphemeralBadger() - if err != nil { - panic("failed to create test database: " + err.Error()) - } - } else { - db = ethdb.NewMemDatabase() - } - + db := ethdb.NewMemDatabase() genesis, _, err := new(Genesis).Commit(db, true /* history */) if err != nil { panic(err) @@ -111,6 +98,7 @@ func testFork(t *testing.T, blockchain *BlockChain, i, n int, full bool, compara t.Fatal("could not make new canonical in testFork", err) } defer blockchain2.Stop() + defer db.Close() // Assert the chains have the same header/block at #i var hash1, hash2 common.Hash @@ -241,8 +229,9 @@ func TestLastBlock(t *testing.T) { t.Fatalf("failed to create pristine chain: %v", err) } defer blockchain.Stop() + defer db.Close() - dbCopy := db.MemCopy() + dbCopy := db.NewBatch() blocks := makeBlockChain(ctx, blockchain.CurrentBlock(), 1, ethash.NewFullFaker(), dbCopy, 0) if _, err := blockchain.InsertChain(context.Background(), blocks); err != nil { t.Fatalf("Failed to insert block: %v", err) @@ -261,11 +250,12 @@ func testExtendCanonical(t *testing.T, full bool) { length := 5 // Make first chain starting from genesis - _, _, processor, err := newCanonical(ethash.NewFaker(), length, full) + _, db, processor, err := newCanonical(ethash.NewFaker(), length, full) if err != nil { t.Fatalf("failed to make new canonical chain: %v", err) } defer processor.Stop() + defer db.Close() // Define the difficulty comparator better := func(td1, td2 *big.Int) { @@ -289,11 +279,12 @@ func testShorterFork(t *testing.T, full bool) { length := 10 // Make first chain starting from genesis - _, _, processor, err := newCanonical(ethash.NewFaker(), length, full) + _, db, processor, err := newCanonical(ethash.NewFaker(), length, full) if err != nil { t.Fatalf("failed to make new canonical chain: %v", err) } defer processor.Stop() + defer db.Close() // Define the difficulty comparator worse := func(td1, td2 *big.Int) { @@ -319,11 +310,12 @@ func testLongerFork(t *testing.T, full bool) { length := 10 // Make first chain starting from genesis - _, _, processor, err := newCanonical(ethash.NewFaker(), length, full) + _, db, processor, err := newCanonical(ethash.NewFaker(), length, full) if err != nil { t.Fatalf("failed to make new canonical chain: %v", err) } defer processor.Stop() + defer db.Close() // Define the difficulty comparator better := func(td1, td2 *big.Int) { @@ -349,11 +341,12 @@ func testEqualFork(t *testing.T, full bool) { length := 10 // Make first chain starting from genesis - _, _, processor, err := newCanonical(ethash.NewFaker(), length, full) + _, db, processor, err := newCanonical(ethash.NewFaker(), length, full) if err != nil { t.Fatalf("failed to make new canonical chain: %v", err) } defer processor.Stop() + defer db.Close() // Define the difficulty comparator equal := func(td1, td2 *big.Int) { @@ -381,6 +374,7 @@ func testBrokenChain(t *testing.T, full bool) { t.Fatalf("failed to make new canonical chain: %v", err) } defer blockchain.Stop() + defer db.Close() // Create a forked chain, and try to insert with a missing link if full { @@ -432,6 +426,7 @@ func testReorg(t *testing.T, first, second []int64, td int64, full bool) { t.Fatalf("failed to create pristine chain: %v", err) } defer blockchain.Stop() + defer db.Close() // Insert an easy and a difficult chain afterwards easyBlocks, _ := GenerateChain(ctx, params.TestChainConfig, blockchain.CurrentBlock(), ethash.NewFaker(), db.MemCopy(), len(first), func(i int, b *BlockGen) { @@ -503,6 +498,7 @@ func testBadHashes(t *testing.T, full bool) { t.Fatalf("failed to create pristine chain: %v", err) } defer blockchain.Stop() + defer db.Close() // Create a chain, ban a hash and try to import if full { @@ -536,6 +532,9 @@ func testReorgBadHashes(t *testing.T, full bool) { if err != nil { t.Fatalf("failed to create pristine chain: %v", err) } + defer blockchain.Stop() + defer db.Close() + // Create a chain, import and ban afterwards headers := makeHeaderChain(ctx, blockchain.CurrentHeader(), 4, ethash.NewFaker(), db.MemCopy(), 10) blocks := makeBlockChain(ctx, blockchain.CurrentBlock(), 4, ethash.NewFaker(), db.MemCopy(), 10) @@ -594,53 +593,62 @@ func TestBlocksInsertNonceError(t *testing.T) { testInsertNonceError(t, true) } func testInsertNonceError(t *testing.T, full bool) { for i := 1; i < 25 && !t.Failed(); i++ { - // Create a pristine chain and database - ctx, db, blockchain, err := newCanonical(ethash.NewFaker(), 0, full) - if err != nil { - t.Fatalf("failed to create pristine chain: %v", err) - } - defer blockchain.Stop() + i := i + t.Run(strconv.Itoa(i), func(t *testing.T) { + // Create a pristine chain and database + ctx, db, blockchain, err := newCanonical(ethash.NewFaker(), 0, full) + if err != nil { + t.Fatalf("failed to create pristine chain: %v", err) + } - // Create and insert a chain with a failing nonce - var ( - failAt int - failRes int - failNum uint64 - ) - if full { - blocks := makeBlockChain(ctx, blockchain.CurrentBlock(), i, ethash.NewFaker(), db.MemCopy(), 0) + defer func(db ethdb.Database) { + // could not close db because .ValidateHeaderChain could not wait for ethash.VerifyHeaders finish + time.Sleep(time.Millisecond) + db.Close() + }(db) + defer blockchain.Stop() + + // Create and insert a chain with a failing nonce + var ( + failAt int + failRes int + failNum uint64 + ) + if full { + blocks := makeBlockChain(ctx, blockchain.CurrentBlock(), i, ethash.NewFaker(), db.NewBatch(), 0) - failAt = rand.Int() % len(blocks) - failNum = blocks[failAt].NumberU64() + failAt = rand.Int() % len(blocks) // nolint:gosec + failNum = blocks[failAt].NumberU64() - blockchain.engine = ethash.NewFakeFailer(failNum) - failRes, err = blockchain.InsertChain(context.Background(), blocks) - } else { - headers := makeHeaderChain(ctx, blockchain.CurrentHeader(), i, ethash.NewFaker(), db.MemCopy(), 0) + blockchain.engine = ethash.NewFakeFailer(failNum) + failRes, err = blockchain.InsertChain(context.Background(), blocks) + } else { + headers := makeHeaderChain(ctx, blockchain.CurrentHeader(), i, ethash.NewFaker(), db.NewBatch(), 0) - failAt = rand.Int() % len(headers) - failNum = headers[failAt].Number.Uint64() + failAt = rand.Int() % len(headers) // nolint:gosec + failNum = headers[failAt].Number.Uint64() - blockchain.engine = ethash.NewFakeFailer(failNum) - blockchain.hc.engine = blockchain.engine - failRes, err = blockchain.InsertHeaderChain(headers, 1) - } - // Check that the returned error indicates the failure - if failRes != failAt { - t.Errorf("test %d: failure (%v) index mismatch: have %d, want %d", i, err, failRes, failAt) - } - // Check that all blocks after the failing block have been inserted - for j := 0; j < i-failAt; j++ { - if full { - if block := blockchain.GetBlockByNumber(failNum + uint64(j)); block != nil { - t.Errorf("test %d: invalid block in chain: %v", i, block) - } - } else { - if header := blockchain.GetHeaderByNumber(failNum + uint64(j)); header != nil { - t.Errorf("test %d: invalid header in chain: %v", i, header) + blockchain.engine = ethash.NewFakeFailer(failNum) + blockchain.hc.engine = blockchain.engine + failRes, _, _, err = blockchain.InsertHeaderChainStaged(headers, 1) + } + // Check that the returned error indicates the failure + if failRes != failAt { + t.Errorf("test %d: failure (%v) index mismatch: have %d, want %d", i, err, failRes, failAt) + } + // Check that all blocks after the failing block have been inserted + for j := 0; j < i-failAt; j++ { + if full { + if block := blockchain.GetBlockByNumber(failNum + uint64(j)); block != nil { + t.Errorf("test %d: invalid block in chain: %v", i, block) + } + } else { + if header := blockchain.GetHeaderByNumber(failNum + uint64(j)); header != nil { + t.Errorf("test %d: invalid header in chain: %v", i, header) + } } } - } + }) } } @@ -1020,17 +1028,19 @@ func TestChainTxReorgs(t *testing.T) { } func TestLogReorgs(t *testing.T) { + db := ethdb.NewMemDatabase() + defer db.Close() var ( key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") addr1 = crypto.PubkeyToAddress(key1.PublicKey) - db = ethdb.NewMemDatabase() // this code generates a log - code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00") - gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}} - genesis = gspec.MustCommit(db) - genesisDb = db.MemCopy() - signer = types.NewEIP155Signer(gspec.Config.ChainID) + code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00") + gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}} + genesis = gspec.MustCommit(db) + signer = types.NewEIP155Signer(gspec.Config.ChainID) ) + genesisDB := db.MemCopy() + defer genesisDB.Close() cacheConfig := &CacheConfig{ TrieCleanLimit: 256, @@ -1046,7 +1056,7 @@ func TestLogReorgs(t *testing.T) { rmLogsCh := make(chan RemovedLogsEvent, 10) blockchain.SubscribeRemovedLogsEvent(rmLogsCh) - chain, _ := GenerateChain(ctx, params.TestChainConfig, genesis, ethash.NewFaker(), genesisDb.MemCopy(), 2, func(i int, gen *BlockGen) { + chain, _ := GenerateChain(ctx, params.TestChainConfig, genesis, ethash.NewFaker(), genesisDB.MemCopy(), 2, func(i int, gen *BlockGen) { if i == 1 { tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(uint256.Int), 1000000, new(uint256.Int), code), signer, key1) if err != nil { @@ -1060,7 +1070,7 @@ func TestLogReorgs(t *testing.T) { t.Fatalf("failed to insert chain: %v", err) } - chain, _ = GenerateChain(ctx, params.TestChainConfig, genesis, ethash.NewFaker(), genesisDb.MemCopy(), 3, func(i int, gen *BlockGen) {}) + chain, _ = GenerateChain(ctx, params.TestChainConfig, genesis, ethash.NewFaker(), genesisDB.MemCopy(), 3, func(i int, gen *BlockGen) {}) done := make(chan struct{}) go func() { ev := <-rmLogsCh @@ -1087,10 +1097,11 @@ var logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd // This test checks that log events and RemovedLogsEvent are sent // when the chain reorganizes. func TestLogRebirth(t *testing.T) { + db := ethdb.NewMemDatabase() + defer db.Close() var ( key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") addr1 = crypto.PubkeyToAddress(key1.PublicKey) - db = ethdb.NewMemDatabase() gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}} genesis = gspec.MustCommit(db) signer = types.NewEIP155Signer(gspec.Config.ChainID) @@ -1772,9 +1783,11 @@ func TestBlockchainHeaderchainReorgConsistency(t *testing.T) { engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := (&Genesis{Config: params.TestChainConfig}).MustCommit(db) diskdb := ethdb.NewMemDatabase() + defer diskdb.Close() (&Genesis{Config: params.TestChainConfig}).MustCommit(diskdb) cacheConfig := &CacheConfig{ @@ -1835,6 +1848,7 @@ func TestLargeReorgTrieGC(t *testing.T) { engine := ethash.NewFaker() diskdb := ethdb.NewMemDatabase() + defer diskdb.Close() (&Genesis{Config: params.TestChainConfig}).MustCommit(diskdb) cacheConfig := &CacheConfig{ TrieCleanLimit: 256, @@ -1850,6 +1864,7 @@ func TestLargeReorgTrieGC(t *testing.T) { ctx := chain.WithContext(context.Background(), big.NewInt(1)) db := ethdb.NewMemDatabase() + defer db.Close() genesis := (&Genesis{Config: params.TestChainConfig}).MustCommit(db) shared, _ := GenerateChain(ctx, params.TestChainConfig, genesis, engine, db, 64, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) @@ -2008,6 +2023,7 @@ func TestLowDiffLongChain(t *testing.T) { // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := new(Genesis).MustCommit(db) var side ethdb.Database @@ -2067,6 +2083,7 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := new(Genesis).MustCommit(db) blocksNum := 2 * triesInMemory @@ -2157,6 +2174,7 @@ func testInsertKnownChainData(t *testing.T, typ string) { engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := new(Genesis).MustCommit(db) blocks, receipts := GenerateChain(context.Background(), params.TestChainConfig, genesis, engine, db, 32, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{1}) }) @@ -2286,6 +2304,7 @@ func getLongAndShortChains() (*BlockChain, []*types.Block, []*types.Block, error // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := new(Genesis).MustCommit(db) // Generate and import the canonical chain, @@ -2433,6 +2452,7 @@ func benchmarkLargeNumberOfValueToNonexisting(b *testing.B, numTxs, numBlocks in // Generate the original common chain segment and the two competing forks engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := gspec.MustCommit(db) blockGenerator := func(i int, block *BlockGen) { @@ -2449,6 +2469,7 @@ func benchmarkLargeNumberOfValueToNonexisting(b *testing.B, numTxs, numBlocks in } diskdb := ethdb.NewMemDatabase() + defer diskdb.Close() gspec.MustCommit(diskdb) chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil, nil) if err != nil { @@ -2541,6 +2562,7 @@ func TestSideImportPrunedBlocks(t *testing.T) { // Generate a canonical chain to act as the main dataset engine := ethash.NewFaker() db := ethdb.NewMemDatabase() + defer db.Close() genesis := new(Genesis).MustCommit(db) // Generate and import the canonical chain @@ -2850,6 +2872,7 @@ func TestDeleteRecreateAccount(t *testing.T) { }) // Import the canonical chain diskdb := ethdb.NewMemDatabase() + defer diskdb.Close() gspec.MustCommit(diskdb) chain, err := NewBlockChain(diskdb, nil, params.TestChainConfig, engine, vm.Config{}, nil, nil) if err != nil { diff --git a/core/dao_test.go b/core/dao_test.go index b1a70699aee30d271c4ff48b5e1cbd4ccf9089ad..8784dbb347af0db89ad14a80769e9eef0e21b314 100644 --- a/core/dao_test.go +++ b/core/dao_test.go @@ -19,6 +19,7 @@ package core import ( "context" "math/big" + "strconv" "testing" "github.com/ledgerwatch/turbo-geth/consensus/ethash" @@ -68,55 +69,63 @@ func TestDAOForkRangeExtradata(t *testing.T) { } // Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ { - // Create a pro-fork block, and try to feed into the no-fork chain - db = ethdb.NewMemDatabase() - gspec.MustCommit(db) - bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{}, nil, nil) - defer bc.Stop() - ctx = bc.WithContext(context.Background(), big.NewInt(conBc.CurrentBlock().Number().Int64()+1)) - - blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64())) - for j := 0; j < len(blocks)/2; j++ { - blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] - } - if _, err := bc.InsertChain(context.Background(), blocks); err != nil { - t.Fatalf("failed to import contra-fork chain for expansion: %v", err) - } - blocks, _ = GenerateChain(ctx, &proConf, conBc.CurrentBlock(), ethash.NewFaker(), conDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) - if _, err := conBc.InsertChain(context.Background(), blocks); err == nil { - t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0]) - } - // Create a proper no-fork block for the contra-forker - blocks, _ = GenerateChain(ctx, &conConf, conBc.CurrentBlock(), ethash.NewFaker(), conDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) - if _, err := conBc.InsertChain(context.Background(), blocks); err != nil { - t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err) - } - // Create a no-fork block, and try to feed into the pro-fork chain - db = ethdb.NewMemDatabase() - gspec.MustCommit(db) - bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{}, nil, nil) - defer bc.Stop() - ctx = bc.WithContext(context.Background(), big.NewInt(proBc.CurrentBlock().Number().Int64()+1)) - - blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64())) - for j := 0; j < len(blocks)/2; j++ { - blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] - } - if _, err := bc.InsertChain(context.Background(), blocks); err != nil { - t.Fatalf("failed to import pro-fork chain for expansion: %v", err) - } - blocks, _ = GenerateChain(ctx, &conConf, proBc.CurrentBlock(), ethash.NewFaker(), proDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) - if _, err := proBc.InsertChain(context.Background(), blocks); err == nil { - t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0]) - } - // Create a proper pro-fork block for the pro-forker - blocks, _ = GenerateChain(ctx, &proConf, proBc.CurrentBlock(), ethash.NewFaker(), proDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) - if _, err := proBc.InsertChain(context.Background(), blocks); err != nil { - t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err) - } + t.Run(strconv.Itoa(int(i)), func(t *testing.T) { + + // Create a pro-fork block, and try to feed into the no-fork chain + db = ethdb.NewMemDatabase() + defer db.Close() + gspec.MustCommit(db) + bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{}, nil, nil) + defer bc.Stop() + ctx = bc.WithContext(context.Background(), big.NewInt(conBc.CurrentBlock().Number().Int64()+1)) + + blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64())) + for j := 0; j < len(blocks)/2; j++ { + blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] + } + if _, err := bc.InsertChain(context.Background(), blocks); err != nil { + t.Fatalf("failed to import contra-fork chain for expansion: %v", err) + } + blocks, _ = GenerateChain(ctx, &proConf, conBc.CurrentBlock(), ethash.NewFaker(), conDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) + if _, err := conBc.InsertChain(context.Background(), blocks); err == nil { + t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0]) + } + // Create a proper no-fork block for the contra-forker + blocks, _ = GenerateChain(ctx, &conConf, conBc.CurrentBlock(), ethash.NewFaker(), conDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) + if _, err := conBc.InsertChain(context.Background(), blocks); err != nil { + t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err) + } + db.Close() + // Create a no-fork block, and try to feed into the pro-fork chain + db = ethdb.NewMemDatabase() + defer db.Close() + + gspec.MustCommit(db) + bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{}, nil, nil) + defer bc.Stop() + ctx = bc.WithContext(context.Background(), big.NewInt(proBc.CurrentBlock().Number().Int64()+1)) + + blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64())) + for j := 0; j < len(blocks)/2; j++ { + blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j] + } + if _, err := bc.InsertChain(context.Background(), blocks); err != nil { + t.Fatalf("failed to import pro-fork chain for expansion: %v", err) + } + blocks, _ = GenerateChain(ctx, &conConf, proBc.CurrentBlock(), ethash.NewFaker(), proDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) + if _, err := proBc.InsertChain(context.Background(), blocks); err == nil { + t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0]) + } + // Create a proper pro-fork block for the pro-forker + blocks, _ = GenerateChain(ctx, &proConf, proBc.CurrentBlock(), ethash.NewFaker(), proDb.MemCopy(), 1, func(i int, gen *BlockGen) {}) + if _, err := proBc.InsertChain(context.Background(), blocks); err != nil { + t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err) + } + }) } // Verify that contra-forkers accept pro-fork extra-datas after forking finishes db = ethdb.NewMemDatabase() + defer db.Close() gspec.MustCommit(db) bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{}, nil, nil) defer bc.Stop() @@ -135,6 +144,7 @@ func TestDAOForkRangeExtradata(t *testing.T) { } // Verify that pro-forkers accept contra-fork extra-datas after forking finishes db = ethdb.NewMemDatabase() + defer db.Close() gspec.MustCommit(db) bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{}, nil, nil) defer bc.Stop() diff --git a/core/generate_index_test.go b/core/generate_index_test.go index f84348af7a30f567e2bab7b28c6b098dd96bfe2e..cb9eca205be420611b4af98d13b2df7fe58eb81a 100644 --- a/core/generate_index_test.go +++ b/core/generate_index_test.go @@ -4,22 +4,24 @@ import ( "bytes" "encoding/binary" "fmt" - "github.com/ledgerwatch/turbo-geth/common" - "github.com/ledgerwatch/turbo-geth/common/dbutils" - "github.com/ledgerwatch/turbo-geth/crypto" - "github.com/ledgerwatch/turbo-geth/ethdb" - "github.com/ledgerwatch/turbo-geth/log" "os" "reflect" "sort" "strconv" "testing" + + "github.com/ledgerwatch/turbo-geth/common" + "github.com/ledgerwatch/turbo-geth/common/dbutils" + "github.com/ledgerwatch/turbo-geth/crypto" + "github.com/ledgerwatch/turbo-geth/ethdb" + "github.com/ledgerwatch/turbo-geth/log" ) func TestIndexGenerator_GenerateIndex_SimpleCase(t *testing.T) { test := func(blocksNum int, csBucket []byte) func(t *testing.T) { return func(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() ig := NewIndexGenerator(db, make(chan struct{})) log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) csInfo, ok := mapper[string(csBucket)] @@ -149,8 +151,8 @@ func TestIndexGenerator_Truncate(t *testing.T) { t.Fatal() } }) + db.Close() } - } func generateTestData(t *testing.T, db ethdb.Database, csBucket []byte, numOfBlocks int) ([][]byte, map[string][][]uint64) { //nolint diff --git a/core/genesis_test.go b/core/genesis_test.go index bea963a33dea2350b2f5192e0702793b7f81269d..a3ed3c66e17ecd84f0f40267747a2a6e57f765db 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -167,5 +167,6 @@ func TestSetupGenesis(t *testing.T) { t.Errorf("%s: block in DB has hash %s, want %s", test.name, stored.Hash(), test.wantHash) } } + db.Close() } } diff --git a/core/rawdb/accessors_chain_test.go b/core/rawdb/accessors_chain_test.go index ed773b407b7a348441703ff3317a2106e4f9ca00..a27e005417714d05e439cb5f26abf134603b04d6 100644 --- a/core/rawdb/accessors_chain_test.go +++ b/core/rawdb/accessors_chain_test.go @@ -37,6 +37,7 @@ import ( // Tests block header storage and retrieval operations. func TestHeaderStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() // Create a test header to move around the database and make sure it's really new header := &types.Header{Number: big.NewInt(42), Extra: []byte("test header")} @@ -70,6 +71,7 @@ func TestHeaderStorage(t *testing.T) { // Tests block body storage and retrieval operations. func TestBodyStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() // Create a test body to move around the database and make sure it's really new body := &types.Body{Uncles: []*types.Header{{Extra: []byte("test header")}}} @@ -108,6 +110,7 @@ func TestBodyStorage(t *testing.T) { // Tests block storage and retrieval operations. func TestBlockStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() // Create a test block to move around the database and make sure it's really new block := types.NewBlockWithHeader(&types.Header{ @@ -158,6 +161,8 @@ func TestBlockStorage(t *testing.T) { // Tests that partial block contents don't get reassembled into full blocks. func TestPartialBlockStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() + block := types.NewBlockWithHeader(&types.Header{ Extra: []byte("test block"), UncleHash: types.EmptyUncleHash, @@ -193,6 +198,7 @@ func TestPartialBlockStorage(t *testing.T) { // Tests block total difficulty storage and retrieval operations. func TestTdStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() // Create a test TD to move around the database and make sure it's really new hash, td := common.Hash{}, big.NewInt(314) @@ -216,6 +222,7 @@ func TestTdStorage(t *testing.T) { // Tests that canonical numbers can be mapped to hashes and retrieved. func TestCanonicalMappingStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() // Create a test canonical number and assinged hash to move around hash, number := common.Hash{0: 0xff}, uint64(314) @@ -239,6 +246,7 @@ func TestCanonicalMappingStorage(t *testing.T) { // Tests that head headers and head blocks can be assigned, individually. func TestHeadStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() blockHead := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block header")}) blockFull := types.NewBlockWithHeader(&types.Header{Extra: []byte("test block full")}) @@ -274,6 +282,8 @@ func TestHeadStorage(t *testing.T) { // Tests that receipts associated with a single block can be stored and retrieved. func TestBlockReceiptStorage(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() + ctx := context.Background() // Create a live block since we need metadata to reconstruct the receipt diff --git a/core/rawdb/accessors_indexes_test.go b/core/rawdb/accessors_indexes_test.go index d3f00b7f85c5a445e2d8406a474e77211be476f2..b28f1b6c9519967d9af2b0134704587be276e4a6 100644 --- a/core/rawdb/accessors_indexes_test.go +++ b/core/rawdb/accessors_indexes_test.go @@ -46,6 +46,7 @@ func TestLookupStorage(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { db := ethdb.NewMemDatabase() + defer db.Close() ctx := context.Background() tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), uint256.NewInt().SetUint64(111), 1111, uint256.NewInt().SetUint64(11111), []byte{0x11, 0x11, 0x11}) diff --git a/core/rlp_test.go b/core/rlp_test.go index a9cbf79914e6262afffb832e9ab0e2ae0ea85c5c..83eb33d05afb58b8364282e96418116408e83273 100644 --- a/core/rlp_test.go +++ b/core/rlp_test.go @@ -35,11 +35,12 @@ import ( ) func getBlock(transactions int, uncles int, dataSize int) *types.Block { + db := ethdb.NewMemDatabase() + defer db.Close() var ( aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa") // Generate a canonical chain to act as the main dataset engine = ethash.NewFaker() - db = ethdb.NewMemDatabase() // A sender who makes transactions, has some funds key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") address = crypto.PubkeyToAddress(key.PublicKey) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 1f6d5f223c5bb5d203ec2a8da9f6ffbb72f7b7a4..edf7ab6aca0e376ea7e0896affd0fb0a707c5d5d 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -195,6 +195,7 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) { func generateTestChain() (*core.Genesis, []*types.Block) { db := ethdb.NewMemDatabase() + defer db.Close() config := params.AllEthashProtocolChanges genesis := &core.Genesis{ Config: config, diff --git a/ethdb/database_test.go b/ethdb/database_test.go index b54fd3d46bbea2cf7a927ad1b9d840e73e57ea97..027ff8c93facb6e34c37e13ef72a0714e2f023e0 100644 --- a/ethdb/database_test.go +++ b/ethdb/database_test.go @@ -188,7 +188,9 @@ func TestBoltDB_ParallelPutGet(t *testing.T) { } func TestMemoryDB_ParallelPutGet(t *testing.T) { - testParallelPutGet(NewMemDatabase()) + db := NewMemDatabase() + defer db.Close() + testParallelPutGet(db) } func TestLMDB_ParallelPutGet(t *testing.T) { diff --git a/ethdb/kv_badger.go b/ethdb/kv_badger.go index c13ed3fccb11839c8e056500a352dc025dc22964..9dcf47857849f910b9c0d6ef0cbb2ecdd486b39b 100644 --- a/ethdb/kv_badger.go +++ b/ethdb/kv_badger.go @@ -32,9 +32,9 @@ func (opts badgerOpts) ReadOnly() badgerOpts { func (opts badgerOpts) Open(ctx context.Context) (KV, error) { logger := log.New("badger_db", opts.Badger.Dir) + opts.Badger = opts.Badger.WithMaxTableSize(128 << 20) // 128MB, default 64Mb if opts.Badger.InMemory { - opts.Badger = opts.Badger.WithMaxTableSize(1 << 20) // 4MB opts.Badger = opts.Badger.WithEventLogging(false).WithNumCompactors(1) } else { oldMaxProcs := runtime.GOMAXPROCS(0) @@ -42,7 +42,6 @@ func (opts badgerOpts) Open(ctx context.Context) (KV, error) { runtime.GOMAXPROCS(minGoMaxProcs) logger.Info("Bumping GOMAXPROCS", "old", oldMaxProcs, "new", minGoMaxProcs) } - opts.Badger = opts.Badger.WithMaxTableSize(128 << 20) // 128MB, default 64Mb } db, err := badger.Open(opts.Badger) diff --git a/ethdb/kv_lmdb.go b/ethdb/kv_lmdb.go index 43b36e2334a42ac38b5c2cb5334c7a508ea115e4..9e9bbb04f6b5499ba9e17a392427103c15590cdb 100644 --- a/ethdb/kv_lmdb.go +++ b/ethdb/kv_lmdb.go @@ -50,18 +50,15 @@ func (opts lmdbOpts) Open(ctx context.Context) (KV, error) { var logger log.Logger if opts.inMem { + err = env.SetMapSize(32 << 20) // 32MB logger = log.New("lmdb", "inMem") - err = env.SetMapSize(1 << 22) // 4MB if err != nil { return nil, err } opts.path, _ = ioutil.TempDir(os.TempDir(), "lmdb") - //opts.path = path.Join(os.TempDir(), "lmdb-in-memory") - //opts.path = "lmdb_tmp" } else { + err = env.SetMapSize(32 << 40) // 32TB logger = log.New("lmdb", path.Base(opts.path)) - - err = env.SetMapSize(1 << 45) // 1TB if err != nil { return nil, err } diff --git a/miner/worker_test.go b/miner/worker_test.go index 30de5812ce9c60bdac18c787d7744546c8c1987b..d483f7ce56df99a138c59c6ea182f6d03b4d7456 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -266,6 +266,7 @@ func testGenerateBlockAndImport(t *testing.T, testCase *testCase, isClique bool) chainConfig *params.ChainConfig db = ethdb.NewMemDatabase() ) + defer db.Close() if isClique { chainConfig = params.AllCliqueProtocolChanges chainConfig.Clique = ¶ms.CliqueConfig{Period: 1, Epoch: 30000} @@ -280,6 +281,7 @@ func testGenerateBlockAndImport(t *testing.T, testCase *testCase, isClique bool) defer w.close() db2 := ethdb.NewMemDatabase() + defer db2.Close() b.genesis.MustCommit(db2) chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil, nil) defer chain.Stop()