@@ -5,7 +5,7 @@ import java.net.InetSocketAddress
5
5
import akka .actor .ActorSystem
6
6
import akka .testkit .{TestKit , TestProbe }
7
7
import com .miguno .akka .testing .VirtualTime
8
- import io .iohk .ethereum .Mocks .MockValidatorsAlwaysSucceed
8
+ import io .iohk .ethereum .Mocks .{ MockValidatorsAlwaysSucceed , MockValidatorsFailingOnBlockBodies }
9
9
import io .iohk .ethereum .BlockHelpers
10
10
import io .iohk .ethereum .Fixtures .{Blocks => FixtureBlocks }
11
11
import io .iohk .ethereum .blockchain .sync .PeersClient .BlacklistPeer
@@ -125,6 +125,32 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
125
125
peersClient.expectMsgClass(classOf [BlacklistPeer ])
126
126
peersClient.expectMsgPF() { case PeersClient .Request (msg, _, _) if msg == firstGetBlockHeadersRequest => () }
127
127
}
128
+
129
+ " should not append new blocks if the received data does not match" in new TestSetup {
130
+
131
+ // Important: Here we are forcing the mismatch between request headers and received bodies
132
+ override lazy val validators = new MockValidatorsFailingOnBlockBodies
133
+
134
+ startFetcher()
135
+
136
+ val getBlockHeadersRequest =
137
+ GetBlockHeaders (Left (1 ), syncConfig.blockHeadersPerRequest, skip = 0 , reverse = false )
138
+ peersClient.expectMsgPF() { case PeersClient .Request (msg, _, _) if msg == getBlockHeadersRequest => () }
139
+
140
+ val chain = BlockHelpers .generateChain(syncConfig.blockHeadersPerRequest, FixtureBlocks .Genesis .block)
141
+ val getBlockHeadersResponse = BlockHeaders (chain.map(_.header))
142
+ peersClient.reply(PeersClient .Response (fakePeer, getBlockHeadersResponse))
143
+
144
+ val getBlockBodiesRequest = GetBlockBodies (chain.map(_.hash))
145
+ peersClient.expectMsgPF() { case PeersClient .Request (msg, _, _) if msg == getBlockBodiesRequest => () }
146
+
147
+ // This response will be invalid given we are using a special validator!
148
+ val getBlockBodiesResponse = BlockBodies (chain.map(_.body))
149
+ peersClient.reply(PeersClient .Response (fakePeer, getBlockBodiesResponse))
150
+
151
+ peersClient.expectMsgClass(classOf [BlacklistPeer ])
152
+ peersClient.expectMsgPF() { case PeersClient .Request (msg, _, _) if msg == getBlockBodiesRequest => () }
153
+ }
128
154
}
129
155
130
156
trait TestSetup extends TestSyncConfig {
@@ -135,7 +161,7 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
135
161
val importer : TestProbe = TestProbe ()
136
162
val regularSync : TestProbe = TestProbe ()
137
163
138
- val validators = new MockValidatorsAlwaysSucceed
164
+ lazy val validators = new MockValidatorsAlwaysSucceed
139
165
140
166
override lazy val syncConfig = defaultSyncConfig.copy(
141
167
// Same request size was selected for simplification purposes of the flow
@@ -148,7 +174,7 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
148
174
val fakePeerActor : TestProbe = TestProbe ()
149
175
val fakePeer = Peer (new InetSocketAddress (" 127.0.0.1" , 9000 ), fakePeerActor.ref, false )
150
176
151
- val blockFetcher = system.actorOf(
177
+ lazy val blockFetcher = system.actorOf(
152
178
BlockFetcher
153
179
.props(
154
180
peersClient.ref,
0 commit comments