Skip to content

[kaizen] Create EmptyBlochainBranch #1044

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class RegularSyncItSpec extends FreeSpecBase with Matchers with BeforeAndAfterAl
_ <- peer2.importBlocksUntil(30)(IdentityUpdate)
_ <- peer1.startRegularSync()
_ <- peer2.startRegularSync()
_ <- peer2.addCheckpointedBlock(peer2.blockchainReader.getBestBranch().get.getBlockByNumber(25).get)
_ <- peer2.addCheckpointedBlock(peer2.blockchainReader.getBestBranch().getBlockByNumber(25).get)
_ <- peer2.waitForRegularSyncLoadLastBlock(length)
_ <- peer1.connectToPeers(Set(peer2.node))
_ <- peer1.waitForRegularSyncLoadLastBlock(length)
Expand Down Expand Up @@ -181,8 +181,8 @@ class RegularSyncItSpec extends FreeSpecBase with Matchers with BeforeAndAfterAl
)
)
(
peer1.blockchainReader.getBestBranch().get.getBlockByNumber(blockNumer + 1),
peer2.blockchainReader.getBestBranch().get.getBlockByNumber(blockNumer + 1)
peer1.blockchainReader.getBestBranch().getBlockByNumber(blockNumer + 1),
peer2.blockchainReader.getBestBranch().getBlockByNumber(blockNumer + 1)
) match {
case (Some(blockP1), Some(blockP2)) =>
assert(peer1.bl.getChainWeightByHash(blockP1.hash) == peer2.bl.getChainWeightByHash(blockP2.hash))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ object RegularSyncItSpecUtils {
case Some(bNumber) =>
blockchainReader
.getBestBranch()
.get
.getBlockByNumber(bNumber)
.getOrElse(throw new RuntimeException(s"block by number: $bNumber doesn't exist"))
case None => blockchainReader.getBestBlock().get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ object DumpChainApp
val blockchain: Blockchain = new BlockchainMock(genesisHash)
val blockchainReader: BlockchainReader = mock[BlockchainReader]
val bestChain: BlockchainBranch = mock[BlockchainBranch]
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
(bestChain.getHashByBlockNumber _).expects(*).returning(Some(genesisHash))

val nodeStatus: NodeStatus =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait OmmersValidator {
val getNBlocksBack: (ByteString, Int) => List[Block] =
(_, n) =>
((blockNumber - n) until blockNumber).toList
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
.flatMap(nb => bestBranch.getBlockByNumber(nb))

validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/scala/io/iohk/ethereum/domain/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ class BlockchainImpl(
override def isInChain(hash: ByteString): Boolean =
(for {
header <- blockchainReader.getBlockHeaderByHash(hash) if header.number <= blockchainReader.getBestBlockNumber()
bestBranch <- blockchainReader.getBestBranch()
hash <- bestBranch.getHashByBlockNumber(header.number)
hash <- blockchainReader.getBestBranch().getHashByBlockNumber(header.number)
} yield header.hash == hash).getOrElse(false)

override def getChainWeightByHash(blockhash: ByteString): Option[ChainWeight] = chainWeightStorage.get(blockhash)
Expand Down Expand Up @@ -231,7 +230,7 @@ class BlockchainImpl(
val latestCheckpointNumber = getLatestCheckpointBlockNumber()

val blockNumberMappingUpdates =
if (blockchainReader.getBestBranch().flatMap(_.getHashByBlockNumber(block.number)).contains(blockHash))
if (blockchainReader.getBestBranch().getHashByBlockNumber(block.number).contains(blockHash))
removeBlockNumberMapping(block.number)
else blockNumberMappingStorage.emptyBatchUpdate

Expand Down Expand Up @@ -300,7 +299,7 @@ class BlockchainImpl(
): BigInt =
if (blockNumberToCheck > 0) {
val maybePreviousCheckpointBlockNumber = for {
currentBlock <- blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockNumberToCheck))
currentBlock <- blockchainReader.getBestBranch().getBlockByNumber(blockNumberToCheck)
if currentBlock.hasCheckpoint &&
currentBlock.number < latestCheckpointBlockNumber
} yield currentBlock.number
Expand Down
21 changes: 11 additions & 10 deletions src/main/scala/io/iohk/ethereum/domain/BlockchainReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.iohk.ethereum.db.storage.ReceiptStorage
import io.iohk.ethereum.db.storage.StateStorage
import io.iohk.ethereum.domain.branch.BestBlockchainBranch
import io.iohk.ethereum.domain.branch.BlockchainBranch
import io.iohk.ethereum.domain.branch.EmptyBlockchainBranch
import io.iohk.ethereum.mpt.MptNode
import io.iohk.ethereum.utils.Logger

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

/** get the current best stored branch */
// FIXME this should not be an option as we should always have the genesis
// but some tests prevent it to simply be BlockchainBranch for now
def getBestBranch(): Option[BlockchainBranch] =
getBestBlock().map { block =>
new BestBlockchainBranch(
block.header,
blockNumberMappingStorage,
this
)
}
def getBestBranch(): BlockchainBranch =
getBestBlock()
.map { block =>
new BestBlockchainBranch(
block.header,
blockNumberMappingStorage,
this
)
}
.getOrElse(EmptyBlockchainBranch)

def getBestBlockNumber(): BigInt = {
val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.iohk.ethereum.domain.branch
import akka.util.ByteString

import io.iohk.ethereum.domain.Block

object EmptyBlockchainBranch extends BlockchainBranch {

override def getBlockByNumber(number: BigInt): Option[Block] = None

override def getHashByBlockNumber(number: BigInt): Option[ByteString] = None
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CheckpointingService(
req.parentCheckpoint.forall(blockchainReader.getBlockHeaderByHash(_).exists(_.number < blockToReturnNum))

Task {
blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockToReturnNum))
blockchainReader.getBestBranch().getBlockByNumber(blockToReturnNum)
}.flatMap {
case Some(b) if isValidParent =>
Task.now(Right(GetLatestBlockResponse(Some(BlockInfo(b.hash, b.number)))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class EthProofService(
def getBlock(number: BigInt): Either[JsonRpcError, Block] =
blockchainReader
.getBestBranch()
.flatMap(_.getBlockByNumber(number))
.getBlockByNumber(number)
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))

def getLatestBlock(): Either[JsonRpcError, Block] =
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/jsonrpc/EthTxService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class EthTxService(
Task {
val bestBranch = blockchainReader.getBestBranch()
val gasPrice = ((bestBlock - blockDifference) to bestBlock)
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
.flatMap(nb => bestBranch.getBlockByNumber(nb))
.flatMap(_.body.transactionList)
.map(_.tx.gasPrice)
if (gasPrice.nonEmpty) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/jsonrpc/ResolveBlock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ trait ResolveBlock {
private def getBlock(number: BigInt): Either[JsonRpcError, Block] =
blockchainReader
.getBestBranch()
.flatMap(_.getBlockByNumber(number))
.getBlockByNumber(number)
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))

private def getLatestBlock(): Either[JsonRpcError, Block] =
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class TestService(

val blockOpt = request.parameters.blockHashOrNumber
.fold(
number => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(number)),
number => blockchainReader.getBestBranch().getBlockByNumber(number),
blockHash => blockchainReader.getBlockByHash(blockHash)
)

Expand Down Expand Up @@ -412,7 +412,7 @@ class TestService(

val blockOpt = request.parameters.blockHashOrNumber
.fold(
number => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(number)),
number => blockchainReader.getBestBranch().getBlockByNumber(number),
hash => blockchainReader.getBlockByHash(hash)
)

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/io/iohk/ethereum/ledger/BlockImport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class BlockImport(
private def removeBlocksUntil(parent: ByteString, fromNumber: BigInt): List[BlockData] = {
@tailrec
def removeBlocksUntil(parent: ByteString, fromNumber: BigInt, acc: List[BlockData]): List[BlockData] =
blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(fromNumber)) match {
blockchainReader.getBestBranch().getBlockByNumber(fromNumber) match {
case Some(block) if block.header.hash == parent || fromNumber == 0 =>
acc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BlockValidation(
val numbers = (block.header.number - remaining) until block.header.number
val bestBranch = blockchainReader.getBestBranch()
val blocks =
(numbers.toList.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb))) :+ block) ::: queuedBlocks
(numbers.toList.flatMap(nb => bestBranch.getBlockByNumber(nb)) :+ block) ::: queuedBlocks
blocks
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReade
private def getTopBlocksFromNumber(from: BigInt): List[Block] = {
val bestBranch = blockchainReader.getBestBranch()
(from to blockchainReader.getBestBlockNumber())
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
.flatMap(nb => bestBranch.getBlockByNumber(nb))
.toList
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestEthBlockServiceWrapper(
.getBlockByNumber(request)
.map(
_.map { blockByBlockResponse =>
val bestBranch = blockchainReader.getBestBranch().get
val bestBranch = blockchainReader.getBestBranch()
val fullBlock = bestBranch.getBlockByNumber(blockByBlockResponse.blockResponse.get.number).get
BlockByNumberResponse(blockByBlockResponse.blockResponse.map(response => toEthResponse(fullBlock, response)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TransactionHistoryService(
val getLastCheckpoint = Task(blockchain.getLatestCheckpointBlockNumber()).memoizeOnSuccess
val txnsFromBlocks = Observable
.from(fromBlocks.reverse)
.mapParallelOrdered(10)(blockNr => Task(blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockNr))))(
.mapParallelOrdered(10)(blockNr => Task(blockchainReader.getBestBranch().getBlockByNumber(blockNr)))(
OverflowStrategy.Unbounded
)
.collect { case Some(block) => block }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class StdOmmersValidatorSpec extends AnyFlatSpec with Matchers with ScalaCheckPr
val getNBlocksBack: (ByteString, Int) => List[Block] =
(_, n) =>
((ommersBlockNumber - n) until ommersBlockNumber).toList
.flatMap(nb => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(nb)))
.flatMap(nb => blockchainReader.getBestBranch().getBlockByNumber(nb))

ommersValidator.validateOmmersAncestors(
ommersBlockParentHash,
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/io/iohk/ethereum/domain/BlockchainSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
val validBlock = Fixtures.Blocks.ValidBlock.block
blockchainWriter.storeBlock(validBlock).commit()
blockchain.saveBestKnownBlocks(validBlock.number)
val block = blockchainReader.getBestBranch().get.getBlockByNumber(validBlock.header.number)
val block = blockchainReader.getBestBranch().getBlockByNumber(validBlock.header.number)
block.isDefined should ===(true)
validBlock should ===(block.get)
}
Expand All @@ -70,7 +70,7 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
it should "not return a value if not stored" in new EphemBlockchainTestSetup {
blockchainReader
.getBestBranch()
.flatMap(_.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number)) shouldBe None
.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number) shouldBe None
blockchainReader.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash) shouldBe None
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class CheckpointingServiceSpec
val blockchain: BlockchainImpl = mock[BlockchainImpl]
val blockchainReader: BlockchainReader = mock[BlockchainReader]
val bestChain: BlockchainBranch = mock[BlockchainBranch]
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
val blockQueue: BlockQueue = mock[BlockQueue]
val syncController: TestProbe = TestProbe()
val checkpointBlockGenerator: CheckpointBlockGenerator = new CheckpointBlockGenerator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ trait MockBlockchain extends MockFactory { self: TestSetupWithVmAndValidators =>
val bestChain: BlockchainBranch = mock[BlockchainBranch]
override lazy val blockchainReader: BlockchainReader = mock[BlockchainReader]
override lazy val blockchainWriter: BlockchainWriter = mock[BlockchainWriter]
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(bestChain)
override lazy val blockchain: BlockchainImpl = mock[BlockchainImpl]
//- cake overrides

Expand Down