Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/it/scala/io/iohk/ethereum/sync/RegularSyncItSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.iohk.ethereum.sync
import com.typesafe.config.ConfigValueFactory
import io.iohk.ethereum.FreeSpecBase
import io.iohk.ethereum.metrics.{Metrics, MetricsConfig}
import io.iohk.ethereum.network.PeerId
import io.iohk.ethereum.sync.util.RegularSyncItSpecUtils.FakePeer
import io.iohk.ethereum.sync.util.SyncCommonItSpec._
import io.iohk.ethereum.utils.Config
Expand Down
23 changes: 22 additions & 1 deletion src/test/scala/io/iohk/ethereum/network/PeerManagerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.iohk.ethereum.network.PeerActor.{ConnectTo, PeerClosedConnection}
import io.iohk.ethereum.network.PeerEventBusActor.PeerEvent.PeerDisconnected
import io.iohk.ethereum.network.PeerEventBusActor.SubscriptionClassifier.PeerHandshaked
import io.iohk.ethereum.network.PeerEventBusActor.{PeerEvent, Publish, Subscribe}
import io.iohk.ethereum.network.PeerManagerActor.{GetPeers, PeerConfiguration, Peers, SendMessage}
import io.iohk.ethereum.network.PeerManagerActor.{GetPeers, PeerAddress, PeerConfiguration, Peers, SendMessage}
import io.iohk.ethereum.network.discovery.{DiscoveryConfig, Node, PeerDiscoveryManager}
import io.iohk.ethereum.network.p2p.messages.CommonMessages.NewBlock
import io.iohk.ethereum.network.p2p.messages.ProtocolVersions
Expand Down Expand Up @@ -52,6 +52,27 @@ class PeerManagerSpec
handleInitialNodesDiscovery()
}

it should "blacklist peer that sent a status msg with invalid genesisHash" in new TestSetup {
start()
handleInitialNodesDiscovery()

val probe: TestProbe = createdPeers(1).probe

probe.expectMsgClass(classOf[PeerActor.ConnectTo])

peerManager ! PeerManagerActor.HandlePeerConnection(incomingConnection1.ref, incomingPeerAddress1)

val probe2: TestProbe = createdPeers(2).probe
val peer = Peer(PeerId("peerid"), incomingPeerAddress1, probe2.ref, incomingConnection = true)

peerManager ! PeerClosedConnection(peer.remoteAddress.getHostString, Disconnect.Reasons.DisconnectRequested)

eventually {
peerManager.underlyingActor.blacklist.keys.size shouldEqual 1
peerManager.underlyingActor.blacklist.isBlacklisted(PeerAddress(peer.remoteAddress.getHostString)) shouldBe true
}
}

it should "blacklist peer that fail to establish tcp connection" in new TestSetup {
start()
handleInitialNodesDiscovery()
Expand Down
31 changes: 30 additions & 1 deletion src/test/scala/io/iohk/ethereum/network/p2p/PeerActorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import io.iohk.ethereum.network.p2p.messages.CommonMessages.Status.StatusEnc
import io.iohk.ethereum.network.p2p.messages.PV62.GetBlockHeaders.GetBlockHeadersEnc
import io.iohk.ethereum.network.p2p.messages.PV62._
import io.iohk.ethereum.network.p2p.messages.{PV64, ProtocolVersions}
import io.iohk.ethereum.network.p2p.messages.WireProtocol.Disconnect.Reasons
import io.iohk.ethereum.network.p2p.messages.WireProtocol.Disconnect.{DisconnectEnc, Reasons}
import io.iohk.ethereum.network.p2p.messages.WireProtocol.Hello.HelloEnc
import io.iohk.ethereum.network.p2p.messages.WireProtocol.Pong.PongEnc
import io.iohk.ethereum.network.p2p.messages.WireProtocol._
Expand Down Expand Up @@ -160,6 +160,35 @@ class PeerActorSpec
knownNodesManager.expectNoMessage()
}

it should "fail handshake with peer that has a wrong genesis hash" in new TestSetup {
val uri = new URI(s"enode://${Hex.toHexString(remoteNodeId.toArray[Byte])}@localhost:9000")
val completeUri = new URI(s"enode://${Hex.toHexString(remoteNodeId.toArray[Byte])}@127.0.0.1:9000?discport=9000")
peer ! PeerActor.ConnectTo(uri)
peer ! PeerActor.ConnectTo(uri)

rlpxConnection.expectMsgClass(classOf[RLPxConnectionHandler.ConnectTo])
rlpxConnection.reply(RLPxConnectionHandler.ConnectionEstablished(remoteNodeId))

//Hello exchange
val remoteHello = Hello(4, "test-client", Seq(Eth63Capability), 9000, ByteString("unused"))
rlpxConnection.expectMsgPF() { case RLPxConnectionHandler.SendMessage(_: HelloEnc) => () }
rlpxConnection.send(peer, RLPxConnectionHandler.MessageReceived(remoteHello))

val remoteStatus = Status(
protocolVersion = ProtocolVersions.PV63,
networkId = peerConf.networkId,
totalDifficulty = daoForkBlockChainTotalDifficulty + 100000, // remote is after the fork
bestHash = ByteString("blockhash"),
genesisHash = genesisHash.drop(2)
)

//Node status exchange
rlpxConnection.expectMsgPF() { case RLPxConnectionHandler.SendMessage(_: StatusEnc) => () }
rlpxConnection.send(peer, RLPxConnectionHandler.MessageReceived(remoteStatus))

rlpxConnection.expectMsgPF() { case RLPxConnectionHandler.SendMessage(_: DisconnectEnc) => () }
}

it should "successfully connect to ETC peer with protocol 64" in new TestSetup {
override def protocol: Version = ProtocolVersions.PV64
val uri = new URI(s"enode://${Hex.toHexString(remoteNodeId.toArray[Byte])}@localhost:9000")
Expand Down