Skip to content

Commit

Permalink
Refactor archiver addition and removal
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajdev committed Apr 23, 2024
1 parent c752ae6 commit 1b27987
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 55 deletions.
32 changes: 2 additions & 30 deletions src/Data/Cycles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,43 +328,15 @@ function updateNodeList(cycle: P2PTypes.CycleCreatorTypes.CycleData): void {
NodeList.removeNodes(lostAfterSelectionPks)

for (const joinedArchiver of joinedArchivers) {
const foundArchiver = State.activeArchivers.find((a) => a.publicKey === joinedArchiver.publicKey)
if (!foundArchiver) {
State.activeArchivers.push(joinedArchiver)
Utils.insertSorted(
State.activeArchiversByPublicKeySorted,
joinedArchiver,
NodeList.byAscendingPublicKey
)
Logger.mainLogger.debug(
'activeArchiversByPublicKeySorted',
State.activeArchiversByPublicKeySorted.map((archiver) => archiver.publicKey)
)
Logger.mainLogger.debug('New archiver added to active list', joinedArchiver)
}
Logger.mainLogger.debug('active archiver list', State.activeArchivers)
State.addArchiver(joinedArchiver)
}

for (const refreshedArchiver of refreshedArchivers) {
const foundArchiver = State.activeArchivers.find((a) => a.publicKey === refreshedArchiver.publicKey)
if (!foundArchiver) {
State.activeArchivers.push(refreshedArchiver)
Utils.insertSorted(
State.activeArchiversByPublicKeySorted,
refreshedArchiver,
NodeList.byAscendingPublicKey
)
Logger.mainLogger.debug(
'activeArchiversByPublicKeySorted',
State.activeArchiversByPublicKeySorted.map((archiver) => archiver.publicKey)
)
Logger.mainLogger.debug('Refreshed archiver added to active list', refreshedArchiver)
}
State.addArchiver(refreshedArchiver)
}

for (const leavingArchiver of leavingArchivers) {
State.removeActiveArchiver(leavingArchiver.publicKey)
State.archiversReputation.delete(leavingArchiver.publicKey)
}

const nodesToUnsubscribed = [...apoptosizedPks, ...removedPks]
Expand Down
27 changes: 17 additions & 10 deletions src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,7 @@ export async function initFromConfig(config: Config, shutDownMode = false): Prom
continue
}
if (response && response.nodeList && response.nodeList.length > 0) {
activeArchivers.push(existingArchivers[i])
Utils.insertSorted(
activeArchiversByPublicKeySorted,
existingArchivers[i],
NodeList.byAscendingPublicKey
)
Logger.mainLogger.debug(
'activeArchiversByPublicKeySorted',
activeArchiversByPublicKeySorted.map((archiver) => archiver.publicKey)
)
addArchiver(existingArchivers[i])
}
}
/* eslint-enable security/detect-object-injection */
Expand Down Expand Up @@ -162,11 +153,27 @@ export function addSigListeners(sigint = true, sigterm = true): void {
Logger.mainLogger.debug('Registerd exit signal listeners.')
}

export function addArchiver(archiver: ArchiverNodeInfo): void {
const foundArchiver = activeArchivers.find((a) => a.publicKey === archiver.publicKey)
if (!foundArchiver) {
Logger.mainLogger.debug('Adding archiver', archiver)
activeArchivers.push(archiver)
Utils.insertSorted(activeArchiversByPublicKeySorted, archiver, NodeList.byAscendingPublicKey)
Logger.mainLogger.debug(
'activeArchiversByPublicKeySorted',
activeArchiversByPublicKeySorted.map((archiver) => archiver.publicKey)
)
Logger.mainLogger.debug('New archiver added to active list', archiver)
}
Logger.mainLogger.debug('archivers list', activeArchivers)
}

export function removeActiveArchiver(publicKey: string): void {
activeArchivers = activeArchivers.filter((a: ArchiverNodeInfo) => a.publicKey !== publicKey)
activeArchiversByPublicKeySorted = activeArchiversByPublicKeySorted.filter(
(a: ArchiverNodeInfo) => a.publicKey !== publicKey
)
archiversReputation.delete(publicKey)
}

export function resetActiveArchivers(archivers: ArchiverNodeInfo[]): void {
Expand Down
20 changes: 5 additions & 15 deletions src/sync-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ import {
robustQueryForArchiverListHash,
getArchiverListFromNode,
} from './queries'
import { ArchiverNodeInfo, activeArchiversByPublicKeySorted } from '../State'
import { ArchiverNodeInfo, resetActiveArchivers } from '../State'
import { getActiveNodeListFromArchiver } from '../NodeList'
import * as NodeList from '../NodeList'
import { verifyArchiverList, verifyCycleRecord, verifyValidatorList } from './verify'
import * as Logger from '../Logger'
import * as Utils from '../Utils'

/**
* Given a list of archivers, queries each one until one returns an active node list.
Expand Down Expand Up @@ -114,18 +113,9 @@ export function syncV2(
NodeList.addNodes(NodeList.NodeStatus.SYNCING, syncingNodeList)
NodeList.addNodes(NodeList.NodeStatus.ACTIVE, activeNodeList)
NodeList.addStandbyNodes(standbyNodeList)

// add archivers
for (const archiver of archiverList) {
if (!activeArchivers.find(obj => obj.publicKey === archiver.publicKey)) {
activeArchivers.push(archiver)
Utils.insertSorted(
activeArchiversByPublicKeySorted,
archiver,
NodeList.byAscendingPublicKey
)
}
}

// reset the active archivers list with the new list
resetActiveArchivers(archiverList)

// return a cycle that we'll store in the database
return okAsync({
Expand All @@ -134,8 +124,8 @@ export function syncV2(
})
})
)
)
)
)
)
}

Expand Down

0 comments on commit 1b27987

Please sign in to comment.