Skip to content

Commit 6a6a836

Browse files
author
Michał Mrożek
authored
Merge branch 'develop' into etcm-301-fix-block-preparation
2 parents 80322c0 + 1adf114 commit 6a6a836

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

src/main/scala/io/iohk/ethereum/domain/Blockchain.scala

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import io.iohk.ethereum.domain
1414
import io.iohk.ethereum.domain.BlockchainImpl.BestBlockLatestCheckpointNumbers
1515
import io.iohk.ethereum.ledger.{InMemoryWorldStateProxy, InMemoryWorldStateProxyStorage}
1616
import io.iohk.ethereum.mpt.{MerklePatriciaTrie, MptNode}
17+
import io.iohk.ethereum.utils.{ByteStringUtils, Logger}
1718
import io.iohk.ethereum.vm.{Storage, WorldStateProxy}
1819
import monix.reactive.Observable
1920

@@ -215,7 +216,8 @@ class BlockchainImpl(
215216
protected val transactionMappingStorage: TransactionMappingStorage,
216217
protected val appStateStorage: AppStateStorage,
217218
protected val stateStorage: StateStorage
218-
) extends Blockchain {
219+
) extends Blockchain
220+
with Logger {
219221

220222
override def getStateStorage: StateStorage = stateStorage
221223

@@ -237,11 +239,17 @@ class BlockchainImpl(
237239
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
238240

239241
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
243251
else
244-
bestBlockNum
252+
bestSavedBlockNumber
245253
}
246254

247255
override def getLatestCheckpointBlockNumber(): BigInt = {
@@ -254,8 +262,11 @@ class BlockchainImpl(
254262
latestCheckpointNumberInStorage
255263
}
256264

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+
}
259270

260271
override def getAccount(address: Address, blockNumber: BigInt): Option[Account] =
261272
getBlockHeaderByNumber(blockNumber).flatMap { bh =>
@@ -280,13 +291,23 @@ class BlockchainImpl(
280291
}
281292

282293
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+
283303
appStateStorage
284-
.putBestBlockNumber(getBestBlockNumber())
285-
.and(appStateStorage.putLatestCheckpointBlockNumber(getLatestCheckpointBlockNumber()))
304+
.putBestBlockNumber(currentBestBlockNumber)
305+
.and(appStateStorage.putLatestCheckpointBlockNumber(currentBestCheckpointNumber))
286306
.commit()
287307
}
288308

289309
def save(block: Block, receipts: Seq[Receipt], weight: ChainWeight, saveAsBestBlock: Boolean): Unit = {
310+
log.debug("Saving new block block {} to database", block.idTag)
290311
storeBlock(block)
291312
.and(storeReceipts(block.header.hash, receipts))
292313
.and(storeChainWeight(block.header.hash, weight))
@@ -297,8 +318,17 @@ class BlockchainImpl(
297318
stateStorage.onBlockSave(block.header.number, appStateStorage.getBestBlockNumber())(persistBestBlocksData)
298319

299320
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+
)
300326
saveBestKnownBlockAndLatestCheckpointNumber(block.header.number, block.header.number)
301327
} else if (saveAsBestBlock) {
328+
log.debug(
329+
"New best known block block number - {}",
330+
block.header.number
331+
)
302332
saveBestKnownBlock(block.header.number)
303333
}
304334
}
@@ -361,6 +391,13 @@ class BlockchainImpl(
361391
// scalastyle:off method.length
362392
override def removeBlock(blockHash: ByteString, withState: Boolean): Unit = {
363393
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+
364401
val maybeTxList = getBlockBodyByHash(blockHash).map(_.transactionList)
365402
val bestBlocks = bestKnownBlockAndLatestCheckpoint.get()
366403
// as we are decreasing block numbers in memory more often than in storage,
@@ -409,13 +446,27 @@ class BlockchainImpl(
409446

410447
// not transactional part
411448
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+
)
412455

413456
maybeBlockHeader.foreach { h =>
414457
if (withState) {
415458
val bestBlocksUpdates = appStateStorage
416459
.putBestBlockNumber(newBestBlockNumber)
417460
.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+
}
419470
}
420471
}
421472
}

src/main/scala/io/iohk/ethereum/ledger/BlockImport.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import io.iohk.ethereum.consensus.validators.BlockHeaderError.HeaderParentNotFou
55
import io.iohk.ethereum.domain._
66
import io.iohk.ethereum.ledger.BlockExecutionError.ValidationBeforeExecError
77
import io.iohk.ethereum.ledger.BlockQueue.Leaf
8-
import io.iohk.ethereum.utils.{BlockchainConfig, Logger}
8+
import io.iohk.ethereum.utils.{BlockchainConfig, ByteStringUtils, Logger}
99
import org.bouncycastle.util.encoders.Hex
1010

1111
import scala.concurrent.{ExecutionContext, Future}
@@ -141,6 +141,7 @@ class BlockImport(
141141
* (oldBranch, newBranch) as lists of blocks
142142
*/
143143
private def reorganiseChainFromQueue(queuedLeaf: ByteString): BlockImportResult = {
144+
log.debug("Reorganising chain from leaf {}", ByteStringUtils.hash2string(queuedLeaf))
144145
blockchain.persistCachedNodes()
145146
val newBranch = blockQueue.getBranch(queuedLeaf, dequeue = true)
146147
val bestNumber = blockchain.getBestBlockNumber()
@@ -150,6 +151,11 @@ class BlockImport(
150151
parentHash = parent.header.parentHash
151152
parentWeight <- blockchain.getChainWeightByHash(parentHash)
152153
} yield {
154+
log.debug(
155+
"Removing blocks starting from number {} and parent {}",
156+
bestNumber,
157+
ByteStringUtils.hash2string(parentHash)
158+
)
153159
val oldBlocksData = removeBlocksUntil(parentHash, bestNumber).reverse
154160
oldBlocksData.foreach(block => blockQueue.enqueueBlock(block.block))
155161
handleBlockExecResult(newBranch, parentWeight, oldBlocksData)

src/main/scala/io/iohk/ethereum/ledger/BlockValidation.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ class BlockValidation(consensus: Consensus, blockchain: Blockchain, blockQueue:
2020
}
2121

2222
private def getNBlocksBackFromChainOrQueue(hash: ByteString, n: Int): List[Block] = {
23-
val queuedBlocks = blockQueue.getBranch(hash, dequeue = false).take(n)
23+
val queuedBlocks = blockQueue.getBranch(hash, dequeue = false).takeRight(n)
2424
if (queuedBlocks.length == n) {
2525
queuedBlocks
2626
} else {
2727
val chainedBlockHash = queuedBlocks.headOption.map(_.header.parentHash).getOrElse(hash)
2828
blockchain.getBlockByHash(chainedBlockHash) match {
2929
case None =>
30+
// The in memory blocks aren't connected to the db ones, we don't have n blocks to return so we return none
3031
Nil
3132

3233
case Some(block) =>
34+
// We already have |block +: queuedBlocks|
3335
val remaining = n - queuedBlocks.length - 1
36+
3437
val numbers = (block.header.number - remaining) until block.header.number
3538
val blocks = (numbers.toList.flatMap(blockchain.getBlockByNumber) :+ block) ::: queuedBlocks
3639
blocks

0 commit comments

Comments
 (0)