@@ -34,6 +34,7 @@ import (
3434 "github.com/ethereum/go-ethereum/core/vm"
3535 "github.com/ethereum/go-ethereum/crypto"
3636 "github.com/ethereum/go-ethereum/eth/downloader"
37+ "github.com/ethereum/go-ethereum/eth/fetcher"
3738 "github.com/ethereum/go-ethereum/event"
3839 "github.com/ethereum/go-ethereum/params"
3940 "github.com/ethereum/go-ethereum/params/types/ctypes"
@@ -182,6 +183,60 @@ func TestMinerStartStopAfterDownloaderEvents(t *testing.T) {
182183 waitForMiningState (t , miner , false )
183184}
184185
186+ // TestMinerStartAfterFetcherInsertEvent tests that the miner resumes mining during a downloader sync attempt
187+ // if a propagated block (announce, broadcast) is inserted successfully to the local chain.
188+ func TestMinerStartAfterFetcherInsertEvent (t * testing.T ) {
189+ miner , mux , cleanup := createMiner (t )
190+ defer cleanup (false )
191+
192+ miner .Start ()
193+ waitForMiningState (t , miner , true )
194+ // Start the downloader
195+ mux .Post (downloader.StartEvent {})
196+ waitForMiningState (t , miner , false )
197+
198+ // During the sync, import a block via the fetcher.
199+ // Miner should resume mining, inferring that we're at or near-enough (<32 block) the head of the network canonical chain.
200+ mux .Post (fetcher.InsertChainEvent {})
201+ waitForMiningState (t , miner , true )
202+
203+ mux .Post (downloader.FailedEvent {})
204+ waitForMiningState (t , miner , true )
205+
206+ mux .Post (downloader.StartEvent {})
207+ waitForMiningState (t , miner , true ) // Has not autopaused mining.
208+
209+ // Downloader finally succeeds.
210+ mux .Post (downloader.DoneEvent {})
211+ waitForMiningState (t , miner , true ) // Still mining.
212+ }
213+
214+ // TestMinerStartAfterFetcherInsertEvent2 tests that the miner does not autopause if outside any downloader sync attempt
215+ // a propagated block (announce, broadcast) is inserted successfully to the local chain; that is,
216+ // a fetcher->import event disables the downloader event listener and autopause mechanism.
217+ func TestMinerStartAfterFetcherInsertEvent2 (t * testing.T ) {
218+ miner , mux , cleanup := createMiner (t )
219+ defer cleanup (false )
220+
221+ miner .Start ()
222+ waitForMiningState (t , miner , true )
223+
224+ // Before we've started the downloader, import a block via the fetcher.
225+ // This should disable subsequent autopausing for downloader events thereafter, since
226+ // we consider the local chain sufficiently synced.
227+ mux .Post (fetcher.InsertChainEvent {})
228+ waitForMiningState (t , miner , true )
229+
230+ // Start the downloader
231+ mux .Post (downloader.StartEvent {})
232+ // Still mining; we've inferred by a successful import of a propagated block
233+ // that we're already synced, and we're no longer treating downloader events as autopause-able.
234+ waitForMiningState (t , miner , true )
235+
236+ mux .Post (downloader.FailedEvent {})
237+ waitForMiningState (t , miner , true )
238+ }
239+
185240func TestStartWhileDownload (t * testing.T ) {
186241 miner , mux , cleanup := createMiner (t )
187242 defer cleanup (false )
0 commit comments