@@ -32,7 +32,6 @@ import {
32
32
BasicRadius ,
33
33
ClientInfoAndCapabilities ,
34
34
ContentMessageType ,
35
- CustomPayloadExtensionsFormat ,
36
35
ErrorPayload ,
37
36
HistoryRadius ,
38
37
MAX_PACKET_SIZE ,
@@ -304,15 +303,12 @@ export abstract class BaseNetwork extends EventEmitter {
304
303
return undefined
305
304
} , 3000 )
306
305
try {
307
- const customPayload = CustomPayloadExtensionsFormat . serialize ( {
308
- type : extensionType ,
309
- payload : this . pingPongPayload ( extensionType ) ,
310
- } )
311
306
const pingMsg = PortalWireMessageType . serialize ( {
312
307
selector : MessageCodes . PING ,
313
308
value : {
314
309
enrSeq : this . enr . seq ,
315
- customPayload,
310
+ payloadType : extensionType ,
311
+ customPayload : this . pingPongPayload ( extensionType ) ,
316
312
} ,
317
313
} )
318
314
this . logger . extend ( `PING` ) ( `Sent to ${ shortId ( enr ) } ` )
@@ -323,31 +319,29 @@ export abstract class BaseNetwork extends EventEmitter {
323
319
const pongMessage = decoded . value as PongMessage
324
320
// Received a PONG message so node is reachable, add to routing table
325
321
this . updateRoutingTable ( enr )
326
- const { type, payload } = CustomPayloadExtensionsFormat . deserialize (
327
- pongMessage . customPayload ,
328
- )
329
- switch ( type ) {
322
+ switch ( pongMessage . payloadType ) {
330
323
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
+ )
333
327
this . logger . extend ( 'PONG' ) (
334
328
`Client ${ shortId ( enr . nodeId ) } is ${ decodeClientInfo ( ClientInfo ) . clientName } node with capabilities: ${ Capabilities } ` ,
335
329
)
336
330
this . routingTable . updateRadius ( enr . nodeId , DataRadius )
337
331
break
338
332
}
339
333
case PingPongPayloadExtensions . BASIC_RADIUS_PAYLOAD : {
340
- const { dataRadius } = BasicRadius . deserialize ( payload )
334
+ const { dataRadius } = BasicRadius . deserialize ( pongMessage . customPayload )
341
335
this . routingTable . updateRadius ( enr . nodeId , dataRadius )
342
336
break
343
337
}
344
338
case PingPongPayloadExtensions . HISTORY_RADIUS_PAYLOAD : {
345
- const { dataRadius } = HistoryRadius . deserialize ( payload )
339
+ const { dataRadius } = HistoryRadius . deserialize ( pongMessage . customPayload )
346
340
this . routingTable . updateRadius ( enr . nodeId , dataRadius )
347
341
break
348
342
}
349
343
case PingPongPayloadExtensions . ERROR_RESPONSE : {
350
- const { errorCode, message } = ErrorPayload . deserialize ( payload )
344
+ const { errorCode, message } = ErrorPayload . deserialize ( pongMessage . customPayload )
351
345
this . logger . extend ( 'PONG' ) (
352
346
`Received error response from ${ shortId ( enr . nodeId ) } : ${ errorCode } - ${ message } ` ,
353
347
)
@@ -372,21 +366,19 @@ export abstract class BaseNetwork extends EventEmitter {
372
366
}
373
367
374
368
handlePing = async ( src : INodeAddress , id : bigint , pingMessage : PingMessage ) => {
375
- const { type, payload } = CustomPayloadExtensionsFormat . deserialize ( pingMessage . customPayload )
376
369
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 } ` ,
386
378
) ,
387
- } ) ,
379
+ ) ,
388
380
} )
389
- return this . sendPong ( src , id , customPayload )
381
+ return this . sendPong ( src , id , customPayload , PingPongPayloadExtensions . ERROR_RESPONSE )
390
382
}
391
383
// Check to see if node is already in corresponding network routing table and add if not
392
384
const enr = this . findEnr ( src . nodeId )
@@ -395,31 +387,33 @@ export abstract class BaseNetwork extends EventEmitter {
395
387
}
396
388
}
397
389
let pongPayload : Uint8Array
398
- if ( this . capabilities . includes ( type ) ) {
399
- switch ( type ) {
390
+ if ( this . capabilities . includes ( pingMessage . payloadType ) ) {
391
+ switch ( pingMessage . payloadType ) {
400
392
case PingPongPayloadExtensions . CLIENT_INFO_RADIUS_AND_CAPABILITIES : {
401
- const { DataRadius } = ClientInfoAndCapabilities . deserialize ( payload )
393
+ const { DataRadius } = ClientInfoAndCapabilities . deserialize ( pingMessage . customPayload )
402
394
this . routingTable . updateRadius ( src . nodeId , DataRadius )
403
- pongPayload = this . pingPongPayload ( type )
395
+ pongPayload = this . pingPongPayload ( pingMessage . payloadType )
404
396
break
405
397
}
406
398
case PingPongPayloadExtensions . BASIC_RADIUS_PAYLOAD : {
407
- const { dataRadius } = BasicRadius . deserialize ( payload )
399
+ const { dataRadius } = BasicRadius . deserialize ( pingMessage . customPayload )
408
400
this . routingTable . updateRadius ( src . nodeId , dataRadius )
409
- pongPayload = this . pingPongPayload ( type )
401
+ pongPayload = this . pingPongPayload ( pingMessage . payloadType )
410
402
break
411
403
}
412
404
case PingPongPayloadExtensions . HISTORY_RADIUS_PAYLOAD : {
413
- const { dataRadius } = HistoryRadius . deserialize ( payload )
405
+ const { dataRadius } = HistoryRadius . deserialize ( pingMessage . customPayload )
414
406
this . routingTable . updateRadius ( src . nodeId , dataRadius )
415
- pongPayload = this . pingPongPayload ( type )
407
+ pongPayload = this . pingPongPayload ( pingMessage . payloadType )
416
408
break
417
409
}
418
410
default : {
419
411
pongPayload = ErrorPayload . serialize ( {
420
412
errorCode : 0 ,
421
413
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
+ ) ,
423
417
) ,
424
418
} )
425
419
}
@@ -428,28 +422,25 @@ export abstract class BaseNetwork extends EventEmitter {
428
422
pongPayload = ErrorPayload . serialize ( {
429
423
errorCode : PingPongErrorCodes . EXTENSION_NOT_SUPPORTED ,
430
424
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
+ ) ,
432
428
) ,
433
429
} )
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 )
442
431
}
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 )
448
433
}
449
434
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
+ ) => {
451
441
const payload = {
452
442
enrSeq : this . enr . seq ,
443
+ payloadType,
453
444
customPayload,
454
445
}
455
446
const pongMsg = PortalWireMessageType . serialize ( {
0 commit comments