Skip to content

Commit 39f6410

Browse files
author
Leonor Boga
committed
ETCM-1058 Extend ConsensusSpec error handling tests
1 parent 0b8477d commit 39f6410

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import io.iohk.ethereum.ledger.{
2626
OmmersTestSetup,
2727
TestSetupWithVmAndValidators
2828
}
29-
import io.iohk.ethereum.mpt.{LeafNode, MerklePatriciaTrie}
29+
import io.iohk.ethereum.mpt.{LeafNode, MerklePatriciaTrie, NullNode}
3030
import io.iohk.ethereum.utils.BlockchainConfig
3131
import org.scalatest.concurrent.ScalaFutures
3232
import org.scalatest.flatspec.AnyFlatSpec
@@ -40,7 +40,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
4040
implicit override val patienceConfig: PatienceConfig =
4141
PatienceConfig(timeout = scaled(2 seconds), interval = scaled(1 second))
4242

43-
"Importing blocks" should "ignore existing block" in new ImportBlockTestSetup {
43+
"Consensus" should "ignore duplicated block" in new ImportBlockTestSetup {
4444
val block1: Block = getBlock()
4545
val block2: Block = getBlock()
4646

@@ -55,7 +55,7 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
5555
whenReady(consensus.evaluateBranchBlock(block2).runToFuture)(_ shouldEqual DuplicateBlock)
5656
}
5757

58-
it should "import a block to top of the main chain" in new ImportBlockTestSetup {
58+
it should "import a block to the top of the main chain" in new ImportBlockTestSetup {
5959
val block: Block = getBlock(6, parent = bestBlock.header.hash)
6060
val difficulty: BigInt = block.header.difficulty
6161
val hash: ByteString = block.header.hash
@@ -112,7 +112,39 @@ class ConsensusSpec extends AnyFlatSpec with Matchers with ScalaFutures {
112112

113113
(blockQueue.removeSubtree _).expects(*)
114114

115-
whenReady(consensus.evaluateBranchBlock(block).runToFuture)(_ shouldBe a[BlockImportFailed])
115+
whenReady(consensus.evaluateBranchBlock(block).runToFuture)(
116+
_ shouldBe BlockImportFailed("MPTError(io.iohk.ethereum.mpt.MerklePatriciaTrie$MPTException: Invalid Node)")
117+
)
118+
}
119+
120+
it should "handle no best block available error when importing to top" in new ImportBlockTestSetup {
121+
val block: Block = getBlock(6, parent = bestBlock.header.hash)
122+
123+
setBlockExists(block, inChain = false, inQueue = false)
124+
(blockchainReader.getBestBlock _).expects().returning(None)
125+
setChainWeightForBlock(bestBlock, currentWeight)
126+
127+
whenReady(consensus.evaluateBranchBlock(block).runToFuture)(
128+
_ shouldBe BlockImportFailed("Couldn't find the current best block")
129+
)
130+
}
131+
132+
it should "handle total difficulty error when importing to top" in new ImportBlockTestSetup {
133+
val block: Block = getBlock(6, parent = bestBlock.header.hash)
134+
135+
setBlockExists(block, inChain = false, inQueue = false)
136+
setBestBlock(bestBlock)
137+
(blockchain.getChainWeightByHash _).expects(*).returning(None)
138+
139+
(blockchainReader.getBlockHeaderByHash _).expects(*).returning(Some(block.header))
140+
141+
whenReady(consensus.evaluateBranchBlock(block).runToFuture) { result =>
142+
result shouldBe a[BlockImportFailed]
143+
result
144+
.asInstanceOf[BlockImportFailed]
145+
.error
146+
.should(startWith("Couldn't get total difficulty for current best block"))
147+
}
116148
}
117149

118150
// scalastyle:off magic.number

0 commit comments

Comments
 (0)