Skip to content

Commit d496188

Browse files
authored
miner: suspend miner if node is syncing (ethereum#27218)
Drop the notions of uncles, and disables activities while syncing - Disable activities (e.g. generate pending state) while node is syncing, - Disable empty block submission (but empty block is still kept for payload building), - Drop uncle notion since (ethash is already deprecated)
1 parent 61dcf76 commit d496188

File tree

8 files changed

+86
-650
lines changed

8 files changed

+86
-650
lines changed

eth/api_backend.go

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
6868
// Pending block is only known by the miner
6969
if number == rpc.PendingBlockNumber {
7070
block := b.eth.miner.PendingBlock()
71+
if block == nil {
72+
return nil, errors.New("pending block is not available")
73+
}
7174
return block.Header(), nil
7275
}
7376
// Otherwise resolve and return the block
@@ -122,6 +125,9 @@ func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumbe
122125
// Pending block is only known by the miner
123126
if number == rpc.PendingBlockNumber {
124127
block := b.eth.miner.PendingBlock()
128+
if block == nil {
129+
return nil, errors.New("pending block is not available")
130+
}
125131
return block, nil
126132
}
127133
// Otherwise resolve and return the block
@@ -196,6 +202,9 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B
196202
// Pending state is only known by the miner
197203
if number == rpc.PendingBlockNumber {
198204
block, state := b.eth.miner.Pending()
205+
if block == nil || state == nil {
206+
return nil, nil, errors.New("pending state is not available")
207+
}
199208
return state, block.Header(), nil
200209
}
201210
// Otherwise resolve the block number and return its state

eth/api_debug.go

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) {
5656
// both the pending block as well as the pending state from
5757
// the miner and operate on those
5858
_, stateDb := api.eth.miner.Pending()
59+
if stateDb == nil {
60+
return state.Dump{}, errors.New("pending state is not available")
61+
}
5962
return stateDb.RawDump(opts), nil
6063
}
6164
var header *types.Header
@@ -141,6 +144,9 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex
141144
// both the pending block as well as the pending state from
142145
// the miner and operate on those
143146
_, stateDb = api.eth.miner.Pending()
147+
if stateDb == nil {
148+
return state.IteratorDump{}, errors.New("pending state is not available")
149+
}
144150
} else {
145151
var header *types.Header
146152
if number == rpc.LatestBlockNumber {

eth/filters/filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func (f *Filter) checkMatches(ctx context.Context, header *types.Header) ([]*typ
334334
// pendingLogs returns the logs matching the filter criteria within the pending block.
335335
func (f *Filter) pendingLogs() []*types.Log {
336336
block, receipts := f.sys.backend.PendingBlockAndReceipts()
337-
if block == nil {
337+
if block == nil || receipts == nil {
338338
return nil
339339
}
340340
if bloomFilter(block.Bloom(), f.addresses, f.topics) {

miner/miner.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,22 @@ func (miner *Miner) update() {
131131
shouldStart = true
132132
log.Info("Mining aborted due to sync")
133133
}
134+
miner.worker.syncing.Store(true)
135+
134136
case downloader.FailedEvent:
135137
canStart = true
136138
if shouldStart {
137139
miner.worker.start()
138140
}
141+
miner.worker.syncing.Store(false)
142+
139143
case downloader.DoneEvent:
140144
canStart = true
141145
if shouldStart {
142146
miner.worker.start()
143147
}
148+
miner.worker.syncing.Store(false)
149+
144150
// Stop reacting to downloader events
145151
events.Unsubscribe()
146152
}
@@ -196,12 +202,14 @@ func (miner *Miner) SetRecommitInterval(interval time.Duration) {
196202
miner.worker.setRecommitInterval(interval)
197203
}
198204

199-
// Pending returns the currently pending block and associated state.
205+
// Pending returns the currently pending block and associated state. The returned
206+
// values can be nil in case the pending block is not initialized
200207
func (miner *Miner) Pending() (*types.Block, *state.StateDB) {
201208
return miner.worker.pending()
202209
}
203210

204-
// PendingBlock returns the currently pending block.
211+
// PendingBlock returns the currently pending block. The returned block can be
212+
// nil in case the pending block is not initialized.
205213
//
206214
// Note, to access both the pending block and the pending state
207215
// simultaneously, please use Pending(), as the pending state can
@@ -211,6 +219,7 @@ func (miner *Miner) PendingBlock() *types.Block {
211219
}
212220

213221
// PendingBlockAndReceipts returns the currently pending block and corresponding receipts.
222+
// The returned values can be nil in case the pending block is not initialized.
214223
func (miner *Miner) PendingBlockAndReceipts() (*types.Block, types.Receipts) {
215224
return miner.worker.pendingBlockAndReceipts()
216225
}
@@ -225,23 +234,6 @@ func (miner *Miner) SetGasCeil(ceil uint64) {
225234
miner.worker.setGasCeil(ceil)
226235
}
227236

228-
// EnablePreseal turns on the preseal mining feature. It's enabled by default.
229-
// Note this function shouldn't be exposed to API, it's unnecessary for users
230-
// (miners) to actually know the underlying detail. It's only for outside project
231-
// which uses this library.
232-
func (miner *Miner) EnablePreseal() {
233-
miner.worker.enablePreseal()
234-
}
235-
236-
// DisablePreseal turns off the preseal mining feature. It's necessary for some
237-
// fake consensus engine which can seal blocks instantaneously.
238-
// Note this function shouldn't be exposed to API, it's unnecessary for users
239-
// (miners) to actually know the underlying detail. It's only for outside project
240-
// which uses this library.
241-
func (miner *Miner) DisablePreseal() {
242-
miner.worker.disablePreseal()
243-
}
244-
245237
// SubscribePendingLogs starts delivering logs from pending transactions
246238
// to the given channel.
247239
func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {

miner/unconfirmed.go

-136
This file was deleted.

miner/unconfirmed_test.go

-87
This file was deleted.

0 commit comments

Comments
 (0)