Skip to content

Commit e2bc270

Browse files
author
Leonor Boga
committed
[ETCM-1048] Remove bestKnownBlockAndLatestCheckpoint cache
1 parent 1df5b99 commit e2bc270

24 files changed

+91
-267
lines changed

src/it/scala/io/iohk/ethereum/sync/util/CommonFakePeer.scala

+3-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import io.iohk.ethereum.db.storage.pruning.PruningMode
3939
import io.iohk.ethereum.domain.Block
4040
import io.iohk.ethereum.domain.Blockchain
4141
import io.iohk.ethereum.domain.BlockchainImpl
42-
import io.iohk.ethereum.domain.BlockchainMetadata
4342
import io.iohk.ethereum.domain.BlockchainReader
4443
import io.iohk.ethereum.domain.BlockchainWriter
4544
import io.iohk.ethereum.domain.ChainWeight
@@ -138,13 +137,9 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
138137
)
139138
)
140139

141-
val blockchainMetadata = new BlockchainMetadata(
142-
storagesInstance.storages.appStateStorage.getBestBlockNumber(),
143-
storagesInstance.storages.appStateStorage.getLatestCheckpointBlockNumber()
144-
)
145-
val blockchainReader: BlockchainReader = BlockchainReader(storagesInstance.storages, blockchainMetadata)
146-
val blockchainWriter: BlockchainWriter = BlockchainWriter(storagesInstance.storages, blockchainMetadata)
147-
val bl: BlockchainImpl = BlockchainImpl(storagesInstance.storages, blockchainReader, blockchainMetadata)
140+
val blockchainReader: BlockchainReader = BlockchainReader(storagesInstance.storages)
141+
val blockchainWriter: BlockchainWriter = BlockchainWriter(storagesInstance.storages)
142+
val bl: BlockchainImpl = BlockchainImpl(storagesInstance.storages, blockchainReader)
148143
val evmCodeStorage = storagesInstance.storages.evmCodeStorage
149144

150145
val genesis: Block = Block(

src/it/scala/io/iohk/ethereum/txExecTest/ECIP1017Test.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ class ECIP1017Test extends AnyFlatSpec with Matchers {
8080

8181
(startBlock to endBlock).foreach { blockToExecute =>
8282
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
83-
val blockchainMetadata = getNewBlockchainMetadata
84-
val blockchainReader = BlockchainReader(storages, blockchainMetadata)
85-
val blockchainWriter = BlockchainWriter(storages, blockchainMetadata)
86-
val blockchain = BlockchainImpl(storages, blockchainReader, blockchainMetadata)
83+
val blockchainReader = BlockchainReader(storages)
84+
val blockchainWriter = BlockchainWriter(storages)
85+
val blockchain = BlockchainImpl(storages, blockchainReader)
8786
val blockValidation =
8887
new BlockValidation(mining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
8988
val blockExecution =

src/it/scala/io/iohk/ethereum/txExecTest/ForksTest.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ class ForksTest extends AnyFlatSpec with Matchers {
5656

5757
(startBlock to endBlock).foreach { blockToExecute =>
5858
val storages = FixtureProvider.prepareStorages(blockToExecute - 1, fixtures)
59-
val blockchainMetadata = getNewBlockchainMetadata
60-
val blockchainReader = BlockchainReader(storages, blockchainMetadata)
61-
val blockchainWriter = BlockchainWriter(storages, blockchainMetadata)
62-
val blockchain = BlockchainImpl(storages, blockchainReader, blockchainMetadata)
59+
val blockchainReader = BlockchainReader(storages)
60+
val blockchainWriter = BlockchainWriter(storages)
61+
val blockchain = BlockchainImpl(storages, blockchainReader)
6362
val blockValidation =
6463
new BlockValidation(mining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
6564
val blockExecution =

src/it/scala/io/iohk/ethereum/txExecTest/ScenarioSetup.scala

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import io.iohk.ethereum.ledger.VMImpl
1010
trait ScenarioSetup extends EphemBlockchainTestSetup {
1111
protected val testBlockchainStorages: BlockchainStorages
1212

13-
val blockchainMetadata = getNewBlockchainMetadata
14-
override lazy val blockchainReader: BlockchainReader = BlockchainReader(testBlockchainStorages, blockchainMetadata)
15-
override lazy val blockchainWriter: BlockchainWriter = BlockchainWriter(testBlockchainStorages, blockchainMetadata)
16-
override lazy val blockchain: BlockchainImpl =
17-
BlockchainImpl(testBlockchainStorages, blockchainReader, blockchainMetadata)
13+
override lazy val blockchainReader: BlockchainReader = BlockchainReader(testBlockchainStorages)
14+
override lazy val blockchainWriter: BlockchainWriter = BlockchainWriter(testBlockchainStorages)
15+
override lazy val blockchain: BlockchainImpl = BlockchainImpl(testBlockchainStorages, blockchainReader)
1816
override lazy val vm: VMImpl = new VMImpl
1917
}

src/it/scala/io/iohk/ethereum/txExecTest/util/DumpChainApp.scala

+1-5
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,10 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
184184
ethCompatibleStorage: Boolean
185185
): StorageProof = EmptyStorageValueProof(StorageProofKey(position))
186186

187-
override def removeBlock(hash: ByteString, withState: Boolean = true): Unit = ???
187+
override def removeBlock(hash: ByteString): Unit = ???
188188

189189
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = ???
190190

191-
def getAccount(address: Address, blockNumber: BigInt): Option[Account] = ???
192-
193191
override def getAccountStorageAt(rootHash: ByteString, position: BigInt, ethCompatibleStorage: Boolean): ByteString =
194192
???
195193

@@ -198,8 +196,6 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
198196

199197
def getBestBlockNumber(): BigInt = ???
200198

201-
def saveBlockNumber(number: BigInt, hash: NodeHash): Unit = ???
202-
203199
def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit = ???
204200

205201
def getBestBlock(): Option[Block] = ???

src/main/scala/io/iohk/ethereum/blockchain/sync/SyncController.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class SyncController(
6868
def start(): Unit = {
6969
import syncConfig.doFastSync
7070

71-
appStateStorage.putSyncStartingBlock(appStateStorage.getBestBlockNumber())
71+
appStateStorage.putSyncStartingBlock(appStateStorage.getBestBlockNumber()).commit
7272
(appStateStorage.isFastSyncDone(), doFastSync) match {
7373
case (false, true) =>
7474
startFastSync()

src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSync.scala

+4-8
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,13 @@ class FastSync(
565565
}
566566

567567
// TODO [ETCM-676]: Move to blockchain and make sure it's atomic
568-
private def discardLastBlocks(startBlock: BigInt, blocksToDiscard: Int): Unit = {
568+
private def discardLastBlocks(startBlock: BigInt, blocksToDiscard: Int): Unit =
569569
// TODO (maybe ETCM-77): Manage last checkpoint number too
570-
appStateStorage.putBestBlockNumber((startBlock - blocksToDiscard - 1).max(0)).commit()
571-
572570
(startBlock to ((startBlock - blocksToDiscard).max(1)) by -1).foreach { n =>
573571
blockchainReader.getBlockHeaderByNumber(n).foreach { headerToRemove =>
574-
blockchain.removeBlock(headerToRemove.hash, withState = false)
572+
blockchain.removeBlock(headerToRemove.hash)
575573
}
576574
}
577-
}
578575

579576
private def validateHeader(header: BlockHeader, peer: Peer): Either[HeaderProcessingResult, BlockHeader] = {
580577
val shouldValidate = header.number >= syncState.nextBlockToFullyValidate
@@ -788,7 +785,7 @@ class FastSync(
788785
|Peers waiting_for_response/connected: ${assignedHandlers.size}/${handshakedPeers.size} (${blacklistedIds.size} blacklisted).
789786
|State: ${syncState.downloadedNodesCount}/${syncState.totalNodesCount} nodes.
790787
|""".stripMargin.replace("\n", " "),
791-
appStateStorage.getBestBlockNumber()
788+
blockchainReader.getBestBlockNumber()
792789
)
793790
log.debug(
794791
s"""|Connection status: connected({})/
@@ -1134,10 +1131,9 @@ class FastSync(
11341131

11351132
if (fullBlocks.nonEmpty) {
11361133
val bestReceivedBlock = fullBlocks.maxBy(_.number)
1137-
val lastStoredBestBlockNumber = appStateStorage.getBestBlockNumber()
1134+
val lastStoredBestBlockNumber = blockchainReader.getBestBlockNumber()
11381135
if (lastStoredBestBlockNumber < bestReceivedBlock.number) {
11391136
blockchain.saveBestKnownBlocks(bestReceivedBlock.number)
1140-
appStateStorage.putBestBlockNumber(bestReceivedBlock.number).commit()
11411137
}
11421138
syncState = syncState.copy(lastFullBlockNumber = bestReceivedBlock.number.max(lastStoredBestBlockNumber))
11431139
}

src/main/scala/io/iohk/ethereum/blockchain/sync/fast/FastSyncBranchResolver.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait FastSyncBranchResolver {
2525
blocksToBeRemoved.foreach { toBeRemoved =>
2626
blockchainReader
2727
.getBlockHeaderByNumber(toBeRemoved)
28-
.foreach(header => blockchain.removeBlock(header.hash, withState = false))
28+
.foreach(header => blockchain.removeBlock(header.hash))
2929
}
3030
}
3131

src/main/scala/io/iohk/ethereum/consensus/ConsensusImpl.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class ConsensusImpl(
333333
weight <- blockchain.getChainWeightByHash(hash)
334334
} yield BlockData(block, receipts, weight)
335335

336-
blockchain.removeBlock(hash, withState = true)
336+
blockchain.removeBlock(hash)
337337

338338
removeBlocksUntil(parent, fromNumber - 1, blockDataOpt.map(_ :: acc).getOrElse(acc))
339339

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

+13-37
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ trait Blockchain {
6262

6363
def getLatestCheckpointBlockNumber(): BigInt
6464

65-
def removeBlock(hash: ByteString, withState: Boolean): Unit
65+
def removeBlock(hash: ByteString): Unit
6666

6767
def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit
6868

@@ -77,15 +77,13 @@ class BlockchainImpl(
7777
protected val transactionMappingStorage: TransactionMappingStorage,
7878
protected val appStateStorage: AppStateStorage,
7979
protected val stateStorage: StateStorage,
80-
blockchainReader: BlockchainReader,
81-
blockchainMetadata: BlockchainMetadata
80+
blockchainReader: BlockchainReader
8281
) extends Blockchain
8382
with Logger {
8483

8584
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
8685

87-
override def getLatestCheckpointBlockNumber(): BigInt =
88-
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().latestCheckpointNumber
86+
override def getLatestCheckpointBlockNumber(): BigInt = appStateStorage.getLatestCheckpointBlockNumber()
8987

9088
override def getAccountStorageAt(
9189
rootHash: ByteString,
@@ -128,22 +126,6 @@ class BlockchainImpl(
128126

129127
def getReadOnlyMptStorage(): MptStorage = stateStorage.getReadOnlyStorage
130128

131-
private def persistBestBlocksData(): Unit = {
132-
val currentBestBlockNumber = blockchainReader.getBestBlockNumber()
133-
val currentBestCheckpointNumber = getLatestCheckpointBlockNumber()
134-
log.debug(
135-
"Persisting app info data into database. Persisted block number is {}. " +
136-
"Persisted checkpoint number is {}",
137-
currentBestBlockNumber,
138-
currentBestCheckpointNumber
139-
)
140-
141-
appStateStorage
142-
.putBestBlockNumber(currentBestBlockNumber)
143-
.and(appStateStorage.putLatestCheckpointBlockNumber(currentBestCheckpointNumber))
144-
.commit()
145-
}
146-
147129
override def saveBestKnownBlocks(bestBlockNumber: BigInt, latestCheckpointNumber: Option[BigInt] = None): Unit =
148130
latestCheckpointNumber match {
149131
case Some(number) =>
@@ -153,28 +135,29 @@ class BlockchainImpl(
153135
}
154136

155137
private def saveBestKnownBlock(bestBlockNumber: BigInt): Unit =
156-
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.updateAndGet(_.copy(bestBlockNumber = bestBlockNumber))
138+
appStateStorage.putBestBlockNumber(bestBlockNumber).commit()
157139

158140
private def saveBestKnownBlockAndLatestCheckpointNumber(number: BigInt, latestCheckpointNumber: BigInt): Unit =
159-
blockchainMetadata.bestKnownBlockAndLatestCheckpoint.set(
160-
BestBlockLatestCheckpointNumbers(number, latestCheckpointNumber)
161-
)
141+
appStateStorage
142+
.putBestBlockNumber(number)
143+
.and(appStateStorage.putLatestCheckpointBlockNumber(latestCheckpointNumber))
144+
.commit()
162145

163146
private def removeBlockNumberMapping(number: BigInt): DataSourceBatchUpdate =
164147
blockNumberMappingStorage.remove(number)
165148

166-
override def removeBlock(blockHash: ByteString, withState: Boolean): Unit = {
149+
override def removeBlock(blockHash: ByteString): Unit = {
167150
val maybeBlock = blockchainReader.getBlockByHash(blockHash)
168151

169152
maybeBlock match {
170-
case Some(block) => removeBlock(block, withState)
153+
case Some(block) => removeBlock(block)
171154
case None =>
172155
log.warn(s"Attempted removing block with hash ${ByteStringUtils.hash2string(blockHash)} that we don't have")
173156
}
174157
}
175158

176159
// scalastyle:off method.length
177-
private def removeBlock(block: Block, withState: Boolean): Unit = {
160+
private def removeBlock(block: Block): Unit = {
178161
val blockHash = block.hash
179162

180163
log.debug(s"Trying to remove block ${block.idTag}")
@@ -229,17 +212,12 @@ class BlockchainImpl(
229212
.and(latestCheckpointNumberUpdates)
230213
.commit()
231214

232-
saveBestKnownBlocks(newBestBlockNumber, Some(newLatestCheckpointNumber))
233215
log.debug(
234216
"Removed block with hash {}. New best block number - {}, new best checkpoint block number - {}",
235217
ByteStringUtils.hash2string(blockHash),
236218
newBestBlockNumber,
237219
newLatestCheckpointNumber
238220
)
239-
240-
// not transactional part
241-
if (withState)
242-
stateStorage.onBlockRollback(block.number, bestBlockNumber)(() => persistBestBlocksData())
243221
}
244222
// scalastyle:on method.length
245223

@@ -288,8 +266,7 @@ trait BlockchainStorages {
288266
object BlockchainImpl {
289267
def apply(
290268
storages: BlockchainStorages,
291-
blockchainReader: BlockchainReader,
292-
metadata: BlockchainMetadata
269+
blockchainReader: BlockchainReader
293270
): BlockchainImpl =
294271
new BlockchainImpl(
295272
blockHeadersStorage = storages.blockHeadersStorage,
@@ -300,7 +277,6 @@ object BlockchainImpl {
300277
transactionMappingStorage = storages.transactionMappingStorage,
301278
appStateStorage = storages.appStateStorage,
302279
stateStorage = storages.stateStorage,
303-
blockchainReader = blockchainReader,
304-
blockchainMetadata = metadata
280+
blockchainReader = blockchainReader
305281
)
306282
}

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

-10
This file was deleted.

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

+6-25
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class BlockchainReader(
2121
blockNumberMappingStorage: BlockNumberMappingStorage,
2222
stateStorage: StateStorage,
2323
receiptStorage: ReceiptStorage,
24-
appStateStorage: AppStateStorage,
25-
blockchainMetadata: BlockchainMetadata
24+
appStateStorage: AppStateStorage
2625
) extends Logger {
2726

2827
/** Allows to query a blockHeader by block hash
@@ -81,29 +80,13 @@ class BlockchainReader(
8180
.getOrElse(EmptyBranch)
8281
}
8382

84-
def getBestBlockNumber(): BigInt = {
85-
val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
86-
val bestKnownBlockNumber = blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().bestBlockNumber
87-
log.debug(
88-
"Current best saved block number {}. Current best known block number {}",
89-
bestSavedBlockNumber,
90-
bestKnownBlockNumber
91-
)
92-
93-
// The cached best block number should always be more up-to-date than the one on disk, we are keeping access to disk
94-
// above only for logging purposes
95-
bestKnownBlockNumber
96-
}
83+
def getBestBlockNumber(): BigInt = appStateStorage.getBestBlockNumber()
9784

98-
//returns the best known block if it's available in the storage, otherwise the best stored block
85+
//returns the best known block if it's available in the storage
9986
def getBestBlock(): Option[Block] = {
10087
val bestBlockNumber = getBestBlockNumber()
10188
log.debug("Trying to get best block with number {}", bestBlockNumber)
102-
getBlockByNumber(bestBlockNumber).orElse(
103-
getBlockByNumber(
104-
appStateStorage.getBestBlockNumber()
105-
)
106-
)
89+
getBlockByNumber(bestBlockNumber)
10790
}
10891

10992
def genesisHeader: BlockHeader =
@@ -199,16 +182,14 @@ class BlockchainReader(
199182
object BlockchainReader {
200183

201184
def apply(
202-
storages: BlockchainStorages,
203-
blockchainMetadata: BlockchainMetadata
185+
storages: BlockchainStorages
204186
): BlockchainReader = new BlockchainReader(
205187
storages.blockHeadersStorage,
206188
storages.blockBodiesStorage,
207189
storages.blockNumberMappingStorage,
208190
storages.stateStorage,
209191
storages.receiptStorage,
210-
storages.appStateStorage,
211-
blockchainMetadata
192+
storages.appStateStorage
212193
)
213194

214195
}

0 commit comments

Comments
 (0)