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

[WIP] getStatus added to SyncTracker #399

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions src/main/scala/scorex/core/network/SyncTracker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class SyncTracker(nvsRef: ActorRef,
import History._
import scorex.core.utils.TimeProvider.Time

private var schedule: Option[Cancellable] = None
protected var schedule: Option[Cancellable] = None

private val statuses = mutable.Map[ConnectedPeer, HistoryComparisonResult]()
private val lastSyncSentTime = mutable.Map[ConnectedPeer, Time]()
protected val statuses = mutable.Map[ConnectedPeer, HistoryComparisonResult]()
protected val lastSyncSentTime = mutable.Map[ConnectedPeer, Time]()

private var lastSyncInfoSentTime: Time = 0L
protected var lastSyncInfoSentTime: Time = 0L

private var stableSyncRegime = false
protected var stableSyncRegime = false

def scheduleSendSyncInfo(): Unit = {
schedule foreach {
Expand All @@ -51,6 +51,13 @@ class SyncTracker(nvsRef: ActorRef,
if (stableSyncRegime) networkSettings.syncIntervalStable
else networkSettings.syncInterval

/**
* Get synchronization status for given connected peer
*/
def getStatus(peer: ConnectedPeer): Option[HistoryComparisonResult] = {
statuses.get(peer)
}

def updateStatus(peer: ConnectedPeer, status: HistoryComparisonResult): Unit = {
val seniorsBefore = numOfSeniors()
statuses += peer -> status
Expand Down Expand Up @@ -89,14 +96,14 @@ class SyncTracker(nvsRef: ActorRef,

def elapsedTimeSinceLastSync(): Long = timeProvider.time() - lastSyncInfoSentTime

private def outdatedPeers(): Seq[ConnectedPeer] =
protected 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)
protected def numOfSeniors(): Int = statuses.count(_._2 == Older)

/**
* Return the peers to which this node should send a sync signal, including:
Expand Down