Skip to content

Commit 85cbcf5

Browse files
committed
[ETCM-283] Add unit test for the failure scenario
1 parent 6a71cb3 commit 85cbcf5

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/BlockFetcherSpec.scala

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.net.InetSocketAddress
55
import akka.actor.ActorSystem
66
import akka.testkit.{TestKit, TestProbe}
77
import com.miguno.akka.testing.VirtualTime
8-
import io.iohk.ethereum.Mocks.MockValidatorsAlwaysSucceed
8+
import io.iohk.ethereum.Mocks.{MockValidatorsAlwaysSucceed, MockValidatorsFailingOnBlockBodies}
99
import io.iohk.ethereum.BlockHelpers
1010
import io.iohk.ethereum.Fixtures.{Blocks => FixtureBlocks}
1111
import io.iohk.ethereum.blockchain.sync.PeersClient.BlacklistPeer
@@ -125,6 +125,32 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
125125
peersClient.expectMsgClass(classOf[BlacklistPeer])
126126
peersClient.expectMsgPF() { case PeersClient.Request(msg, _, _) if msg == firstGetBlockHeadersRequest => () }
127127
}
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+
}
128154
}
129155

130156
trait TestSetup extends TestSyncConfig {
@@ -135,7 +161,7 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
135161
val importer: TestProbe = TestProbe()
136162
val regularSync: TestProbe = TestProbe()
137163

138-
val validators = new MockValidatorsAlwaysSucceed
164+
lazy val validators = new MockValidatorsAlwaysSucceed
139165

140166
override lazy val syncConfig = defaultSyncConfig.copy(
141167
// Same request size was selected for simplification purposes of the flow
@@ -148,7 +174,7 @@ class BlockFetcherSpec extends TestKit(ActorSystem("BlockFetcherSpec_System")) w
148174
val fakePeerActor: TestProbe = TestProbe()
149175
val fakePeer = Peer(new InetSocketAddress("127.0.0.1", 9000), fakePeerActor.ref, false)
150176

151-
val blockFetcher = system.actorOf(
177+
lazy val blockFetcher = system.actorOf(
152178
BlockFetcher
153179
.props(
154180
peersClient.ref,

0 commit comments

Comments
 (0)