miner: remove chain events goroutine in TestGenerateAndImport*

This commit is contained in:
Felix Lange 2020-04-03 15:12:51 +02:00
parent 7bb87de617
commit de2cf974cf

View file

@ -17,7 +17,6 @@
package miner package miner
import ( import (
"fmt"
"math/big" "math/big"
"math/rand" "math/rand"
"sync/atomic" "sync/atomic"
@ -210,58 +209,37 @@ func testGenerateBlockAndImport(t *testing.T, isClique bool) {
w, b := newTestWorker(t, chainConfig, engine, db, 0) w, b := newTestWorker(t, chainConfig, engine, db, 0)
defer w.close() defer w.close()
// This test chain imports the mined blocks.
db2 := rawdb.NewMemoryDatabase() db2 := rawdb.NewMemoryDatabase()
b.genesis.MustCommit(db2) b.genesis.MustCommit(db2)
chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil) chain, _ := core.NewBlockChain(db2, nil, b.chain.Config(), engine, vm.Config{}, nil)
defer chain.Stop() defer chain.Stop()
var ( // Ignore empty commit here for less noise.
loopErr = make(chan error)
newBlock = make(chan struct{})
stop = make(chan struct{})
subscribe = make(chan struct{})
)
listenNewBlock := func() {
sub := w.mux.Subscribe(core.NewMinedBlockEvent{})
defer sub.Unsubscribe()
subscribe <- struct{}{}
for item := range sub.Chan() {
block := item.Data.(core.NewMinedBlockEvent).Block
_, err := chain.InsertChain([]*types.Block{block})
if err != nil {
select {
case <-stop:
return
case loopErr <- fmt.Errorf("failed to insert new mined block:%d, error:%v", block.NumberU64(), err):
}
}
select {
case <-stop:
return
case newBlock <- struct{}{}:
}
}
}
// Ignore empty commit here for less noise
w.skipSealHook = func(task *task) bool { w.skipSealHook = func(task *task) bool {
return len(task.receipts) == 0 return len(task.receipts) == 0
} }
go listenNewBlock()
<-subscribe // Ensure the subscription is created // Wait for mined blocks.
w.start() // Start mining! sub := w.mux.Subscribe(core.NewMinedBlockEvent{})
defer sub.Unsubscribe()
// Start mining!
w.start()
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
b.txPool.AddLocal(b.newRandomTx(true)) b.txPool.AddLocal(b.newRandomTx(true))
b.txPool.AddLocal(b.newRandomTx(false)) b.txPool.AddLocal(b.newRandomTx(false))
w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()}) w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()})
w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()}) w.postSideBlock(core.ChainSideEvent{Block: b.newRandomUncle()})
select { select {
case e := <-loopErr: case ev := <-sub.Chan():
t.Fatal(e) block := ev.Data.(core.NewMinedBlockEvent).Block
case <-newBlock: if _, err := chain.InsertChain([]*types.Block{block}); err != nil {
case <-time.NewTimer(3 * time.Second).C: // Worker needs 1s to include new changes. t.Fatalf("failed to insert new mined block %d: %v", block.NumberU64(), err)
}
case <-time.After(3 * time.Second): // Worker needs 1s to include new changes.
t.Fatalf("timeout") t.Fatalf("timeout")
} }
} }