Skip to content

Commit 08b649e

Browse files
author
Aurélien Richez
committed
fix unit tests
1 parent bec2614 commit 08b649e

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) {

0 commit comments

Comments
 (0)