@@ -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 ( {
0 commit comments