-
Notifications
You must be signed in to change notification settings - Fork 75
[ETCM-1048] Remove bestKnownBlockAndLatestCheckpoint cache #1092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -567,16 +567,13 @@ class FastSync( | |
} | ||
|
||
// TODO [ETCM-676]: Move to blockchain and make sure it's atomic | ||
private def discardLastBlocks(startBlock: BigInt, blocksToDiscard: Int): Unit = { | ||
private def discardLastBlocks(startBlock: BigInt, blocksToDiscard: Int): Unit = | ||
// TODO (maybe ETCM-77): Manage last checkpoint number too | ||
appStateStorage.putBestBlockNumber((startBlock - blocksToDiscard - 1).max(0)).commit() | ||
|
||
(startBlock to ((startBlock - blocksToDiscard).max(1)) by -1).foreach { n => | ||
blockchainReader.getBlockHeaderByNumber(n).foreach { headerToRemove => | ||
blockchain.removeBlock(headerToRemove.hash, withState = false) | ||
blockchain.removeBlock(headerToRemove.hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to remark: perhaps this parameter was also added to prevent issues with write amplification, but we won't need to actually remove blocks anymore (or at least from sync logic) when we can save tentative chains / blocks. (Just need to make sure the cleanup is done in a decently efficient way.) |
||
} | ||
} | ||
} | ||
|
||
private def validateHeader(header: BlockHeader, peer: Peer): Either[HeaderProcessingResult, BlockHeader] = { | ||
val shouldValidate = header.number >= syncState.nextBlockToFullyValidate | ||
|
@@ -790,7 +787,7 @@ class FastSync( | |
|Peers waiting_for_response/connected: ${assignedHandlers.size}/${handshakedPeers.size} (${blacklistedIds.size} blacklisted). | ||
|State: ${syncState.downloadedNodesCount}/${syncState.totalNodesCount} nodes. | ||
|""".stripMargin.replace("\n", " "), | ||
appStateStorage.getBestBlockNumber() | ||
blockchainReader.getBestBlockNumber() | ||
) | ||
log.debug( | ||
s"""|Connection status: connected({})/ | ||
|
@@ -1136,9 +1133,8 @@ class FastSync( | |
|
||
if (fullBlocks.nonEmpty) { | ||
val bestReceivedBlock = fullBlocks.maxBy(_.number) | ||
val lastStoredBestBlockNumber = appStateStorage.getBestBlockNumber() | ||
val lastStoredBestBlockNumber = blockchainReader.getBestBlockNumber() | ||
if (lastStoredBestBlockNumber < bestReceivedBlock.number) { | ||
blockchain.saveBestKnownBlocks(bestReceivedBlock.number) | ||
// TODO ETCM-1089 move direct calls to storages to blockchain or blockchain writer | ||
appStateStorage | ||
.putBestBlockNumber(bestReceivedBlock.number) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ trait Blockchain { | |
|
||
def getLatestCheckpointBlockNumber(): BigInt | ||
|
||
def removeBlock(hash: ByteString, withState: Boolean): Unit | ||
def removeBlock(hash: ByteString): Unit | ||
|
||
def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit | ||
|
||
|
@@ -77,15 +77,13 @@ class BlockchainImpl( | |
protected val transactionMappingStorage: TransactionMappingStorage, | ||
protected val appStateStorage: AppStateStorage, | ||
protected val stateStorage: StateStorage, | ||
blockchainReader: BlockchainReader, | ||
blockchainMetadata: BlockchainMetadata | ||
blockchainReader: BlockchainReader | ||
) extends Blockchain | ||
with Logger { | ||
|
||
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash) | ||
|
||
override def getLatestCheckpointBlockNumber(): BigInt = | ||
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().latestCheckpointNumber | ||
override def getLatestCheckpointBlockNumber(): BigInt = appStateStorage.getLatestCheckpointBlockNumber() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. I created this ticket https://jira.iohk.io/browse/ETCM-1092 |
||
|
||
override def getAccountStorageAt( | ||
rootHash: ByteString, | ||
|
@@ -128,22 +126,6 @@ class BlockchainImpl( | |
|
||
def getReadOnlyMptStorage(): MptStorage = stateStorage.getReadOnlyStorage | ||
|
||
private def persistBestBlocksData(): Unit = { | ||
val currentBestBlockNumber = blockchainReader.getBestBlockNumber() | ||
val currentBestCheckpointNumber = getLatestCheckpointBlockNumber() | ||
log.debug( | ||
"Persisting app info data into database. Persisted block number is {}. " + | ||
"Persisted checkpoint number is {}", | ||
currentBestBlockNumber, | ||
currentBestCheckpointNumber | ||
) | ||
|
||
appStateStorage | ||
.putBestBlockNumber(currentBestBlockNumber) | ||
.and(appStateStorage.putLatestCheckpointBlockNumber(currentBestCheckpointNumber)) | ||
.commit() | ||
} | ||
|
||
override def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit = | ||
latestCheckpointNumber match { | ||
case Some(number) => | ||
|
@@ -153,28 +135,29 @@ class BlockchainImpl( | |
} | ||
|
||
private def saveBestKnownBlock(bestBlockNumber: BigInt): Unit = | ||
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.updateAndGet(_.copy(bestBlockNumber = bestBlockNumber)) | ||
appStateStorage.putBestBlockNumber(bestBlockNumber).commit() | ||
|
||
private def saveBestKnownBlockAndLatestCheckpointNumber(number: BigInt, latestCheckpointNumber: BigInt): Unit = | ||
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.set( | ||
BestBlockLatestCheckpointNumbers(number, latestCheckpointNumber) | ||
) | ||
appStateStorage | ||
.putBestBlockNumber(number) | ||
.and(appStateStorage.putLatestCheckpointBlockNumber(latestCheckpointNumber)) | ||
.commit() | ||
|
||
private def removeBlockNumberMapping(number: BigInt): DataSourceBatchUpdate = | ||
blockNumberMappingStorage.remove(number) | ||
|
||
override def removeBlock(blockHash: ByteString, withState: Boolean): Unit = { | ||
override def removeBlock(blockHash: ByteString): Unit = { | ||
val maybeBlock = blockchainReader.getBlockByHash(blockHash) | ||
|
||
maybeBlock match { | ||
case Some(block) => removeBlock(block, withState) | ||
case Some(block) => removeBlock(block) | ||
case None => | ||
log.warn(s"Attempted removing block with hash ${ByteStringUtils.hash2string(blockHash)} that we don't have") | ||
} | ||
} | ||
|
||
// scalastyle:off method.length | ||
private def removeBlock(block: Block, withState: Boolean): Unit = { | ||
private def removeBlock(block: Block): Unit = { | ||
val blockHash = block.hash | ||
|
||
log.debug(s"Trying to remove block ${block.idTag}") | ||
|
@@ -229,17 +212,12 @@ class BlockchainImpl( | |
.and(latestCheckpointNumberUpdates) | ||
.commit() | ||
|
||
saveBestKnownBlocks(newBestBlockNumber, Some(newLatestCheckpointNumber)) | ||
log.debug( | ||
"Removed block with hash {}. New best block number - {}, new best checkpoint block number - {}", | ||
ByteStringUtils.hash2string(blockHash), | ||
newBestBlockNumber, | ||
newLatestCheckpointNumber | ||
) | ||
|
||
// not transactional part | ||
if (withState) | ||
stateStorage.onBlockRollback(block.number, bestBlockNumber)(() => persistBestBlocksData()) | ||
} | ||
// scalastyle:on method.length | ||
|
||
|
@@ -288,8 +266,7 @@ trait BlockchainStorages { | |
object BlockchainImpl { | ||
def apply( | ||
storages: BlockchainStorages, | ||
blockchainReader: BlockchainReader, | ||
metadata: BlockchainMetadata | ||
blockchainReader: BlockchainReader | ||
): BlockchainImpl = | ||
new BlockchainImpl( | ||
blockHeadersStorage = storages.blockHeadersStorage, | ||
|
@@ -300,7 +277,6 @@ object BlockchainImpl { | |
transactionMappingStorage = storages.transactionMappingStorage, | ||
appStateStorage = storages.appStateStorage, | ||
stateStorage = storages.stateStorage, | ||
blockchainReader = blockchainReader, | ||
blockchainMetadata = metadata | ||
blockchainReader = blockchainReader | ||
) | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is saving to the storage removed here?🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After I did all the changes I had a bunch of tests failing in FastSyncItSpec, because the best blocknumber was always a bit inferior than expected, and the issue was that line.
Now that we don't have the caches anymore and always save to storage that instructions was bringing issues.