diff --git a/src/Data/Cycles.ts b/src/Data/Cycles.ts index 46de6800..f4aa04ce 100644 --- a/src/Data/Cycles.ts +++ b/src/Data/Cycles.ts @@ -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] diff --git a/src/State.ts b/src/State.ts index c1b925f0..a98cc078 100644 --- a/src/State.ts +++ b/src/State.ts @@ -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 */ @@ -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 { diff --git a/src/sync-v2/index.ts b/src/sync-v2/index.ts index 5ccb6609..d13359c4 100644 --- a/src/sync-v2/index.ts +++ b/src/sync-v2/index.ts @@ -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. @@ -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({ @@ -134,8 +124,8 @@ export function syncV2( }) }) ) + ) ) - ) ) }