Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
fixing PeerConnectionHandler (#396)
Browse files Browse the repository at this point in the history
* fixing PeerConnectionHandler

Signed-off-by: pragmaxim <pragmaxim@gmail.com>

* adding handy SyncTracker.peersByStatus method

Signed-off-by: pragmaxim <pragmaxim@gmail.com>
  • Loading branch information
pragmaxim committed May 21, 2021
1 parent 30f3bea commit a6fe141
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/main/scala/scorex/core/network/SyncTracker.scala
Expand Up @@ -92,6 +92,10 @@ class SyncTracker(nvsRef: ActorRef,
private def outdatedPeers(): Seq[ConnectedPeer] =
lastSyncSentTime.filter(t => (timeProvider.time() - t._2).millis > maxInterval()).keys.toSeq


def peersByStatus: Map[HistoryComparisonResult, Iterable[ConnectedPeer]] =
statuses.groupBy(_._2).mapValues(_.keys).view.force

private def numOfSeniors(): Int = statuses.count(_._2 == Older)

/**
Expand Down
Expand Up @@ -43,21 +43,19 @@ class MessageSerializer(specs: Seq[MessageSpec[_]], magicBytes: Array[Byte]) {
val msgCode = it.getByte
val length = it.getInt

//peer is from another network
if (!java.util.Arrays.equals(magic, magicBytes)) {
throw MaliciousBehaviorException(s"Wrong magic bytes, expected ${magicBytes.mkString}, got ${magic.mkString}")
}

//peer is trying to cause buffer overflow or breaking the parsing
if (length < 0) {
throw MaliciousBehaviorException("Data length is negative!")
}

val spec = specsMap.getOrElse(msgCode, throw new Error(s"No message handler found for $msgCode"))

if (length != 0 && byteString.length < length + HeaderLength + ChecksumLength) {
None
} else {
//peer is from another network
if (!java.util.Arrays.equals(magic, magicBytes)) {
throw MaliciousBehaviorException(s"Wrong magic bytes, expected ${magicBytes.mkString}, got ${magic.mkString} in : ${byteString.utf8String}")
}
val spec = specsMap.getOrElse(msgCode, throw new Error(s"No message handler found for $msgCode"))
val msgData = if (length > 0) {
val checksum = it.getBytes(ChecksumLength)
val data = it.getBytes(length)
Expand Down

0 comments on commit a6fe141

Please sign in to comment.