Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nodelist issue #24

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/Data/Cycles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,11 @@ function updateNodeList(cycle: P2PTypes.CycleCreatorTypes.CycleData): void {
}, [])
NodeList.removeNodes(apoptosizedPks)

const lostAfterSelectionConsensusNodes: NodeList.ConsensusNodeInfo[] = []
const lostAfterSelectionPks = lostAfterSelection.reduce((keys: string[], id) => {
const nodeInfo = NodeList.getNodeInfoById(id)
if (nodeInfo) {
lostAfterSelectionConsensusNodes.push(nodeInfo)
keys.push(nodeInfo.publicKey)
}
return keys
Expand Down Expand Up @@ -335,6 +337,7 @@ function updateNodeList(cycle: P2PTypes.CycleCreatorTypes.CycleData): void {
cycle: cycle.counter,
removed: removedConsensusNodes,
apoptosized: apoptosizedConsensusNodes,
lostAfterSelection: lostAfterSelectionConsensusNodes,
})
while (removedAndApopedNodes.length > 10) {
removedAndApopedNodes.shift()
Expand Down
15 changes: 5 additions & 10 deletions src/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ export const fromP2PTypesNode = (node: P2PTypes.NodeListTypes.Node): JoinedConse
const standbyList: Map<string, ConsensusNodeInfo> = new Map()
const syncingList: Map<string, ConsensusNodeInfo> = new Map()
const activeList: Map<string, ConsensusNodeInfo> = new Map()

// Map to get node public key by node Id
const byId: Map<string, string> = new Map()

// Map to get node info by public key, stores all nodes
// Map to get node info by public key, stores syncing and active nodes
export const byPublicKey: Map<string, ConsensusNodeInfo> = new Map()

// Array of active nodes sorted by id
export let activeListByIdSorted: ConsensusNodeInfo[] = []

Expand Down Expand Up @@ -101,7 +98,6 @@ export function addNodes(status: NodeStatus, nodes: Node[]): void {
Logger.mainLogger.debug(`Adding ${status} nodes to the list`, nodes.length, nodes)

for (const node of nodes) {

// If node not in lists, add it
// eslint-disable-next-line security/detect-object-injection
if (!byPublicKey.has(node.publicKey)) {
Expand Down Expand Up @@ -147,7 +143,6 @@ export function refreshNodes(status: NodeStatus, nodes: ConsensusNodeInfo[] | Jo
if (nodes.length === 0) return
Logger.mainLogger.debug('Refreshing nodes', nodes.length, nodes)
for (const node of nodes) {

// If node not in lists, add it
// eslint-disable-next-line security/detect-object-injection
if (!byPublicKey.has(node.publicKey)) {
Expand Down Expand Up @@ -255,7 +250,7 @@ export function setStatus(status: Exclude<NodeStatus, NodeStatus.STANDBY>, publi
}

export function getFirstNode(): ConsensusNodeInfo | undefined {
return byPublicKey.values().next().value;
return byPublicKey?.values()?.next()?.value
}

export function getActiveList(id_sorted = true): ConsensusNodeInfo[] {
Expand Down Expand Up @@ -290,7 +285,7 @@ export const getCachedNodeList = (): SignedNodeList => {

for (let index = 0; index < config.N_RANDOM_NODELIST_BUCKETS; index++) {
// If we dont have any active nodes, send back the first node in our list
const nodeList = nodeCount < 1 ? (isEmpty() ? [] : [getFirstNode()]) : getRandomActiveNodes(nodeCount);
const nodeList = nodeCount < 1 ? (isEmpty() ? [] : [getFirstNode()]) : getRandomActiveNodes(nodeCount)
const sortedNodeList = [...nodeList].sort(byAscendingNodeId)
const signedSortedNodeList = Crypto.sign({
nodeList: sortedNodeList,
Expand Down Expand Up @@ -388,8 +383,8 @@ export function getStandbyList(): ConsensusNodeInfo[] {
}

export function getNodeInfoById(id: string): ConsensusNodeInfo | undefined {
// eslint-disable-next-line security/detect-object-injection
return byId[id]
const pk = byId.get(id)
return pk ? byPublicKey.get(pk) : undefined
}

export function changeNodeListInRestore(): void {
Expand Down
Loading