Skip to content

Commit b7f2b32

Browse files
committed
test Ommers validation error when node restarts(lost state)
1 parent b39cbb1 commit b7f2b32

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/it/scala/io/iohk/ethereum/ledger/BlockImporterItSpec.scala

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import cats.data.NonEmptyList
66
import io.iohk.ethereum.blockchain.sync.regular.BlockImporter.NewCheckpoint
77
import io.iohk.ethereum.blockchain.sync.regular.{BlockFetcher, BlockImporter}
88
import io.iohk.ethereum.checkpointing.CheckpointingTestHelpers
9+
import io.iohk.ethereum.consensus.{GetBlockHeaderByHash, GetNBlocksBack}
910
import io.iohk.ethereum.consensus.blocks.CheckpointBlockGenerator
11+
import io.iohk.ethereum.consensus.ethash.validators.{OmmersValidator, StdOmmersValidator}
12+
import io.iohk.ethereum.consensus.validators.Validators
1013
import io.iohk.ethereum.domain._
1114
import io.iohk.ethereum.mpt.MerklePatriciaTrie
1215
import io.iohk.ethereum.utils.Config.SyncConfig
1316
import io.iohk.ethereum.utils.Config
14-
import io.iohk.ethereum.{Fixtures, ObjectGenerators, crypto}
17+
import io.iohk.ethereum.{Fixtures, Mocks, ObjectGenerators, crypto}
1518
import io.iohk.ethereum.ledger.Ledger.BlockResult
1619
import monix.execution.Scheduler
1720
import org.scalamock.scalatest.MockFactory
@@ -62,6 +65,18 @@ class BlockImporterItSpec
6265
ethCompatibleStorage = true
6366
)
6467

68+
override protected lazy val successValidators: Validators = new Mocks.MockValidatorsAlwaysSucceed {
69+
override val ommersValidator: OmmersValidator = (
70+
parentHash: ByteString,
71+
blockNumber: BigInt,
72+
ommers: Seq[BlockHeader],
73+
getBlockHeaderByHash: GetBlockHeaderByHash,
74+
getNBlocksBack: GetNBlocksBack
75+
) =>
76+
new StdOmmersValidator(blockchainConfig, blockHeaderValidator)
77+
.validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
78+
}
79+
6580
override lazy val ledger = new TestLedgerImpl(successValidators) {
6681
override private[ledger] lazy val blockExecution =
6782
new BlockExecution(blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) {
@@ -153,14 +168,19 @@ class BlockImporterItSpec
153168
blockchain.getBestBlock().get shouldEqual newBlock3
154169
}
155170

156-
it should "return Unknown branch, don't start reorganisation in case of PickedBlocks with block that has a parent who is not present in the chain " in {
171+
it should "return Unknown branch, don't start reorganisation(therefore no block/ommer validation) in case of PickedBlocks with block that has a parent who is not present in the chain(or ommer not in chain)" in {
157172
val newcomerBlock4: Block =
158173
getBlock(genesisBlock.number + 4, difficulty = 104, parent = oldBlock3.header.hash)
159174
val newcomerWeight4Duplicate = oldWeight3.increase(newcomerBlock4.header)
160175

161176
//Block n5 with oldBlock4 as parent
162177
val newComerBlock5WithOldBlock4Parent: Block =
163-
getBlock(genesisBlock.number + 5, difficulty = 108, parent = oldBlock4.header.hash)
178+
getBlock(
179+
genesisBlock.number + 5,
180+
difficulty = 108,
181+
parent = oldBlock4.header.hash,
182+
ommers = Seq(oldBlock4.header)
183+
)
164184

165185
blockchain.save(oldBlock2, Nil, oldWeight2, saveAsBestBlock = true)
166186
blockchain.save(oldBlock3, Nil, oldWeight3, saveAsBestBlock = true)
@@ -169,7 +189,7 @@ class BlockImporterItSpec
169189
blockchain.saveBestKnownBlocks(blockchain.getBestBlockNumber() - 1)
170190
blockchain.save(newcomerBlock4, Nil, newcomerWeight4Duplicate, saveAsBestBlock = true)
171191

172-
//this is not reorganising anymore until oldBlock4(not part of the chain anymore) // but resolveBranch is returning UnknownBranch
192+
//not reorganising anymore until oldBlock4(not part of the chain anymore), no block/ommer validation when not part of the chain, resolveBranch is returning UnknownBranch
173193
blockImporter ! BlockFetcher.PickedBlocks(NonEmptyList.fromListUnsafe(List(newComerBlock5WithOldBlock4Parent)))
174194

175195
Thread.sleep(200)

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,14 @@ trait MockBlockchain extends MockFactory { self: TestSetupWithVmAndValidators =>
393393
.once()
394394
}
395395

396-
def setHeaderInChain(hash: ByteString, result: Boolean = true): CallHandler1[ByteString, Boolean] = {
396+
def setHeaderInChain(hash: ByteString, result: Boolean = true): CallHandler1[ByteString, Boolean] =
397397
(blockchain.isInChain _).expects(hash).returning(result)
398-
}
399398

400399
def setBlockByNumber(number: BigInt, block: Option[Block]): CallHandler1[BigInt, Option[Block]] =
401400
(blockchain.getBlockByNumber _).expects(number).returning(block)
402401

403-
def setGenesisHeader(header: BlockHeader): Unit = {
402+
def setGenesisHeader(header: BlockHeader): Unit =
404403
(() => blockchain.genesisHeader).expects().returning(header)
405-
}
406404
}
407405

408406
trait EphemBlockchain extends TestSetupWithVmAndValidators with MockFactory {

0 commit comments

Comments
 (0)