Skip to content

Commit 555880c

Browse files
author
Aurélien Richez
committed
Create EmptyBlochainBranch and make getBestChain return it if there no known best block
1 parent 85538c8 commit 555880c

21 files changed

+46
-36
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class RegularSyncItSpec extends FreeSpecBase with Matchers with BeforeAndAfterAl
124124
_ <- peer2.importBlocksUntil(30)(IdentityUpdate)
125125
_ <- peer1.startRegularSync()
126126
_ <- peer2.startRegularSync()
127-
_ <- peer2.addCheckpointedBlock(peer2.blockchainReader.getBestBranch().get.getBlockByNumber(25).get)
127+
_ <- peer2.addCheckpointedBlock(peer2.blockchainReader.getBestBranch().getBlockByNumber(25).get)
128128
_ <- peer2.waitForRegularSyncLoadLastBlock(length)
129129
_ <- peer1.connectToPeers(Set(peer2.node))
130130
_ <- peer1.waitForRegularSyncLoadLastBlock(length)
@@ -181,8 +181,8 @@ class RegularSyncItSpec extends FreeSpecBase with Matchers with BeforeAndAfterAl
181181
)
182182
)
183183
(
184-
peer1.blockchainReader.getBestBranch().get.getBlockByNumber(blockNumer + 1),
185-
peer2.blockchainReader.getBestBranch().get.getBlockByNumber(blockNumer + 1)
184+
peer1.blockchainReader.getBestBranch().getBlockByNumber(blockNumer + 1),
185+
peer2.blockchainReader.getBestBranch().getBlockByNumber(blockNumer + 1)
186186
) match {
187187
case (Some(blockP1), Some(blockP2)) =>
188188
assert(peer1.bl.getChainWeightByHash(blockP1.hash) == peer2.bl.getChainWeightByHash(blockP2.hash))

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ object RegularSyncItSpecUtils {
187187
case Some(bNumber) =>
188188
blockchainReader
189189
.getBestBranch()
190-
.get
191190
.getBlockByNumber(bNumber)
192191
.getOrElse(throw new RuntimeException(s"block by number: $bNumber doesn't exist"))
193192
case None => blockchainReader.getBestBlock().get

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object DumpChainApp
103103
val blockchain: Blockchain = new BlockchainMock(genesisHash)
104104
val blockchainReader: BlockchainReader = mock[BlockchainReader]
105105
val bestChain: BlockchainBranch = mock[BlockchainBranch]
106-
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
106+
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
107107
(bestChain.getHashByBlockNumber _).expects(*).returning(Some(genesisHash))
108108

109109
val nodeStatus: NodeStatus =

src/main/scala/io/iohk/ethereum/consensus/pow/validators/OmmersValidator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait OmmersValidator {
3333
val getNBlocksBack: (ByteString, Int) => List[Block] =
3434
(_, n) =>
3535
((blockNumber - n) until blockNumber).toList
36-
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
36+
.flatMap(nb => bestBranch.getBlockByNumber(nb))
3737

3838
validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
3939
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ class BlockchainImpl(
103103
override def isInChain(hash: ByteString): Boolean =
104104
(for {
105105
header <- blockchainReader.getBlockHeaderByHash(hash) if header.number <= blockchainReader.getBestBlockNumber()
106-
bestBranch <- blockchainReader.getBestBranch()
107-
hash <- bestBranch.getHashByBlockNumber(header.number)
106+
hash <- blockchainReader.getBestBranch().getHashByBlockNumber(header.number)
108107
} yield header.hash == hash).getOrElse(false)
109108

110109
override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
@@ -224,7 +223,7 @@ class BlockchainImpl(
224223
val latestCheckpointNumber = getLatestCheckpointBlockNumber()
225224

226225
val blockNumberMappingUpdates =
227-
if (blockchainReader.getBestBranch().flatMap(_.getHashByBlockNumber(block.number)).contains(blockHash))
226+
if (blockchainReader.getBestBranch().getHashByBlockNumber(block.number).contains(blockHash))
228227
removeBlockNumberMapping(block.number)
229228
else blockNumberMappingStorage.emptyBatchUpdate
230229

@@ -293,7 +292,7 @@ class BlockchainImpl(
293292
): BigInt =
294293
if (blockNumberToCheck > 0) {
295294
val maybePreviousCheckpointBlockNumber = for {
296-
currentBlock <- blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockNumberToCheck))
295+
currentBlock <- blockchainReader.getBestBranch().getBlockByNumber(blockNumberToCheck)
297296
if currentBlock.hasCheckpoint &&
298297
currentBlock.number < latestCheckpointBlockNumber
299298
} yield currentBlock.number

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.iohk.ethereum.db.storage.ReceiptStorage
1010
import io.iohk.ethereum.db.storage.StateStorage
1111
import io.iohk.ethereum.domain.branch.BestBlockchainBranch
1212
import io.iohk.ethereum.domain.branch.BlockchainBranch
13+
import io.iohk.ethereum.domain.branch.EmptyBlockchainBranch
1314
import io.iohk.ethereum.mpt.MptNode
1415
import io.iohk.ethereum.utils.Logger
1516

@@ -70,16 +71,16 @@ class BlockchainReader(
7071
def getReceiptsByHash(blockhash: ByteString): Option[Seq[Receipt]] = receiptStorage.get(blockhash)
7172

7273
/** get the current best stored branch */
73-
// FIXME this should not be an option as we should always have the genesis
74-
// but some tests prevent it to simply be BlockchainBranch for now
75-
def getBestBranch(): Option[BlockchainBranch] =
76-
getBestBlock().map { block =>
77-
new BestBlockchainBranch(
78-
block.header,
79-
blockNumberMappingStorage,
80-
this
81-
)
82-
}
74+
def getBestBranch(): BlockchainBranch =
75+
getBestBlock()
76+
.map { block =>
77+
new BestBlockchainBranch(
78+
block.header,
79+
blockNumberMappingStorage,
80+
this
81+
)
82+
}
83+
.getOrElse(EmptyBlockchainBranch)
8384

8485
def getBestBlockNumber(): BigInt = {
8586
val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.iohk.ethereum.domain.branch
2+
import akka.util.ByteString
3+
4+
import io.iohk.ethereum.domain.Block
5+
6+
object EmptyBlockchainBranch extends BlockchainBranch {
7+
8+
override def getBlockByNumber(number: BigInt): Option[Block] = None
9+
10+
override def getHashByBlockNumber(number: BigInt): Option[ByteString] = None
11+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class CheckpointingService(
3636
req.parentCheckpoint.forall(blockchainReader.getBlockHeaderByHash(_).exists(_.number < blockToReturnNum))
3737

3838
Task {
39-
blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockToReturnNum))
39+
blockchainReader.getBestBranch().getBlockByNumber(blockToReturnNum)
4040
}.flatMap {
4141
case Some(b) if isValidParent =>
4242
Task.now(Right(GetLatestBlockResponse(Some(BlockInfo(b.hash, b.number)))))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ class EthProofService(
206206
def getBlock(number: BigInt): Either[JsonRpcError, Block] =
207207
blockchainReader
208208
.getBestBranch()
209-
.flatMap(_.getBlockByNumber(number))
209+
.getBlockByNumber(number)
210210
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
211211

212212
def getLatestBlock(): Either[JsonRpcError, Block] =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class EthTxService(
158158
Task {
159159
val bestBranch = blockchainReader.getBestBranch()
160160
val gasPrice = ((bestBlock - blockDifference) to bestBlock)
161-
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
161+
.flatMap(nb => bestBranch.getBlockByNumber(nb))
162162
.flatMap(_.body.transactionList)
163163
.map(_.tx.gasPrice)
164164
if (gasPrice.nonEmpty) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait ResolveBlock {
3535
private def getBlock(number: BigInt): Either[JsonRpcError, Block] =
3636
blockchainReader
3737
.getBestBranch()
38-
.flatMap(_.getBlockByNumber(number))
38+
.getBlockByNumber(number)
3939
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
4040

4141
private def getLatestBlock(): Either[JsonRpcError, Block] =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class TestService(
372372

373373
val blockOpt = request.parameters.blockHashOrNumber
374374
.fold(
375-
number => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(number)),
375+
number => blockchainReader.getBestBranch().getBlockByNumber(number),
376376
blockHash => blockchainReader.getBlockByHash(blockHash)
377377
)
378378

@@ -412,7 +412,7 @@ class TestService(
412412

413413
val blockOpt = request.parameters.blockHashOrNumber
414414
.fold(
415-
number => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(number)),
415+
number => blockchainReader.getBestBranch().getBlockByNumber(number),
416416
hash => blockchainReader.getBlockByHash(hash)
417417
)
418418

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class BlockImport(
305305
private def removeBlocksUntil(parent: ByteString, fromNumber: BigInt): List[BlockData] = {
306306
@tailrec
307307
def removeBlocksUntil(parent: ByteString, fromNumber: BigInt, acc: List[BlockData]): List[BlockData] =
308-
blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(fromNumber)) match {
308+
blockchainReader.getBestBranch().getBlockByNumber(fromNumber) match {
309309
case Some(block) if block.header.hash == parent || fromNumber == 0 =>
310310
acc
311311

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class BlockValidation(
4343
val numbers = (block.header.number - remaining) until block.header.number
4444
val bestBranch = blockchainReader.getBestBranch()
4545
val blocks =
46-
(numbers.toList.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb))) :+ block) ::: queuedBlocks
46+
(numbers.toList.flatMap(nb => bestBranch.getBlockByNumber(nb)) :+ block) ::: queuedBlocks
4747
blocks
4848
}
4949
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReade
7575
private def getTopBlocksFromNumber(from: BigInt): List[Block] = {
7676
val bestBranch = blockchainReader.getBestBranch()
7777
(from to blockchainReader.getBestBlockNumber())
78-
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
78+
.flatMap(nb => bestBranch.getBlockByNumber(nb))
7979
.toList
8080
}
8181
}

src/main/scala/io/iohk/ethereum/testmode/TestEthBlockServiceWrapper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class TestEthBlockServiceWrapper(
7575
.getBlockByNumber(request)
7676
.map(
7777
_.map { blockByBlockResponse =>
78-
val bestBranch = blockchainReader.getBestBranch().get
78+
val bestBranch = blockchainReader.getBestBranch()
7979
val fullBlock = bestBranch.getBlockByNumber(blockByBlockResponse.blockResponse.get.number).get
8080
BlockByNumberResponse(blockByBlockResponse.blockResponse.map(response => toEthResponse(fullBlock, response)))
8181
}

src/main/scala/io/iohk/ethereum/transactions/TransactionHistoryService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TransactionHistoryService(
3333
val getLastCheckpoint = Task(blockchain.getLatestCheckpointBlockNumber()).memoizeOnSuccess
3434
val txnsFromBlocks = Observable
3535
.from(fromBlocks.reverse)
36-
.mapParallelOrdered(10)(blockNr => Task(blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockNr))))(
36+
.mapParallelOrdered(10)(blockNr => Task(blockchainReader.getBestBranch().getBlockByNumber(blockNr)))(
3737
OverflowStrategy.Unbounded
3838
)
3939
.collect { case Some(block) => block }

src/test/scala/io/iohk/ethereum/consensus/pow/validators/StdOmmersValidatorSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class StdOmmersValidatorSpec extends AnyFlatSpec with Matchers with ScalaCheckPr
8181
val getNBlocksBack: (ByteString, Int) => List[Block] =
8282
(_, n) =>
8383
((ommersBlockNumber - n) until ommersBlockNumber).toList
84-
.flatMap(nb => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(nb)))
84+
.flatMap(nb => blockchainReader.getBestBranch().getBlockByNumber(nb))
8585

8686
ommersValidator.validateOmmersAncestors(
8787
ommersBlockParentHash,

src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
4545
val validBlock = Fixtures.Blocks.ValidBlock.block
4646
blockchainWriter.storeBlock(validBlock).commit()
4747
blockchain.saveBestKnownBlocks(validBlock.number)
48-
val block = blockchainReader.getBestBranch().get.getBlockByNumber(validBlock.header.number)
48+
val block = blockchainReader.getBestBranch().getBlockByNumber(validBlock.header.number)
4949
block.isDefined should ===(true)
5050
validBlock should ===(block.get)
5151
}
@@ -70,7 +70,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
7070
it should "not return a value if not stored" in new EphemBlockchainTestSetup {
7171
blockchainReader
7272
.getBestBranch()
73-
.flatMap(_.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number)) shouldBe None
73+
.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number) shouldBe None
7474
blockchainReader.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash) shouldBe None
7575
}
7676

src/test/scala/io/iohk/ethereum/jsonrpc/CheckpointingServiceSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CheckpointingServiceSpec
187187
val blockchain: BlockchainImpl = mock[BlockchainImpl]
188188
val blockchainReader: BlockchainReader = mock[BlockchainReader]
189189
val bestChain: BlockchainBranch = mock[BlockchainBranch]
190-
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
190+
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
191191
val blockQueue: BlockQueue = mock[BlockQueue]
192192
val syncController: TestProbe = TestProbe()
193193
val checkpointBlockGenerator: CheckpointBlockGenerator = new CheckpointBlockGenerator()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ trait MockBlockchain extends MockFactory { self: TestSetupWithVmAndValidators =>
415415
val bestChain: BlockchainBranch = mock[BlockchainBranch]
416416
override lazy val blockchainReader: BlockchainReader = mock[BlockchainReader]
417417
override lazy val blockchainWriter: BlockchainWriter = mock[BlockchainWriter]
418-
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
418+
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
419419
override lazy val blockchain: BlockchainImpl = mock[BlockchainImpl]
420420
//- cake overrides
421421

0 commit comments

Comments
 (0)