Skip to content

Commit 8d8d6cb

Browse files
author
Leonor Boga
committed
[ETCM-1045] Move getChainWeightByHash from Blockchain to BlockchainReader
1 parent 5dc67e6 commit 8d8d6cb

File tree

16 files changed

+56
-48
lines changed

16 files changed

+56
-48
lines changed

src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,23 @@ class RegularSyncItSpec extends FreeSpecBase with Matchers with BeforeAndAfterAl
178178
_ <- peer2.waitForRegularSyncLoadLastBlock(blockNumer + 3)
179179
} yield {
180180
assert(
181-
peer1.bl.getChainWeightByHash(peer1.blockchainReader.getBestBlock().get.hash) == peer2.bl.getChainWeightByHash(
182-
peer2.blockchainReader.getBestBlock().get.hash
183-
)
181+
peer1.blockchainReader.getChainWeightByHash(
182+
peer1.blockchainReader.getBestBlock().get.hash
183+
) == peer2.blockchainReader
184+
.getChainWeightByHash(
185+
peer2.blockchainReader.getBestBlock().get.hash
186+
)
184187
)
185188
(
186189
peer1.blockchainReader.getBlockByNumber(peer1.blockchainReader.getBestBranch(), blockNumer + 1),
187190
peer2.blockchainReader.getBlockByNumber(peer2.blockchainReader.getBestBranch(), blockNumer + 1)
188191
) match {
189192
case (Some(blockP1), Some(blockP2)) =>
190-
assert(peer1.bl.getChainWeightByHash(blockP1.hash) == peer2.bl.getChainWeightByHash(blockP2.hash))
193+
assert(
194+
peer1.blockchainReader.getChainWeightByHash(blockP1.hash) == peer2.blockchainReader.getChainWeightByHash(
195+
blockP2.hash
196+
)
197+
)
191198
case (_, _) => fail("invalid difficulty validation")
192199
}
193200
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
283283
def getCurrentState(): BlockchainState = {
284284
val bestBlock = blockchainReader.getBestBlock().get
285285
val currentWorldState = getMptForBlock(bestBlock)
286-
val currentWeight = bl.getChainWeightByHash(bestBlock.hash).get
286+
val currentWeight = blockchainReader.getChainWeightByHash(bestBlock.hash).get
287287
BlockchainState(bestBlock, currentWorldState, currentWeight)
288288
}
289289

@@ -356,7 +356,7 @@ abstract class CommonFakePeer(peerName: String, fakePeerCustomConfig: FakePeerCu
356356
currentBestBlock: Block
357357
)(updateWorldForBlock: (BigInt, InMemoryWorldStateProxy) => InMemoryWorldStateProxy): Task[Unit] =
358358
Task {
359-
val currentWeight = bl.getChainWeightByHash(currentBestBlock.hash).get
359+
val currentWeight = blockchainReader.getChainWeightByHash(currentBestBlock.hash).get
360360
val currentWorld = getMptForBlock(currentBestBlock)
361361
val (newBlock, newWeight, _) =
362362
createChildBlock(currentBestBlock, currentWeight, currentWorld)(updateWorldForBlock)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class BlockchainMock(genesisHash: ByteString) extends Blockchain {
186186

187187
override def removeBlock(hash: ByteString): Unit = ???
188188

189-
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = ???
189+
def getAccount(address: Address, blockNumber: BigInt): Option[Account] = ???
190190

191191
override def getAccountStorageAt(rootHash: ByteString, position: BigInt, ethCompatibleStorage: Boolean): ByteString =
192192
???

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class SyncController(
130130
consensus,
131131
blockchainReader,
132132
stateStorage,
133-
new BranchResolution(blockchain, blockchainReader),
133+
new BranchResolution(blockchainReader),
134134
validators.blockValidator,
135135
blacklist,
136136
syncConfig,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ class FastSync(
643643
} yield (validatedHeader, parentWeight)
644644

645645
def getParentChainWeight(header: BlockHeader) =
646-
blockchain.getChainWeightByHash(header.parentHash).toRight(ParentChainWeightNotFound(header))
646+
blockchainReader.getChainWeightByHash(header.parentHash).toRight(ParentChainWeightNotFound(header))
647647

648648
@tailrec
649649
def processHeaders(headers: Seq[BlockHeader]): HeaderProcessingResult =
@@ -941,7 +941,7 @@ class FastSync(
941941
}
942942

943943
def assignBlockchainWork(peerWithInfo: PeerWithInfo): Unit = {
944-
val PeerWithInfo(peer, peerInfo) = peerWithInfo
944+
val PeerWithInfo(peer, _) = peerWithInfo
945945
log.debug(s"Assigning blockchain work for peer [{}]", peer.id.value)
946946
if (syncState.receiptsQueue.nonEmpty) {
947947
requestReceipts(peer)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ConsensusImpl(
7272
log.debug("Ignoring duplicated block: {}", block.idTag)
7373
Task.now(DuplicateBlock)
7474
} else {
75-
blockchain.getChainWeightByHash(bestBlock.header.hash) match {
75+
blockchainReader.getChainWeightByHash(bestBlock.header.hash) match {
7676
case Some(weight) =>
7777
doBlockPreValidation(block).flatMap {
7878
case Left(error) => Task.now(BlockImportFailed(error.reason.toString))
@@ -234,7 +234,7 @@ class ConsensusImpl(
234234
val reorgResult = for {
235235
parent <- newBranch.headOption
236236
parentHash = parent.header.parentHash
237-
parentWeight <- blockchain.getChainWeightByHash(parentHash)
237+
parentWeight <- blockchainReader.getChainWeightByHash(parentHash)
238238
} yield {
239239
log.debug(
240240
"Removing blocks starting from number {} and parent {}",
@@ -330,7 +330,7 @@ class ConsensusImpl(
330330

331331
val blockDataOpt = for {
332332
receipts <- blockchainReader.getReceiptsByHash(hash)
333-
weight <- blockchain.getChainWeightByHash(hash)
333+
weight <- blockchainReader.getChainWeightByHash(hash)
334334
} yield BlockData(block, receipts, weight)
335335

336336
blockchain.removeBlock(hash)

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ trait Blockchain {
5454
*/
5555
def getReadOnlyMptStorage(): MptStorage
5656

57-
/** Looks up ChainWeight for a given chain
58-
* @param blockhash Hash of top block in the chain
59-
* @return ChainWeight if found
60-
*/
61-
def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight]
62-
6357
def getLatestCheckpointBlockNumber(): BigInt
6458

6559
def removeBlock(hash: ByteString): Unit
@@ -81,8 +75,6 @@ class BlockchainImpl(
8175
) extends Blockchain
8276
with Logger {
8377

84-
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
85-
8678
override def getLatestCheckpointBlockNumber(): BigInt = appStateStorage.getLatestCheckpointBlockNumber()
8779

8880
override def getAccountStorageAt(

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package io.iohk.ethereum.domain
22

33
import akka.util.ByteString
4-
5-
import io.iohk.ethereum.db.storage.AppStateStorage
6-
import io.iohk.ethereum.db.storage.BlockBodiesStorage
7-
import io.iohk.ethereum.db.storage.BlockHeadersStorage
8-
import io.iohk.ethereum.db.storage.BlockNumberMappingStorage
9-
import io.iohk.ethereum.db.storage.ReceiptStorage
10-
import io.iohk.ethereum.db.storage.StateStorage
4+
import io.iohk.ethereum.db.storage.{
5+
AppStateStorage,
6+
BlockBodiesStorage,
7+
BlockHeadersStorage,
8+
BlockNumberMappingStorage,
9+
ChainWeightStorage,
10+
ReceiptStorage,
11+
StateStorage
12+
}
1113
import io.iohk.ethereum.domain.branch.BestBranch
1214
import io.iohk.ethereum.domain.branch.Branch
1315
import io.iohk.ethereum.domain.branch.EmptyBranch
@@ -21,7 +23,8 @@ class BlockchainReader(
2123
blockNumberMappingStorage: BlockNumberMappingStorage,
2224
stateStorage: StateStorage,
2325
receiptStorage: ReceiptStorage,
24-
appStateStorage: AppStateStorage
26+
appStateStorage: AppStateStorage,
27+
chainWeightStorage: ChainWeightStorage
2528
) extends Logger {
2629

2730
/** Allows to query a blockHeader by block hash
@@ -150,6 +153,12 @@ class BlockchainReader(
150153
case EmptyBranch => None
151154
}
152155

156+
/** Looks up ChainWeight for a given chain
157+
* @param blockhash Hash of top block in the chain
158+
* @return ChainWeight if found
159+
*/
160+
def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
161+
153162
/** Allows to query for a block based on it's number
154163
*
155164
* @param number Block number
@@ -189,7 +198,8 @@ object BlockchainReader {
189198
storages.blockNumberMappingStorage,
190199
storages.stateStorage,
191200
storages.receiptStorage,
192-
storages.appStateStorage
201+
storages.appStateStorage,
202+
storages.chainWeightStorage
193203
)
194204

195205
}

src/main/scala/io/iohk/ethereum/jsonrpc/EthBlocksService.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class EthBlocksService(
7575
def getByBlockHash(request: BlockByBlockHashRequest): ServiceResponse[BlockByBlockHashResponse] = Task {
7676
val BlockByBlockHashRequest(blockHash, fullTxs) = request
7777
val blockOpt = blockchainReader.getBlockByHash(blockHash).orElse(blockQueue.getBlockByHash(blockHash))
78-
val weight = blockchain.getChainWeightByHash(blockHash).orElse(blockQueue.getChainWeightByHash(blockHash))
78+
val weight = blockchainReader.getChainWeightByHash(blockHash).orElse(blockQueue.getChainWeightByHash(blockHash))
7979

8080
val blockResponseOpt = blockOpt.map(block => BlockResponse(block, weight, fullTxs = fullTxs))
8181
Right(BlockByBlockHashResponse(blockResponseOpt))
@@ -90,7 +90,7 @@ class EthBlocksService(
9090
val BlockByNumberRequest(blockParam, fullTxs) = request
9191
val blockResponseOpt =
9292
resolveBlock(blockParam).toOption.map { case ResolvedBlock(block, pending) =>
93-
val weight = blockchain.getChainWeightByHash(block.header.hash)
93+
val weight = blockchainReader.getChainWeightByHash(block.header.hash)
9494
BlockResponse(block, weight, fullTxs = fullTxs, pendingBlock = pending.isDefined)
9595
}
9696
Right(BlockByNumberResponse(blockResponseOpt))
@@ -122,7 +122,7 @@ class EthBlocksService(
122122
else
123123
None
124124
}
125-
val weight = uncleHeaderOpt.flatMap(uncleHeader => blockchain.getChainWeightByHash(uncleHeader.hash))
125+
val weight = uncleHeaderOpt.flatMap(uncleHeader => blockchainReader.getChainWeightByHash(uncleHeader.hash))
126126

127127
//The block in the response will not have any txs or uncles
128128
val uncleBlockResponseOpt = uncleHeaderOpt.map { uncleHeader =>
@@ -144,7 +144,7 @@ class EthBlocksService(
144144
.flatMap { case ResolvedBlock(block, pending) =>
145145
if (uncleIndex >= 0 && uncleIndex < block.body.uncleNodesList.size) {
146146
val uncleHeader = block.body.uncleNodesList.apply(uncleIndex.toInt)
147-
val weight = blockchain.getChainWeightByHash(uncleHeader.hash)
147+
val weight = blockchainReader.getChainWeightByHash(uncleHeader.hash)
148148

149149
//The block in the response will not have any txs or uncles
150150
Some(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class BlockQueue(
6565
None
6666

6767
case None =>
68-
val parentWeight = blockchain.getChainWeightByHash(parentHash)
68+
val parentWeight = blockchainReader.getChainWeightByHash(parentHash)
6969

7070
parentWeight match {
7171

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import cats.data.NonEmptyList
44

55
import io.iohk.ethereum.domain.Block
66
import io.iohk.ethereum.domain.BlockHeader
7-
import io.iohk.ethereum.domain.Blockchain
87
import io.iohk.ethereum.domain.BlockchainReader
98
import io.iohk.ethereum.domain.ChainWeight
109
import io.iohk.ethereum.utils.Logger
1110

12-
class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReader) extends Logger {
11+
class BranchResolution(blockchainReader: BlockchainReader) extends Logger {
1312

1413
def resolveBranch(headers: NonEmptyList[BlockHeader]): BranchResolutionResult =
1514
if (!doHeadersFormChain(headers)) {
@@ -49,7 +48,7 @@ class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReade
4948
.map(_.header)
5049
.orElse(newHeaders.headOption)
5150
.map { header =>
52-
blockchain
51+
blockchainReader
5352
.getChainWeightByHash(header.parentHash)
5453
.toRight(s"ChainWeight for ${header.idTag} not found when resolving branch: $newHeaders")
5554
}

src/main/scala/io/iohk/ethereum/network/handshaker/EtcNodeStatus64ExchangeState.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ case class EtcNodeStatus64ExchangeState(
1919

2020
override protected def createStatusMsg(): MessageSerializable = {
2121
val bestBlockHeader = getBestBlockHeader()
22-
val chainWeight = blockchain.getChainWeightByHash(bestBlockHeader.hash).get
22+
val chainWeight = blockchainReader.getChainWeightByHash(bestBlockHeader.hash).get
2323

2424
val status = ETC64.Status(
2525
protocolVersion = Capability.ETC64.version,

src/main/scala/io/iohk/ethereum/network/handshaker/EthNodeStatus63ExchangeState.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ case class EthNodeStatus63ExchangeState(
2020

2121
override protected def createStatusMsg(): MessageSerializable = {
2222
val bestBlockHeader = getBestBlockHeader()
23-
val chainWeight = blockchain.getChainWeightByHash(bestBlockHeader.hash).get
23+
val chainWeight = blockchainReader.getChainWeightByHash(bestBlockHeader.hash).get
2424

2525
val status = BaseETH6XMessages.Status(
2626
protocolVersion = Capability.ETH63.version,

src/main/scala/io/iohk/ethereum/network/handshaker/EthNodeStatus64ExchangeState.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ case class EthNodeStatus64ExchangeState(
3535

3636
override protected def createStatusMsg(): MessageSerializable = {
3737
val bestBlockHeader = getBestBlockHeader()
38-
val chainWeight = blockchain.getChainWeightByHash(bestBlockHeader.hash).get
38+
val chainWeight = blockchainReader.getChainWeightByHash(bestBlockHeader.hash).get
3939
val genesisHash = blockchainReader.genesisHeader.hash
4040

4141
val status = ETH64.Status(

src/test/scala/io/iohk/ethereum/consensus/ConsensusSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
133133

134134
setBlockExists(block, inChain = false, inQueue = false)
135135
setBestBlock(bestBlock)
136-
(blockchain.getChainWeightByHash _).expects(*).returning(None)
136+
(blockchainReader.getChainWeightByHash _).expects(*).returning(None)
137137

138138
(blockchainReader.getBlockHeaderByHash _).expects(*).returning(Some(block.header))
139139

@@ -191,7 +191,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
191191
blockchainWriter.save(blockData3.block, blockData3.receipts, blockData3.weight, saveAsBestBlock = true)
192192

193193
blockchainReader.getBestBlock().get shouldEqual newBlock3
194-
blockchain.getChainWeightByHash(newBlock3.header.hash) shouldEqual Some(newWeight3)
194+
blockchainReader.getChainWeightByHash(newBlock3.header.hash) shouldEqual Some(newWeight3)
195195

196196
blockQueue.isQueued(oldBlock2.header.hash) shouldBe true
197197
blockQueue.isQueued(oldBlock3.header.hash) shouldBe true
@@ -235,7 +235,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
235235
}
236236

237237
blockchainReader.getBestBlock().get shouldEqual oldBlock3
238-
blockchain.getChainWeightByHash(oldBlock3.header.hash) shouldEqual Some(oldWeight3)
238+
blockchainReader.getChainWeightByHash(oldBlock3.header.hash) shouldEqual Some(oldWeight3)
239239

240240
blockQueue.isQueued(newBlock2.header.hash) shouldBe true
241241
blockQueue.isQueued(newBlock3.header.hash) shouldBe false
@@ -348,7 +348,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
348348
it should "correctly import a checkpoint block" in new EphemBlockchain with CheckpointHelpers {
349349
val parentBlock: Block = getBlock(bestNum)
350350
val regularBlock: Block = getBlock(bestNum + 1, difficulty = 200, parent = parentBlock.hash)
351-
val checkpointBlock: Block = getCheckpointBlock(parentBlock, difficulty = 100)
351+
val checkpointBlock: Block = getCheckpointBlock(parentBlock)
352352

353353
val weightParent = ChainWeight.totalDifficultyOnly(parentBlock.header.difficulty + 999)
354354
val weightRegular = weightParent.increase(regularBlock.header)
@@ -376,15 +376,15 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
376376
blockchainWriter.save(checkpointBlock, Nil, weightCheckpoint, saveAsBestBlock = true)
377377

378378
blockchainReader.getBestBlock().get shouldEqual checkpointBlock
379-
blockchain.getChainWeightByHash(checkpointBlock.hash) shouldEqual Some(weightCheckpoint)
379+
blockchainReader.getChainWeightByHash(checkpointBlock.hash) shouldEqual Some(weightCheckpoint)
380380
}
381381

382382
it should "not import a block with higher difficulty that does not follow a checkpoint" in new EphemBlockchain
383383
with CheckpointHelpers {
384384

385385
val parentBlock: Block = getBlock(bestNum)
386386
val regularBlock: Block = getBlock(bestNum + 1, difficulty = 200, parent = parentBlock.hash)
387-
val checkpointBlock: Block = getCheckpointBlock(parentBlock, difficulty = 100)
387+
val checkpointBlock: Block = getCheckpointBlock(parentBlock)
388388

389389
val weightParent = ChainWeight.totalDifficultyOnly(parentBlock.header.difficulty + 999)
390390
val weightCheckpoint = weightParent.increase(checkpointBlock.header)

src/test/scala/io/iohk/ethereum/ledger/BlockQueueSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class BlockQueueSpec extends AnyFlatSpec with Matchers with MockFactory {
176176
block: Block,
177177
weight: Option[ChainWeight] = None
178178
): CallHandler1[ByteString, Option[ChainWeight]] =
179-
(blockchain.getChainWeightByHash _).expects(block.header.parentHash).returning(weight)
179+
(blockchainReader.getChainWeightByHash _).expects(block.header.parentHash).returning(weight)
180180

181181
def randomHash(): ByteString =
182182
ObjectGenerators.byteStringOfLengthNGen(32).sample.get

0 commit comments

Comments
 (0)