aboutsummaryrefslogtreecommitdiffstats
path: root/core/chain_makers.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2018-11-07 22:07:43 +0800
committerGitHub <noreply@github.com>2018-11-07 22:07:43 +0800
commit0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d (patch)
tree58ca06c4e7a74b586214b62d879101bc98d970e3 /core/chain_makers.go
parent36ca85fa1c2936087b2c3d976d3576f0f5d2157e (diff)
downloadgo-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar.gz
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar.bz2
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar.lz
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar.xz
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.tar.zst
go-tangerine-0bcff8f52505fa1eece6c8fbfc5cab8aa9d9ed5d.zip
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.
Diffstat (limited to 'core/chain_makers.go')
-rw-r--r--core/chain_makers.go44
1 files changed, 28 insertions, 16 deletions
diff --git a/core/chain_makers.go b/core/chain_makers.go
index 0bc453fdf..e3a5537a4 100644
--- a/core/chain_makers.go
+++ b/core/chain_makers.go
@@ -33,12 +33,11 @@ import (
// BlockGen creates blocks for testing.
// See GenerateChain for a detailed explanation.
type BlockGen struct {
- i int
- parent *types.Block
- chain []*types.Block
- chainReader consensus.ChainReader
- header *types.Header
- statedb *state.StateDB
+ i int
+ parent *types.Block
+ chain []*types.Block
+ header *types.Header
+ statedb *state.StateDB
gasPool *GasPool
txs []*types.Transaction
@@ -138,7 +137,7 @@ func (b *BlockGen) AddUncle(h *types.Header) {
// For index -1, PrevBlock returns the parent block given to GenerateChain.
func (b *BlockGen) PrevBlock(index int) *types.Block {
if index >= b.i {
- panic("block index out of range")
+ panic(fmt.Errorf("block index %d out of range (%d,%d)", index, -1, b.i))
}
if index == -1 {
return b.parent
@@ -154,7 +153,8 @@ func (b *BlockGen) OffsetTime(seconds int64) {
if b.header.Time.Cmp(b.parent.Header().Time) <= 0 {
panic("block time out of range")
}
- b.header.Difficulty = b.engine.CalcDifficulty(b.chainReader, b.header.Time.Uint64(), b.parent.Header())
+ chainreader := &fakeChainReader{config: b.config}
+ b.header.Difficulty = b.engine.CalcDifficulty(chainreader, b.header.Time.Uint64(), b.parent.Header())
}
// 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
config = params.TestChainConfig
}
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
+ chainreader := &fakeChainReader{config: config}
genblock := func(i int, parent *types.Block, statedb *state.StateDB) (*types.Block, types.Receipts) {
- // TODO(karalabe): This is needed for clique, which depends on multiple blocks.
- // It's nonetheless ugly to spin up a blockchain here. Get rid of this somehow.
- blockchain, _ := NewBlockChain(db, nil, config, engine, vm.Config{}, nil)
- defer blockchain.Stop()
-
- b := &BlockGen{i: i, parent: parent, chain: blocks, chainReader: blockchain, statedb: statedb, config: config, engine: engine}
- b.header = makeHeader(b.chainReader, parent, statedb, b.engine)
+ b := &BlockGen{i: i, chain: blocks, parent: parent, statedb: statedb, config: config, engine: engine}
+ b.header = makeHeader(chainreader, parent, statedb, b.engine)
// Mutate the state and block according to any hard-fork specs
if daoBlock := config.DAOForkBlock; daoBlock != nil {
@@ -201,7 +197,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
}
if b.engine != nil {
// Finalize and seal the block
- block, _ := b.engine.Finalize(b.chainReader, b.header, statedb, b.txs, b.uncles, b.receipts)
+ block, _ := b.engine.Finalize(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
// Write state changes to db
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
})
return blocks
}
+
+type fakeChainReader struct {
+ config *params.ChainConfig
+ genesis *types.Block
+}
+
+// Config returns the chain configuration.
+func (cr *fakeChainReader) Config() *params.ChainConfig {
+ return cr.config
+}
+
+func (cr *fakeChainReader) CurrentHeader() *types.Header { return nil }
+func (cr *fakeChainReader) GetHeaderByNumber(number uint64) *types.Header { return nil }
+func (cr *fakeChainReader) GetHeaderByHash(hash common.Hash) *types.Header { return nil }
+func (cr *fakeChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { return nil }
+func (cr *fakeChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { return nil }