@@ -6,16 +6,20 @@ import cats.data.NonEmptyList
6
6
import io .iohk .ethereum .blockchain .sync .regular .BlockImporter .NewCheckpoint
7
7
import io .iohk .ethereum .blockchain .sync .regular .{BlockFetcher , BlockImporter }
8
8
import io .iohk .ethereum .checkpointing .CheckpointingTestHelpers
9
+ import io .iohk .ethereum .consensus .{GetBlockHeaderByHash , GetNBlocksBack }
9
10
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
10
13
import io .iohk .ethereum .domain ._
11
14
import io .iohk .ethereum .mpt .MerklePatriciaTrie
12
15
import io .iohk .ethereum .utils .Config .SyncConfig
13
16
import io .iohk .ethereum .utils .Config
14
- import io .iohk .ethereum .{Fixtures , ObjectGenerators , crypto }
17
+ import io .iohk .ethereum .{Fixtures , Mocks , ObjectGenerators , crypto }
15
18
import io .iohk .ethereum .ledger .Ledger .BlockResult
16
19
import monix .execution .Scheduler
17
20
import org .scalamock .scalatest .MockFactory
18
21
import org .scalatest .BeforeAndAfterAll
22
+ import org .scalatest .concurrent .Eventually .eventually
19
23
import org .scalatest .flatspec .AsyncFlatSpecLike
20
24
import org .scalatest .matchers .should .Matchers
21
25
@@ -62,6 +66,18 @@ class BlockImporterItSpec
62
66
ethCompatibleStorage = true
63
67
)
64
68
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
+
65
81
override lazy val ledger = new TestLedgerImpl (successValidators) {
66
82
override private [ledger] lazy val blockExecution =
67
83
new BlockExecution (blockchain, blockchainConfig, consensus.blockPreparator, blockValidation) {
@@ -135,9 +151,8 @@ class BlockImporterItSpec
135
151
blockImporter ! BlockImporter .Start
136
152
blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
137
153
138
- Thread .sleep(1000 )
139
154
// 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 }
141
156
}
142
157
143
158
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
149
164
150
165
blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
151
166
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 }
154
195
}
155
196
156
197
it should " switch to a branch with a checkpoint" in {
@@ -163,9 +204,8 @@ class BlockImporterItSpec
163
204
164
205
blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
165
206
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 }
169
209
}
170
210
171
211
it should " switch to a branch with a newer checkpoint" in {
@@ -178,9 +218,8 @@ class BlockImporterItSpec
178
218
179
219
blockImporter ! BlockFetcher .PickedBlocks (NonEmptyList .fromListUnsafe(newBranch))
180
220
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 }
184
223
}
185
224
186
225
it should " return a correct checkpointed block after receiving a request for generating a new checkpoint" in {
@@ -199,8 +238,9 @@ class BlockImporterItSpec
199
238
200
239
val checkpointBlock = checkpointBlockGenerator.generate(newBlock5, Checkpoint (signatures))
201
240
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
+ }
205
245
}
206
246
}
0 commit comments