Skip to content

Commit 58bd756

Browse files
committed
update extensions types
1 parent 065084d commit 58bd756

File tree

4 files changed

+254
-77
lines changed

4 files changed

+254
-77
lines changed

packages/portalnetwork/src/networks/network.ts

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
BasicRadius,
3333
ClientInfoAndCapabilities,
3434
ContentMessageType,
35-
CustomPayloadExtensionsFormat,
3635
ErrorPayload,
3736
HistoryRadius,
3837
MAX_PACKET_SIZE,
@@ -304,15 +303,12 @@ export abstract class BaseNetwork extends EventEmitter {
304303
return undefined
305304
}, 3000)
306305
try {
307-
const customPayload = CustomPayloadExtensionsFormat.serialize({
308-
type: extensionType,
309-
payload: this.pingPongPayload(extensionType),
310-
})
311306
const pingMsg = PortalWireMessageType.serialize({
312307
selector: MessageCodes.PING,
313308
value: {
314309
enrSeq: this.enr.seq,
315-
customPayload,
310+
payloadType: extensionType,
311+
customPayload: this.pingPongPayload(extensionType),
316312
},
317313
})
318314
this.logger.extend(`PING`)(`Sent to ${shortId(enr)}`)
@@ -323,31 +319,29 @@ export abstract class BaseNetwork extends EventEmitter {
323319
const pongMessage = decoded.value as PongMessage
324320
// Received a PONG message so node is reachable, add to routing table
325321
this.updateRoutingTable(enr)
326-
const { type, payload } = CustomPayloadExtensionsFormat.deserialize(
327-
pongMessage.customPayload,
328-
)
329-
switch (type) {
322+
switch (pongMessage.payloadType) {
330323
case PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES: {
331-
const { ClientInfo, Capabilities, DataRadius } =
332-
ClientInfoAndCapabilities.deserialize(payload)
324+
const { ClientInfo, Capabilities, DataRadius } = ClientInfoAndCapabilities.deserialize(
325+
pongMessage.customPayload,
326+
)
333327
this.logger.extend('PONG')(
334328
`Client ${shortId(enr.nodeId)} is ${decodeClientInfo(ClientInfo).clientName} node with capabilities: ${Capabilities}`,
335329
)
336330
this.routingTable.updateRadius(enr.nodeId, DataRadius)
337331
break
338332
}
339333
case PingPongPayloadExtensions.BASIC_RADIUS_PAYLOAD: {
340-
const { dataRadius } = BasicRadius.deserialize(payload)
334+
const { dataRadius } = BasicRadius.deserialize(pongMessage.customPayload)
341335
this.routingTable.updateRadius(enr.nodeId, dataRadius)
342336
break
343337
}
344338
case PingPongPayloadExtensions.HISTORY_RADIUS_PAYLOAD: {
345-
const { dataRadius } = HistoryRadius.deserialize(payload)
339+
const { dataRadius } = HistoryRadius.deserialize(pongMessage.customPayload)
346340
this.routingTable.updateRadius(enr.nodeId, dataRadius)
347341
break
348342
}
349343
case PingPongPayloadExtensions.ERROR_RESPONSE: {
350-
const { errorCode, message } = ErrorPayload.deserialize(payload)
344+
const { errorCode, message } = ErrorPayload.deserialize(pongMessage.customPayload)
351345
this.logger.extend('PONG')(
352346
`Received error response from ${shortId(enr.nodeId)}: ${errorCode} - ${message}`,
353347
)
@@ -372,21 +366,19 @@ export abstract class BaseNetwork extends EventEmitter {
372366
}
373367

374368
handlePing = async (src: INodeAddress, id: bigint, pingMessage: PingMessage) => {
375-
const { type, payload } = CustomPayloadExtensionsFormat.deserialize(pingMessage.customPayload)
376369
if (!this.routingTable.getWithPending(src.nodeId)?.value) {
377-
if (type !== PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES) {
378-
const customPayload = CustomPayloadExtensionsFormat.serialize({
379-
type: PingPongPayloadExtensions.ERROR_RESPONSE,
380-
payload: ErrorPayload.serialize({
381-
errorCode: PingPongErrorCodes.EXTENSION_NOT_SUPPORTED,
382-
message: hexToBytes(
383-
fromAscii(
384-
`First PING message must be type 0: CLIENT_INFO_RADIUS_AND_CAPABILITIES. Received type ${type}`,
385-
),
370+
if (
371+
pingMessage.payloadType !== PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES
372+
) {
373+
const customPayload = ErrorPayload.serialize({
374+
errorCode: PingPongErrorCodes.EXTENSION_NOT_SUPPORTED,
375+
message: hexToBytes(
376+
fromAscii(
377+
`First PING message must be type 0: CLIENT_INFO_RADIUS_AND_CAPABILITIES. Received type ${pingMessage.payloadType}`,
386378
),
387-
}),
379+
),
388380
})
389-
return this.sendPong(src, id, customPayload)
381+
return this.sendPong(src, id, customPayload, PingPongPayloadExtensions.ERROR_RESPONSE)
390382
}
391383
// Check to see if node is already in corresponding network routing table and add if not
392384
const enr = this.findEnr(src.nodeId)
@@ -395,31 +387,33 @@ export abstract class BaseNetwork extends EventEmitter {
395387
}
396388
}
397389
let pongPayload: Uint8Array
398-
if (this.capabilities.includes(type)) {
399-
switch (type) {
390+
if (this.capabilities.includes(pingMessage.payloadType)) {
391+
switch (pingMessage.payloadType) {
400392
case PingPongPayloadExtensions.CLIENT_INFO_RADIUS_AND_CAPABILITIES: {
401-
const { DataRadius } = ClientInfoAndCapabilities.deserialize(payload)
393+
const { DataRadius } = ClientInfoAndCapabilities.deserialize(pingMessage.customPayload)
402394
this.routingTable.updateRadius(src.nodeId, DataRadius)
403-
pongPayload = this.pingPongPayload(type)
395+
pongPayload = this.pingPongPayload(pingMessage.payloadType)
404396
break
405397
}
406398
case PingPongPayloadExtensions.BASIC_RADIUS_PAYLOAD: {
407-
const { dataRadius } = BasicRadius.deserialize(payload)
399+
const { dataRadius } = BasicRadius.deserialize(pingMessage.customPayload)
408400
this.routingTable.updateRadius(src.nodeId, dataRadius)
409-
pongPayload = this.pingPongPayload(type)
401+
pongPayload = this.pingPongPayload(pingMessage.payloadType)
410402
break
411403
}
412404
case PingPongPayloadExtensions.HISTORY_RADIUS_PAYLOAD: {
413-
const { dataRadius } = HistoryRadius.deserialize(payload)
405+
const { dataRadius } = HistoryRadius.deserialize(pingMessage.customPayload)
414406
this.routingTable.updateRadius(src.nodeId, dataRadius)
415-
pongPayload = this.pingPongPayload(type)
407+
pongPayload = this.pingPongPayload(pingMessage.payloadType)
416408
break
417409
}
418410
default: {
419411
pongPayload = ErrorPayload.serialize({
420412
errorCode: 0,
421413
message: hexToBytes(
422-
fromAscii(`${this.constructor.name} does not support PING extension type: ${type}`),
414+
fromAscii(
415+
`${this.constructor.name} does not support PING extension type: ${pingMessage.payloadType}`,
416+
),
423417
),
424418
})
425419
}
@@ -428,28 +422,25 @@ export abstract class BaseNetwork extends EventEmitter {
428422
pongPayload = ErrorPayload.serialize({
429423
errorCode: PingPongErrorCodes.EXTENSION_NOT_SUPPORTED,
430424
message: hexToBytes(
431-
fromAscii(`${this.constructor.name} does not support PING extension type: ${type}`),
425+
fromAscii(
426+
`${this.constructor.name} does not support PING extension type: ${pingMessage.payloadType}`,
427+
),
432428
),
433429
})
434-
return this.sendPong(
435-
src,
436-
id,
437-
CustomPayloadExtensionsFormat.serialize({
438-
type: PingPongPayloadExtensions.ERROR_RESPONSE,
439-
payload: pongPayload,
440-
}),
441-
)
430+
return this.sendPong(src, id, pongPayload, PingPongPayloadExtensions.ERROR_RESPONSE)
442431
}
443-
const customPayload = CustomPayloadExtensionsFormat.serialize({
444-
type,
445-
payload: pongPayload,
446-
})
447-
return this.sendPong(src, id, customPayload)
432+
return this.sendPong(src, id, pongPayload, pingMessage.payloadType)
448433
}
449434

450-
sendPong = async (src: INodeAddress, requestId: bigint, customPayload: Uint8Array) => {
435+
sendPong = async (
436+
src: INodeAddress,
437+
requestId: bigint,
438+
customPayload: Uint8Array,
439+
payloadType: number,
440+
) => {
451441
const payload = {
452442
enrSeq: this.enr.seq,
443+
payloadType,
453444
customPayload,
454445
}
455446
const pongMsg = PortalWireMessageType.serialize({

packages/portalnetwork/src/wire/payloadExtensions.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
import { ByteListType, ContainerType, ListBasicType, UintBigintType, UintNumberType } from "@chainsafe/ssz";
22
import { bytesToHex, fromAscii, hexToBytes, toAscii } from "@ethereumjs/util";
3+
import { PingPongPayloadType } from "./types.js";
34

45

5-
/**
6-
* Numeric identifier which tells clients how the payload field should be decoded.
7-
*/
8-
export const PingPongPayloadType = new UintNumberType(2)
96

10-
/**
11-
* SSZ encoded extension payload
12-
*/
13-
export const PingPongPayload = new ByteListType(1100)
14-
15-
/**
16-
* All payloads used in the Ping custom_payload MUST follow the Ping Custom Payload Extensions format.
17-
*/
18-
export const CustomPayloadExtensionsFormat = new ContainerType({
19-
type: PingPongPayloadType,
20-
payload: PingPongPayload
21-
})
227

238

249
/**
@@ -53,13 +38,16 @@ export interface IClientInfo {
5338

5439
export const MAX_CLIENT_INFO_BYTE_LENGTH = 200
5540

41+
export function clientInfoStringToBytes(clientInfo: string): Uint8Array {
42+
return hexToBytes(fromAscii(clientInfo))
43+
}
5644
/**
5745
* Encode Client info as ASCII hex encoded string.
5846
* @param clientInfo
5947
* @returns
6048
*/
6149
export function encodeClientInfo(clientInfo: IClientInfo): Uint8Array {
62-
const clientInfoBytes = hexToBytes(fromAscii(Object.values(clientInfo).join("/")))
50+
const clientInfoBytes = clientInfoStringToBytes(Object.values(clientInfo).join("/"))
6351
if (clientInfoBytes.length > MAX_CLIENT_INFO_BYTE_LENGTH) {
6452
throw new Error(`Client info is too long: ${clientInfoBytes.length} > ${MAX_CLIENT_INFO_BYTE_LENGTH}`)
6553
}
@@ -76,6 +64,7 @@ export function decodeClientInfo(clientInfo: Uint8Array): IClientInfo {
7664
};
7765
}
7866

67+
7968
export const ClientInfo = new ByteListType(MAX_CLIENT_INFO_BYTE_LENGTH)
8069

8170

packages/portalnetwork/src/wire/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,37 @@ export enum MessageCodes {
3636
export const ByteList = new ByteListType(2048)
3737
export const Bytes2 = new ByteVectorType(2)
3838
export const ENRs = new ListCompositeType(ByteList, 32)
39+
/**
40+
* Numeric identifier which tells clients how the payload field should be decoded.
41+
*/
42+
export const PingPongPayloadType = new UintNumberType(2)
43+
44+
/**
45+
* SSZ encoded extension payload
46+
*/
47+
export const PingPongPayload = new ByteListType(1100)
3948
export type PingMessage = {
4049
enrSeq: bigint
50+
payloadType: number
4151
customPayload: PingPongCustomData
4252
}
4353

4454
export type PongMessage = {
4555
enrSeq: bigint
56+
payloadType: number
4657
customPayload: PingPongCustomData
4758
}
4859

4960
export const PingMessageType = new ContainerType({
5061
enrSeq: new UintBigintType(8),
51-
customPayload: ByteList,
62+
payloadType: PingPongPayloadType,
63+
customPayload: PingPongPayload,
5264
})
5365

5466
export const PongMessageType = new ContainerType({
5567
enrSeq: new UintBigintType(8),
56-
customPayload: ByteList,
68+
payloadType: PingPongPayloadType,
69+
customPayload: PingPongPayload,
5770
})
5871

5972
export type FindNodesMessage = {

0 commit comments

Comments
 (0)