@@ -24,7 +24,6 @@ import monix.execution.Scheduler
24
24
25
25
import scala .concurrent .duration ._
26
26
27
- // scalastyle:off cyclomatic.complexity
28
27
class BlockImporter (
29
28
fetcher : ActorRef ,
30
29
ledger : Ledger ,
@@ -53,16 +52,9 @@ class BlockImporter(
53
52
start()
54
53
}
55
54
56
- private def handleTopMessages (state : ImporterState , currentBehavior : Behavior ): Receive = {
57
- case OnTop => context become currentBehavior(state.onTop())
58
- case NotOnTop => context become currentBehavior(state.notOnTop())
59
- }
60
-
61
- private def running (state : ImporterState ): Receive = handleTopMessages(state, running) orElse {
55
+ private def running (state : ImporterState ): Receive = {
62
56
case ReceiveTimeout => self ! PickBlocks
63
57
64
- case PrintStatus => log.info(" Block: {}, is on top?: {}" , blockchain.getBestBlockNumber(), state.isOnTop)
65
-
66
58
case BlockFetcher .PickedBlocks (blocks) =>
67
59
SignedTransaction .retrieveSendersInBackGround(blocks.toList.map(_.body))
68
60
importBlocks(blocks, DefaultBlockImport )(state)
@@ -89,7 +81,7 @@ class BlockImporter(
89
81
internally = true
90
82
)(state)
91
83
92
- case ImportNewBlock (block, peerId) if state.isOnTop && ! state.importing =>
84
+ case ImportNewBlock (block, peerId) if ! state.importing =>
93
85
importBlock(
94
86
block,
95
87
new NewBlockImportMessages (block, peerId),
@@ -121,36 +113,34 @@ class BlockImporter(
121
113
running(state.resolvingBranch(from))
122
114
123
115
private def start (): Unit = {
124
- log.debug(" Starting Regular Sync, current best block is {}" , startingBlockNumber )
125
- fetcher ! BlockFetcher .Start (self, startingBlockNumber )
126
- supervisor ! ProgressProtocol .StartingFrom (startingBlockNumber )
116
+ log.debug(" Starting Regular Sync, current best block is {}" , bestKnownBlockNumber )
117
+ fetcher ! BlockFetcher .Start (self, bestKnownBlockNumber )
118
+ supervisor ! ProgressProtocol .StartingFrom (bestKnownBlockNumber )
127
119
context become running(ImporterState .initial)
128
120
}
129
121
130
122
private def pickBlocks (state : ImporterState ): Unit = {
131
- val msg =
132
- state.resolvingBranchFrom.fold[BlockFetcher .FetchCommand ](
133
- BlockFetcher .PickBlocks (syncConfig.blocksBatchSize, self)
134
- )(from => BlockFetcher .StrictPickBlocks (from, startingBlockNumber, self))
123
+ val msg = state.resolvingBranchFrom.fold[BlockFetcher .FetchCommand ](
124
+ BlockFetcher .PickBlocks (syncConfig.blocksBatchSize, self)
125
+ )(from => BlockFetcher .StrictPickBlocks (from, bestKnownBlockNumber, self))
135
126
136
127
fetcher ! msg
137
128
}
138
129
139
130
private def importBlocks (blocks : NonEmptyList [Block ], blockImportType : BlockImportType ): ImportFn = importWith(
140
- {
141
- Task (
131
+ Task
132
+ .now {
142
133
log.debug(
143
134
" Attempting to import blocks starting from {} and ending with {}" ,
144
135
blocks.head.number,
145
136
blocks.last.number
146
137
)
147
- )
148
- .flatMap(_ => Task .now(resolveBranch(blocks)))
149
- .flatMap {
150
- case Right (blocksToImport) => handleBlocksImport(blocksToImport)
151
- case Left (resolvingFrom) => Task .now(ResolvingBranch (resolvingFrom))
152
- }
153
- },
138
+ resolveBranch(blocks)
139
+ }
140
+ .flatMap {
141
+ case Right (blocksToImport) => handleBlocksImport(blocksToImport)
142
+ case Left (resolvingFrom) => Task .now(ResolvingBranch (resolvingFrom))
143
+ },
154
144
blockImportType
155
145
)
156
146
@@ -187,12 +177,9 @@ class BlockImporter(
187
177
importedBlocks : List [Block ] = Nil
188
178
): Task [(List [Block ], Option [Any ])] =
189
179
if (blocks.isEmpty) {
190
- importedBlocks.headOption match {
191
- case Some (block) =>
192
- supervisor ! ProgressProtocol .ImportedBlock (block.number, internally = false )
193
- case None => ()
194
- }
195
-
180
+ importedBlocks.headOption.foreach(block =>
181
+ supervisor ! ProgressProtocol .ImportedBlock (block.number, internally = false )
182
+ )
196
183
Task .now((importedBlocks, None ))
197
184
} else {
198
185
val restOfBlocks = blocks.tail
@@ -244,27 +231,22 @@ class BlockImporter(
244
231
broadcastBlocks(blocks, weights)
245
232
updateTxPool(importedBlocksData.map(_.block), Seq .empty)
246
233
supervisor ! ProgressProtocol .ImportedBlock (block.number, internally)
247
- case BlockEnqueued => ()
248
- case DuplicateBlock => ()
249
- case UnknownParent => () // This is normal when receiving broadcast blocks
250
234
case ChainReorganised (oldBranch, newBranch, weights) =>
251
235
updateTxPool(newBranch, oldBranch)
252
236
broadcastBlocks(newBranch, weights)
253
- newBranch.lastOption match {
254
- case Some (newBlock) =>
255
- supervisor ! ProgressProtocol .ImportedBlock (newBlock.number, internally)
256
- case None => ()
257
- }
237
+ newBranch.lastOption.foreach(block =>
238
+ supervisor ! ProgressProtocol .ImportedBlock (block.number, internally)
239
+ )
258
240
case BlockImportFailedDueToMissingNode (missingNodeException) if syncConfig.redownloadMissingStateNodes =>
259
241
// state node re-download will be handled when downloading headers
260
242
doLog(importMessages.missingStateNode(missingNodeException))
261
243
Running
262
244
case BlockImportFailedDueToMissingNode (missingNodeException) =>
263
245
Task .raiseError(missingNodeException)
264
- case BlockImportFailed (error) =>
265
- if (informFetcherOnFail) {
266
- fetcher ! BlockFetcher . BlockImportFailed (block.number, BlacklistReason . BlockImportError (error) )
267
- }
246
+ case BlockImportFailed (error) if informFetcherOnFail =>
247
+ fetcher ! BlockFetcher . BlockImportFailed (block.number, BlacklistReason . BlockImportError (error))
248
+ case BlockEnqueued | DuplicateBlock | UnknownParent | BlockImportFailed (_) => ( )
249
+ case result => log.error( " Unknown block import result {} " , result)
268
250
}
269
251
.map(_ => Running )
270
252
},
@@ -279,9 +261,7 @@ class BlockImporter(
279
261
280
262
private def updateTxPool (blocksAdded : Seq [Block ], blocksRemoved : Seq [Block ]): Unit = {
281
263
blocksRemoved.foreach(block => pendingTransactionsManager ! AddUncheckedTransactions (block.body.transactionList))
282
- blocksAdded.foreach { block =>
283
- pendingTransactionsManager ! RemoveTransactions (block.body.transactionList)
284
- }
264
+ blocksAdded.foreach(block => pendingTransactionsManager ! RemoveTransactions (block.body.transactionList))
285
265
}
286
266
287
267
private def importWith (importTask : Task [NewBehavior ], blockImportType : BlockImportType )(
@@ -303,7 +283,6 @@ class BlockImporter(
303
283
case NewBetterBranch (oldBranch) =>
304
284
val transactionsToAdd = oldBranch.flatMap(_.body.transactionList)
305
285
pendingTransactionsManager ! PendingTransactionsManager .AddUncheckedTransactions (transactionsToAdd)
306
-
307
286
// Add first block from branch as an ommer
308
287
oldBranch.headOption.map(_.header).foreach(ommersPool ! AddOmmers (_))
309
288
Right (blocks.toList)
@@ -312,23 +291,21 @@ class BlockImporter(
312
291
ommersPool ! AddOmmers (blocks.head.header)
313
292
Right (Nil )
314
293
case UnknownBranch =>
315
- val currentBlock = blocks.head.number.min(startingBlockNumber )
294
+ val currentBlock = blocks.head.number.min(bestKnownBlockNumber )
316
295
val goingBackTo = (currentBlock - syncConfig.branchResolutionRequestSize).max(0 )
317
296
val msg = s " Unknown branch, going back to block nr $goingBackTo in order to resolve branches "
318
-
319
297
log.info(msg)
320
298
fetcher ! BlockFetcher .InvalidateBlocksFrom (goingBackTo, msg, shouldBlacklist = false )
321
299
Left (goingBackTo)
322
300
case InvalidBranch =>
323
301
val goingBackTo = blocks.head.number
324
302
val msg = s " Invalid branch, going back to $goingBackTo"
325
-
326
303
log.info(msg)
327
304
fetcher ! BlockFetcher .InvalidateBlocksFrom (goingBackTo, msg)
328
305
Right (Nil )
329
306
}
330
307
331
- private def startingBlockNumber : BigInt = blockchain.getBestBlockNumber()
308
+ private def bestKnownBlockNumber : BigInt = blockchain.getBestBlockNumber()
332
309
333
310
private def getBehavior (newBehavior : NewBehavior , blockImportType : BlockImportType ): Behavior = newBehavior match {
334
311
case Running => running
@@ -338,7 +315,6 @@ class BlockImporter(
338
315
}
339
316
340
317
object BlockImporter {
341
- // scalastyle:off parameter.number
342
318
def props (
343
319
fetcher : ActorRef ,
344
320
ledger : Ledger ,
@@ -367,8 +343,6 @@ object BlockImporter {
367
343
368
344
sealed trait ImporterMsg
369
345
case object Start extends ImporterMsg
370
- case object OnTop extends ImporterMsg
371
- case object NotOnTop extends ImporterMsg
372
346
case class MinedBlock (block : Block ) extends ImporterMsg
373
347
case class NewCheckpoint (block : Block ) extends ImporterMsg
374
348
case class ImportNewBlock (block : Block , peerId : PeerId ) extends ImporterMsg
@@ -402,14 +376,9 @@ object BlockImporter {
402
376
}
403
377
404
378
case class ImporterState (
405
- isOnTop : Boolean ,
406
379
importing : Boolean ,
407
380
resolvingBranchFrom : Option [BigInt ]
408
381
) {
409
- def onTop (): ImporterState = copy(isOnTop = true )
410
-
411
- def notOnTop (): ImporterState = copy(isOnTop = false )
412
-
413
382
def importingBlocks (): ImporterState = copy(importing = true )
414
383
415
384
def notImportingBlocks (): ImporterState = copy(importing = false )
@@ -423,7 +392,6 @@ object BlockImporter {
423
392
424
393
object ImporterState {
425
394
def initial : ImporterState = ImporterState (
426
- isOnTop = false ,
427
395
importing = false ,
428
396
resolvingBranchFrom = None
429
397
)
0 commit comments