@@ -14,6 +14,7 @@ import io.iohk.ethereum.domain
14
14
import io .iohk .ethereum .domain .BlockchainImpl .BestBlockLatestCheckpointNumbers
15
15
import io .iohk .ethereum .ledger .{InMemoryWorldStateProxy , InMemoryWorldStateProxyStorage }
16
16
import io .iohk .ethereum .mpt .{MerklePatriciaTrie , MptNode }
17
+ import io .iohk .ethereum .utils .{ByteStringUtils , Logger }
17
18
import io .iohk .ethereum .vm .{Storage , WorldStateProxy }
18
19
import monix .reactive .Observable
19
20
@@ -215,7 +216,8 @@ class BlockchainImpl(
215
216
protected val transactionMappingStorage : TransactionMappingStorage ,
216
217
protected val appStateStorage : AppStateStorage ,
217
218
protected val stateStorage : StateStorage
218
- ) extends Blockchain {
219
+ ) extends Blockchain
220
+ with Logger {
219
221
220
222
override def getStateStorage : StateStorage = stateStorage
221
223
@@ -237,11 +239,17 @@ class BlockchainImpl(
237
239
override def getChainWeightByHash (blockhash : ByteString ): Option [ChainWeight ] = chainWeightStorage.get(blockhash)
238
240
239
241
override def getBestBlockNumber (): BigInt = {
240
- val bestBlockNum = appStateStorage.getBestBlockNumber()
241
- if (bestKnownBlockAndLatestCheckpoint.get().bestBlockNumber > bestBlockNum)
242
- bestKnownBlockAndLatestCheckpoint.get().bestBlockNumber
242
+ val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
243
+ val bestKnownBlockNumber = bestKnownBlockAndLatestCheckpoint.get().bestBlockNumber
244
+ log.debug(
245
+ " Current best saved block number {}. Current best known block number {}" ,
246
+ bestSavedBlockNumber,
247
+ bestKnownBlockNumber
248
+ )
249
+ if (bestKnownBlockNumber > bestSavedBlockNumber)
250
+ bestKnownBlockNumber
243
251
else
244
- bestBlockNum
252
+ bestSavedBlockNumber
245
253
}
246
254
247
255
override def getLatestCheckpointBlockNumber (): BigInt = {
@@ -254,8 +262,11 @@ class BlockchainImpl(
254
262
latestCheckpointNumberInStorage
255
263
}
256
264
257
- override def getBestBlock (): Block =
258
- getBlockByNumber(getBestBlockNumber()).get
265
+ override def getBestBlock (): Block = {
266
+ val bestBlockNumber = getBestBlockNumber()
267
+ log.debug(" Trying to get best block with number {}" , bestBlockNumber)
268
+ getBlockByNumber(bestBlockNumber).get
269
+ }
259
270
260
271
override def getAccount (address : Address , blockNumber : BigInt ): Option [Account ] =
261
272
getBlockHeaderByNumber(blockNumber).flatMap { bh =>
@@ -280,13 +291,23 @@ class BlockchainImpl(
280
291
}
281
292
282
293
private def persistBestBlocksData (): Unit = {
294
+ val currentBestBlockNumber = getBestBlockNumber()
295
+ val currentBestCheckpointNumber = getLatestCheckpointBlockNumber()
296
+ log.debug(
297
+ " Persisting block info data into database. Persisted block number is {}. " +
298
+ " Persisted checkpoint number is {}" ,
299
+ currentBestBlockNumber,
300
+ currentBestCheckpointNumber
301
+ )
302
+
283
303
appStateStorage
284
- .putBestBlockNumber(getBestBlockNumber() )
285
- .and(appStateStorage.putLatestCheckpointBlockNumber(getLatestCheckpointBlockNumber() ))
304
+ .putBestBlockNumber(currentBestBlockNumber )
305
+ .and(appStateStorage.putLatestCheckpointBlockNumber(currentBestCheckpointNumber ))
286
306
.commit()
287
307
}
288
308
289
309
def save (block : Block , receipts : Seq [Receipt ], weight : ChainWeight , saveAsBestBlock : Boolean ): Unit = {
310
+ log.debug(" Saving new block block {} to database" , block.idTag)
290
311
storeBlock(block)
291
312
.and(storeReceipts(block.header.hash, receipts))
292
313
.and(storeChainWeight(block.header.hash, weight))
@@ -297,8 +318,17 @@ class BlockchainImpl(
297
318
stateStorage.onBlockSave(block.header.number, appStateStorage.getBestBlockNumber())(persistBestBlocksData)
298
319
299
320
if (saveAsBestBlock && block.hasCheckpoint) {
321
+ log.debug(
322
+ " New best known block block number - {}, new best checkpoint number - {}" ,
323
+ block.header.number,
324
+ block.header.number
325
+ )
300
326
saveBestKnownBlockAndLatestCheckpointNumber(block.header.number, block.header.number)
301
327
} else if (saveAsBestBlock) {
328
+ log.debug(
329
+ " New best known block block number - {}" ,
330
+ block.header.number
331
+ )
302
332
saveBestKnownBlock(block.header.number)
303
333
}
304
334
}
@@ -361,6 +391,13 @@ class BlockchainImpl(
361
391
// scalastyle:off method.length
362
392
override def removeBlock (blockHash : ByteString , withState : Boolean ): Unit = {
363
393
val maybeBlockHeader = getBlockHeaderByHash(blockHash)
394
+
395
+ log.debug(
396
+ " Trying to remove block with hash {} and number {}" ,
397
+ ByteStringUtils .hash2string(blockHash),
398
+ maybeBlockHeader.map(_.number)
399
+ )
400
+
364
401
val maybeTxList = getBlockBodyByHash(blockHash).map(_.transactionList)
365
402
val bestBlocks = bestKnownBlockAndLatestCheckpoint.get()
366
403
// as we are decreasing block numbers in memory more often than in storage,
@@ -409,13 +446,27 @@ class BlockchainImpl(
409
446
410
447
// not transactional part
411
448
saveBestKnownBlocks(newBestBlockNumber, prevCheckpointNumber)
449
+ log.debug(
450
+ " Removed block with hash {}. New best block number - {}, new best checkpoint block number - {}" ,
451
+ ByteStringUtils .hash2string(blockHash),
452
+ newBestBlockNumber,
453
+ prevCheckpointNumber
454
+ )
412
455
413
456
maybeBlockHeader.foreach { h =>
414
457
if (withState) {
415
458
val bestBlocksUpdates = appStateStorage
416
459
.putBestBlockNumber(newBestBlockNumber)
417
460
.and(checkpointUpdates)
418
- stateStorage.onBlockRollback(h.number, bestBlockNumber)(() => bestBlocksUpdates.commit())
461
+ stateStorage.onBlockRollback(h.number, bestBlockNumber) { () =>
462
+ log.debug(
463
+ " Persisting block info data into database. Persisted block number is {}. " +
464
+ " Persisted checkpoint number is {}" ,
465
+ newBestBlockNumber,
466
+ prevCheckpointNumber
467
+ )
468
+ bestBlocksUpdates.commit()
469
+ }
419
470
}
420
471
}
421
472
}
0 commit comments