Skip to content

Commit 2a5bc6c

Browse files
author
Leonor Boga
committed
ETCM-1058 Fix unit tests
1 parent 85477be commit 2a5bc6c

File tree

5 files changed

+95
-64
lines changed

5 files changed

+95
-64
lines changed

src/test/scala/io/iohk/ethereum/blockchain/sync/ScenarioSetup.scala

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,32 @@ trait ScenarioSetup extends StdTestMiningBuilder with StxLedgerBuilder {
6969
protected def newTestMining(validators: Validators = mining.validators, vm: VMImpl = mining.vm): Mining =
7070
mining.withValidators(validators).withVM(vm)
7171

72+
protected def mkBlockExecution(validators: Validators = validators) = {
73+
val consensuz = mining.withValidators(validators).withVM(new Mocks.MockVM())
74+
val blockValidation = new BlockValidation(consensuz, blockchainReader, blockQueue)
75+
new BlockExecution(
76+
blockchain,
77+
blockchainReader,
78+
blockchainWriter,
79+
storagesInstance.storages.evmCodeStorage,
80+
consensuz.blockPreparator,
81+
blockValidation
82+
)
83+
}
84+
7285
protected def mkConsensus(
7386
validators: Validators = validators,
7487
blockExecutionOpt: Option[BlockExecution] = None
7588
): Consensus = {
76-
val consensuz = mining.withValidators(validators).withVM(new Mocks.MockVM())
77-
val blockValidation = new BlockValidation(consensuz, blockchainReader, blockQueue)
89+
val testMining = mining.withValidators(validators).withVM(new Mocks.MockVM())
90+
val blockValidation = new BlockValidation(testMining, blockchainReader, blockQueue)
7891
new ConsensusImpl(
7992
blockchain,
8093
blockchainReader,
8194
blockchainWriter,
8295
blockQueue,
8396
blockValidation,
84-
blockExecutionOpt.getOrElse(
85-
new BlockExecution(
86-
blockchain,
87-
blockchainReader,
88-
blockchainWriter,
89-
storagesInstance.storages.evmCodeStorage,
90-
consensuz.blockPreparator,
91-
blockValidation
92-
)
93-
),
97+
blockExecutionOpt.getOrElse(mkBlockExecution(validators)),
9498
Scheduler(system.dispatchers.lookup("validation-context"))
9599
)
96100
}

src/test/scala/io/iohk/ethereum/consensus/blocks/BlockGeneratorSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
109109
)
110110

111111
// Import Block, to create some existing state
112-
blockImport.importBlock(fullBlock).runSyncUnsafe()
112+
consensus.evaluateBranchBlock(fullBlock).runSyncUnsafe()
113113

114114
// Create new pending block, with updated stateRootHash
115115
val pendBlockAndState = blockGenerator.generateBlock(
@@ -690,9 +690,9 @@ class BlockGeneratorSpec extends AnyFlatSpec with Matchers with ScalaCheckProper
690690
lazy val blockGenerator: TestBlockGenerator =
691691
mining.blockGenerator.withBlockTimestampProvider(blockTimestampProvider)
692692

693-
lazy val blockValidation =
693+
override lazy val blockValidation =
694694
new BlockValidation(mining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
695-
lazy val blockExecution =
695+
override lazy val blockExecution =
696696
new BlockExecution(
697697
blockchain,
698698
blockchainReader,

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
6161

6262
val mockValidators = new MockValidatorsFailOnSpecificBlockNumber(block1.header.number)
6363
val newMining: TestMining = mining.withVM(vm).withValidators(mockValidators)
64-
val blockValidation =
64+
override lazy val blockValidation =
6565
new BlockValidation(newMining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
66-
val blockExecution =
66+
override lazy val blockExecution =
6767
new BlockExecution(
6868
blockchain,
6969
blockchainReader,
@@ -103,9 +103,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
103103
)
104104
val mockValidators = new MockValidatorsFailOnSpecificBlockNumber(block2.header.number)
105105
val newMining: TestMining = mining.withVM(mockVm).withValidators(mockValidators)
106-
val blockValidation =
106+
override lazy val blockValidation =
107107
new BlockValidation(newMining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
108-
val blockExecution =
108+
override lazy val blockExecution =
109109
new BlockExecution(
110110
blockchain,
111111
blockchainReader,
@@ -138,9 +138,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
138138
)
139139
val mockValidators = new MockValidatorsFailOnSpecificBlockNumber(chain.last.number)
140140
val newMining: TestMining = mining.withVM(mockVm).withValidators(mockValidators)
141-
val blockValidation =
141+
override lazy val blockValidation =
142142
new BlockValidation(newMining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
143-
val blockExecution =
143+
override lazy val blockExecution =
144144
new BlockExecution(
145145
blockchain,
146146
blockchainReader,
@@ -169,9 +169,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
169169

170170
val mockValidators = MockValidatorsAlwaysSucceed
171171
val newMining: TestMining = mining.withVM(vm).withValidators(mockValidators)
172-
val blockValidation =
172+
override lazy val blockValidation =
173173
new BlockValidation(newMining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
174-
val blockExecution =
174+
override lazy val blockExecution =
175175
new BlockExecution(
176176
blockchain,
177177
blockchainReader,
@@ -212,7 +212,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
212212

213213
txsExecResult.isRight shouldBe true
214214

215-
val BlockResult(resultingWorldState, resultingGasUsed, resultingReceipts) = txsExecResult.toOption.get
215+
val BlockResult(_, resultingGasUsed, resultingReceipts) = txsExecResult.toOption.get
216216
resultingGasUsed shouldBe 0
217217
resultingReceipts shouldBe Nil
218218
}
@@ -236,9 +236,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
236236

237237
val newMining: TestMining = mining.withVM(mockVm)
238238

239-
val blockValidation =
239+
override lazy val blockValidation =
240240
new BlockValidation(newMining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
241-
val blockExecution =
241+
override lazy val blockExecution =
242242
new BlockExecution(
243243
blockchain,
244244
blockchainReader,
@@ -434,7 +434,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
434434
)
435435
val block = Block(blockHeader, blockBodyWithOmmers)
436436

437-
val blockExecResult = blockImport.blockExecution.executeAndValidateBlock(block)
437+
val blockExecResult = blockExecution.executeAndValidateBlock(block)
438438
assert(blockExecResult.isRight)
439439
}
440440
}
@@ -481,7 +481,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
481481
val block = Block(blockHeader, validBlockBodyWithNoTxs)
482482

483483
assert(seqFailingValidators.forall { _ =>
484-
val blockExecResult = blockImport.blockExecution.executeAndValidateBlock(block)
484+
val blockExecResult = blockExecution.executeAndValidateBlock(block)
485485

486486
blockExecResult.left.forall {
487487
case _: BlockExecutionError.ValidationBeforeExecError => true
@@ -530,12 +530,12 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
530530
)
531531

532532
forAll(table) { (stateRootHash, cumulativeGasUsedBlock, validators) =>
533-
val blockImport = mkBlockImport(validators = validators)
533+
val blockExecution = mkBlockExecution(validators = validators)
534534
val blockHeader: BlockHeader =
535535
validBlockHeader.copy(gasUsed = cumulativeGasUsedBlock, stateRoot = stateRootHash)
536536
val block = Block(blockHeader, validBlockBodyWithNoTxs)
537537

538-
val blockExecResult = blockImport.blockExecution.executeAndValidateBlock(block)
538+
val blockExecResult = blockExecution.executeAndValidateBlock(block)
539539

540540
assert(blockExecResult match {
541541
case Left(_: BlockExecutionError.ValidationAfterExecError) => true
@@ -585,7 +585,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
585585
val validBlockBodyWithTxs: BlockBody = validBlockBodyWithNoTxs.copy(transactionList = Seq(stx1.tx, stx2.tx))
586586
val block = Block(validBlockHeader, validBlockBodyWithTxs)
587587

588-
val txsExecResult = blockImport.blockExecution.executeBlockTransactions(block, initialWorld)
588+
val txsExecResult = blockExecution.executeBlockTransactions(block, initialWorld)
589589

590590
assert(txsExecResult.isRight)
591591
val BlockResult(resultingWorldState, resultingGasUsed, resultingReceipts) = txsExecResult.toOption.get
@@ -641,7 +641,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
641641
val blockWithCorrectStateAndGasUsed = block.copy(
642642
header = block.header.copy(stateRoot = blockExpectedStateRoot, gasUsed = gasUsedReceipt2)
643643
)
644-
assert(blockImport.blockExecution.executeAndValidateBlock(blockWithCorrectStateAndGasUsed).isRight)
644+
assert(blockExecution.executeAndValidateBlock(blockWithCorrectStateAndGasUsed).isRight)
645645
}
646646
}
647647

@@ -662,7 +662,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
662662
}
663663

664664
// We don't care about block txs in this test
665-
blockImport.blockExecution.executeBlockTransactions(
665+
blockExecution.executeBlockTransactions(
666666
proDaoBlock.copy(body = proDaoBlock.body.copy(transactionList = Seq.empty)),
667667
initialWorld
668668
)
@@ -675,7 +675,7 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
675675
}
676676

677677
// We don't care about block txs in this test
678-
blockImport.blockExecution.executeBlockTransactions(
678+
blockExecution.executeBlockTransactions(
679679
proDaoBlock.copy(body = proDaoBlock.body.copy(transactionList = Seq.empty)),
680680
initialWorld
681681
)
@@ -684,9 +684,9 @@ class BlockExecutionSpec extends AnyWordSpec with Matchers with ScalaCheckProper
684684

685685
trait BlockExecutionTestSetup extends BlockchainSetup {
686686

687-
val blockValidation =
687+
override lazy val blockValidation =
688688
new BlockValidation(mining, blockchainReader, BlockQueue(blockchain, blockchainReader, syncConfig))
689-
val blockExecution =
689+
override lazy val blockExecution =
690690
new BlockExecution(
691691
blockchain,
692692
blockchainReader,

0 commit comments

Comments
 (0)