From ad598652cf9c10df340f7f93be8d9e42dcb0806c Mon Sep 17 00:00:00 2001 From: Alex Sharov <AskAlexSharov@gmail.com> Date: Sat, 1 May 2021 10:51:10 +0700 Subject: [PATCH] less blockchain object in tests (#1848) --- core/blockchain.go | 78 ++++----------------------------- core/headerchain.go | 30 ++----------- turbo/stages/blockchain_test.go | 72 ++++-------------------------- 3 files changed, 20 insertions(+), 160 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 7ee8a5330c..dba68eac3d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -21,7 +21,6 @@ import ( "context" "encoding/json" "fmt" - "math/big" "os" "sync" "sync/atomic" @@ -195,7 +194,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par if err != nil { return nil, err } - bc.genesisBlock = bc.GetBlockByNumber(0) + bc.genesisBlock, _ = rawdb.ReadBlockByNumber(db, 0) if bc.genesisBlock == nil { return nil, ErrNoGenesis } @@ -286,55 +285,14 @@ func (bc *BlockChain) Genesis() *types.Block { // GetBlock retrieves a block from the database by hash and number, // caching it if found. -func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { - // Short circuit if the block's already in the cache, retrieve otherwise - block := rawdb.ReadBlock(bc.db, hash, number) - if block == nil { - return nil - } - return block -} - -// GetBlockByHash retrieves a block from the database by hash, caching it if found. -func (bc *BlockChain) GetBlockByHash(hash common.Hash) *types.Block { - number := bc.hc.GetBlockNumber(bc.db, hash) - if number == nil { - return nil - } - return bc.GetBlock(hash, *number) -} - -// GetBlockByNumber retrieves a block from the database by number, caching it -// (associated with its hash) if found. -func (bc *BlockChain) GetBlockByNumber(number uint64) *types.Block { - hash, err := rawdb.ReadCanonicalHash(bc.db, number) - if err != nil { - log.Warn("ReadCanonicalHash failed", "err", err) - return nil - } - - if hash == (common.Hash{}) { - return nil - } - return bc.GetBlock(hash, number) -} - -// GetReceiptsByHash retrieves the receipts for all transactions in a given block. -func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { - if receipts, ok := bc.receiptsCache.Get(hash); ok { - return receipts.(types.Receipts) - } - number := rawdb.ReadHeaderNumber(bc.db, hash) - if number == nil { - return nil - } - receipts := rawdb.ReadReceipts(bc.db, hash, *number) - if receipts == nil { - return nil - } - bc.receiptsCache.Add(hash, receipts) - return receipts -} +//func (bc *BlockChain) GetBlock(hash common.Hash, number uint64) *types.Block { +// Short circuit if the block's already in the cache, retrieve otherwise +//block := rawdb.ReadBlock(bc.db, hash, number) +//if block == nil { +// return nil +//} +//return block +//} // Stop stops the blockchain service. If any imports are currently in progress // it will abort them using the procInterrupt. @@ -440,18 +398,6 @@ func (bc *BlockChain) CurrentHeader() *types.Header { return bc.hc.CurrentHeader() } -// GetTd retrieves a block's total difficulty in the canonical chain from the -// database by hash and number, caching it if found. -func (bc *BlockChain) GetTd(hash common.Hash, number uint64) *big.Int { - return bc.hc.GetTd(bc.db, hash, number) -} - -// GetTdByHash retrieves a block's total difficulty in the canonical chain from the -// database by hash, caching it if found. -func (bc *BlockChain) GetTdByHash(hash common.Hash) *big.Int { - return bc.hc.GetTdByHash(hash) -} - // GetHeader retrieves a block header from the database by hash and number, // caching it if found. func (bc *BlockChain) GetHeader(hash common.Hash, number uint64) *types.Header { @@ -464,12 +410,6 @@ func (bc *BlockChain) GetHeaderByHash(hash common.Hash) *types.Header { return bc.hc.GetHeaderByHash(hash) } -// HasHeader checks if a block header is present in the database or not, caching -// it if present. -func (bc *BlockChain) HasHeader(hash common.Hash, number uint64) bool { - return bc.hc.HasHeader(hash, number) -} - // GetCanonicalHash returns the canonical hash for a given block number func (bc *BlockChain) GetCanonicalHash(number uint64) common.Hash { return bc.hc.GetCanonicalHash(number) diff --git a/core/headerchain.go b/core/headerchain.go index 8a48fd022a..fd7bdf54f2 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -73,18 +73,18 @@ func NewHeaderChain(chainDb ethdb.Database, config *params.ChainConfig, engine c engine: engine, } - hc.genesisHeader = hc.GetHeaderByNumber(0) + hc.genesisHeader = rawdb.ReadHeaderByNumber(chainDb, 0) if hc.genesisHeader == nil { return nil, ErrNoGenesis } hc.currentHeader.Store(hc.genesisHeader) if head := rawdb.ReadHeadBlockHash(chainDb); head != (common.Hash{}) { - if chead := hc.GetHeaderByHash(head); chead != nil { + if chead, _ := rawdb.ReadHeaderByHash(chainDb, head); chead != nil { hc.currentHeader.Store(chead) } } - hc.currentHeaderHash = hc.CurrentHeader().Hash() + hc.currentHeaderHash = rawdb.ReadCurrentHeader(chainDb).Hash() //headHeaderGauge.Update(hc.CurrentHeader().Number.Int64()) return hc, nil @@ -120,23 +120,6 @@ func (hc *HeaderChain) GetBlockHashesFromHash(hash common.Hash, max uint64) []co return chain } -// GetTd retrieves a block's total difficulty in the canonical chain from the -// database by hash and number, caching it if found. -func (hc *HeaderChain) GetTd(dbr ethdb.Getter, hash common.Hash, number uint64) *big.Int { - td, _ := rawdb.ReadTd(dbr, hash, number) - return td -} - -// GetTdByHash retrieves a block's total difficulty in the canonical chain from the -// database by hash, caching it if found. -func (hc *HeaderChain) GetTdByHash(hash common.Hash) *big.Int { - number := hc.GetBlockNumber(hc.chainDb, hash) - if number == nil { - return nil - } - return hc.GetTd(hc.chainDb, hash, *number) -} - // GetHeader retrieves a block header from the database by hash and number, // caching it if found. func (hc *HeaderChain) GetHeader(hash common.Hash, number uint64) *types.Header { @@ -157,13 +140,6 @@ func (hc *HeaderChain) GetHeaderByHash(hash common.Hash) *types.Header { return hc.GetHeader(hash, *number) } -// HasHeader checks if a block header is present in the database or not. -// In theory, if header is present in the database, all relative components -// like td and hash->number should be present too. -func (hc *HeaderChain) HasHeader(hash common.Hash, number uint64) bool { - return rawdb.HasHeader(hc.chainDb, hash, number) -} - // GetHeaderByNumber retrieves a block header from the database by number, // caching it (associated with its hash) if found. func (hc *HeaderChain) GetHeaderByNumber(number uint64) *types.Header { diff --git a/turbo/stages/blockchain_test.go b/turbo/stages/blockchain_test.go index fddf53e39d..b3dd4ac785 100644 --- a/turbo/stages/blockchain_test.go +++ b/turbo/stages/blockchain_test.go @@ -693,17 +693,7 @@ func TestLogReorgs(t *testing.T) { signer = types.LatestSigner(gspec.Config) ) - cacheConfig := &core.CacheConfig{ - NoHistory: false, - Pruning: false, - } - txCacher := core.NewTxSenderCacher(1) - blockchain, _ := core.NewBlockChain(db, cacheConfig, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, txCacher) - blockchain.EnableReceipts(true) - defer blockchain.Stop() - rmLogsCh := make(chan core.RemovedLogsEvent, 10) - blockchain.SubscribeRemovedLogsEvent(rmLogsCh) chain, _, err := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *core.BlockGen) { if i == 1 { tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(uint256.Int), 1000000, new(uint256.Int), code), *signer, key1) @@ -756,24 +746,18 @@ var logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd func TestLogRebirth(t *testing.T) { db := ethdb.NewMemDatabase() defer db.Close() - txCacher := core.NewTxSenderCacher(1) var ( - key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") - addr1 = crypto.PubkeyToAddress(key1.PublicKey) - gspec = &core.Genesis{Config: params.TestChainConfig, Alloc: core.GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}} - genesis = gspec.MustCommit(db) - signer = types.LatestSigner(gspec.Config) - engine = ethash.NewFaker() - blockchain, _ = core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{}, nil, txCacher) + key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") + addr1 = crypto.PubkeyToAddress(key1.PublicKey) + gspec = &core.Genesis{Config: params.TestChainConfig, Alloc: core.GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}} + genesis = gspec.MustCommit(db) + signer = types.LatestSigner(gspec.Config) + engine = ethash.NewFaker() ) - blockchain.EnableReceipts(true) - defer blockchain.Stop() // The event channels. newLogCh := make(chan []*types.Log, 10) rmLogsCh := make(chan core.RemovedLogsEvent, 10) - blockchain.SubscribeLogsEvent(newLogCh) - blockchain.SubscribeRemovedLogsEvent(rmLogsCh) // This chain contains a single log. chain, _, chainErr := core.GenerateChain(params.TestChainConfig, genesis, engine, db, 2, func(i int, gen *core.BlockGen) { @@ -849,15 +833,8 @@ func TestSideLogRebirth(t *testing.T) { signer = types.LatestSigner(gspec.Config) ) - txCacher := core.NewTxSenderCacher(1) - blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, txCacher) - defer blockchain.Stop() - newLogCh := make(chan []*types.Log, 10) rmLogsCh := make(chan core.RemovedLogsEvent, 10) - blockchain.SubscribeLogsEvent(newLogCh) - blockchain.SubscribeRemovedLogsEvent(rmLogsCh) - // Generate main chain chain, _, err := core.GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *core.BlockGen) { if i == 1 { @@ -973,10 +950,6 @@ func TestEIP155Transition(t *testing.T) { genesis = gspec.MustCommit(db) ) - txCacher := core.NewTxSenderCacher(1) - blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, txCacher) - defer blockchain.Stop() - blocks, _, chainErr := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 4, func(i int, block *core.BlockGen) { var ( tx types.Transaction @@ -1025,12 +998,12 @@ func TestEIP155Transition(t *testing.T) { if _, chainErr = stagedsync.InsertBlocksInStages(db, ethdb.DefaultStorageMode, gspec.Config, &vm.Config{}, ethash.NewFaker(), blocks, true /* checkRoot */); chainErr != nil { t.Fatal(chainErr) } - block := blockchain.GetBlockByNumber(1) + block, _ := rawdb.ReadBlockByNumber(db, 1) if block.Transactions()[0].Protected() { t.Error("Expected block[0].txs[0] to not be replay protected") } - block = blockchain.GetBlockByNumber(3) + block, _ = rawdb.ReadBlockByNumber(db, 3) if block.Transactions()[0].Protected() { t.Error("Expected block[3].txs[0] to not be replay protected") } @@ -1090,20 +1063,6 @@ func doModesTest(history, preimages, receipts, txlookup bool) error { genesis, _, _ = gspec.Commit(db, history) ) - cacheConfig := &core.CacheConfig{ - Pruning: false, - BlocksBeforePruning: 1024, - DownloadOnly: false, - NoHistory: !history, - } - - txCacher := core.NewTxSenderCacher(1) - blockchain, _ := core.NewBlockChain(db, cacheConfig, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, txCacher) - blockchain.EnableReceipts(receipts) - blockchain.EnablePreimages(preimages) - blockchain.EnableTxLookupIndex(txlookup) - defer blockchain.Stop() - blocks, _, err := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 4, func(i int, block *core.BlockGen) { var ( tx types.Transaction @@ -1313,10 +1272,6 @@ func TestDoubleAccountRemoval(t *testing.T) { genesis = gspec.MustCommit(db) ) - txCacher := core.NewTxSenderCacher(1) - blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, txCacher) - defer blockchain.Stop() - var theAddr common.Address blocks, _, err := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 3, func(i int, block *core.BlockGen) { @@ -1440,17 +1395,6 @@ func TestLargeReorgTrieGC(t *testing.T) { diskdb := ethdb.NewMemDatabase() defer diskdb.Close() (&core.Genesis{Config: params.TestChainConfig}).MustCommit(diskdb) - cacheConfig := &core.CacheConfig{ - NoHistory: false, - Pruning: false, - } - txCacher := core.NewTxSenderCacher(1) - chain, err := core.NewBlockChain(diskdb, cacheConfig, params.TestChainConfig, engine, vm.Config{}, nil, txCacher) - if err != nil { - t.Fatalf("failed to create tester chain: %v", err) - } - defer chain.Stop() - db := ethdb.NewMemDatabase() defer db.Close() genesis := (&core.Genesis{Config: params.TestChainConfig}).MustCommit(db) -- GitLab