@@ -6,16 +6,20 @@ import cats.data.NonEmptyList
66import io .iohk .ethereum .blockchain .sync .regular .BlockImporter .NewCheckpoint
77import io .iohk .ethereum .blockchain .sync .regular .{BlockFetcher , BlockImporter }
88import io .iohk .ethereum .checkpointing .CheckpointingTestHelpers
9+ import io .iohk .ethereum .consensus .{GetBlockHeaderByHash , GetNBlocksBack }
910import io .iohk .ethereum .consensus .blocks .CheckpointBlockGenerator
11+ import io .iohk .ethereum .consensus .ethash .validators .{OmmersValidator , StdOmmersValidator }
12+ import io .iohk .ethereum .consensus .validators .Validators
1013import io .iohk .ethereum .domain ._
1114import io .iohk .ethereum .mpt .MerklePatriciaTrie
1215import io .iohk .ethereum .utils .Config .SyncConfig
1316import io .iohk .ethereum .utils .Config
14- import io .iohk .ethereum .{Fixtures , ObjectGenerators , crypto }
17+ import io .iohk .ethereum .{Fixtures , Mocks , ObjectGenerators , crypto }
1518import io .iohk .ethereum .ledger .Ledger .BlockResult
1619import monix .execution .Scheduler
1720import org .scalamock .scalatest .MockFactory
1821import org .scalatest .BeforeAndAfterAll
22+ import org .scalatest .concurrent .Eventually .eventually
1923import org .scalatest .flatspec .AsyncFlatSpecLike
2024import org .scalatest .matchers .should .Matchers
2125
@@ -62,6 +66,18 @@ class BlockImporterItSpec
6266 ethCompatibleStorage = true
6367 )
6468
69+ override protected lazy val successValidators : Validators = new Mocks .MockValidatorsAlwaysSucceed {
70+ override val ommersValidator : OmmersValidator = (
71+ parentHash : ByteString ,
72+ blockNumber : BigInt ,
73+ ommers : Seq [BlockHeader ],
74+ getBlockHeaderByHash : GetBlockHeaderByHash ,
75+ getNBlocksBack : GetNBlocksBack
76+ ) =>
77+ new StdOmmersValidator (blockchainConfig, blockHeaderValidator)
78+ .validate(parentHash, blockNumber, ommers, getBlockHeaderByHash, getNBlocksBack)
79+ }
80+
6581 override lazy val ledger = new TestLedgerImpl (successValidators) {
6682 override private [ledger] lazy val blockExecution =
6783 new BlockExecution (blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) {
@@ -135,9 +151,8 @@ class BlockImporterItSpec
135151 blockImporter ! BlockImporter .Start
136152 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
137153
138- Thread .sleep(1000 )
139154 // because the blocks are not valid, we shouldn't reorganise, but at least stay with a current chain, and the best block of the current chain is oldBlock4
140- blockchain.getBestBlock().get shouldEqual oldBlock4
155+ eventually { blockchain.getBestBlock().get shouldEqual oldBlock4 }
141156 }
142157
143158 it should " return a correct new best block after reorganising longer chain to a shorter one if its weight is bigger" in {
@@ -149,8 +164,34 @@ class BlockImporterItSpec
149164
150165 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
151166
152- Thread .sleep(200 )
153- blockchain.getBestBlock().get shouldEqual newBlock3
167+ eventually { Thread .sleep(200 ); blockchain.getBestBlock().get shouldEqual newBlock3 }
168+ }
169+
170+ it should " return Unknown branch, in case of PickedBlocks with block that has a parent that's not in the chain" in {
171+ val newBlock4ParentOldBlock3 : Block =
172+ getBlock(genesisBlock.number + 4 , difficulty = 104 , parent = oldBlock3.header.hash)
173+ val newBlock4WeightParentOldBlock3 = oldWeight3.increase(newBlock4ParentOldBlock3.header)
174+
175+ // Block n5 with oldBlock4 as parent
176+ val newBlock5ParentOldBlock4 : Block =
177+ getBlock(
178+ genesisBlock.number + 5 ,
179+ difficulty = 108 ,
180+ parent = oldBlock4.header.hash,
181+ ommers = Seq (oldBlock4.header)
182+ )
183+
184+ blockchain.save(oldBlock2, Nil , oldWeight2, saveAsBestBlock = true )
185+ blockchain.save(oldBlock3, Nil , oldWeight3, saveAsBestBlock = true )
186+ blockchain.save(oldBlock4, Nil , oldWeight4, saveAsBestBlock = true )
187+ // simulation of node restart
188+ blockchain.saveBestKnownBlocks(blockchain.getBestBlockNumber() - 1 )
189+ blockchain.save(newBlock4ParentOldBlock3, Nil , newBlock4WeightParentOldBlock3, saveAsBestBlock = true )
190+
191+ // 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
192+ blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(List (newBlock5ParentOldBlock4)))
193+
194+ eventually { blockchain.getBestBlock().get shouldEqual newBlock4ParentOldBlock3 }
154195 }
155196
156197 it should " switch to a branch with a checkpoint" in {
@@ -163,9 +204,8 @@ class BlockImporterItSpec
163204
164205 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
165206
166- Thread .sleep(200 )
167- blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint
168- blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number
207+ eventually { blockchain.getBestBlock().get shouldEqual oldBlock5WithCheckpoint }
208+ eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual oldBlock5WithCheckpoint.header.number }
169209 }
170210
171211 it should " switch to a branch with a newer checkpoint" in {
@@ -178,9 +218,8 @@ class BlockImporterItSpec
178218
179219 blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
180220
181- Thread .sleep(200 )
182- blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint
183- blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number
221+ eventually { blockchain.getBestBlock().get shouldEqual newBlock4WithCheckpoint }
222+ eventually { blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock4WithCheckpoint.header.number }
184223 }
185224
186225 it should " return a correct checkpointed block after receiving a request for generating a new checkpoint" in {
@@ -199,8 +238,9 @@ class BlockImporterItSpec
199238
200239 val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint (signatures))
201240
202- Thread .sleep(1000 )
203- blockchain.getBestBlock().get shouldEqual checkpointBlock
204- blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
241+ eventually { Thread .sleep(1000 ); blockchain.getBestBlock().get shouldEqual checkpointBlock }
242+ eventually {
243+ Thread .sleep(1000 ); blockchain.getLatestCheckpointBlockNumber() shouldEqual newBlock5.header.number + 1
244+ }
205245 }
206246}
0 commit comments