Skip to content

Commit

Permalink
core: use in-memory freezer for tests (#29720)
Browse files Browse the repository at this point in the history
* core: simplify chain tests

* core, eth, cmd: use in-memory freezer for tests

* core: restore tests
  • Loading branch information
rjl493456442 authored May 8, 2024
1 parent e96de64 commit 9ec5008
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
3 changes: 1 addition & 2 deletions cmd/utils/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ func TestHistoryImportAndExport(t *testing.T) {
}

// Now import Era.
freezer := t.TempDir()
db2, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), freezer, "", false)
db2, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
panic(err)
}
Expand Down
2 changes: 0 additions & 2 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,10 @@ func testHeaderVerificationForMerging(t *testing.T, isClique bool) {
preHeaders := make([]*types.Header, len(preBlocks))
for i, block := range preBlocks {
preHeaders[i] = block.Header()
t.Logf("Pre-merge header: %d", block.NumberU64())
}
postHeaders := make([]*types.Header, len(postBlocks))
for i, block := range postBlocks {
postHeaders[i] = block.Header()
t.Logf("Post-merge header: %d", block.NumberU64())
}
// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
chain, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{}, nil, nil)
Expand Down
33 changes: 17 additions & 16 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ func testFastVsFullChains(t *testing.T, scheme string) {
t.Fatalf("failed to insert receipt %d: %v", n, err)
}
// Freezer style fast import the chain.
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
t.Fatalf("failed to create temp freezer db: %v", err)
}
Expand Down Expand Up @@ -875,12 +875,12 @@ func testLightVsFastVsFullChainHeads(t *testing.T, scheme string) {
BaseFee: big.NewInt(params.InitialBaseFee),
}
)
height := uint64(1024)
height := uint64(64)
_, blocks, receipts := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(height), nil)

// makeDb creates a db instance for testing.
makeDb := func() ethdb.Database {
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
t.Fatalf("failed to create temp freezer db: %v", err)
}
Expand Down Expand Up @@ -1768,7 +1768,7 @@ func testLargeReorgTrieGC(t *testing.T, scheme string) {
competitor, _ := GenerateChain(genesis.Config, shared[len(shared)-1], engine, genDb, 2*state.TriesInMemory+1, func(i int, b *BlockGen) { b.SetCoinbase(common.Address{3}) })

// Import the shared chain and the original canonical one
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
defer db.Close()

chain, err := NewBlockChain(db, DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil)
Expand Down Expand Up @@ -1833,7 +1833,7 @@ func testBlockchainRecovery(t *testing.T, scheme string) {
funds = big.NewInt(1000000000)
gspec = &Genesis{Config: params.TestChainConfig, Alloc: types.GenesisAlloc{address: {Balance: funds}}}
)
height := uint64(1024)
height := uint64(64)
_, blocks, receipts := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(height), nil)

// Import the chain as a ancient-first node and ensure all pointers are updated
Expand Down Expand Up @@ -1908,7 +1908,7 @@ func testInsertReceiptChainRollback(t *testing.T, scheme string) {
}

// Set up a BlockChain that uses the ancient store.
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
ancientDb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
t.Fatalf("failed to create temp freezer db: %v", err)
}
Expand Down Expand Up @@ -1978,7 +1978,7 @@ func testLowDiffLongChain(t *testing.T, scheme string) {
})

// Import the canonical chain
diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
defer diskdb.Close()

chain, err := NewBlockChain(diskdb, DefaultCacheConfigWithScheme(scheme), genesis, nil, engine, vm.Config{}, nil, nil)
Expand Down Expand Up @@ -2190,7 +2190,7 @@ func testInsertKnownChainData(t *testing.T, typ string, scheme string) {
b.OffsetTime(-9) // A higher difficulty
})
// Import the shared chain and the original canonical one
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
t.Fatalf("failed to create temp freezer db: %v", err)
}
Expand Down Expand Up @@ -2361,7 +2361,7 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
}
})
// Import the shared chain and the original canonical one
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
chaindb, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
t.Fatalf("failed to create temp freezer db: %v", err)
}
Expand Down Expand Up @@ -3634,18 +3634,19 @@ func testSetCanonical(t *testing.T, scheme string) {
Alloc: types.GenesisAlloc{address: {Balance: funds}},
BaseFee: big.NewInt(params.InitialBaseFee),
}
signer = types.LatestSigner(gspec.Config)
engine = ethash.NewFaker()
signer = types.LatestSigner(gspec.Config)
engine = ethash.NewFaker()
chainLength = 10
)
// Generate and import the canonical chain
_, canon, _ := GenerateChainWithGenesis(gspec, engine, 2*state.TriesInMemory, func(i int, gen *BlockGen) {
_, canon, _ := GenerateChainWithGenesis(gspec, engine, chainLength, func(i int, gen *BlockGen) {
tx, err := types.SignTx(types.NewTransaction(gen.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, gen.header.BaseFee, nil), signer, key)
if err != nil {
panic(err)
}
gen.AddTx(tx)
})
diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), t.TempDir(), "", false)
diskdb, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
defer diskdb.Close()

chain, err := NewBlockChain(diskdb, DefaultCacheConfigWithScheme(scheme), gspec, nil, engine, vm.Config{}, nil, nil)
Expand All @@ -3659,7 +3660,7 @@ func testSetCanonical(t *testing.T, scheme string) {
}

// Generate the side chain and import them
_, side, _ := GenerateChainWithGenesis(gspec, engine, 2*state.TriesInMemory, func(i int, gen *BlockGen) {
_, side, _ := GenerateChainWithGenesis(gspec, engine, chainLength, func(i int, gen *BlockGen) {
tx, err := types.SignTx(types.NewTransaction(gen.TxNonce(address), common.Address{0x00}, big.NewInt(1), params.TxGas, gen.header.BaseFee, nil), signer, key)
if err != nil {
panic(err)
Expand Down Expand Up @@ -3698,8 +3699,8 @@ func testSetCanonical(t *testing.T, scheme string) {
verify(side[len(side)-1])

// Reset the chain head to original chain
chain.SetCanonical(canon[state.TriesInMemory-1])
verify(canon[state.TriesInMemory-1])
chain.SetCanonical(canon[chainLength-1])
verify(canon[chainLength-1])
}

// TestCanonicalHashMarker tests all the canonical hash markers are updated/deleted
Expand Down
5 changes: 1 addition & 4 deletions core/txindexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core

import (
"math/big"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -211,8 +210,7 @@ func TestTxIndexer(t *testing.T) {
},
}
for _, c := range cases {
frdir := t.TempDir()
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), frdir, "", false)
db, _ := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
rawdb.WriteAncientBlocks(db, append([]*types.Block{gspec.ToBlock()}, blocks...), append([]types.Receipts{{}}, receipts...), big.NewInt(0))

// Index the initial blocks from ancient store
Expand All @@ -238,6 +236,5 @@ func TestTxIndexer(t *testing.T) {
verify(db, 0, indexer)

db.Close()
os.RemoveAll(frdir)
}
}
12 changes: 3 additions & 9 deletions eth/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package downloader
import (
"fmt"
"math/big"
"os"
"sync"
"sync/atomic"
"testing"
Expand All @@ -43,7 +42,6 @@ import (

// downloadTester is a test simulator for mocking out local block chain.
type downloadTester struct {
freezer string
chain *core.BlockChain
downloader *Downloader

Expand All @@ -58,8 +56,7 @@ func newTester(t *testing.T) *downloadTester {

// newTesterWithNotification creates a new downloader test mocker.
func newTesterWithNotification(t *testing.T, success func()) *downloadTester {
freezer := t.TempDir()
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), freezer, "", false)
db, err := rawdb.NewDatabaseWithFreezer(rawdb.NewMemoryDatabase(), "", "", false)
if err != nil {
panic(err)
}
Expand All @@ -76,9 +73,8 @@ func newTesterWithNotification(t *testing.T, success func()) *downloadTester {
panic(err)
}
tester := &downloadTester{
freezer: freezer,
chain: chain,
peers: make(map[string]*downloadTesterPeer),
chain: chain,
peers: make(map[string]*downloadTesterPeer),
}
tester.downloader = New(db, new(event.TypeMux), tester.chain, nil, tester.dropPeer, success)
return tester
Expand All @@ -89,8 +85,6 @@ func newTesterWithNotification(t *testing.T, success func()) *downloadTester {
func (dl *downloadTester) terminate() {
dl.downloader.Terminate()
dl.chain.Stop()

os.RemoveAll(dl.freezer)
}

// newPeer registers a new block download source into the downloader.
Expand Down

0 comments on commit 9ec5008

Please sign in to comment.