Skip to content

Commit 0bcff8f

Browse files
authored
eth/downloader: speed up tests by generating chain only once (#17916)
* core: speed up GenerateChain Use a mock implementation of ChainReader instead of creating and destroying a BlockChain object for each generated block. * eth/downloader: speed up tests by generating chain only once This change reworks the downloader tests so they share a common test blockchain instead of generating a chain in every test. The tests are roughly twice as fast now.
1 parent 36ca85f commit 0bcff8f

3 files changed

Lines changed: 519 additions & 560 deletions

File tree

‎core/chain_makers.go‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ import (
3333
// BlockGen creates blocks for testing.
3434
// See GenerateChain for a detailed explanation.
3535
type BlockGen struct {
36-
i int
37-
parent *types.Block
38-
chain []*types.Block
39-
chainReader consensus.ChainReader
40-
header *types.Header
41-
statedb *state.StateDB
36+
i int
37+
parent *types.Block
38+
chain []*types.Block
39+
header *types.Header
40+
statedb *state.StateDB
4241

4342
gasPool *GasPool
4443
txs []*types.Transaction
@@ -138,7 +137,7 @@ func (b *BlockGen) AddUncle(h *types.Header) {
138137
// For index -1, PrevBlock returns the parent block given to GenerateChain.
139138
func (b *BlockGen) PrevBlock(index int) *types.Block {
140139
if index >= b.i {
141-
panic("block index out of range")
140+
panic(fmt.Errorf("block index %d out of range (%d,%d)", index, -1, b.i))
142141
}
143142
if index == -1 {
144143
return b.parent
@@ -154,7 +153,8 @@ func (b *BlockGen) OffsetTime(seconds int64) {
154153
if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
155154
panic("block time out of range")
156155
}
157-
b.header.Difficulty = b.engine.CalcDifficulty(b.chainReader, b.header.Time.Uint64(), b.parent.Header())
156+
chainreader := &fakeChainReader{config: b.config}
157+
b.header.Difficulty = b.engine.CalcDifficulty(chainreader, b.header.Time.Uint64(), b.parent.Header())
158158
}
159159

160160
// GenerateChain creates a chain of n blocks. The first block's
@@ -174,14 +174,10 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
174174
config = params.TestChainConfig
175175
}
176176
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
177+
chainreader := &fakeChainReader{config: config}
177178
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
178-
// TODO(karalabe): This is needed for clique, which depends on multiple blocks.
179-
// It's nonetheless ugly to spin up a blockchain here. Get rid of this somehow.
180-
blockchain, _ := NewBlockChain(db, nil, config, engine, vm.Config{}, nil)
181-
defer blockchain.Stop()
182-
183-
b := &BlockGen{i: i, parent: parent, chain: blocks, chainReader: blockchain, statedb: statedb, config: config, engine: engine}
184-
b.header = makeHeader(b.chainReader, parent, statedb, b.engine)
179+
b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
180+
b.header = makeHeader(chainreader, parent, statedb, b.engine)
185181

186182
// Mutate the state and block according to any hard-fork specs
187183
if daoBlock := config.DAOForkBlock; daoBlock != nil {
@@ -201,7 +197,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
201197
}
202198
if b.engine != nil {
203199
// Finalize and seal the block
204-
block, _ := b.engine.Finalize(b.chainReader, b.header, statedb, b.txs, b.uncles, b.receipts)
200+
block, _ := b.engine.Finalize(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
205201

206202
// Write state changes to db
207203
root, err := statedb.Commit(config.IsEIP158(b.header.Number))
@@ -269,3 +265,19 @@ func makeBlockChain(parent *types.Block, n int, engine consensus.Engine, db ethd
269265
})
270266
return blocks
271267
}
268+
269+
type fakeChainReader struct {
270+
config *params.ChainConfig
271+
genesis *types.Block
272+
}
273+
274+
// Config returns the chain configuration.
275+
func (cr *fakeChainReader) Config() *params.ChainConfig {
276+
return cr.config
277+
}
278+
279+
func (cr *fakeChainReader) CurrentHeader() *types.Header { return nil }
280+
func (cr *fakeChainReader) GetHeaderByNumber(number uint64) *types.Header { return nil }
281+
func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *types.Header { return nil }
282+
func (cr *fakeChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { return nil }
283+
func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }

0 commit comments

Comments
 (0)