Skip to content

Commit ea2596b

Browse files
author
Aurélien Richez
committed
fix unit tests
1 parent 03db870 commit ea2596b

25 files changed

+94
-43
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.getBlockByNumber(25).get)
127+
_ <- peer2.addCheckpointedBlock(peer2.blockchainReader.getBestBranch().get.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.getBlockByNumber(blockNumer + 1),
185-
peer2.blockchainReader.getBlockByNumber(blockNumer + 1)
184+
peer1.blockchainReader.getBestBranch().get.getBlockByNumber(blockNumer + 1),
185+
peer2.blockchainReader.getBestBranch().get.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ object RegularSyncItSpecUtils {
184184
Task(blockNumber match {
185185
case Some(bNumber) =>
186186
blockchainReader
187+
.getBestBranch()
188+
.get
187189
.getBlockByNumber(bNumber)
188190
.getOrElse(throw new RuntimeException(s"block by number: $bNumber doesn't exist"))
189191
case None => blockchainReader.getBestBlock().get

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import io.iohk.ethereum.db.storage.pruning.PruningMode
2727
import io.iohk.ethereum.domain.BlockHeader.HeaderExtraFields.HefEmpty
2828
import io.iohk.ethereum.domain.Blockchain
2929
import io.iohk.ethereum.domain._
30+
import io.iohk.ethereum.domain.branch.BlockchainBranch
3031
import io.iohk.ethereum.jsonrpc.ProofService.EmptyStorageValueProof
3132
import io.iohk.ethereum.jsonrpc.ProofService.StorageProof
3233
import io.iohk.ethereum.jsonrpc.ProofService.StorageProofKey
@@ -102,7 +103,9 @@ object DumpChainApp
102103

103104
val blockchain: Blockchain = new BlockchainMock(genesisHash)
104105
val blockchainReader: BlockchainReader = mock[BlockchainReader]
105-
(blockchainReader.getHashByBlockNumber _).expects(*).returning(Some(genesisHash))
106+
val bestChain: BlockchainBranch = mock[BlockchainBranch]
107+
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
108+
(bestChain.getHashByBlockNumber _).expects(*).returning(Some(genesisHash))
106109

107110
val nodeStatus: NodeStatus =
108111
NodeStatus(key = nodeKey, serverStatus = ServerStatus.NotListening, discoveryStatus = ServerStatus.NotListening)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ trait OmmersValidator {
2929
): Either[OmmersError, OmmersValid] = {
3030

3131
val getBlockHeaderByHash: ByteString => Option[BlockHeader] = blockchainReader.getBlockHeaderByHash
32+
val bestBranch = blockchainReader.getBestBranch()
3233
val getNBlocksBack: (ByteString, Int) => List[Block] =
33-
(_, n) => ((blockNumber - n) until blockNumber).toList.flatMap(blockchainReader.getBestBranch.getBlockByNumber)
34+
(_, n) =>
35+
((blockNumber - n) until blockNumber).toList
36+
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
3437

3538
validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
3639
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class BlockchainImpl(
107107
override def isInChain(hash: ByteString): Boolean =
108108
(for {
109109
header <- blockchainReader.getBlockHeaderByHash(hash) if header.number <= blockchainReader.getBestBlockNumber()
110-
hash <- blockchainReader.getBestBranch().getHashByBlockNumber(header.number)
110+
bestBranch <- blockchainReader.getBestBranch()
111+
hash <- bestBranch.getHashByBlockNumber(header.number)
111112
} yield header.hash == hash).getOrElse(false)
112113

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

232233
val blockNumberMappingUpdates =
233-
if (blockchainReader.getBestBranch().getHashByBlockNumber(block.number).contains(blockHash))
234+
if (blockchainReader.getBestBranch().flatMap(_.getHashByBlockNumber(block.number)).contains(blockHash))
234235
removeBlockNumberMapping(block.number)
235236
else blockNumberMappingStorage.emptyBatchUpdate
236237

@@ -299,7 +300,7 @@ class BlockchainImpl(
299300
): BigInt =
300301
if (blockNumberToCheck > 0) {
301302
val maybePreviousCheckpointBlockNumber = for {
302-
currentBlock <- blockchainReader.getBestBranch.getBlockByNumber(blockNumberToCheck)
303+
currentBlock <- blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(blockNumberToCheck))
303304
if currentBlock.hasCheckpoint &&
304305
currentBlock.number < latestCheckpointBlockNumber
305306
} yield currentBlock.number

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ class BlockchainReader(
6969
*/
7070
def getReceiptsByHash(blockhash: ByteString): Option[Seq[Receipt]] = receiptStorage.get(blockhash)
7171

72-
def getBestBranch(): BlockchainBranch =
73-
new BestBlockchainBranch(
74-
getBestBlock().map(_.header).getOrElse(genesisHeader),
75-
blockNumberMappingStorage,
76-
this
77-
)
72+
/** 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+
}
7883

7984
def getBestBlockNumber(): BigInt = {
8085
val bestSavedBlockNumber = appStateStorage.getBestBlockNumber()
@@ -105,7 +110,7 @@ class BlockchainReader(
105110
getBlockHeaderByNumber(0).get
106111

107112
def genesisBlock: Block =
108-
getBestBranch().getBlockByNumber(0).get
113+
getBlockByNumber(0).get
109114

110115
/** Allows to query for a block based on it's number
111116
*

src/main/scala/io/iohk/ethereum/domain/branch/BestBlockchainBranch.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BestBlockchainBranch(
2525
* */
2626

2727
override def getBlockByNumber(number: BigInt): Option[Block] =
28-
if (tipBlockHeader.number <= number && number > 0) {
28+
if (tipBlockHeader.number >= number && number >= 0) {
2929
for {
3030
hash <- getHashByBlockNumber(number)
3131
block <- blockchainReader.getBlockByHash(hash)

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.getBlockByNumber(blockToReturnNum)
39+
blockchainReader.getBestBranch().flatMap(_.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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ class EthProofService(
204204

205205
private def resolveBlock(blockParam: BlockParam): Either[JsonRpcError, ResolvedBlock] = {
206206
def getBlock(number: BigInt): Either[JsonRpcError, Block] =
207-
blockchainReader.getBestBranch
208-
.getBlockByNumber(number)
207+
blockchainReader
208+
.getBestBranch()
209+
.flatMap(_.getBlockByNumber(number))
209210
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
210211

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ class EthTxService(
156156
val bestBlock = blockchainReader.getBestBlockNumber()
157157

158158
Task {
159+
val bestBranch = blockchainReader.getBestBranch()
159160
val gasPrice = ((bestBlock - blockDifference) to bestBlock)
160-
.flatMap(blockchainReader.getBestBranch.getBlockByNumber)
161+
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
161162
.flatMap(_.body.transactionList)
162163
.map(_.tx.gasPrice)
163164
if (gasPrice.nonEmpty) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ trait ResolveBlock {
3333
}
3434

3535
private def getBlock(number: BigInt): Either[JsonRpcError, Block] =
36-
blockchainReader.getBestBranch
37-
.getBlockByNumber(number)
36+
blockchainReader
37+
.getBestBranch()
38+
.flatMap(_.getBlockByNumber(number))
3839
.toRight(JsonRpcError.InvalidParams(s"Block $number not found"))
3940

4041
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.getBlockByNumber(number),
375+
number => blockchainReader.getBestBranch().flatMap(_.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.getBlockByNumber(number),
415+
number => blockchainReader.getBestBranch().flatMap(_.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.getBlockByNumber(fromNumber) match {
308+
blockchainReader.getBestBranch().flatMap(_.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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ class BlockValidation(
4141
val remaining = n - queuedBlocks.length - 1
4242

4343
val numbers = (block.header.number - remaining) until block.header.number
44+
val bestBranch = blockchainReader.getBestBranch()
4445
val blocks =
45-
(numbers.toList.flatMap(blockchainReader.getBestBranch.getBlockByNumber) :+ block) ::: queuedBlocks
46+
(numbers.toList.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb))) :+ block) ::: queuedBlocks
4647
blocks
4748
}
4849
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ class BranchResolution(blockchain: Blockchain, blockchainReader: BlockchainReade
7272
}
7373
}
7474

75-
private def getTopBlocksFromNumber(from: BigInt): List[Block] =
75+
private def getTopBlocksFromNumber(from: BigInt): List[Block] = {
76+
val bestBranch = blockchainReader.getBestBranch()
7677
(from to blockchainReader.getBestBlockNumber())
77-
.flatMap(blockchainReader.getBestBranch.getBlockByNumber)
78+
.flatMap(nb => bestBranch.flatMap(_.getBlockByNumber(nb)))
7879
.toList
80+
}
7981
}
8082

8183
sealed trait BranchResolutionResult

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

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

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.getBlockByNumber(blockNr)))(
36+
.mapParallelOrdered(10)(blockNr => Task(blockchainReader.getBestBranch().flatMap(_.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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ class StdOmmersValidatorSpec extends AnyFlatSpec with Matchers with ScalaCheckPr
7979

8080
it should "report failure if there is an ommer which that is not parent of an ancestor" in new BlockUtils {
8181
val getNBlocksBack: (ByteString, Int) => List[Block] =
82-
(_, n) => ((ommersBlockNumber - n) until ommersBlockNumber).toList.flatMap(blockchainReader.getBlockByNumber)
82+
(_, n) =>
83+
((ommersBlockNumber - n) until ommersBlockNumber).toList
84+
.flatMap(nb => blockchainReader.getBestBranch().flatMap(_.getBlockByNumber(nb)))
8385

8486
ommersValidator.validateOmmersAncestors(
8587
ommersBlockParentHash,
@@ -462,6 +464,7 @@ class StdOmmersValidatorSpec extends AnyFlatSpec with Matchers with ScalaCheckPr
462464
.and(blockchainWriter.storeBlock(block95))
463465
.and(blockchainWriter.storeBlock(block96))
464466
.commit()
467+
blockchain.saveBestKnownBlocks(block96.number)
465468

466469
}
467470
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
4444
it should "be able to store a block and retrieve it by number" in new EphemBlockchainTestSetup {
4545
val validBlock = Fixtures.Blocks.ValidBlock.block
4646
blockchainWriter.storeBlock(validBlock).commit()
47-
val block = blockchainReader.getBlockByNumber(validBlock.header.number)
47+
blockchain.saveBestKnownBlocks(validBlock.number)
48+
val block = blockchainReader.getBestBranch().get.getBlockByNumber(validBlock.header.number)
4849
block.isDefined should ===(true)
4950
validBlock should ===(block.get)
5051
}
@@ -67,8 +68,10 @@ class BlockchainSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyCh
6768
}
6869

6970
it should "not return a value if not stored" in new EphemBlockchainTestSetup {
70-
blockchainReader.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number).isEmpty should ===(true)
71-
blockchainReader.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash).isEmpty should ===(true)
71+
blockchainReader
72+
.getBestBranch()
73+
.flatMap(_.getBlockByNumber(Fixtures.Blocks.ValidBlock.header.number)) shouldBe None
74+
blockchainReader.getBlockByHash(Fixtures.Blocks.ValidBlock.header.hash) shouldBe None
7275
}
7376

7477
it should "be able to store a block with checkpoint and retrieve it and checkpoint" in new EphemBlockchainTestSetup {

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.iohk.ethereum.domain.BlockBody
2323
import io.iohk.ethereum.domain.BlockchainImpl
2424
import io.iohk.ethereum.domain.BlockchainReader
2525
import io.iohk.ethereum.domain.Checkpoint
26+
import io.iohk.ethereum.domain.branch.BlockchainBranch
2627
import io.iohk.ethereum.jsonrpc.CheckpointingService._
2728
import io.iohk.ethereum.ledger.BlockQueue
2829

@@ -53,7 +54,7 @@ class CheckpointingServiceSpec
5354
val expectedResponse = GetLatestBlockResponse(Some(BlockInfo(block.hash, block.number)))
5455

5556
(blockchainReader.getBestBlockNumber _).expects().returning(bestBlockNum)
56-
(blockchainReader.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
57+
(bestChain.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
5758
val result = service.getLatestBlock(request)
5859

5960
result.runSyncUnsafe() shouldEqual Right(expectedResponse)
@@ -83,7 +84,7 @@ class CheckpointingServiceSpec
8384
(blockchainReader.getBlockHeaderByHash _)
8485
.expects(hash)
8586
.returning(Some(previousCheckpoint.header.copy(number = 0)))
86-
(blockchainReader.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
87+
(bestChain.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
8788
val result = service.getLatestBlock(request)
8889

8990
result.runSyncUnsafe() shouldEqual Right(expectedResponse)
@@ -111,7 +112,7 @@ class CheckpointingServiceSpec
111112
(blockchainReader.getBlockHeaderByHash _)
112113
.expects(hash)
113114
.returning(Some(previousCheckpoint.header.copy(number = bestBlockNum)))
114-
(blockchainReader.getBlockByNumber _).expects(*).returning(Some(previousCheckpoint))
115+
(bestChain.getBlockByNumber _).expects(*).returning(Some(previousCheckpoint))
115116
val result = service.getLatestBlock(request)
116117

117118
result.runSyncUnsafe() shouldEqual Right(expectedResponse)
@@ -139,7 +140,7 @@ class CheckpointingServiceSpec
139140

140141
(blockchainReader.getBestBlockNumber _).expects().returning(bestBlockNum)
141142
(blockchainReader.getBlockHeaderByHash _).expects(hash).returning(None)
142-
(blockchainReader.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
143+
(bestChain.getBlockByNumber _).expects(checkpointedBlockNum).returning(Some(block))
143144
val result = service.getLatestBlock(request)
144145

145146
result.runSyncUnsafe() shouldEqual Right(expectedResponse)
@@ -167,13 +168,13 @@ class CheckpointingServiceSpec
167168
(blockchainReader.getBestBlockNumber _)
168169
.expects()
169170
.returning(7)
170-
(blockchainReader.getBlockByNumber _)
171+
(bestChain.getBlockByNumber _)
171172
.expects(BigInt(4))
172173
.returning(None)
173174
(blockchainReader.getBestBlockNumber _)
174175
.expects()
175176
.returning(7)
176-
(blockchainReader.getBlockByNumber _)
177+
(bestChain.getBlockByNumber _)
177178
.expects(BigInt(4))
178179
.returning(Some(block))
179180

@@ -185,6 +186,8 @@ class CheckpointingServiceSpec
185186
trait TestSetup {
186187
val blockchain: BlockchainImpl = mock[BlockchainImpl]
187188
val blockchainReader: BlockchainReader = mock[BlockchainReader]
189+
val bestChain: BlockchainBranch = mock[BlockchainBranch]
190+
(blockchainReader.getBestBranch _).expects().anyNumberOfTimes().returning(Some(bestChain))
188191
val blockQueue: BlockQueue = mock[BlockQueue]
189192
val syncController: TestProbe = TestProbe()
190193
val checkpointBlockGenerator: CheckpointBlockGenerator = new CheckpointBlockGenerator()

0 commit comments

Comments
 (0)