Skip to content

Commit

Permalink
Merge pull request #2021 from ergoplatform/i2005
Browse files Browse the repository at this point in the history
Drop connections with nodes < 4.0.100
  • Loading branch information
kushti authored Aug 15, 2023
2 parents dd3e911 + 09170cf commit 883d41f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/scorex/core/app/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Version {

val UtxoSnapsnotActivationVersion: Version = Version(5, 0, 12)

val NipopowActivationVersion: Version = Version(5, 0, 13) // todo: set proper version around release
val NipopowActivationVersion: Version = Version(5, 0, 13)
}

object ApplicationVersionSerializer extends ErgoSerializer[Version] {
Expand Down
33 changes: 20 additions & 13 deletions src/main/scala/scorex/core/network/PeerConnectionHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import akka.actor.{Actor, ActorRef, Cancellable, Props, SupervisorStrategy}
import akka.io.Tcp
import akka.io.Tcp._
import akka.util.{ByteString, CompactByteString}
import scorex.core.app.Version.Eip37ForkVersion
import scorex.core.app.{ScorexContext, Version}
import scorex.core.network.NetworkController.ReceivableMessages.{Handshaked, PenalizePeer}
import scorex.core.network.PeerConnectionHandler.ReceivableMessages
Expand Down Expand Up @@ -61,20 +62,16 @@ class PeerConnectionHandler(scorexSettings: ScorexSettings,
override def postStop(): Unit = log.info(s"Peer handler to $connectionId destroyed")

private def handshaking: Receive = {
handshakeTimeoutCancellableOpt = Some(context.system.scheduler.scheduleOnce(networkSettings.handshakeTimeout)
(self ! HandshakeTimeout))
handshakeTimeoutCancellableOpt =
Some(context.system.scheduler.scheduleOnce(networkSettings.handshakeTimeout)(self ! HandshakeTimeout))
val hb = HandshakeSerializer.toBytes(createHandshakeMessage())
connection ! Tcp.Write(ByteString(hb))
log.info(s"Handshake sent to $connectionId")

receiveAndHandleHandshake { receivedHandshake =>
log.info(s"Got a Handshake from $connectionId")

val peerInfo = PeerInfo(
receivedHandshake.peerSpec,
System.currentTimeMillis(),
Some(direction)
)
val peerInfo = PeerInfo(receivedHandshake.peerSpec, System.currentTimeMillis(), Some(direction))
val peer = ConnectedPeer(connectionDescription.connectionId, self, 0, Some(peerInfo))
selfPeer = Some(peer)

Expand All @@ -85,17 +82,27 @@ class PeerConnectionHandler(scorexSettings: ScorexSettings,
} orElse handshakeTimeout orElse closeCommands
}

//ban this peer for the wrong handshake message
//peer will be added to the blacklist and the network controller will send CloseConnection
private def banPeer(): Unit = {
selfPeer.foreach(c => networkControllerRef ! PenalizePeer(c.connectionId.remoteAddress, PenaltyType.PermanentPenalty))
}

private def receiveAndHandleHandshake(handler: Handshake => Unit): Receive = {
case Received(data) =>
HandshakeSerializer.parseBytesTry(data.toArray) match {
case Success(handshake) =>
handler(handshake)
if (handshake.peerSpec.protocolVersion < Eip37ForkVersion) {
// peers not suporting EIP-37 hard-fork are stuck on another chain
log.info(s"Peer of version < 4.0.100 sent handshake $handshake")
banPeer()
} else {
handler(handshake)
}

case Failure(t) =>
log.info(s"Error during parsing a handshake: ${t.getMessage}")
//ban the peer for the wrong handshake message
//peer will be added to the blacklist and the network controller will send CloseConnection
selfPeer.foreach(c => networkControllerRef ! PenalizePeer(c.connectionId.remoteAddress, PenaltyType.PermanentPenalty))
log.info(s"Error during parsing a handshake: ${t.getMessage}", t)
banPeer()
}
}

Expand Down Expand Up @@ -234,7 +241,7 @@ class PeerConnectionHandler(scorexSettings: ScorexSettings,
}
}

private def createHandshakeMessage() = {
private def createHandshakeMessage(): Handshake = {
Handshake(
PeerSpec(
networkSettings.agentName,
Expand Down

0 comments on commit 883d41f

Please sign in to comment.