@lodestar/beacon-node 1.52.0-dev.f2144dd2ac → 2.0.0-rc.0
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/api/impl/beacon/blocks/index.d.ts.map +1 -1
- package/lib/api/impl/beacon/blocks/index.js +18 -5
- package/lib/api/impl/beacon/blocks/index.js.map +1 -1
- package/lib/api/impl/beacon/pool/index.d.ts.map +1 -1
- package/lib/api/impl/beacon/pool/index.js +3 -1
- package/lib/api/impl/beacon/pool/index.js.map +1 -1
- package/lib/api/impl/beacon/state/utils.d.ts.map +1 -1
- package/lib/api/impl/beacon/state/utils.js +2 -1
- package/lib/api/impl/beacon/state/utils.js.map +1 -1
- package/lib/api/impl/debug/index.d.ts.map +1 -1
- package/lib/api/impl/debug/index.js +6 -2
- package/lib/api/impl/debug/index.js.map +1 -1
- package/lib/api/impl/validator/index.d.ts.map +1 -1
- package/lib/api/impl/validator/index.js +13 -4
- package/lib/api/impl/validator/index.js.map +1 -1
- package/lib/chain/ColumnReconstructionTracker.d.ts +4 -0
- package/lib/chain/ColumnReconstructionTracker.d.ts.map +1 -1
- package/lib/chain/ColumnReconstructionTracker.js +4 -0
- package/lib/chain/ColumnReconstructionTracker.js.map +1 -1
- package/lib/chain/archiveStore/historicalState/getHistoricalState.d.ts.map +1 -1
- package/lib/chain/archiveStore/historicalState/getHistoricalState.js +7 -0
- package/lib/chain/archiveStore/historicalState/getHistoricalState.js.map +1 -1
- package/lib/chain/archiveStore/utils/pruneHistory.d.ts.map +1 -1
- package/lib/chain/archiveStore/utils/pruneHistory.js +8 -4
- package/lib/chain/archiveStore/utils/pruneHistory.js.map +1 -1
- package/lib/chain/blocks/importExecutionPayload.d.ts +1 -1
- package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -1
- package/lib/chain/blocks/importExecutionPayload.js +9 -3
- package/lib/chain/blocks/importExecutionPayload.js.map +1 -1
- package/lib/chain/blocks/index.d.ts.map +1 -1
- package/lib/chain/blocks/index.js +3 -17
- package/lib/chain/blocks/index.js.map +1 -1
- package/lib/chain/chain.d.ts +6 -5
- package/lib/chain/chain.d.ts.map +1 -1
- package/lib/chain/chain.js +62 -20
- package/lib/chain/chain.js.map +1 -1
- package/lib/chain/errors/attestationError.d.ts +15 -1
- package/lib/chain/errors/attestationError.d.ts.map +1 -1
- package/lib/chain/errors/attestationError.js +8 -0
- package/lib/chain/errors/attestationError.js.map +1 -1
- package/lib/chain/errors/executionPayloadEnvelope.d.ts +48 -0
- package/lib/chain/errors/executionPayloadEnvelope.d.ts.map +1 -1
- package/lib/chain/errors/executionPayloadEnvelope.js +7 -0
- package/lib/chain/errors/executionPayloadEnvelope.js.map +1 -1
- package/lib/chain/interface.d.ts +1 -1
- package/lib/chain/interface.d.ts.map +1 -1
- package/lib/chain/lightClient/index.d.ts +13 -6
- package/lib/chain/lightClient/index.d.ts.map +1 -1
- package/lib/chain/lightClient/index.js +95 -115
- package/lib/chain/lightClient/index.js.map +1 -1
- package/lib/chain/opPools/opPool.js +6 -6
- package/lib/chain/opPools/opPool.js.map +1 -1
- package/lib/chain/seenCache/seenAttesters.d.ts +1 -0
- package/lib/chain/seenCache/seenAttesters.d.ts.map +1 -1
- package/lib/chain/seenCache/seenAttesters.js +1 -1
- package/lib/chain/seenCache/seenAttesters.js.map +1 -1
- package/lib/chain/seenCache/seenBlockAttesters.d.ts +8 -2
- package/lib/chain/seenCache/seenBlockAttesters.d.ts.map +1 -1
- package/lib/chain/seenCache/seenBlockAttesters.js +43 -6
- package/lib/chain/seenCache/seenBlockAttesters.js.map +1 -1
- package/lib/chain/validation/aggregateAndProof.d.ts.map +1 -1
- package/lib/chain/validation/aggregateAndProof.js +24 -7
- package/lib/chain/validation/aggregateAndProof.js.map +1 -1
- package/lib/chain/validation/attestation.d.ts.map +1 -1
- package/lib/chain/validation/attestation.js +32 -16
- package/lib/chain/validation/attestation.js.map +1 -1
- package/lib/chain/validation/block.d.ts.map +1 -1
- package/lib/chain/validation/block.js +9 -1
- package/lib/chain/validation/block.js.map +1 -1
- package/lib/chain/validation/executionPayloadBid.d.ts.map +1 -1
- package/lib/chain/validation/executionPayloadBid.js +1 -29
- package/lib/chain/validation/executionPayloadBid.js.map +1 -1
- package/lib/chain/validation/executionPayloadEnvelope.d.ts.map +1 -1
- package/lib/chain/validation/executionPayloadEnvelope.js +79 -4
- package/lib/chain/validation/executionPayloadEnvelope.js.map +1 -1
- package/lib/chain/validation/payloadAttestationMessage.js +9 -0
- package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
- package/lib/chain/validatorMonitor.js +43 -43
- package/lib/chain/validatorMonitor.js.map +1 -1
- package/lib/network/gossip/gossipsub.js +2 -2
- package/lib/network/gossip/gossipsub.js.map +1 -1
- package/lib/network/gossip/topic.d.ts +1 -7
- package/lib/network/gossip/topic.d.ts.map +1 -1
- package/lib/network/gossip/topic.js +0 -13
- package/lib/network/gossip/topic.js.map +1 -1
- package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
- package/lib/network/processor/gossipHandlers.js +19 -6
- package/lib/network/processor/gossipHandlers.js.map +1 -1
- package/lib/network/processor/index.d.ts.map +1 -1
- package/lib/network/processor/index.js +4 -2
- package/lib/network/processor/index.js.map +1 -1
- package/lib/sync/range/batch.d.ts +6 -0
- package/lib/sync/range/batch.d.ts.map +1 -1
- package/lib/sync/range/batch.js +17 -0
- package/lib/sync/range/batch.js.map +1 -1
- package/lib/sync/range/chain.d.ts.map +1 -1
- package/lib/sync/range/chain.js +8 -1
- package/lib/sync/range/chain.js.map +1 -1
- package/lib/sync/range/range.d.ts.map +1 -1
- package/lib/sync/range/range.js +43 -3
- package/lib/sync/range/range.js.map +1 -1
- package/lib/sync/types.d.ts +12 -1
- package/lib/sync/types.d.ts.map +1 -1
- package/lib/sync/types.js +2 -0
- package/lib/sync/types.js.map +1 -1
- package/lib/sync/unknownBlock.d.ts +28 -1
- package/lib/sync/unknownBlock.d.ts.map +1 -1
- package/lib/sync/unknownBlock.js +256 -24
- package/lib/sync/unknownBlock.js.map +1 -1
- package/lib/sync/utils/downloadByRange.d.ts +14 -1
- package/lib/sync/utils/downloadByRange.d.ts.map +1 -1
- package/lib/sync/utils/downloadByRange.js +12 -2
- package/lib/sync/utils/downloadByRange.js.map +1 -1
- package/lib/sync/utils/downloadByRoot.d.ts +24 -2
- package/lib/sync/utils/downloadByRoot.d.ts.map +1 -1
- package/lib/sync/utils/downloadByRoot.js +43 -1
- package/lib/sync/utils/downloadByRoot.js.map +1 -1
- package/lib/util/eventLoop.d.ts +3 -3
- package/lib/util/eventLoop.d.ts.map +1 -1
- package/lib/util/eventLoop.js +3 -3
- package/lib/util/eventLoop.js.map +1 -1
- package/package.json +18 -18
- package/src/api/impl/beacon/blocks/index.ts +20 -5
- package/src/api/impl/beacon/pool/index.ts +9 -7
- package/src/api/impl/beacon/state/utils.ts +2 -1
- package/src/api/impl/debug/index.ts +6 -2
- package/src/api/impl/validator/index.ts +15 -5
- package/src/chain/ColumnReconstructionTracker.ts +5 -0
- package/src/chain/archiveStore/historicalState/getHistoricalState.ts +9 -0
- package/src/chain/archiveStore/utils/pruneHistory.ts +9 -4
- package/src/chain/blocks/importExecutionPayload.ts +9 -2
- package/src/chain/blocks/index.ts +3 -31
- package/src/chain/chain.ts +95 -37
- package/src/chain/errors/attestationError.ts +11 -1
- package/src/chain/errors/executionPayloadEnvelope.ts +50 -0
- package/src/chain/interface.ts +1 -1
- package/src/chain/lightClient/index.ts +108 -142
- package/src/chain/opPools/opPool.ts +6 -6
- package/src/chain/seenCache/seenAttesters.ts +1 -1
- package/src/chain/seenCache/seenBlockAttesters.ts +48 -6
- package/src/chain/validation/aggregateAndProof.ts +24 -9
- package/src/chain/validation/attestation.ts +31 -20
- package/src/chain/validation/block.ts +9 -1
- package/src/chain/validation/executionPayloadBid.ts +1 -31
- package/src/chain/validation/executionPayloadEnvelope.ts +87 -3
- package/src/chain/validation/payloadAttestationMessage.ts +10 -0
- package/src/chain/validatorMonitor.ts +43 -43
- package/src/network/gossip/gossipsub.ts +2 -2
- package/src/network/gossip/topic.ts +1 -14
- package/src/network/processor/gossipHandlers.ts +25 -13
- package/src/network/processor/index.ts +7 -2
- package/src/sync/range/batch.ts +20 -0
- package/src/sync/range/chain.ts +8 -0
- package/src/sync/range/range.ts +56 -3
- package/src/sync/types.ts +11 -0
- package/src/sync/unknownBlock.ts +295 -24
- package/src/sync/utils/downloadByRange.ts +29 -3
- package/src/sync/utils/downloadByRoot.ts +68 -2
- package/src/util/eventLoop.ts +3 -3
package/src/sync/range/range.ts
CHANGED
|
@@ -3,13 +3,20 @@ import {StrictEventEmitter} from "strict-event-emitter-types";
|
|
|
3
3
|
import {BeaconConfig} from "@lodestar/config";
|
|
4
4
|
import {IBeaconStateViewGloas, computeStartSlotAtEpoch, isStatePostGloas} from "@lodestar/state-transition";
|
|
5
5
|
import {Epoch, Status, fulu, ssz} from "@lodestar/types";
|
|
6
|
-
import {Logger, byteArrayEquals, prettyPrintIndices, toRootHex} from "@lodestar/utils";
|
|
6
|
+
import {Logger, byteArrayEquals, prettyPrintIndices, toHex, toRootHex} from "@lodestar/utils";
|
|
7
7
|
import {IBlockInput} from "../../chain/blocks/blockInput/types.js";
|
|
8
8
|
import {PayloadError, PayloadErrorCode} from "../../chain/blocks/importExecutionPayload.js";
|
|
9
9
|
import {AttestationImportOpt, ImportBlockOpts} from "../../chain/blocks/index.js";
|
|
10
10
|
import {assertLinearChainSegment} from "../../chain/blocks/utils/chainSegment.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
BlockError,
|
|
13
|
+
BlockErrorCode,
|
|
14
|
+
BlockGossipError,
|
|
15
|
+
DataColumnSidecarErrorCode,
|
|
16
|
+
DataColumnSidecarValidationError,
|
|
17
|
+
} from "../../chain/errors/index.js";
|
|
12
18
|
import {IBeaconChain} from "../../chain/index.js";
|
|
19
|
+
import {verifyBlockProposerSignature} from "../../chain/validation/block.js";
|
|
13
20
|
import {Metrics} from "../../metrics/index.js";
|
|
14
21
|
import {INetwork} from "../../network/index.js";
|
|
15
22
|
import {PeerIdStr} from "../../util/peerId.js";
|
|
@@ -291,7 +298,7 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
|
|
|
291
298
|
...requests,
|
|
292
299
|
});
|
|
293
300
|
const {responses, payloadEnvelopes: downloadedPayloadEnvelopes} = result;
|
|
294
|
-
const {blocks, payloadEnvelopes} = cacheByRangeResponses({
|
|
301
|
+
const {blocks, payloadEnvelopes, signatureMismatch} = cacheByRangeResponses({
|
|
295
302
|
cache: this.chain.seenBlockInputCache,
|
|
296
303
|
seenPayloadEnvelopeInputCache: this.chain.seenPayloadEnvelopeInputCache,
|
|
297
304
|
peerIdStr: peer.peerId,
|
|
@@ -304,6 +311,52 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
|
|
|
304
311
|
seenTimestampSec: Date.now() / 1000,
|
|
305
312
|
});
|
|
306
313
|
|
|
314
|
+
if (signatureMismatch !== null) {
|
|
315
|
+
const {blockInput, columnSidecar} = signatureMismatch;
|
|
316
|
+
const block = blockInput.getBlock();
|
|
317
|
+
let isValidBlockSignature: boolean | null = true;
|
|
318
|
+
try {
|
|
319
|
+
await verifyBlockProposerSignature(this.chain, block, blockInput.blockRootHex);
|
|
320
|
+
} catch (e) {
|
|
321
|
+
isValidBlockSignature =
|
|
322
|
+
e instanceof BlockGossipError && e.type.code === BlockErrorCode.PROPOSAL_SIGNATURE_INVALID ? false : null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (isValidBlockSignature) {
|
|
326
|
+
throw new DataColumnSidecarValidationError(
|
|
327
|
+
{
|
|
328
|
+
code: DataColumnSidecarErrorCode.INCORRECT_BLOCK,
|
|
329
|
+
slot: blockInput.slot,
|
|
330
|
+
columnIndex: columnSidecar.index,
|
|
331
|
+
expected: toHex(block.signature),
|
|
332
|
+
actual: toHex(columnSidecar.signedBlockHeader.signature),
|
|
333
|
+
},
|
|
334
|
+
"DataColumnSidecar header signature doesn't match block signature"
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Later downloads reuse a cached block, evict it and the other unverified blocks of a peer
|
|
339
|
+
// proven to serve invalid signatures
|
|
340
|
+
const blockPeer = isValidBlockSignature === false ? blockInput.getBlockSource().peerIdStr : undefined;
|
|
341
|
+
const cache = this.chain.seenBlockInputCache;
|
|
342
|
+
for (const b of blocks) {
|
|
343
|
+
const isSuspect = b === blockInput || (blockPeer !== undefined && b.getBlockSource().peerIdStr === blockPeer);
|
|
344
|
+
if (
|
|
345
|
+
isSuspect &&
|
|
346
|
+
cache.get(b.blockRootHex) === b &&
|
|
347
|
+
!cache.isVerifiedProposerSignature(b.slot, b.blockRootHex, b.getBlock().signature)
|
|
348
|
+
) {
|
|
349
|
+
cache.remove(b.blockRootHex);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
throw new DownloadByRangeError({
|
|
353
|
+
code: DownloadByRangeErrorCode.INVALID_CACHED_BLOCK,
|
|
354
|
+
slot: blockInput.slot,
|
|
355
|
+
blockRoot: blockInput.blockRootHex,
|
|
356
|
+
blockPeer,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
307
360
|
const segmentBlocks = blocks.filter((b) => b.hasBlock()).sort((a, b) => a.slot - b.slot);
|
|
308
361
|
const envelopeSlots = payloadEnvelopes
|
|
309
362
|
? Array.from(payloadEnvelopes.entries())
|
package/src/sync/types.ts
CHANGED
|
@@ -107,6 +107,8 @@ export enum DroppedItemReason {
|
|
|
107
107
|
invalidSignature = "invalid_signature",
|
|
108
108
|
/** an unexpected / unhandled error code led to removal */
|
|
109
109
|
unknown = "unknown",
|
|
110
|
+
/** a block waiting on its parent was evicted because the peer that gossiped it exceeded its byte budget */
|
|
111
|
+
peerBudget = "peer_budget",
|
|
110
112
|
}
|
|
111
113
|
|
|
112
114
|
export enum PendingPayloadInputStatus {
|
|
@@ -123,6 +125,12 @@ export type PendingBlockInput = {
|
|
|
123
125
|
timeAddedSec: number;
|
|
124
126
|
timeSyncedSec?: number;
|
|
125
127
|
peerIdStrings: Set<string>;
|
|
128
|
+
/** Peer that gossiped a block retained for unknown parent sync */
|
|
129
|
+
retainedBy?: string;
|
|
130
|
+
retainedBytes?: number;
|
|
131
|
+
failedDownloads?: number;
|
|
132
|
+
/** A failed download is not retried before this time */
|
|
133
|
+
retryAtMs?: number;
|
|
126
134
|
};
|
|
127
135
|
|
|
128
136
|
export type PendingRootHex = {
|
|
@@ -131,6 +139,9 @@ export type PendingRootHex = {
|
|
|
131
139
|
timeAddedSec: number;
|
|
132
140
|
timeSyncedSec?: number;
|
|
133
141
|
peerIdStrings: Set<string>;
|
|
142
|
+
failedDownloads?: number;
|
|
143
|
+
/** A failed download is not retried before this time */
|
|
144
|
+
retryAtMs?: number;
|
|
134
145
|
};
|
|
135
146
|
|
|
136
147
|
export type BlockInputSyncCacheItem = PendingBlockInput | PendingRootHex;
|
package/src/sync/unknownBlock.ts
CHANGED
|
@@ -10,7 +10,14 @@ import {isBlockInputColumns} from "../chain/blocks/blockInput/blockInput.js";
|
|
|
10
10
|
import {BlockInputSource, IBlockInput} from "../chain/blocks/blockInput/types.js";
|
|
11
11
|
import {PayloadError, PayloadErrorCode} from "../chain/blocks/importExecutionPayload.js";
|
|
12
12
|
import {PayloadEnvelopeInput, PayloadEnvelopeInputSource} from "../chain/blocks/payloadEnvelopeInput/index.js";
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
BlockError,
|
|
15
|
+
BlockErrorCode,
|
|
16
|
+
DataColumnSidecarErrorCode,
|
|
17
|
+
DataColumnSidecarValidationError,
|
|
18
|
+
ExecutionPayloadEnvelopeError,
|
|
19
|
+
ExecutionPayloadEnvelopeErrorCode,
|
|
20
|
+
} from "../chain/errors/index.js";
|
|
14
21
|
import {ChainEvent, ChainEventData, IBeaconChain} from "../chain/index.js";
|
|
15
22
|
import {validateGloasBlockDataColumnSidecars} from "../chain/validation/dataColumnSidecar.js";
|
|
16
23
|
import {validateGossipExecutionPayloadEnvelope} from "../chain/validation/executionPayloadEnvelope.js";
|
|
@@ -47,13 +54,22 @@ import {
|
|
|
47
54
|
isPendingPayloadEnvelope,
|
|
48
55
|
isPendingPayloadInput,
|
|
49
56
|
} from "./types.js";
|
|
50
|
-
import {DownloadByRootError, downloadByRoot} from "./utils/downloadByRoot.js";
|
|
57
|
+
import {DownloadByRootError, DownloadByRootErrorCode, downloadByRoot} from "./utils/downloadByRoot.js";
|
|
51
58
|
import {getAllDescendantBlocks, getUnknownAndAncestorBlocks} from "./utils/pendingBlocksTree.js";
|
|
52
59
|
import {getRateLimitedUntilMs} from "./utils/rateLimit.js";
|
|
53
60
|
|
|
54
61
|
const MAX_ATTEMPTS_PER_BLOCK = 5;
|
|
55
62
|
const MAX_KNOWN_BAD_BLOCKS = 500;
|
|
56
63
|
const MAX_PENDING_BLOCKS = 100;
|
|
64
|
+
/**
|
|
65
|
+
* A failed download is retried after this delay, doubling per failure up to one slot.
|
|
66
|
+
*/
|
|
67
|
+
const DOWNLOAD_RETRY_BASE_MS = 1_000;
|
|
68
|
+
/**
|
|
69
|
+
* Requests per peer per window, half the BeaconBlocksByRoot quota peers enforce, they disconnect a peer that exceeds it.
|
|
70
|
+
*/
|
|
71
|
+
const MAX_REQUESTS_PER_PEER = 64;
|
|
72
|
+
const REQUESTS_PER_PEER_WINDOW_MS = 10_000;
|
|
57
73
|
/**
|
|
58
74
|
* A pending payload without slot can only stay at most 2 epochs.
|
|
59
75
|
*/
|
|
@@ -127,6 +143,8 @@ export class BlockInputSync {
|
|
|
127
143
|
private subscribedToNetworkEvents = false;
|
|
128
144
|
private peerBalancer: UnknownBlockPeerBalancer;
|
|
129
145
|
private rateLimitBackoffTimeout: NodeJS.Timeout | undefined;
|
|
146
|
+
private downloadRetryTimeout: NodeJS.Timeout | undefined;
|
|
147
|
+
private downloadRetryAtMs: number | undefined;
|
|
130
148
|
/**
|
|
131
149
|
* Search unknown payload starting at PAYLOAD_DUE_BPS.
|
|
132
150
|
*/
|
|
@@ -178,6 +196,7 @@ export class BlockInputSync {
|
|
|
178
196
|
this.network.events.on(NetworkEvent.peerConnected, this.onPeerConnected);
|
|
179
197
|
this.network.events.on(NetworkEvent.peerDisconnected, this.onPeerDisconnected);
|
|
180
198
|
this.chain.clock.on(ClockEvent.slot, this.startPayloadPollAtSlot);
|
|
199
|
+
this.chain.clock.on(ClockEvent.slot, this.pruneUnresolvedBlocks);
|
|
181
200
|
|
|
182
201
|
// Seed the balancer with peers that connected before we subscribed
|
|
183
202
|
for (const peerId of this.network.getConnectedPeers()) {
|
|
@@ -198,7 +217,9 @@ export class BlockInputSync {
|
|
|
198
217
|
unsubscribeFromNetwork(): void {
|
|
199
218
|
this.logger.verbose("BlockInputSync disabled.");
|
|
200
219
|
this.clearRateLimitBackoffTimer();
|
|
220
|
+
this.clearDownloadRetryTimer();
|
|
201
221
|
this.chain.clock.off(ClockEvent.slot, this.startPayloadPollAtSlot);
|
|
222
|
+
this.chain.clock.off(ClockEvent.slot, this.pruneUnresolvedBlocks);
|
|
202
223
|
this.payloadPollAbortController?.abort();
|
|
203
224
|
this.payloadPollAbortController = undefined;
|
|
204
225
|
this.deferredPayloadRoots.clear();
|
|
@@ -346,6 +367,9 @@ export class BlockInputSync {
|
|
|
346
367
|
this.addByRootHex(missingDependency.rootHex, data.peer);
|
|
347
368
|
}
|
|
348
369
|
this.addByBlockInput(data.blockInput, data.peer);
|
|
370
|
+
if (data.source === BlockInputSource.gossip) {
|
|
371
|
+
this.limitRetainedBlocks(data.blockInput, data.peer);
|
|
372
|
+
}
|
|
349
373
|
this.triggerUnknownBlockSearch();
|
|
350
374
|
this.metrics?.blockInputSync.requests.inc({type: PendingBlockType.UNKNOWN_PARENT});
|
|
351
375
|
this.metrics?.blockInputSync.source.inc({source: data.source});
|
|
@@ -376,7 +400,8 @@ export class BlockInputSync {
|
|
|
376
400
|
// because the payload at the finalized block is not finalized
|
|
377
401
|
const finalizedSlot = this.chain.forkChoice.getFinalizedBlock().slot;
|
|
378
402
|
const nowSec = Date.now() / 1000;
|
|
379
|
-
const
|
|
403
|
+
const slotDurationMs = this.config.getSlotDurationMs(this.config.getForkName(this.chain.clock.currentSlot));
|
|
404
|
+
const maxSecInCache = (PRUNE_UNRESOLVED_SLOT_EPOCHS * SLOTS_PER_EPOCH * slotDurationMs) / 1000;
|
|
380
405
|
|
|
381
406
|
let belowFinalizedPayloads = 0;
|
|
382
407
|
let agedOutPayloads = 0;
|
|
@@ -484,13 +509,7 @@ export class BlockInputSync {
|
|
|
484
509
|
pendingBlock.peerIdStrings.add(peerIdStr);
|
|
485
510
|
}
|
|
486
511
|
|
|
487
|
-
|
|
488
|
-
// Limit pending blocks to prevent DOS attacks that cause OOM
|
|
489
|
-
const prunedItemCount = pruneSetToMax(this.pendingBlocks, this.maxPendingBlocks);
|
|
490
|
-
if (prunedItemCount > 0) {
|
|
491
|
-
this.metrics?.blockInputSync.removedBlocks.inc({reason: DroppedItemReason.capacity}, prunedItemCount);
|
|
492
|
-
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
493
|
-
}
|
|
512
|
+
this.prunePendingBlocks();
|
|
494
513
|
return added;
|
|
495
514
|
};
|
|
496
515
|
|
|
@@ -526,15 +545,68 @@ export class BlockInputSync {
|
|
|
526
545
|
pendingBlock.peerIdStrings.add(peerIdStr);
|
|
527
546
|
}
|
|
528
547
|
|
|
529
|
-
|
|
530
|
-
// Limit pending blocks to prevent DOS attacks that cause OOM
|
|
531
|
-
const prunedItemCount = pruneSetToMax(this.pendingBlocks, this.maxPendingBlocks);
|
|
532
|
-
if (prunedItemCount > 0) {
|
|
533
|
-
this.metrics?.blockInputSync.removedBlocks.inc({reason: DroppedItemReason.capacity}, prunedItemCount);
|
|
534
|
-
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
535
|
-
}
|
|
548
|
+
this.prunePendingBlocks();
|
|
536
549
|
};
|
|
537
550
|
|
|
551
|
+
/**
|
|
552
|
+
* Limit pending blocks to prevent DOS attacks that cause OOM. Any gossip message with an unknown root adds a root-only
|
|
553
|
+
* entry before validation, so those nothing waits on are evicted before a block or a root a pending block waits on.
|
|
554
|
+
* A downloaded block is only checked against its root and proposer signature, not that the proposer is due at its
|
|
555
|
+
* slot, so this only applies while blocks and the roots they wait on fill at most half the cap, otherwise they could
|
|
556
|
+
* pin it and evict every new root on insertion.
|
|
557
|
+
*/
|
|
558
|
+
private prunePendingBlocks(): void {
|
|
559
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const awaitedRoots = new Set<RootHex>();
|
|
564
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
565
|
+
if (isPendingBlockInput(pendingBlock)) {
|
|
566
|
+
awaitedRoots.add(pendingBlock.blockInput.parentRootHex);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const isKept = (rootHex: RootHex, pendingBlock: BlockInputSyncCacheItem): boolean =>
|
|
570
|
+
isPendingBlockInput(pendingBlock) || awaitedRoots.has(rootHex);
|
|
571
|
+
let keptCount = 0;
|
|
572
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
573
|
+
if (isKept(rootHex, pendingBlock)) {
|
|
574
|
+
keptCount++;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
let prunedItemCount = 0;
|
|
579
|
+
if (keptCount <= this.maxPendingBlocks / 2) {
|
|
580
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
581
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
if (!isKept(rootHex, pendingBlock)) {
|
|
585
|
+
this.pendingBlocks.delete(rootHex);
|
|
586
|
+
prunedItemCount++;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
591
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
this.pendingBlocks.delete(rootHex);
|
|
595
|
+
prunedItemCount++;
|
|
596
|
+
// The block input cache is only trimmed on import, a block whose parent never resolves would stay there
|
|
597
|
+
if (
|
|
598
|
+
isPendingBlockInput(pendingBlock) &&
|
|
599
|
+
pendingBlock.status !== PendingBlockInputStatus.processing &&
|
|
600
|
+
this.getMissingBlockDependency(pendingBlock.blockInput).kind === "parentBlock"
|
|
601
|
+
) {
|
|
602
|
+
this.chain.seenBlockInputCache.remove(rootHex);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
this.metrics?.blockInputSync.removedBlocks.inc({reason: DroppedItemReason.capacity}, prunedItemCount);
|
|
607
|
+
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
608
|
+
}
|
|
609
|
+
|
|
538
610
|
private addByPayloadRootHex = (rootHex: RootHex, peerIdStr?: PeerIdStr, slot?: Slot): boolean => {
|
|
539
611
|
if (this.knownBadBlocks.has(rootHex)) {
|
|
540
612
|
this.logger.debug("Ignoring payload for known bad block root", {
|
|
@@ -665,6 +737,79 @@ export class BlockInputSync {
|
|
|
665
737
|
this.scheduleRateLimitBackoffRetry();
|
|
666
738
|
};
|
|
667
739
|
|
|
740
|
+
/**
|
|
741
|
+
* A peer can make us retain a signed block per validator key and slot until its parent resolves. Bound the bytes
|
|
742
|
+
* retained per peer, its oldest blocks still waiting on their parent are removed first.
|
|
743
|
+
*/
|
|
744
|
+
private limitRetainedBlocks(blockInput: IBlockInput, peerIdStr: PeerIdStr): void {
|
|
745
|
+
const pendingBlock = this.pendingBlocks.get(blockInput.blockRootHex);
|
|
746
|
+
if (pendingBlock === undefined || !isPendingBlockInput(pendingBlock) || pendingBlock.retainedBy !== undefined) {
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
pendingBlock.retainedBy = peerIdStr;
|
|
750
|
+
pendingBlock.retainedBytes = this.config
|
|
751
|
+
.getForkTypes(blockInput.slot)
|
|
752
|
+
.SignedBeaconBlock.value_serializedSize(blockInput.getBlock());
|
|
753
|
+
|
|
754
|
+
const retainedBlocks: PendingBlockInput[] = [];
|
|
755
|
+
let retainedBytes = 0;
|
|
756
|
+
for (const item of this.pendingBlocks.values()) {
|
|
757
|
+
if (isPendingBlockInput(item) && item.retainedBy === peerIdStr && this.isWaitingOnParent(item)) {
|
|
758
|
+
retainedBlocks.push(item);
|
|
759
|
+
retainedBytes += item.retainedBytes ?? 0;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
// One block of the max gossip size fits, honest blocks are far smaller
|
|
763
|
+
for (const item of retainedBlocks) {
|
|
764
|
+
if (retainedBytes <= this.config.MAX_PAYLOAD_SIZE) {
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
retainedBytes -= item.retainedBytes ?? 0;
|
|
768
|
+
this.removeUnresolvedBlock(item, DroppedItemReason.peerBudget);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Blocks are otherwise only pruned once finalization passes them, which never happens for a parent that does not resolve
|
|
774
|
+
*/
|
|
775
|
+
private pruneUnresolvedBlocks = (): void => {
|
|
776
|
+
const nowSec = Date.now() / 1000;
|
|
777
|
+
const slotDurationMs = this.config.getSlotDurationMs(this.config.getForkName(this.chain.clock.currentSlot));
|
|
778
|
+
const maxSecInCache = (PRUNE_UNRESOLVED_SLOT_EPOCHS * SLOTS_PER_EPOCH * slotDurationMs) / 1000;
|
|
779
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
780
|
+
if (
|
|
781
|
+
isPendingBlockInput(pendingBlock) &&
|
|
782
|
+
nowSec - pendingBlock.timeAddedSec > maxSecInCache &&
|
|
783
|
+
this.isWaitingOnParent(pendingBlock)
|
|
784
|
+
) {
|
|
785
|
+
this.removeUnresolvedBlock(pendingBlock, DroppedItemReason.agedOut);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
|
|
790
|
+
private isWaitingOnParent(pendingBlock: PendingBlockInput): boolean {
|
|
791
|
+
if (pendingBlock.status === PendingBlockInputStatus.processing) {
|
|
792
|
+
return false;
|
|
793
|
+
}
|
|
794
|
+
const {kind} = this.getMissingBlockDependency(pendingBlock.blockInput);
|
|
795
|
+
return kind === "parentBlock" || kind === "parentPayload";
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Removes only the block itself, its descendants request it again by root and its claimed ancestors stay cached
|
|
800
|
+
*/
|
|
801
|
+
private removeUnresolvedBlock(pendingBlock: PendingBlockInput, reason: DroppedItemReason): void {
|
|
802
|
+
const rootHex = pendingBlock.blockInput.blockRootHex;
|
|
803
|
+
this.pendingBlocks.delete(rootHex);
|
|
804
|
+
this.chain.seenBlockInputCache.remove(rootHex);
|
|
805
|
+
this.metrics?.blockInputSync.removedBlocks.inc({reason});
|
|
806
|
+
this.logger.debug("Removed unresolved BlockInputSync block", {
|
|
807
|
+
slot: pendingBlock.blockInput.slot,
|
|
808
|
+
root: rootHex,
|
|
809
|
+
reason,
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
|
|
668
813
|
/**
|
|
669
814
|
* Post-gloas, a locally complete block can still be blocked on its parent's execution payload lineage.
|
|
670
815
|
* Distinguish which dependency is missing so the scheduler can enqueue the right follow-up work.
|
|
@@ -813,8 +958,19 @@ export class BlockInputSync {
|
|
|
813
958
|
if (!hasConnectedPeers) {
|
|
814
959
|
this.logger.debug("No connected peers, skipping unknown block download.");
|
|
815
960
|
} else {
|
|
961
|
+
// Blocks and roots a pending block waits on are downloaded first, any gossip message with an unknown root adds
|
|
962
|
+
// another root, and they all share the per-peer request budget
|
|
963
|
+
const awaitedRoots = new Set<RootHex>();
|
|
964
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
965
|
+
if (isPendingBlockInput(pendingBlock)) {
|
|
966
|
+
awaitedRoots.add(pendingBlock.blockInput.parentRootHex);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
const isAwaited = (block: BlockInputSyncCacheItem): boolean =>
|
|
970
|
+
isPendingBlockInput(block) || awaitedRoots.has(block.rootHex);
|
|
971
|
+
|
|
816
972
|
// Most of the time there is exactly 1 unknown block
|
|
817
|
-
for (const block of unknowns) {
|
|
973
|
+
for (const block of [...unknowns.filter(isAwaited), ...unknowns.filter((block) => !isAwaited(block))]) {
|
|
818
974
|
this.downloadBlock(block).catch((e) => {
|
|
819
975
|
this.logger.debug("Unexpected error - downloadBlock", {root: getBlockInputSyncCacheItemRootHex(block)}, e);
|
|
820
976
|
});
|
|
@@ -897,6 +1053,31 @@ export class BlockInputSync {
|
|
|
897
1053
|
}
|
|
898
1054
|
}
|
|
899
1055
|
|
|
1056
|
+
private scheduleDownloadRetry(retryAtMs: number): void {
|
|
1057
|
+
if (this.downloadRetryAtMs !== undefined && this.downloadRetryAtMs <= retryAtMs) {
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
this.clearDownloadRetryTimer();
|
|
1062
|
+
this.downloadRetryAtMs = retryAtMs;
|
|
1063
|
+
this.downloadRetryTimeout = setTimeout(
|
|
1064
|
+
() => {
|
|
1065
|
+
this.downloadRetryTimeout = undefined;
|
|
1066
|
+
this.downloadRetryAtMs = undefined;
|
|
1067
|
+
this.triggerUnknownBlockSearch();
|
|
1068
|
+
},
|
|
1069
|
+
Math.max(0, retryAtMs - Date.now())
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
private clearDownloadRetryTimer(): void {
|
|
1074
|
+
if (this.downloadRetryTimeout !== undefined) {
|
|
1075
|
+
clearTimeout(this.downloadRetryTimeout);
|
|
1076
|
+
this.downloadRetryTimeout = undefined;
|
|
1077
|
+
}
|
|
1078
|
+
this.downloadRetryAtMs = undefined;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
900
1081
|
/**
|
|
901
1082
|
* Start this slot's payload poll: end the previous slot's loop, move roots it never got to
|
|
902
1083
|
* into pendingPayloads, then poll from PAYLOAD_DUE to the end of the slot.
|
|
@@ -1018,6 +1199,10 @@ export class BlockInputSync {
|
|
|
1018
1199
|
if (block.status !== PendingBlockInputStatus.pending) {
|
|
1019
1200
|
return;
|
|
1020
1201
|
}
|
|
1202
|
+
if (block.retryAtMs !== undefined && block.retryAtMs > Date.now()) {
|
|
1203
|
+
this.scheduleDownloadRetry(block.retryAtMs);
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
1021
1206
|
|
|
1022
1207
|
const rootHex = getBlockInputSyncCacheItemRootHex(block);
|
|
1023
1208
|
const blockSlot = getBlockInputSyncCacheItemSlot(block);
|
|
@@ -1098,6 +1283,7 @@ export class BlockInputSync {
|
|
|
1098
1283
|
const pendingBlock = this.pendingBlocks.get(rootHex);
|
|
1099
1284
|
if (pendingBlock) {
|
|
1100
1285
|
pendingBlock.status = PendingBlockInputStatus.pending;
|
|
1286
|
+
pendingBlock.retryAtMs = res.err.retryAt;
|
|
1101
1287
|
}
|
|
1102
1288
|
this.logger.debug("Deferring unknown block download due to peer rate limit", logCtx, res.err);
|
|
1103
1289
|
if (!this.scheduleRateLimitBackoffRetry() || res.err.retryAt <= Date.now()) {
|
|
@@ -1108,11 +1294,31 @@ export class BlockInputSync {
|
|
|
1108
1294
|
return;
|
|
1109
1295
|
}
|
|
1110
1296
|
|
|
1297
|
+
if (
|
|
1298
|
+
res.err instanceof DownloadByRootError &&
|
|
1299
|
+
res.err.type.code === DownloadByRootErrorCode.BELOW_FINALIZED_SLOT
|
|
1300
|
+
) {
|
|
1301
|
+
this.logger.debug("Downloaded block is before finalized slot", logCtx, res.err);
|
|
1302
|
+
this.removeAllDescendants(block, DroppedItemReason.belowFinalized);
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1111
1306
|
this.metrics?.blockInputSync.downloadedBlocksError.inc();
|
|
1112
1307
|
this.logger.debug("Failed to download unknown block root, retained for retry", logCtx, res.err);
|
|
1113
1308
|
const pendingBlock = this.pendingBlocks.get(rootHex);
|
|
1114
|
-
|
|
1309
|
+
// A partial download replaces the entry with a pending one that does not carry the failure count
|
|
1310
|
+
if (
|
|
1311
|
+
pendingBlock &&
|
|
1312
|
+
(pendingBlock.status === PendingBlockInputStatus.fetching ||
|
|
1313
|
+
pendingBlock.status === PendingBlockInputStatus.pending)
|
|
1314
|
+
) {
|
|
1115
1315
|
pendingBlock.status = PendingBlockInputStatus.pending;
|
|
1316
|
+
// Not retried on every search, any gossip message with an unknown root triggers one
|
|
1317
|
+
pendingBlock.failedDownloads = (block.failedDownloads ?? 0) + 1;
|
|
1318
|
+
const slotDurationMs = this.config.getSlotDurationMs(this.config.getForkName(this.chain.clock.currentSlot));
|
|
1319
|
+
pendingBlock.retryAtMs =
|
|
1320
|
+
Date.now() + Math.min(DOWNLOAD_RETRY_BASE_MS * 2 ** (pendingBlock.failedDownloads - 1), slotDurationMs);
|
|
1321
|
+
this.scheduleDownloadRetry(pendingBlock.retryAtMs);
|
|
1116
1322
|
}
|
|
1117
1323
|
}
|
|
1118
1324
|
}
|
|
@@ -1304,7 +1510,7 @@ export class BlockInputSync {
|
|
|
1304
1510
|
const pendingPayloadByRoot: PendingPayloadRootHex = {
|
|
1305
1511
|
status: PendingPayloadInputStatus.pending,
|
|
1306
1512
|
rootHex,
|
|
1307
|
-
slot:
|
|
1513
|
+
slot: payloadInput.slot,
|
|
1308
1514
|
timeAddedSec: pendingPayload.timeAddedSec,
|
|
1309
1515
|
peerIdStrings: new Set(pendingPayload.peerIdStrings),
|
|
1310
1516
|
};
|
|
@@ -1670,6 +1876,27 @@ export class BlockInputSync {
|
|
|
1670
1876
|
} else {
|
|
1671
1877
|
excludedPeers.add(peerId);
|
|
1672
1878
|
}
|
|
1879
|
+
|
|
1880
|
+
if (e instanceof ExecutionPayloadEnvelopeError) {
|
|
1881
|
+
switch (e.type.code) {
|
|
1882
|
+
// Caused by local state, the downloaded envelope may still be valid
|
|
1883
|
+
case ExecutionPayloadEnvelopeErrorCode.BLOCK_ROOT_UNKNOWN:
|
|
1884
|
+
case ExecutionPayloadEnvelopeErrorCode.ENVELOPE_ALREADY_KNOWN:
|
|
1885
|
+
case ExecutionPayloadEnvelopeErrorCode.PAYLOAD_ENVELOPE_INPUT_MISSING:
|
|
1886
|
+
case ExecutionPayloadEnvelopeErrorCode.UNKNOWN_BLOCK_STATE:
|
|
1887
|
+
break;
|
|
1888
|
+
// Every copy fails once the block is finalized, only a wrong slot makes the served copy invalid
|
|
1889
|
+
case ExecutionPayloadEnvelopeErrorCode.BELONG_TO_FINALIZED_BLOCK:
|
|
1890
|
+
if (envelope?.message.payload.slotNumber !== payloadInput?.slot) {
|
|
1891
|
+
envelope = payloadInput?.hasPayloadEnvelope() ? payloadInput.getPayloadEnvelope() : undefined;
|
|
1892
|
+
}
|
|
1893
|
+
break;
|
|
1894
|
+
// The served copy is invalid, take the envelope from payloadInput or another peer on the next attempt
|
|
1895
|
+
default:
|
|
1896
|
+
envelope = payloadInput?.hasPayloadEnvelope() ? payloadInput.getPayloadEnvelope() : undefined;
|
|
1897
|
+
break;
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1673
1900
|
} finally {
|
|
1674
1901
|
this.peerBalancer.onRequestCompleted(peerId);
|
|
1675
1902
|
}
|
|
@@ -1831,8 +2058,12 @@ export class BlockInputSync {
|
|
|
1831
2058
|
for (const warning of warnings) {
|
|
1832
2059
|
this.logger.debug("BlockInputSync.fetchBlockInput: downloaded with warning", logCtx, warning);
|
|
1833
2060
|
this.metrics?.blockInputSync.downloadByRoot.warn.inc({code: warning.type.code, client: peerClient});
|
|
2061
|
+
// A partial response is legal, columns that were not requested are not
|
|
2062
|
+
if (warning.type.code === DownloadByRootErrorCode.EXTRA_SIDECAR_RECEIVED) {
|
|
2063
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, warning.type.code);
|
|
2064
|
+
excludedPeers.add(peerId);
|
|
2065
|
+
}
|
|
1834
2066
|
}
|
|
1835
|
-
// TODO: penalize peer?
|
|
1836
2067
|
}
|
|
1837
2068
|
} catch (e) {
|
|
1838
2069
|
this.logger.debug(
|
|
@@ -1841,10 +2072,36 @@ export class BlockInputSync {
|
|
|
1841
2072
|
e as Error
|
|
1842
2073
|
);
|
|
1843
2074
|
const downloadByRootMetrics = this.metrics?.blockInputSync.downloadByRoot;
|
|
1844
|
-
// TODO: penalize peer?
|
|
1845
2075
|
if (e instanceof DownloadByRootError) {
|
|
1846
2076
|
const errorCode = e.type.code;
|
|
1847
2077
|
downloadByRootMetrics?.error.inc({code: errorCode, client: peerClient});
|
|
2078
|
+
if (errorCode === DownloadByRootErrorCode.BELOW_FINALIZED_SLOT) {
|
|
2079
|
+
throw e;
|
|
2080
|
+
}
|
|
2081
|
+
// Low as in range sync, not Fatal as peers may serve unverified copies of a root with a bad signature
|
|
2082
|
+
if (
|
|
2083
|
+
errorCode === DownloadByRootErrorCode.MISMATCH_BLOCK_ROOT ||
|
|
2084
|
+
errorCode === DownloadByRootErrorCode.PROPOSAL_SIGNATURE_INVALID ||
|
|
2085
|
+
errorCode === DownloadByRootErrorCode.UNKNOWN_PROPOSER
|
|
2086
|
+
) {
|
|
2087
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, errorCode);
|
|
2088
|
+
}
|
|
2089
|
+
excludedPeers.add(peerId);
|
|
2090
|
+
} else if (e instanceof DataColumnSidecarValidationError) {
|
|
2091
|
+
const errorCode = e.type.code;
|
|
2092
|
+
downloadByRootMetrics?.error.inc({code: errorCode, client: peerClient});
|
|
2093
|
+
// Not PROPOSAL_SIGNATURE_INVALID or INCORRECT_HEADER_ROOT, which compares signatures too: the spec does not
|
|
2094
|
+
// require verifying the header signature of req/resp columns, so honest peers may serve a bad one
|
|
2095
|
+
switch (errorCode) {
|
|
2096
|
+
case DataColumnSidecarErrorCode.INCORRECT_BLOCK:
|
|
2097
|
+
case DataColumnSidecarErrorCode.INVALID_INDEX:
|
|
2098
|
+
case DataColumnSidecarErrorCode.INCORRECT_CELL_COUNT:
|
|
2099
|
+
case DataColumnSidecarErrorCode.INCORRECT_KZG_COMMITMENTS_COUNT:
|
|
2100
|
+
case DataColumnSidecarErrorCode.INCORRECT_KZG_PROOF_COUNT:
|
|
2101
|
+
case DataColumnSidecarErrorCode.INCLUSION_PROOF_INVALID:
|
|
2102
|
+
case DataColumnSidecarErrorCode.INVALID_KZG_PROOF_BATCH:
|
|
2103
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, errorCode);
|
|
2104
|
+
}
|
|
1848
2105
|
excludedPeers.add(peerId);
|
|
1849
2106
|
} else if (e instanceof RequestError) {
|
|
1850
2107
|
// should look into req_resp metrics in this case
|
|
@@ -2050,11 +2307,13 @@ export class UnknownBlockPeerBalancer {
|
|
|
2050
2307
|
readonly peersMeta: Map<PeerIdStr, PeerSyncMeta>;
|
|
2051
2308
|
readonly activeRequests: Map<PeerIdStr, number>;
|
|
2052
2309
|
readonly rateLimitedUntilByPeer: Map<PeerIdStr, number>;
|
|
2310
|
+
readonly requestTimesByPeer: Map<PeerIdStr, number[]>;
|
|
2053
2311
|
|
|
2054
2312
|
constructor() {
|
|
2055
2313
|
this.peersMeta = new Map();
|
|
2056
2314
|
this.activeRequests = new Map();
|
|
2057
2315
|
this.rateLimitedUntilByPeer = new Map();
|
|
2316
|
+
this.requestTimesByPeer = new Map();
|
|
2058
2317
|
}
|
|
2059
2318
|
|
|
2060
2319
|
/** Trigger on each peer re-status */
|
|
@@ -2070,6 +2329,7 @@ export class UnknownBlockPeerBalancer {
|
|
|
2070
2329
|
this.peersMeta.delete(peerId);
|
|
2071
2330
|
this.activeRequests.delete(peerId);
|
|
2072
2331
|
this.rateLimitedUntilByPeer.delete(peerId);
|
|
2332
|
+
this.requestTimesByPeer.delete(peerId);
|
|
2073
2333
|
}
|
|
2074
2334
|
|
|
2075
2335
|
/** Reset all tracked peer state, e.g. when BlockInputSync unsubscribes from the network. */
|
|
@@ -2077,6 +2337,7 @@ export class UnknownBlockPeerBalancer {
|
|
|
2077
2337
|
this.peersMeta.clear();
|
|
2078
2338
|
this.activeRequests.clear();
|
|
2079
2339
|
this.rateLimitedUntilByPeer.clear();
|
|
2340
|
+
this.requestTimesByPeer.clear();
|
|
2080
2341
|
}
|
|
2081
2342
|
|
|
2082
2343
|
onRateLimited(peerId: PeerIdStr, rateLimitedUntilMs: number): void {
|
|
@@ -2143,7 +2404,7 @@ export class UnknownBlockPeerBalancer {
|
|
|
2143
2404
|
);
|
|
2144
2405
|
|
|
2145
2406
|
const bestPeerId = sortedEligiblePeers[0];
|
|
2146
|
-
this.onRequest(bestPeerId);
|
|
2407
|
+
this.onRequest(bestPeerId, now);
|
|
2147
2408
|
return this.peersMeta.get(bestPeerId) ?? null;
|
|
2148
2409
|
}
|
|
2149
2410
|
|
|
@@ -2151,8 +2412,18 @@ export class UnknownBlockPeerBalancer {
|
|
|
2151
2412
|
* Consumers don't need to call this method directly, it is called internally by bestPeer*() methods
|
|
2152
2413
|
* make this public for testing
|
|
2153
2414
|
*/
|
|
2154
|
-
onRequest(peerId: PeerIdStr): void {
|
|
2415
|
+
onRequest(peerId: PeerIdStr, now = Date.now()): void {
|
|
2155
2416
|
this.activeRequests.set(peerId, (this.activeRequests.get(peerId) ?? 0) + 1);
|
|
2417
|
+
|
|
2418
|
+
const requestTimes = (this.requestTimesByPeer.get(peerId) ?? []).filter(
|
|
2419
|
+
(time) => now - time < REQUESTS_PER_PEER_WINDOW_MS
|
|
2420
|
+
);
|
|
2421
|
+
requestTimes.push(now);
|
|
2422
|
+
this.requestTimesByPeer.set(peerId, requestTimes);
|
|
2423
|
+
if (requestTimes.length >= MAX_REQUESTS_PER_PEER) {
|
|
2424
|
+
const budgetUntilMs = requestTimes[0] + REQUESTS_PER_PEER_WINDOW_MS;
|
|
2425
|
+
this.onRateLimited(peerId, Math.max(this.rateLimitedUntilByPeer.get(peerId) ?? 0, budgetUntilMs));
|
|
2426
|
+
}
|
|
2156
2427
|
}
|
|
2157
2428
|
|
|
2158
2429
|
/**
|