@lodestar/reqresp 1.52.0-dev.9c81d25e01 → 1.52.0-dev.a3cc0e0e58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/response/index.d.ts.map +1 -1
- package/lib/response/index.js +3 -2
- package/lib/response/index.js.map +1 -1
- package/lib/utils/stream.d.ts +3 -0
- package/lib/utils/stream.d.ts.map +1 -1
- package/lib/utils/stream.js +38 -3
- package/lib/utils/stream.js.map +1 -1
- package/package.json +13 -13
- package/src/response/index.ts +7 -4
- package/src/utils/stream.ts +48 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/response/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAE,MAAM,EAAC,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAC,MAAM,EAA4B,MAAM,iBAAiB,CAAC;AAIlE,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AACtC,OAAO,EAAC,kBAAkB,EAAC,MAAM,uCAAuC,CAAC;AAEzE,OAAO,EAAC,QAAQ,EAAiB,MAAM,aAAa,CAAC;AAErD,OAAO,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAC,aAAa,EAAC,CAAC;AAGvB,eAAO,MAAM,uBAAuB,QAAW,CAAC;AAEhD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,QAAQ,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,kBAAkB,CAAC;IAChC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CA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C,EAClC,MAAM,EACN,OAAO,EACP,MAAM,EACN,MAAM,EACN,QAAQ,EACR,UAAU,EACV,WAAW,EACX,MAAM,EACN,SAAa,EACb,UAAsB,EACtB,gBAAgB,GACjB,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAuFnC"}
|
package/lib/response/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { requestDecode } from "../encoders/requestDecode.js";
|
|
|
3
3
|
import { responseEncodeError, responseEncodeSuccess } from "../encoders/responseEncode.js";
|
|
4
4
|
import { RespStatus } from "../interface.js";
|
|
5
5
|
import { RequestError, RequestErrorCode } from "../request/errors.js";
|
|
6
|
-
import { prettyPrintPeerId, sendChunks } from "../utils/index.js";
|
|
6
|
+
import { STREAM_DRAIN_TIMEOUT_MS, prettyPrintPeerId, sendChunks } from "../utils/index.js";
|
|
7
7
|
import { ResponseError } from "./errors.js";
|
|
8
8
|
export { ResponseError };
|
|
9
9
|
// https://github.com/ethereum/consensus-specs/blob/v1.6.1/specs/phase0/p2p-interface.md#the-reqresp-domain
|
|
@@ -70,7 +70,8 @@ export async function handleRequest({ logger, metrics, stream, peerId, protocol,
|
|
|
70
70
|
stream.abort(streamSendError);
|
|
71
71
|
}
|
|
72
72
|
else {
|
|
73
|
-
|
|
73
|
+
// close() waits for the stream to drain, bound it the same way as sendChunks
|
|
74
|
+
await withTimeout((timeoutSignal) => stream.close({ signal: timeoutSignal }), STREAM_DRAIN_TIMEOUT_MS).catch((e) => {
|
|
74
75
|
stream.abort(e);
|
|
75
76
|
});
|
|
76
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/response/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,YAAY,EAAE,WAAW,EAAC,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAC,aAAa,EAAC,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAC,mBAAmB,EAAE,qBAAqB,EAAC,MAAM,+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AE,CAAC;QAC3B,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,MAAM,EAAE,aAAa,CAAC,CAAC;QACpD,MAAM,aAAa,CAAC;IACtB,CAAC;IACD,0EAA0E;IAC1E,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;AACtC,CAAC"}
|
package/lib/utils/stream.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { Stream } from "@libp2p/interface";
|
|
2
2
|
import { ByteStream } from "@libp2p/utils";
|
|
3
3
|
import { Uint8ArrayList } from "uint8arraylist";
|
|
4
|
+
/** Max time a write waits for the stream to drain, so a peer that stops reading cannot hold the stream open */
|
|
5
|
+
export declare const STREAM_DRAIN_TIMEOUT_MS = 10000;
|
|
6
|
+
export declare const STREAM_WRITE_BUFFER_HIGH_WATER_MARK: number;
|
|
4
7
|
export declare function sendChunks(stream: Stream, source: Iterable<Uint8Array | Uint8ArrayList> | AsyncIterable<Uint8Array | Uint8ArrayList>, signal?: AbortSignal): Promise<void>;
|
|
5
8
|
export declare function drainByteStream(bytes: ByteStream<Stream>): Uint8Array | undefined;
|
|
6
9
|
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/utils/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/utils/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,MAAM,EAAmB,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAC,UAAU,EAAoB,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAC,cAAc,EAAC,MAAM,gBAAgB,CAAC;AAG9C,+GAA+G;AAC/G,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAM9C,eAAO,MAAM,mCAAmC,QAAa,CAAC;AAE9D,wBAAsB,UAAU,CAC9B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,QAAQ,CAAC,UAAU,GAAG,cAAc,CAAC,GAAG,aAAa,CAAC,UAAU,GAAG,cAAc,CAAC,EAC1F,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,IAAI,CAAC,CAgBf;AA8BD,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC,GAAG,UAAU,GAAG,SAAS,CAYjF"}
|
package/lib/utils/stream.js
CHANGED
|
@@ -1,14 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StreamClosedError } from "@libp2p/utils";
|
|
2
|
+
import { ErrorAborted, withTimeout } from "@lodestar/utils";
|
|
3
|
+
/** Max time a write waits for the stream to drain, so a peer that stops reading cannot hold the stream open */
|
|
4
|
+
export const STREAM_DRAIN_TIMEOUT_MS = 10_000;
|
|
5
|
+
// Wait for the stream to drain only once this many bytes are buffered, so small chunks coalesce. QUIC
|
|
6
|
+
// signals backpressure (send returns false, writableNeedsDrain) after every write even when it accepted
|
|
7
|
+
// the bytes and nothing is buffered; gating on buffered bytes avoids a drain round trip per chunk there
|
|
8
|
+
// while still bounding the buffer for a peer that reads slowly over a muxer with a per-stream write queue.
|
|
9
|
+
export const STREAM_WRITE_BUFFER_HIGH_WATER_MARK = 256 * 1024;
|
|
2
10
|
export async function sendChunks(stream, source, signal) {
|
|
3
11
|
for await (const chunk of source) {
|
|
4
12
|
if (signal?.aborted) {
|
|
5
13
|
throw new ErrorAborted("sendChunks");
|
|
6
14
|
}
|
|
7
|
-
|
|
8
|
-
|
|
15
|
+
stream.send(chunk);
|
|
16
|
+
while (stream.writeBufferLength >= STREAM_WRITE_BUFFER_HIGH_WATER_MARK) {
|
|
17
|
+
await withTimeout((timeoutAndParentSignal) => waitForDrain(stream, timeoutAndParentSignal), STREAM_DRAIN_TIMEOUT_MS, signal);
|
|
9
18
|
}
|
|
10
19
|
}
|
|
11
20
|
}
|
|
21
|
+
// Not `stream.onDrain()`: libp2p never resets its drain promise once resolved, so every later call resolves
|
|
22
|
+
// immediately and the rest of the response is buffered in memory regardless of how slowly the peer reads
|
|
23
|
+
function waitForDrain(stream, signal) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const cleanup = () => {
|
|
26
|
+
stream.removeEventListener("drain", onDrain);
|
|
27
|
+
stream.removeEventListener("close", onClose);
|
|
28
|
+
signal?.removeEventListener("abort", onAbort);
|
|
29
|
+
};
|
|
30
|
+
const onDrain = () => {
|
|
31
|
+
cleanup();
|
|
32
|
+
resolve();
|
|
33
|
+
};
|
|
34
|
+
const onClose = (evt) => {
|
|
35
|
+
cleanup();
|
|
36
|
+
reject(evt.error ?? new StreamClosedError());
|
|
37
|
+
};
|
|
38
|
+
const onAbort = () => {
|
|
39
|
+
cleanup();
|
|
40
|
+
reject(new ErrorAborted("sendChunks"));
|
|
41
|
+
};
|
|
42
|
+
stream.addEventListener("drain", onDrain);
|
|
43
|
+
stream.addEventListener("close", onClose);
|
|
44
|
+
signal?.addEventListener("abort", onAbort);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
12
47
|
export function drainByteStream(bytes) {
|
|
13
48
|
const readBuffer = bytes.readBuffer;
|
|
14
49
|
if (readBuffer && readBuffer.byteLength > 0) {
|
package/lib/utils/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/utils/stream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/utils/stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,iBAAiB,EAAC,MAAM,eAAe,CAAC;AAE5D,OAAO,EAAC,YAAY,EAAE,WAAW,EAAC,MAAM,iBAAiB,CAAC;AAE1D,+GAA+G;AAC/G,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC;AAE9C,sGAAsG;AACtG,wGAAwG;AACxG,wGAAwG;AACxG,2GAA2G;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AAM,OAAO,GAAG,GAAS,EAAE;YACzB,OAAO,EAAE,CAAC;YACV,MAAM,CAAC,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC;QACzC,CAAC,CAAC;QAEF,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1C,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC1C,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,KAAyB;IACvD,MAAM,UAAU,GACd,KAGD,CAAC,UAAU,CAAC;IACb,IAAI,UAAU,IAAI,UAAU,CAAC,UAAU,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,OAAO,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAC;QACtC,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;QAC1C,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/ChainSafe/lodestar/issues"
|
|
13
13
|
},
|
|
14
|
-
"version": "1.52.0-dev.
|
|
14
|
+
"version": "1.52.0-dev.a3cc0e0e58",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
@@ -47,24 +47,24 @@
|
|
|
47
47
|
"@chainsafe/fast-crc32c": "^4.2.0",
|
|
48
48
|
"@chainsafe/snappy-wasm": "^0.5.2",
|
|
49
49
|
"@libp2p/interface": "^3.4.0",
|
|
50
|
-
"@libp2p/utils": "^7.4.
|
|
51
|
-
"@lodestar/config": "^1.52.0-dev.
|
|
52
|
-
"@lodestar/params": "^1.52.0-dev.
|
|
53
|
-
"@lodestar/utils": "^1.52.0-dev.
|
|
50
|
+
"@libp2p/utils": "^7.4.6",
|
|
51
|
+
"@lodestar/config": "^1.52.0-dev.a3cc0e0e58",
|
|
52
|
+
"@lodestar/params": "^1.52.0-dev.a3cc0e0e58",
|
|
53
|
+
"@lodestar/utils": "^1.52.0-dev.a3cc0e0e58",
|
|
54
54
|
"snappy": "^7.4.3",
|
|
55
55
|
"uint8-varint": "^2.0.2",
|
|
56
56
|
"uint8arraylist": "^3.0.2"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@chainsafe/ssz": "^1.9.
|
|
60
|
-
"@libp2p/crypto": "^5.
|
|
61
|
-
"@libp2p/peer-id": "^6.0.
|
|
62
|
-
"@lodestar/logger": "^1.52.0-dev.
|
|
63
|
-
"@lodestar/types": "^1.52.0-dev.
|
|
64
|
-
"libp2p": "3.
|
|
59
|
+
"@chainsafe/ssz": "^1.9.1",
|
|
60
|
+
"@libp2p/crypto": "^5.2.0",
|
|
61
|
+
"@libp2p/peer-id": "^6.0.18",
|
|
62
|
+
"@lodestar/logger": "^1.52.0-dev.a3cc0e0e58",
|
|
63
|
+
"@lodestar/types": "^1.52.0-dev.a3cc0e0e58",
|
|
64
|
+
"libp2p": "3.5.2"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"libp2p": "^3.
|
|
67
|
+
"libp2p": "^3.5.2"
|
|
68
68
|
},
|
|
69
69
|
"keywords": [
|
|
70
70
|
"ethereum",
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"reqresp",
|
|
75
75
|
"blockchain"
|
|
76
76
|
],
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "8e7cbc66e703fda1944c4c0a381688dc1fc61fb8"
|
|
78
78
|
}
|
package/src/response/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {Metrics} from "../metrics.js";
|
|
|
7
7
|
import {ReqRespRateLimiter} from "../rate_limiter/ReqRespRateLimiter.js";
|
|
8
8
|
import {RequestError, RequestErrorCode} from "../request/errors.js";
|
|
9
9
|
import {Protocol, ReqRespRequest} from "../types.js";
|
|
10
|
-
import {prettyPrintPeerId, sendChunks} from "../utils/index.js";
|
|
10
|
+
import {STREAM_DRAIN_TIMEOUT_MS, prettyPrintPeerId, sendChunks} from "../utils/index.js";
|
|
11
11
|
import {ResponseError} from "./errors.js";
|
|
12
12
|
|
|
13
13
|
export {ResponseError};
|
|
@@ -118,9 +118,12 @@ export async function handleRequest({
|
|
|
118
118
|
if (streamSendError) {
|
|
119
119
|
stream.abort(streamSendError);
|
|
120
120
|
} else {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
// close() waits for the stream to drain, bound it the same way as sendChunks
|
|
122
|
+
await withTimeout((timeoutSignal) => stream.close({signal: timeoutSignal}), STREAM_DRAIN_TIMEOUT_MS).catch(
|
|
123
|
+
(e) => {
|
|
124
|
+
stream.abort(e as Error);
|
|
125
|
+
}
|
|
126
|
+
);
|
|
124
127
|
}
|
|
125
128
|
metrics?.incomingClosedStreams.inc({method: protocol.method});
|
|
126
129
|
}
|
package/src/utils/stream.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import type {Stream} from "@libp2p/interface";
|
|
2
|
-
import {ByteStream} from "@libp2p/utils";
|
|
1
|
+
import type {Stream, StreamCloseEvent} from "@libp2p/interface";
|
|
2
|
+
import {ByteStream, StreamClosedError} from "@libp2p/utils";
|
|
3
3
|
import {Uint8ArrayList} from "uint8arraylist";
|
|
4
|
-
import {ErrorAborted} from "@lodestar/utils";
|
|
4
|
+
import {ErrorAborted, withTimeout} from "@lodestar/utils";
|
|
5
|
+
|
|
6
|
+
/** Max time a write waits for the stream to drain, so a peer that stops reading cannot hold the stream open */
|
|
7
|
+
export const STREAM_DRAIN_TIMEOUT_MS = 10_000;
|
|
8
|
+
|
|
9
|
+
// Wait for the stream to drain only once this many bytes are buffered, so small chunks coalesce. QUIC
|
|
10
|
+
// signals backpressure (send returns false, writableNeedsDrain) after every write even when it accepted
|
|
11
|
+
// the bytes and nothing is buffered; gating on buffered bytes avoids a drain round trip per chunk there
|
|
12
|
+
// while still bounding the buffer for a peer that reads slowly over a muxer with a per-stream write queue.
|
|
13
|
+
export const STREAM_WRITE_BUFFER_HIGH_WATER_MARK = 256 * 1024;
|
|
5
14
|
|
|
6
15
|
export async function sendChunks(
|
|
7
16
|
stream: Stream,
|
|
@@ -13,12 +22,46 @@ export async function sendChunks(
|
|
|
13
22
|
throw new ErrorAborted("sendChunks");
|
|
14
23
|
}
|
|
15
24
|
|
|
16
|
-
|
|
17
|
-
|
|
25
|
+
stream.send(chunk);
|
|
26
|
+
|
|
27
|
+
while (stream.writeBufferLength >= STREAM_WRITE_BUFFER_HIGH_WATER_MARK) {
|
|
28
|
+
await withTimeout(
|
|
29
|
+
(timeoutAndParentSignal) => waitForDrain(stream, timeoutAndParentSignal),
|
|
30
|
+
STREAM_DRAIN_TIMEOUT_MS,
|
|
31
|
+
signal
|
|
32
|
+
);
|
|
18
33
|
}
|
|
19
34
|
}
|
|
20
35
|
}
|
|
21
36
|
|
|
37
|
+
// Not `stream.onDrain()`: libp2p never resets its drain promise once resolved, so every later call resolves
|
|
38
|
+
// immediately and the rest of the response is buffered in memory regardless of how slowly the peer reads
|
|
39
|
+
function waitForDrain(stream: Stream, signal?: AbortSignal): Promise<void> {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
const cleanup = (): void => {
|
|
42
|
+
stream.removeEventListener("drain", onDrain);
|
|
43
|
+
stream.removeEventListener("close", onClose);
|
|
44
|
+
signal?.removeEventListener("abort", onAbort);
|
|
45
|
+
};
|
|
46
|
+
const onDrain = (): void => {
|
|
47
|
+
cleanup();
|
|
48
|
+
resolve();
|
|
49
|
+
};
|
|
50
|
+
const onClose = (evt: StreamCloseEvent): void => {
|
|
51
|
+
cleanup();
|
|
52
|
+
reject(evt.error ?? new StreamClosedError());
|
|
53
|
+
};
|
|
54
|
+
const onAbort = (): void => {
|
|
55
|
+
cleanup();
|
|
56
|
+
reject(new ErrorAborted("sendChunks"));
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
stream.addEventListener("drain", onDrain);
|
|
60
|
+
stream.addEventListener("close", onClose);
|
|
61
|
+
signal?.addEventListener("abort", onAbort);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
22
65
|
export function drainByteStream(bytes: ByteStream<Stream>): Uint8Array | undefined {
|
|
23
66
|
const readBuffer = (
|
|
24
67
|
bytes as unknown as {
|