Skip to content

Commit

Permalink
feat: expose function to look up last 1000 removed nodes public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
PudgyPug authored and mhanson-github committed Jan 27, 2025
1 parent 64bdd8b commit 0b6af9c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ const SERVER_CONFIG: StrictServerConfiguration = {
networkTransactionsToProcessPerCycle: 20,
getTxTimestampTimeoutOffset: 0,
dropNGTByGossipEnabled: false,
timestampCacheFixSize: 10000
timestampCacheFixSize: 10000,
removedNodeIDCacheSize: 1000
},
ip: {
externalIp: '0.0.0.0',
Expand Down
25 changes: 24 additions & 1 deletion src/p2p/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { P2P } from '@shardeum-foundation/lib-types'
import { Logger } from 'log4js'
import { isDebugModeMiddleware, isDebugModeMiddlewareLow } from '../network/debugMiddleware'
import { ShardusEvent } from '../shardus/shardus-types'
import { binarySearch, getTime, insertSorted, linearInsertSorted, propComparator, propComparator2 } from '../utils'
import {
binarySearch,
FIFOCache,
insertSorted,
linearInsertSorted,
propComparator,
propComparator2
} from '../utils'
import * as Comms from './Comms'
import { config, crypto, logger, network } from './Context'
import * as CycleChain from './CycleChain'
Expand Down Expand Up @@ -41,6 +48,10 @@ export let readyByTimeAndIdOrder: P2P.NodeListTypes.Node[]
export let activeOthersByIdOrder: P2P.NodeListTypes.Node[]
export let potentiallyRemoved: Set<P2P.NodeListTypes.Node['id']>
export let selectedById: Map<P2P.NodeListTypes.Node['id'], number>
export let removedNodeIDCache: FIFOCache<
P2P.NodeListTypes.Node['id'],
P2P.NodeListTypes.Node['publicKey']
>

const VERBOSE = false // Use to dump complete NodeList and CycleChain data

Expand Down Expand Up @@ -179,6 +190,10 @@ export function addNodes(newNodes: P2P.NodeListTypes.Node[], caller: string) {
}
}

export function getRemovedNodePubKeyFromCache(nodeId: P2P.NodeListTypes.Node['id']) {
return removedNodeIDCache.get(nodeId);
}

export function removeSelectedNode(id: string) {
selectedById.delete(id)
const idx = binarySearch(selectedByIdOrder, { id }, propComparator('id'))
Expand Down Expand Up @@ -264,6 +279,14 @@ export function removeNode(
selectedById.delete(id)
//readyByTimeAndIdOrder = readyByTimeAndIdOrder.filter((node) => node.id !== id)

// add to removed node cache
if (!removedNodeIDCache) {
removedNodeIDCache = new FIFOCache<P2P.NodeListTypes.Node['id'], P2P.NodeListTypes.Node['publicKey']>(
config.p2p.removedNodeIDCacheSize
)
}
removedNodeIDCache.set(id, node.publicKey)

Comms.evictCachedSockets([node])

if (raiseEvents) {
Expand Down
4 changes: 4 additions & 0 deletions src/p2p/Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ class State extends EventEmitter {
return NodeList.nodes.get(id)
}

getRemovedNodePubKeyFromCache(id: string): P2PTypings.NodeListTypes.Node['publicKey'] | undefined {
return NodeList.getRemovedNodePubKeyFromCache(id)
}

getNodes() {
return NodeList.nodes
}
Expand Down
4 changes: 4 additions & 0 deletions src/shardus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,10 @@ class Shardus extends EventEmitter {
return this.p2p.state.getNode(id)
}

getRemovedNodePubKeyFromCache(id: string): ShardusTypes.Node['publicKey'] | undefined {
return this.p2p.state.getRemovedNodePubKeyFromCache(id)
}

getNodeByPubKey(id: string): ShardusTypes.Node {
return this.p2p.state.getNodeByPubKey(id)
}
Expand Down
4 changes: 3 additions & 1 deletion src/shardus/shardus-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ export interface ServerConfiguration {
enableProblematicNodeRemoval?: boolean
/** when true, we will remove problematic nodes even when calculateToAcceptV2 says we should not remove any nodes. This is useful in development when testing this feature. */
enableDangerousProblematicNodeRemoval?: boolean
/** enable problematic node removal on a specific cycle. This is to allow the network to stabilize before removing problematic nodes.
/** enable problematic node removal on a specific cycle. This is to allow the network to stabilize before removing problematic nodes.
* enableProblematicNodeRemoval must be true for this to take effect*/
enableProblematicNodeRemovalOnCycle?: number
/** The problematicNodeRemovalCycleFrequency parameter is an Integer specifying the number of cycles between problematic node removals. */
Expand Down Expand Up @@ -986,6 +986,8 @@ export interface ServerConfiguration {
getTxTimestampTimeoutOffset?: number // default timeout is 5 seconds so this can be used to add or subtract time from that
/** allow dropping NGTs by hitting a single node's endpoint and the drop mesage being sent to other nodes by gossip */
dropNGTByGossipEnabled: boolean
// cache size for mapping of nodeId to pubKey for the `n` last removed nodes
removedNodeIDCacheSize: number
timestampCacheFixSize: number
}
/** Server IP configuration */
Expand Down

0 comments on commit 0b6af9c

Please sign in to comment.