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

Network Census #727

Closed
wants to merge 7 commits into from
Closed
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
83 changes: 83 additions & 0 deletions packages/portalnetwork/src/client/routingTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import { KademliaRoutingTable } from '@chainsafe/discv5'

import type { ENR, NodeId } from '@chainsafe/enr'
import type { Debugger } from 'debug'
import type { IClientInfo, INodeAddress } from '../index.js'
import { shortId } from '../index.js'
import { ScoredPeer } from '../networks/peers.js'
export class PortalNetworkRoutingTable extends KademliaRoutingTable {
public logger?: Debugger
private radiusMap: Map<NodeId, bigint>
private gossipMap: Map<NodeId, Set<Uint8Array>>
private ignored: [number, NodeId][]
/**
* Map of all known peers in the network
*/
public networkCensus: Map<string, ScoredPeer>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public networkCensus: Map<string, ScoredPeer>
public networkCensus: Map<NodeId, ScoredPeer>

Import NodeId from @chainsafe/enr

constructor(nodeId: NodeId) {
super(nodeId)
this.radiusMap = new Map()
this.gossipMap = new Map()
this.ignored = []
this.networkCensus = new Map()
}

public setLogger(logger: Debugger) {
Expand Down Expand Up @@ -103,4 +110,80 @@ export class PortalNetworkRoutingTable extends KademliaRoutingTable {
before - this.ignored.length > 0 &&
this.logger!(`${before - this.ignored.length} nodeId's are no longer ignored`)
}

public nodeInCensus = (nodeId: NodeId) => {
return this.networkCensus.has(nodeId)
}

public updateNodeFromPing = (
nodeAddress: INodeAddress,
{
capabilities,
clientInfo,
radius,
}: {
radius: bigint
capabilities?: number[]
clientInfo?: IClientInfo
},
) => {
this.updateCensusFromNodeAddress(nodeAddress)
this.updateRadius(nodeAddress.nodeId, radius)
const peer = this.networkCensus.get(nodeAddress.nodeId)!
peer.radius = radius
if (capabilities !== undefined) {
peer.capabilities = capabilities
}
if (clientInfo !== undefined) {
peer.clientInfo = clientInfo
}
}

public updateNodeFromPong = (
enr: ENR,
{
capabilities,
clientInfo,
radius,
}: {
radius: bigint
capabilities?: number[]
clientInfo?: IClientInfo
},
) => {
this.updateCensusFromENR(enr)
this.updateRadius(enr.nodeId, radius)
const peer = this.networkCensus.get(enr.nodeId)!
peer.enr = enr
peer.radius = radius
if (capabilities !== undefined) {
peer.capabilities = capabilities
}
if (clientInfo !== undefined) {
peer.clientInfo = clientInfo
}

}

public updateCensusFromENR = (enr: ENR) => {
const peer = this.networkCensus.get(enr.nodeId)
if (peer) {
peer.enr = enr
} else {
const nodeAddress: INodeAddress = {
nodeId: enr.nodeId,
socketAddr: enr.getLocationMultiaddr('udp')!,
}
this.networkCensus.set(enr.nodeId, new ScoredPeer({ nodeAddress, enr }))
}
}

public updateCensusFromNodeAddress = (nodeAddress: INodeAddress) => {
const peer = this.networkCensus.get(nodeAddress.nodeId)
if (peer) {
peer.nodeAddress = nodeAddress
} else {
this.networkCensus.set(nodeAddress.nodeId, new ScoredPeer({ nodeAddress }))
}
}
}
29 changes: 20 additions & 9 deletions packages/portalnetwork/src/networks/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ export abstract class BaseNetwork extends EventEmitter {
this.logger(`Invalid ENR provided. PING aborted`)
return
}
if (!this.routingTable.getWithPending(enr.nodeId)?.value && extensionType !== 0) {
throw new Error(`First PING message must be type 0: CLIENT_INFO_RADIUS_AND_CAPABILITIES.`)
const peerCapabilities = this.routingTable.networkCensus.get(enr.nodeId)?.capabilities ?? [0]
if (!peerCapabilities.includes(extensionType)) {
throw new Error(`Peer is not know to support extension type: ${extensionType}`)
}
const timeout = setTimeout(() => {
return undefined
Expand Down Expand Up @@ -327,17 +328,21 @@ export abstract class BaseNetwork extends EventEmitter {
this.logger.extend('PONG')(
`Client ${shortId(enr.nodeId)} is ${decodeClientInfo(ClientInfo).clientName} node with capabilities: ${Capabilities}`,
)
this.routingTable.updateRadius(enr.nodeId, DataRadius)
this.routingTable.updateNodeFromPong(enr, {
capabilities: Capabilities,
clientInfo: decodeClientInfo(ClientInfo),
radius: DataRadius,
})
break
}
case PingPongPayloadExtensions.BASIC_RADIUS_PAYLOAD: {
const { dataRadius } = BasicRadius.deserialize(pongMessage.customPayload)
this.routingTable.updateRadius(enr.nodeId, dataRadius)
this.routingTable.updateNodeFromPong(enr, { radius: dataRadius })
break
}
case PingPongPayloadExtensions.HISTORY_RADIUS_PAYLOAD: {
const { dataRadius } = HistoryRadius.deserialize(pongMessage.customPayload)
this.routingTable.updateRadius(enr.nodeId, dataRadius)
this.routingTable.updateNodeFromPong(enr, { radius: dataRadius })
break
}
case PingPongPayloadExtensions.ERROR_RESPONSE: {
Expand Down Expand Up @@ -377,20 +382,26 @@ export abstract class BaseNetwork extends EventEmitter {
if (this.capabilities.includes(pingMessage.payloadType)) {
switch (pingMessage.payloadType) {
case PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES: {
const { DataRadius } = ClientInfoAndCapabilities.deserialize(pingMessage.customPayload)
this.routingTable.updateRadius(src.nodeId, DataRadius)
const { DataRadius, Capabilities, ClientInfo } = ClientInfoAndCapabilities.deserialize(
pingMessage.customPayload,
)
this.routingTable.updateNodeFromPing(src, {
capabilities: Capabilities,
clientInfo: decodeClientInfo(ClientInfo),
radius: DataRadius,
})
pongPayload = this.pingPongPayload(pingMessage.payloadType)
break
}
case PingPongPayloadExtensions.BASIC_RADIUS_PAYLOAD: {
const { dataRadius } = BasicRadius.deserialize(pingMessage.customPayload)
this.routingTable.updateRadius(src.nodeId, dataRadius)
this.routingTable.updateNodeFromPing(src, { radius: dataRadius })
pongPayload = this.pingPongPayload(pingMessage.payloadType)
break
}
case PingPongPayloadExtensions.HISTORY_RADIUS_PAYLOAD: {
const { dataRadius } = HistoryRadius.deserialize(pingMessage.customPayload)
this.routingTable.updateRadius(src.nodeId, dataRadius)
this.routingTable.updateNodeFromPing(src, { radius: dataRadius })
pongPayload = this.pingPongPayload(pingMessage.payloadType)
break
}
Expand Down
49 changes: 49 additions & 0 deletions packages/portalnetwork/src/networks/peers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { ENR } from '@chainsafe/enr'
import type { INodeAddress } from '../client/types.js'
import type { IClientInfo } from '../index.js'

export type PeerScore = {
errors: number
}

export class ScoredPeer {
nodeAddress: INodeAddress
enr?: ENR
score: PeerScore = {
errors: 0,
}
capabilities: Array<number> = []
clientInfo?: IClientInfo
radius?: bigint
ignored: boolean = false
banned: boolean = false
constructor({
nodeAddress,
enr,
score,
capabilities,
clientInfo,
radius,
ignored,
banned,
}: {
nodeAddress: INodeAddress
enr?: ENR
score?: PeerScore
capabilities?: Array<number>
clientInfo?: IClientInfo
radius?: bigint
ignored?: boolean
banned?: boolean
}) {
this.nodeAddress = nodeAddress
this.enr = enr
this.score = score ?? this.score
this.capabilities = capabilities ?? this.capabilities
this.clientInfo = clientInfo
this.radius = radius
this.ignored = ignored ?? this.ignored
this.banned = banned ?? this.banned
}
}

32 changes: 22 additions & 10 deletions packages/portalnetwork/test/integration/pingpong.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { keys } from '@libp2p/crypto'
import { multiaddr } from '@multiformats/multiaddr'
import { hexToBytes } from 'ethereum-cryptography/utils'
import { assert, describe, it } from 'vitest'
import type { HistoryNetwork, INodeAddress } from '../../src'
import type { HistoryNetwork } from '../../src'
import {
ClientInfoAndCapabilities,
HistoryRadius,
NetworkId,
PingPongPayloadExtensions,
PortalNetwork,
PortalWireMessageType,
TransportLayer,
} from '../../src/index.js'
import { ScoredPeer } from '../../src/networks/peers.js'

const privateKeys = [
'0x0a2700250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c12250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c1a2408021220aae0fff4ac28fdcdf14ee8ecb591c7f1bc78651206d86afe16479a63d9cb73bd',
'0x0a27002508021221039909a8a7e81dbdc867480f0eeb7468189d1e7a1dd7ee8a13ee486c8cbd743764122508021221039909a8a7e81dbdc867480f0eeb7468189d1e7a1dd7ee8a13ee486c8cbd7437641a2408021220c6eb3ae347433e8cfe7a0a195cc17fc8afcd478b9fb74be56d13bccc67813130',
]
'0x0a2700250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c12250802122102273097673a2948af93317235d2f02ad9cf3b79a34eeb37720c5f19e09f11783c1a2408021220aae0fff4ac28fdcdf14ee8ecb591c7f1bc78651206d86afe16479a63d9cb73bd',
'0x0a27002508021221039909a8a7e81dbdc867480f0eeb7468189d1e7a1dd7ee8a13ee486c8cbd743764122508021221039909a8a7e81dbdc867480f0eeb7468189d1e7a1dd7ee8a13ee486c8cbd7437641a2408021220c6eb3ae347433e8cfe7a0a195cc17fc8afcd478b9fb74be56d13bccc67813130',
]

const pk1 = keys.privateKeyFromProtobuf(hexToBytes(privateKeys[0]).slice(-36))
const enr1 = SignableENR.createFromPrivateKey(pk1)
Expand Down Expand Up @@ -62,16 +62,16 @@ describe('PING/PONG', async () => {
await network1.sendPing(network2?.enr!.toENR(), 1)
assert.fail('should have failed')
} catch (e) {
assert.equal(
e.message,
'First PING message must be type 0: CLIENT_INFO_RADIUS_AND_CAPABILITIES.',
)
assert.equal(e.message, 'Peer is not know to support extension type: 1')
}
})
it('should exchange type 0 PING/PONG', async () => {
const pingpong = await network1.sendPing(network2?.enr!.toENR(), 0)
assert.exists(pingpong, 'should have received a pong')
assert.equal(pingpong!.payloadType, PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES)
assert.equal(
pingpong!.payloadType,
PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES,
)
const { DataRadius } = ClientInfoAndCapabilities.deserialize(pingpong!.customPayload)
assert.equal(DataRadius, network2.nodeRadius)
})
Expand All @@ -83,6 +83,18 @@ describe('PING/PONG', async () => {
assert.equal(dataRadius, network2.nodeRadius)
})
it('should receive error response from unsupported capability', async () => {
// falsely update peer's capabilities in network census
network1.routingTable.networkCensus.set(
network2.enr!.nodeId,
new ScoredPeer({
nodeAddress: {
nodeId: network2.enr!.nodeId,
socketAddr: network2.enr!.getLocationMultiaddr('udp')!,
},
capabilities: [1],
}),
)
// send ping with unsupported capability
const pingpong = await network1.sendPing(network2?.enr!.toENR(), 1)
assert.exists(pingpong, 'should have received a pong')
assert.equal(pingpong!.payloadType, PingPongPayloadExtensions.ERROR_RESPONSE)
Expand Down