From 7c72c5ee0bd9048c5c48932e2386215f1d91a870 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Mon, 30 Aug 2021 10:23:12 -0400 Subject: [PATCH 1/2] devp2p: add snappy compression to ping/pong --- packages/devp2p/src/rlpx/peer.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/devp2p/src/rlpx/peer.ts b/packages/devp2p/src/rlpx/peer.ts index 51ace77668..91c70c6b51 100644 --- a/packages/devp2p/src/rlpx/peer.ts +++ b/packages/devp2p/src/rlpx/peer.ts @@ -202,6 +202,7 @@ export class Peer extends EventEmitter { */ _sendMessage(code: number, data: Buffer) { if (this._closed) return false + const msg = Buffer.concat([rlp.encode(code), data]) const header = this._eciesSession.createHeader(msg.length) if (!header || this._socket.destroyed) return @@ -263,7 +264,11 @@ export class Peer extends EventEmitter { */ _sendPing() { debug(`Send PING to ${this._socket.remoteAddress}:${this._socket.remotePort}`) - const data = rlp.encode([]) + let data = rlp.encode([]) + if (this._hello?.protocolVersion && this._hello.protocolVersion >= 5) { + data = snappy.compress(data) + } + if (!this._sendMessage(PREFIXES.PING, data)) return clearTimeout(this._pingTimeoutId!) @@ -277,7 +282,11 @@ export class Peer extends EventEmitter { */ _sendPong() { debug(`Send PONG to ${this._socket.remoteAddress}:${this._socket.remotePort}`) - const data = rlp.encode([]) + let data = rlp.encode([]) + + if (this._hello?.protocolVersion && this._hello.protocolVersion >= 5) { + data = snappy.compress(data) + } this._sendMessage(PREFIXES.PONG, data) } From 09ea12c7767e9008c0ad71fa9e1de2ab5c579d10 Mon Sep 17 00:00:00 2001 From: acolytec3 <17355484+acolytec3@users.noreply.github.com> Date: Mon, 30 Aug 2021 10:23:42 -0400 Subject: [PATCH 2/2] lint --- packages/devp2p/src/rlpx/peer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/devp2p/src/rlpx/peer.ts b/packages/devp2p/src/rlpx/peer.ts index 91c70c6b51..3a24d2473f 100644 --- a/packages/devp2p/src/rlpx/peer.ts +++ b/packages/devp2p/src/rlpx/peer.ts @@ -282,7 +282,7 @@ export class Peer extends EventEmitter { */ _sendPong() { debug(`Send PONG to ${this._socket.remoteAddress}:${this._socket.remotePort}`) - let data = rlp.encode([]) + let data = rlp.encode([]) if (this._hello?.protocolVersion && this._hello.protocolVersion >= 5) { data = snappy.compress(data)