|
1 | 1 | package io.iohk.ethereum.blockchain.sync.regular
|
2 | 2 |
|
3 |
| -import akka.actor.{ActorRef, ActorSystem} |
| 3 | +import akka.actor.{ActorRef, ActorSystem, typed} |
| 4 | +import akka.actor.typed.{ActorRef => TypedActorRef} |
4 | 5 | import akka.testkit.TestActor.AutoPilot
|
5 |
| -import akka.testkit.TestKit |
| 6 | +import akka.testkit.{TestKit, TestProbe} |
6 | 7 | import akka.util.ByteString
|
7 | 8 | import cats.data.NonEmptyList
|
8 | 9 | import cats.effect.Resource
|
9 | 10 | import cats.syntax.traverse._
|
10 | 11 | import io.iohk.ethereum.blockchain.sync.Blacklist.BlacklistReason
|
11 | 12 | import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status
|
12 | 13 | import io.iohk.ethereum.blockchain.sync.SyncProtocol.Status.Progress
|
| 14 | +import io.iohk.ethereum.blockchain.sync.regular.BlockFetcher.Start |
13 | 15 | import io.iohk.ethereum.blockchain.sync.regular.RegularSync.NewCheckpoint
|
14 | 16 | import io.iohk.ethereum.blockchain.sync.{PeersClient, SyncProtocol}
|
15 | 17 | import io.iohk.ethereum.crypto.kec256
|
@@ -37,6 +39,7 @@ import org.scalatest.{Assertion, BeforeAndAfterEach}
|
37 | 39 |
|
38 | 40 | import scala.concurrent.duration._
|
39 | 41 | import scala.concurrent.{Await, Future, Promise}
|
| 42 | +import scala.math.BigInt |
40 | 43 |
|
41 | 44 | class RegularSyncSpec
|
42 | 45 | extends WordSpecBase
|
@@ -112,29 +115,101 @@ class RegularSyncSpec
|
112 | 115 | peersClient.expectMsg(PeersClient.BlacklistPeer(defaultPeer.id, BlacklistReason.RegularSyncRequestFailed("a random reason")))
|
113 | 116 | })
|
114 | 117 |
|
115 |
| - //TODO: To be re-enabled with ETCM-370 |
116 |
| - "blacklist peer which returns headers starting from one with higher number than expected" ignore sync( |
117 |
| - new Fixture( |
118 |
| - testSystem |
119 |
| - ) { |
| 118 | + "blacklist peer which returns headers starting from one with higher number than expected" in sync( |
| 119 | + new Fixture(testSystem) { |
| 120 | + var blockFetcher: ActorRef = _ |
| 121 | + |
120 | 122 | regularSync ! SyncProtocol.Start
|
| 123 | + peerEventBus.expectMsgClass(classOf[Subscribe]) |
| 124 | + blockFetcher = peerEventBus.sender() |
121 | 125 |
|
122 | 126 | peersClient.expectMsgEq(blockHeadersChunkRequest(0))
|
123 |
| - peersClient.reply(PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked(1).headers))) |
| 127 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked.head.headers))) |
| 128 | + |
| 129 | + val getBodies: PeersClient.Request[GetBlockBodies] = PeersClient.Request.create( |
| 130 | + GetBlockBodies(testBlocksChunked.head.headers.map(_.hash)), |
| 131 | + PeersClient.BestPeer |
| 132 | + ) |
| 133 | + peersClient.expectMsgEq(getBodies) |
| 134 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockBodies(testBlocksChunked.head.bodies))) |
| 135 | + |
| 136 | + blockFetcher ! MessageFromPeer(NewBlock(testBlocks.last, ChainWeight.totalDifficultyOnly(testBlocks.last.number)), defaultPeer.id) |
| 137 | + peersClient.expectMsgEq(blockHeadersChunkRequest(1)) |
| 138 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked(5).headers))) |
124 | 139 | peersClient.expectMsgPF() {
|
125 | 140 | case PeersClient.BlacklistPeer(id, _) if id == defaultPeer.id => true
|
126 | 141 | }
|
127 | 142 | }
|
128 | 143 | )
|
129 | 144 |
|
130 |
| - //TODO: To be re-enabled with ETCM-370 |
131 |
| - "blacklist peer which returns headers not forming a chain" ignore sync(new Fixture(testSystem) { |
| 145 | + "blacklist peer which returns headers not forming a chain" in sync(new Fixture(testSystem) { |
132 | 146 | regularSync ! SyncProtocol.Start
|
133 | 147 |
|
134 | 148 | peersClient.expectMsgEq(blockHeadersChunkRequest(0))
|
135 | 149 | peersClient.reply(
|
136 |
| - PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked.head.headers.filter(_.number % 2 == 0))) |
| 150 | + PeersClient.Response(defaultPeer, BlockHeaders(testBlocks.headers.filter(_.number % 2 == 0))) |
| 151 | + ) |
| 152 | + peersClient.expectMsgPF() { |
| 153 | + case PeersClient.BlacklistPeer(id, _) if id == defaultPeer.id => true |
| 154 | + } |
| 155 | + }) |
| 156 | + |
| 157 | + "blacklist peer which sends headers that were not requested" in sync(new Fixture(testSystem) { |
| 158 | + import akka.actor.typed.scaladsl.adapter._ |
| 159 | + |
| 160 | + val blockImporter = TestProbe() |
| 161 | + val fetcher: typed.ActorRef[BlockFetcher.FetchCommand] = |
| 162 | + system.spawn( |
| 163 | + BlockFetcher(peersClient.ref, peerEventBus.ref, regularSync, syncConfig, validators.blockValidator), |
| 164 | + "block-fetcher" |
| 165 | + ) |
| 166 | + |
| 167 | + fetcher ! Start(blockImporter.ref, 0) |
| 168 | + |
| 169 | + peersClient.expectMsgEq(blockHeadersChunkRequest(0)) |
| 170 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked.head.headers))) |
| 171 | + |
| 172 | + val getBodies: PeersClient.Request[GetBlockBodies] = PeersClient.Request.create( |
| 173 | + GetBlockBodies(testBlocksChunked.head.headers.map(_.hash)), |
| 174 | + PeersClient.BestPeer |
137 | 175 | )
|
| 176 | + |
| 177 | + peersClient.expectMsgEq(getBodies) |
| 178 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockBodies(testBlocksChunked.head.bodies))) |
| 179 | + |
| 180 | + fetcher ! BlockFetcher.ReceivedHeaders(defaultPeer, testBlocksChunked(3).headers) |
| 181 | + |
| 182 | + peersClient.expectMsgPF() { |
| 183 | + case PeersClient.BlacklistPeer(id, _) if id == defaultPeer.id => true |
| 184 | + } |
| 185 | + }) |
| 186 | + |
| 187 | + "blacklist peer which sends bodies that were not requested" in sync(new Fixture(testSystem) { |
| 188 | + import akka.actor.typed.scaladsl.adapter._ |
| 189 | + |
| 190 | + var blockFetcherAdapter: TypedActorRef[MessageFromPeer] = _ |
| 191 | + val blockImporter = TestProbe() |
| 192 | + val fetcher: typed.ActorRef[BlockFetcher.FetchCommand] = |
| 193 | + system.spawn( |
| 194 | + BlockFetcher(peersClient.ref, peerEventBus.ref, regularSync, syncConfig, validators.blockValidator), |
| 195 | + "block-fetcher" |
| 196 | + ) |
| 197 | + |
| 198 | + fetcher ! Start(blockImporter.ref, 0) |
| 199 | + |
| 200 | + peersClient.expectMsgEq(blockHeadersChunkRequest(0)) |
| 201 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockHeaders(testBlocksChunked.head.headers))) |
| 202 | + |
| 203 | + val getBodies: PeersClient.Request[GetBlockBodies] = PeersClient.Request.create( |
| 204 | + GetBlockBodies(testBlocksChunked.head.headers.map(_.hash)), |
| 205 | + PeersClient.BestPeer |
| 206 | + ) |
| 207 | + |
| 208 | + peersClient.expectMsgEq(getBodies) |
| 209 | + peersClient.reply(PeersClient.Response(defaultPeer, BlockBodies(testBlocksChunked.head.bodies))) |
| 210 | + |
| 211 | + fetcher ! BlockFetcher.ReceivedBodies(defaultPeer, testBlocksChunked(3).bodies) |
| 212 | + |
138 | 213 | peersClient.expectMsgPF() {
|
139 | 214 | case PeersClient.BlacklistPeer(id, _) if id == defaultPeer.id => true
|
140 | 215 | }
|
|
0 commit comments