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