miner: fix regression, add test for starting while download (#21547)

Fixes a regression introduced in #21536
This commit is contained in:
Marius van der Wijden 2020-09-11 18:17:09 +02:00 committed by GitHub
parent 7cf56d6f06
commit df219e23df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -88,6 +88,7 @@ func (miner *Miner) update() {
defer events.Unsubscribe() defer events.Unsubscribe()
shouldStart := false shouldStart := false
canStart := true
for { for {
select { select {
case ev := <-events.Chan(): case ev := <-events.Chan():
@ -98,21 +99,27 @@ func (miner *Miner) update() {
case downloader.StartEvent: case downloader.StartEvent:
wasMining := miner.Mining() wasMining := miner.Mining()
miner.worker.stop() miner.worker.stop()
canStart = false
if wasMining { if wasMining {
// Resume mining after sync was finished // Resume mining after sync was finished
shouldStart = true shouldStart = true
log.Info("Mining aborted due to sync") log.Info("Mining aborted due to sync")
} }
case downloader.DoneEvent, downloader.FailedEvent: case downloader.DoneEvent, downloader.FailedEvent:
canStart = true
if shouldStart { if shouldStart {
miner.SetEtherbase(miner.coinbase) miner.SetEtherbase(miner.coinbase)
miner.worker.start() miner.worker.start()
} }
} }
case addr := <-miner.startCh: case addr := <-miner.startCh:
miner.SetEtherbase(addr) if canStart {
miner.worker.start() miner.SetEtherbase(addr)
miner.worker.start()
}
shouldStart = true
case <-miner.stopCh: case <-miner.stopCh:
shouldStart = false
miner.worker.stop() miner.worker.stop()
case <-miner.exitCh: case <-miner.exitCh:
miner.worker.close() miner.worker.close()

View File

@ -97,6 +97,19 @@ func TestMiner(t *testing.T) {
waitForMiningState(t, miner, true) waitForMiningState(t, miner, true)
} }
func TestStartWhileDownload(t *testing.T) {
miner, mux := createMiner(t)
waitForMiningState(t, miner, false)
miner.Start(common.HexToAddress("0x12345"))
waitForMiningState(t, miner, true)
// Stop the downloader and wait for the update loop to run
mux.Post(downloader.StartEvent{})
waitForMiningState(t, miner, false)
// Starting the miner after the downloader should not work
miner.Start(common.HexToAddress("0x12345"))
waitForMiningState(t, miner, false)
}
func TestStartStopMiner(t *testing.T) { func TestStartStopMiner(t *testing.T) {
miner, _ := createMiner(t) miner, _ := createMiner(t)
waitForMiningState(t, miner, false) waitForMiningState(t, miner, false)