@lodestar/beacon-node 1.52.0-dev.b1d1e990d9 → 1.52.0-dev.b59a889970
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/validator/index.d.ts.map +1 -1
- package/lib/api/impl/validator/index.js +13 -5
- 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.js +0 -1
- package/lib/chain/blocks/index.js.map +1 -1
- package/lib/chain/chain.d.ts +3 -2
- package/lib/chain/chain.d.ts.map +1 -1
- package/lib/chain/chain.js +21 -2
- 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/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/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/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/network/gossip/gossipsub.d.ts +1 -0
- package/lib/network/gossip/gossipsub.d.ts.map +1 -1
- package/lib/network/gossip/gossipsub.js +5 -2
- package/lib/network/gossip/gossipsub.js.map +1 -1
- package/lib/network/network.d.ts.map +1 -1
- package/lib/network/network.js +6 -1
- package/lib/network/network.js.map +1 -1
- package/lib/network/peers/peerManager.d.ts.map +1 -1
- package/lib/network/peers/peerManager.js +12 -1
- package/lib/network/peers/peerManager.js.map +1 -1
- package/lib/network/peers/utils/prioritizePeers.d.ts +2 -0
- package/lib/network/peers/utils/prioritizePeers.d.ts.map +1 -1
- package/lib/network/peers/utils/prioritizePeers.js +11 -2
- package/lib/network/peers/utils/prioritizePeers.js.map +1 -1
- package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
- package/lib/network/processor/gossipHandlers.js +15 -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 +294 -23
- 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 +48 -5
- 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 +26 -26
- package/src/api/impl/validator/index.ts +15 -6
- 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 +0 -1
- package/src/chain/chain.ts +26 -3
- package/src/chain/errors/attestationError.ts +11 -1
- package/src/chain/errors/executionPayloadEnvelope.ts +50 -0
- package/src/chain/lightClient/index.ts +108 -142
- 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/executionPayloadEnvelope.ts +87 -3
- package/src/network/gossip/gossipsub.ts +6 -2
- package/src/network/network.ts +6 -2
- package/src/network/peers/peerManager.ts +12 -1
- package/src/network/peers/utils/prioritizePeers.ts +14 -1
- package/src/network/processor/gossipHandlers.ts +15 -7
- 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 +338 -23
- package/src/sync/utils/downloadByRange.ts +29 -3
- package/src/sync/utils/downloadByRoot.ts +75 -7
- package/src/util/eventLoop.ts +3 -3
package/lib/sync/unknownBlock.js
CHANGED
|
@@ -7,7 +7,7 @@ import { isBlockInputColumns } from "../chain/blocks/blockInput/blockInput.js";
|
|
|
7
7
|
import { BlockInputSource } from "../chain/blocks/blockInput/types.js";
|
|
8
8
|
import { PayloadError, PayloadErrorCode } from "../chain/blocks/importExecutionPayload.js";
|
|
9
9
|
import { PayloadEnvelopeInputSource } from "../chain/blocks/payloadEnvelopeInput/index.js";
|
|
10
|
-
import { BlockError, BlockErrorCode } from "../chain/errors/index.js";
|
|
10
|
+
import { BlockError, BlockErrorCode, DataColumnSidecarErrorCode, DataColumnSidecarValidationError, ExecutionPayloadEnvelopeError, ExecutionPayloadEnvelopeErrorCode, } from "../chain/errors/index.js";
|
|
11
11
|
import { ChainEvent } from "../chain/index.js";
|
|
12
12
|
import { validateGloasBlockDataColumnSidecars } from "../chain/validation/dataColumnSidecar.js";
|
|
13
13
|
import { validateGossipExecutionPayloadEnvelope } from "../chain/validation/executionPayloadEnvelope.js";
|
|
@@ -19,12 +19,21 @@ import { sortBy } from "../util/sortBy.js";
|
|
|
19
19
|
import { wrapError } from "../util/wrapError.js";
|
|
20
20
|
import { MAX_CONCURRENT_REQUESTS } from "./constants.js";
|
|
21
21
|
import { DeferredPayloadResult, DownloadResult, DroppedItemReason, FetchResult, PendingBlockInputStatus, PendingBlockType, PendingPayloadInputStatus, getBlockInputSyncCacheItemRootHex, getBlockInputSyncCacheItemSlot, getPayloadSyncCacheItemRootHex, getPayloadSyncCacheItemSlot, isPendingBlockInput, isPendingPayloadEnvelope, isPendingPayloadInput, } from "./types.js";
|
|
22
|
-
import { DownloadByRootError, downloadByRoot } from "./utils/downloadByRoot.js";
|
|
22
|
+
import { DownloadByRootError, DownloadByRootErrorCode, downloadByRoot } from "./utils/downloadByRoot.js";
|
|
23
23
|
import { getAllDescendantBlocks, getUnknownAndAncestorBlocks } from "./utils/pendingBlocksTree.js";
|
|
24
24
|
import { getRateLimitedUntilMs } from "./utils/rateLimit.js";
|
|
25
25
|
const MAX_ATTEMPTS_PER_BLOCK = 5;
|
|
26
26
|
const MAX_KNOWN_BAD_BLOCKS = 500;
|
|
27
27
|
const MAX_PENDING_BLOCKS = 100;
|
|
28
|
+
/**
|
|
29
|
+
* A failed download is retried after this delay, doubling per failure up to one slot.
|
|
30
|
+
*/
|
|
31
|
+
const DOWNLOAD_RETRY_BASE_MS = 1_000;
|
|
32
|
+
/**
|
|
33
|
+
* Requests per peer per window, half the BeaconBlocksByRoot quota peers enforce, they disconnect a peer that exceeds it.
|
|
34
|
+
*/
|
|
35
|
+
const MAX_REQUESTS_PER_PEER = 64;
|
|
36
|
+
const REQUESTS_PER_PEER_WINDOW_MS = 10_000;
|
|
28
37
|
/**
|
|
29
38
|
* A pending payload without slot can only stay at most 2 epochs.
|
|
30
39
|
*/
|
|
@@ -93,6 +102,8 @@ export class BlockInputSync {
|
|
|
93
102
|
subscribedToNetworkEvents = false;
|
|
94
103
|
peerBalancer;
|
|
95
104
|
rateLimitBackoffTimeout;
|
|
105
|
+
downloadRetryTimeout;
|
|
106
|
+
downloadRetryAtMs;
|
|
96
107
|
/**
|
|
97
108
|
* Search unknown payload starting at PAYLOAD_DUE_BPS.
|
|
98
109
|
*/
|
|
@@ -133,6 +144,7 @@ export class BlockInputSync {
|
|
|
133
144
|
this.network.events.on(NetworkEvent.peerConnected, this.onPeerConnected);
|
|
134
145
|
this.network.events.on(NetworkEvent.peerDisconnected, this.onPeerDisconnected);
|
|
135
146
|
this.chain.clock.on(ClockEvent.slot, this.startPayloadPollAtSlot);
|
|
147
|
+
this.chain.clock.on(ClockEvent.slot, this.pruneUnresolvedBlocks);
|
|
136
148
|
// Seed the balancer with peers that connected before we subscribed
|
|
137
149
|
for (const peerId of this.network.getConnectedPeers()) {
|
|
138
150
|
try {
|
|
@@ -151,7 +163,9 @@ export class BlockInputSync {
|
|
|
151
163
|
unsubscribeFromNetwork() {
|
|
152
164
|
this.logger.verbose("BlockInputSync disabled.");
|
|
153
165
|
this.clearRateLimitBackoffTimer();
|
|
166
|
+
this.clearDownloadRetryTimer();
|
|
154
167
|
this.chain.clock.off(ClockEvent.slot, this.startPayloadPollAtSlot);
|
|
168
|
+
this.chain.clock.off(ClockEvent.slot, this.pruneUnresolvedBlocks);
|
|
155
169
|
this.payloadPollAbortController?.abort();
|
|
156
170
|
this.payloadPollAbortController = undefined;
|
|
157
171
|
this.deferredPayloadRoots.clear();
|
|
@@ -293,6 +307,9 @@ export class BlockInputSync {
|
|
|
293
307
|
this.addByRootHex(missingDependency.rootHex, data.peer);
|
|
294
308
|
}
|
|
295
309
|
this.addByBlockInput(data.blockInput, data.peer);
|
|
310
|
+
if (data.source === BlockInputSource.gossip) {
|
|
311
|
+
this.limitRetainedBlocks(data.blockInput, data.peer);
|
|
312
|
+
}
|
|
296
313
|
this.triggerUnknownBlockSearch();
|
|
297
314
|
this.metrics?.blockInputSync.requests.inc({ type: PendingBlockType.UNKNOWN_PARENT });
|
|
298
315
|
this.metrics?.blockInputSync.source.inc({ source: data.source });
|
|
@@ -415,13 +432,7 @@ export class BlockInputSync {
|
|
|
415
432
|
if (peerIdStr && pendingBlock.peerIdStrings.size < MAX_PEERS_PER_ROOT) {
|
|
416
433
|
pendingBlock.peerIdStrings.add(peerIdStr);
|
|
417
434
|
}
|
|
418
|
-
|
|
419
|
-
// Limit pending blocks to prevent DOS attacks that cause OOM
|
|
420
|
-
const prunedItemCount = pruneSetToMax(this.pendingBlocks, this.maxPendingBlocks);
|
|
421
|
-
if (prunedItemCount > 0) {
|
|
422
|
-
this.metrics?.blockInputSync.removedBlocks.inc({ reason: DroppedItemReason.capacity }, prunedItemCount);
|
|
423
|
-
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
424
|
-
}
|
|
435
|
+
this.prunePendingBlocks();
|
|
425
436
|
return added;
|
|
426
437
|
};
|
|
427
438
|
addByBlockInput = (blockInput, peerIdStr) => {
|
|
@@ -452,14 +463,60 @@ export class BlockInputSync {
|
|
|
452
463
|
if (peerIdStr && pendingBlock.peerIdStrings.size < MAX_PEERS_PER_ROOT) {
|
|
453
464
|
pendingBlock.peerIdStrings.add(peerIdStr);
|
|
454
465
|
}
|
|
455
|
-
|
|
456
|
-
// Limit pending blocks to prevent DOS attacks that cause OOM
|
|
457
|
-
const prunedItemCount = pruneSetToMax(this.pendingBlocks, this.maxPendingBlocks);
|
|
458
|
-
if (prunedItemCount > 0) {
|
|
459
|
-
this.metrics?.blockInputSync.removedBlocks.inc({ reason: DroppedItemReason.capacity }, prunedItemCount);
|
|
460
|
-
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
461
|
-
}
|
|
466
|
+
this.prunePendingBlocks();
|
|
462
467
|
};
|
|
468
|
+
/**
|
|
469
|
+
* Limit pending blocks to prevent DOS attacks that cause OOM. Any gossip message with an unknown root adds a root-only
|
|
470
|
+
* entry before validation, so those nothing waits on are evicted before a block or a root a pending block waits on.
|
|
471
|
+
* A downloaded block is only checked against its root and proposer signature, not that the proposer is due at its
|
|
472
|
+
* slot, so this only applies while blocks and the roots they wait on fill at most half the cap, otherwise they could
|
|
473
|
+
* pin it and evict every new root on insertion.
|
|
474
|
+
*/
|
|
475
|
+
prunePendingBlocks() {
|
|
476
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const awaitedRoots = new Set();
|
|
480
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
481
|
+
if (isPendingBlockInput(pendingBlock)) {
|
|
482
|
+
awaitedRoots.add(pendingBlock.blockInput.parentRootHex);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
const isKept = (rootHex, pendingBlock) => isPendingBlockInput(pendingBlock) || awaitedRoots.has(rootHex);
|
|
486
|
+
let keptCount = 0;
|
|
487
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
488
|
+
if (isKept(rootHex, pendingBlock)) {
|
|
489
|
+
keptCount++;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
let prunedItemCount = 0;
|
|
493
|
+
if (keptCount <= this.maxPendingBlocks / 2) {
|
|
494
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
495
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
if (!isKept(rootHex, pendingBlock)) {
|
|
499
|
+
this.pendingBlocks.delete(rootHex);
|
|
500
|
+
prunedItemCount++;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
for (const [rootHex, pendingBlock] of this.pendingBlocks) {
|
|
505
|
+
if (this.pendingBlocks.size <= this.maxPendingBlocks) {
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
this.pendingBlocks.delete(rootHex);
|
|
509
|
+
prunedItemCount++;
|
|
510
|
+
// The block input cache is only trimmed on import, a block whose parent never resolves would stay there
|
|
511
|
+
if (isPendingBlockInput(pendingBlock) &&
|
|
512
|
+
pendingBlock.status !== PendingBlockInputStatus.processing &&
|
|
513
|
+
this.getMissingBlockDependency(pendingBlock.blockInput).kind === "parentBlock") {
|
|
514
|
+
this.chain.seenBlockInputCache.remove(rootHex);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.metrics?.blockInputSync.removedBlocks.inc({ reason: DroppedItemReason.capacity }, prunedItemCount);
|
|
518
|
+
this.logger.verbose(`Pruned ${prunedItemCount} items from BlockInputSync.pendingBlocks`);
|
|
519
|
+
}
|
|
463
520
|
addByPayloadRootHex = (rootHex, peerIdStr, slot) => {
|
|
464
521
|
if (this.knownBadBlocks.has(rootHex)) {
|
|
465
522
|
this.logger.debug("Ignoring payload for known bad block root", {
|
|
@@ -575,6 +632,72 @@ export class BlockInputSync {
|
|
|
575
632
|
this.peerBalancer.onPeerDisconnected(peerId);
|
|
576
633
|
this.scheduleRateLimitBackoffRetry();
|
|
577
634
|
};
|
|
635
|
+
/**
|
|
636
|
+
* A peer can make us retain a signed block per validator key and slot until its parent resolves. Bound the bytes
|
|
637
|
+
* retained per peer, its oldest blocks still waiting on their parent are removed first.
|
|
638
|
+
*/
|
|
639
|
+
limitRetainedBlocks(blockInput, peerIdStr) {
|
|
640
|
+
const pendingBlock = this.pendingBlocks.get(blockInput.blockRootHex);
|
|
641
|
+
if (pendingBlock === undefined || !isPendingBlockInput(pendingBlock) || pendingBlock.retainedBy !== undefined) {
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
pendingBlock.retainedBy = peerIdStr;
|
|
645
|
+
pendingBlock.retainedBytes = this.config
|
|
646
|
+
.getForkTypes(blockInput.slot)
|
|
647
|
+
.SignedBeaconBlock.value_serializedSize(blockInput.getBlock());
|
|
648
|
+
const retainedBlocks = [];
|
|
649
|
+
let retainedBytes = 0;
|
|
650
|
+
for (const item of this.pendingBlocks.values()) {
|
|
651
|
+
if (isPendingBlockInput(item) && item.retainedBy === peerIdStr && this.isWaitingOnParent(item)) {
|
|
652
|
+
retainedBlocks.push(item);
|
|
653
|
+
retainedBytes += item.retainedBytes ?? 0;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
// One block of the max gossip size fits, honest blocks are far smaller
|
|
657
|
+
for (const item of retainedBlocks) {
|
|
658
|
+
if (retainedBytes <= this.config.MAX_PAYLOAD_SIZE) {
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
retainedBytes -= item.retainedBytes ?? 0;
|
|
662
|
+
this.removeUnresolvedBlock(item, DroppedItemReason.peerBudget);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Blocks are otherwise only pruned once finalization passes them, which never happens for a parent that does not resolve
|
|
667
|
+
*/
|
|
668
|
+
pruneUnresolvedBlocks = () => {
|
|
669
|
+
const nowSec = Date.now() / 1000;
|
|
670
|
+
const slotDurationMs = this.config.getSlotDurationMs(this.config.getForkName(this.chain.clock.currentSlot));
|
|
671
|
+
const maxSecInCache = (PRUNE_UNRESOLVED_SLOT_EPOCHS * SLOTS_PER_EPOCH * slotDurationMs) / 1000;
|
|
672
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
673
|
+
if (isPendingBlockInput(pendingBlock) &&
|
|
674
|
+
nowSec - pendingBlock.timeAddedSec > maxSecInCache &&
|
|
675
|
+
this.isWaitingOnParent(pendingBlock)) {
|
|
676
|
+
this.removeUnresolvedBlock(pendingBlock, DroppedItemReason.agedOut);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
isWaitingOnParent(pendingBlock) {
|
|
681
|
+
if (pendingBlock.status === PendingBlockInputStatus.processing) {
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
const { kind } = this.getMissingBlockDependency(pendingBlock.blockInput);
|
|
685
|
+
return kind === "parentBlock" || kind === "parentPayload";
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Removes only the block itself, its descendants request it again by root and its claimed ancestors stay cached
|
|
689
|
+
*/
|
|
690
|
+
removeUnresolvedBlock(pendingBlock, reason) {
|
|
691
|
+
const rootHex = pendingBlock.blockInput.blockRootHex;
|
|
692
|
+
this.pendingBlocks.delete(rootHex);
|
|
693
|
+
this.chain.seenBlockInputCache.remove(rootHex);
|
|
694
|
+
this.metrics?.blockInputSync.removedBlocks.inc({ reason });
|
|
695
|
+
this.logger.debug("Removed unresolved BlockInputSync block", {
|
|
696
|
+
slot: pendingBlock.blockInput.slot,
|
|
697
|
+
root: rootHex,
|
|
698
|
+
reason,
|
|
699
|
+
});
|
|
700
|
+
}
|
|
578
701
|
/**
|
|
579
702
|
* Post-gloas, a locally complete block can still be blocked on its parent's execution payload lineage.
|
|
580
703
|
* Distinguish which dependency is missing so the scheduler can enqueue the right follow-up work.
|
|
@@ -693,8 +816,17 @@ export class BlockInputSync {
|
|
|
693
816
|
this.logger.debug("No connected peers, skipping unknown block download.");
|
|
694
817
|
}
|
|
695
818
|
else {
|
|
819
|
+
// Blocks and roots a pending block waits on are downloaded first, any gossip message with an unknown root adds
|
|
820
|
+
// another root, and they all share the per-peer request budget
|
|
821
|
+
const awaitedRoots = new Set();
|
|
822
|
+
for (const pendingBlock of this.pendingBlocks.values()) {
|
|
823
|
+
if (isPendingBlockInput(pendingBlock)) {
|
|
824
|
+
awaitedRoots.add(pendingBlock.blockInput.parentRootHex);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
const isAwaited = (block) => isPendingBlockInput(block) || awaitedRoots.has(block.rootHex);
|
|
696
828
|
// Most of the time there is exactly 1 unknown block
|
|
697
|
-
for (const block of unknowns) {
|
|
829
|
+
for (const block of [...unknowns.filter(isAwaited), ...unknowns.filter((block) => !isAwaited(block))]) {
|
|
698
830
|
this.downloadBlock(block).catch((e) => {
|
|
699
831
|
this.logger.debug("Unexpected error - downloadBlock", { root: getBlockInputSyncCacheItemRootHex(block) }, e);
|
|
700
832
|
});
|
|
@@ -764,6 +896,25 @@ export class BlockInputSync {
|
|
|
764
896
|
this.rateLimitBackoffTimeout = undefined;
|
|
765
897
|
}
|
|
766
898
|
}
|
|
899
|
+
scheduleDownloadRetry(retryAtMs) {
|
|
900
|
+
if (this.downloadRetryAtMs !== undefined && this.downloadRetryAtMs <= retryAtMs) {
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
this.clearDownloadRetryTimer();
|
|
904
|
+
this.downloadRetryAtMs = retryAtMs;
|
|
905
|
+
this.downloadRetryTimeout = setTimeout(() => {
|
|
906
|
+
this.downloadRetryTimeout = undefined;
|
|
907
|
+
this.downloadRetryAtMs = undefined;
|
|
908
|
+
this.triggerUnknownBlockSearch();
|
|
909
|
+
}, Math.max(0, retryAtMs - Date.now()));
|
|
910
|
+
}
|
|
911
|
+
clearDownloadRetryTimer() {
|
|
912
|
+
if (this.downloadRetryTimeout !== undefined) {
|
|
913
|
+
clearTimeout(this.downloadRetryTimeout);
|
|
914
|
+
this.downloadRetryTimeout = undefined;
|
|
915
|
+
}
|
|
916
|
+
this.downloadRetryAtMs = undefined;
|
|
917
|
+
}
|
|
767
918
|
/**
|
|
768
919
|
* Start this slot's payload poll: end the previous slot's loop, move roots it never got to
|
|
769
920
|
* into pendingPayloads, then poll from PAYLOAD_DUE to the end of the slot.
|
|
@@ -878,6 +1029,10 @@ export class BlockInputSync {
|
|
|
878
1029
|
if (block.status !== PendingBlockInputStatus.pending) {
|
|
879
1030
|
return;
|
|
880
1031
|
}
|
|
1032
|
+
if (block.retryAtMs !== undefined && block.retryAtMs > Date.now()) {
|
|
1033
|
+
this.scheduleDownloadRetry(block.retryAtMs);
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
881
1036
|
const rootHex = getBlockInputSyncCacheItemRootHex(block);
|
|
882
1037
|
const blockSlot = getBlockInputSyncCacheItemSlot(block);
|
|
883
1038
|
const logCtx = {
|
|
@@ -952,6 +1107,7 @@ export class BlockInputSync {
|
|
|
952
1107
|
const pendingBlock = this.pendingBlocks.get(rootHex);
|
|
953
1108
|
if (pendingBlock) {
|
|
954
1109
|
pendingBlock.status = PendingBlockInputStatus.pending;
|
|
1110
|
+
pendingBlock.retryAtMs = res.err.retryAt;
|
|
955
1111
|
}
|
|
956
1112
|
this.logger.debug("Deferring unknown block download due to peer rate limit", logCtx, res.err);
|
|
957
1113
|
if (!this.scheduleRateLimitBackoffRetry() || res.err.retryAt <= Date.now()) {
|
|
@@ -961,11 +1117,26 @@ export class BlockInputSync {
|
|
|
961
1117
|
}
|
|
962
1118
|
return;
|
|
963
1119
|
}
|
|
1120
|
+
if (res.err instanceof DownloadByRootError &&
|
|
1121
|
+
res.err.type.code === DownloadByRootErrorCode.BELOW_FINALIZED_SLOT) {
|
|
1122
|
+
this.logger.debug("Downloaded block is before finalized slot", logCtx, res.err);
|
|
1123
|
+
this.removeAllDescendants(block, DroppedItemReason.belowFinalized);
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
964
1126
|
this.metrics?.blockInputSync.downloadedBlocksError.inc();
|
|
965
1127
|
this.logger.debug("Failed to download unknown block root, retained for retry", logCtx, res.err);
|
|
966
1128
|
const pendingBlock = this.pendingBlocks.get(rootHex);
|
|
967
|
-
|
|
1129
|
+
// A partial download replaces the entry with a pending one that does not carry the failure count
|
|
1130
|
+
if (pendingBlock &&
|
|
1131
|
+
(pendingBlock.status === PendingBlockInputStatus.fetching ||
|
|
1132
|
+
pendingBlock.status === PendingBlockInputStatus.pending)) {
|
|
968
1133
|
pendingBlock.status = PendingBlockInputStatus.pending;
|
|
1134
|
+
// Not retried on every search, any gossip message with an unknown root triggers one
|
|
1135
|
+
pendingBlock.failedDownloads = (block.failedDownloads ?? 0) + 1;
|
|
1136
|
+
const slotDurationMs = this.config.getSlotDurationMs(this.config.getForkName(this.chain.clock.currentSlot));
|
|
1137
|
+
pendingBlock.retryAtMs =
|
|
1138
|
+
Date.now() + Math.min(DOWNLOAD_RETRY_BASE_MS * 2 ** (pendingBlock.failedDownloads - 1), slotDurationMs);
|
|
1139
|
+
this.scheduleDownloadRetry(pendingBlock.retryAtMs);
|
|
969
1140
|
}
|
|
970
1141
|
}
|
|
971
1142
|
}
|
|
@@ -1128,7 +1299,7 @@ export class BlockInputSync {
|
|
|
1128
1299
|
const pendingPayloadByRoot = {
|
|
1129
1300
|
status: PendingPayloadInputStatus.pending,
|
|
1130
1301
|
rootHex,
|
|
1131
|
-
slot:
|
|
1302
|
+
slot: payloadInput.slot,
|
|
1132
1303
|
timeAddedSec: pendingPayload.timeAddedSec,
|
|
1133
1304
|
peerIdStrings: new Set(pendingPayload.peerIdStrings),
|
|
1134
1305
|
};
|
|
@@ -1430,6 +1601,26 @@ export class BlockInputSync {
|
|
|
1430
1601
|
else {
|
|
1431
1602
|
excludedPeers.add(peerId);
|
|
1432
1603
|
}
|
|
1604
|
+
if (e instanceof ExecutionPayloadEnvelopeError) {
|
|
1605
|
+
switch (e.type.code) {
|
|
1606
|
+
// Caused by local state, the downloaded envelope may still be valid
|
|
1607
|
+
case ExecutionPayloadEnvelopeErrorCode.BLOCK_ROOT_UNKNOWN:
|
|
1608
|
+
case ExecutionPayloadEnvelopeErrorCode.ENVELOPE_ALREADY_KNOWN:
|
|
1609
|
+
case ExecutionPayloadEnvelopeErrorCode.PAYLOAD_ENVELOPE_INPUT_MISSING:
|
|
1610
|
+
case ExecutionPayloadEnvelopeErrorCode.UNKNOWN_BLOCK_STATE:
|
|
1611
|
+
break;
|
|
1612
|
+
// Every copy fails once the block is finalized, only a wrong slot makes the served copy invalid
|
|
1613
|
+
case ExecutionPayloadEnvelopeErrorCode.BELONG_TO_FINALIZED_BLOCK:
|
|
1614
|
+
if (envelope?.message.payload.slotNumber !== payloadInput?.slot) {
|
|
1615
|
+
envelope = payloadInput?.hasPayloadEnvelope() ? payloadInput.getPayloadEnvelope() : undefined;
|
|
1616
|
+
}
|
|
1617
|
+
break;
|
|
1618
|
+
// The served copy is invalid, take the envelope from payloadInput or another peer on the next attempt
|
|
1619
|
+
default:
|
|
1620
|
+
envelope = payloadInput?.hasPayloadEnvelope() ? payloadInput.getPayloadEnvelope() : undefined;
|
|
1621
|
+
break;
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1433
1624
|
}
|
|
1434
1625
|
finally {
|
|
1435
1626
|
this.peerBalancer.onRequestCompleted(peerId);
|
|
@@ -1498,7 +1689,19 @@ export class BlockInputSync {
|
|
|
1498
1689
|
}
|
|
1499
1690
|
let i = 0;
|
|
1500
1691
|
let deferredByRateLimit = false;
|
|
1692
|
+
let requestedWhenReconstructable = false;
|
|
1501
1693
|
while (i++ < this.getMaxDownloadAttempts()) {
|
|
1694
|
+
const reconstructable = isPendingBlockInput(cacheItem) &&
|
|
1695
|
+
isBlockInputColumns(cacheItem.blockInput) &&
|
|
1696
|
+
cacheItem.blockInput.hasBlockAndAllData();
|
|
1697
|
+
// Reconstruction recovers whole rows however many columns are missing, so only one more request is worth it,
|
|
1698
|
+
// further peers could each hold the import until their response times out
|
|
1699
|
+
if (reconstructable) {
|
|
1700
|
+
if (requestedWhenReconstructable) {
|
|
1701
|
+
break;
|
|
1702
|
+
}
|
|
1703
|
+
requestedWhenReconstructable = true;
|
|
1704
|
+
}
|
|
1502
1705
|
const pendingColumns = isPendingBlockInput(cacheItem) && isBlockInputColumns(cacheItem.blockInput)
|
|
1503
1706
|
? new Set(cacheItem.blockInput.getMissingSampledColumnMeta().missing)
|
|
1504
1707
|
: defaultPendingColumns;
|
|
@@ -1507,6 +1710,9 @@ export class BlockInputSync {
|
|
|
1507
1710
|
// prefer peers that gossiped this block root to us (#9923)
|
|
1508
1711
|
const peerMeta = this.peerBalancer.bestPeerForPendingColumns(pendingColumns, excludedPeers, cacheItem.peerIdStrings, now);
|
|
1509
1712
|
if (peerMeta === null) {
|
|
1713
|
+
if (reconstructable) {
|
|
1714
|
+
break;
|
|
1715
|
+
}
|
|
1510
1716
|
const retryAt = this.peerBalancer.getNextRateLimitRetryAt(pendingColumns, excludedPeers, now);
|
|
1511
1717
|
if (retryAt !== null) {
|
|
1512
1718
|
throw new UnknownBlockRateLimitedError(`Error fetching UnknownBlockRoot slot=${slot} root=${rootHex} after ${i}: peers with needed columns are rate-limited`, retryAt);
|
|
@@ -1542,17 +1748,50 @@ export class BlockInputSync {
|
|
|
1542
1748
|
for (const warning of warnings) {
|
|
1543
1749
|
this.logger.debug("BlockInputSync.fetchBlockInput: downloaded with warning", logCtx, warning);
|
|
1544
1750
|
this.metrics?.blockInputSync.downloadByRoot.warn.inc({ code: warning.type.code, client: peerClient });
|
|
1751
|
+
// A partial response is legal, columns that were not requested are not
|
|
1752
|
+
if (warning.type.code === DownloadByRootErrorCode.EXTRA_SIDECAR_RECEIVED) {
|
|
1753
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, warning.type.code);
|
|
1754
|
+
excludedPeers.add(peerId);
|
|
1755
|
+
}
|
|
1756
|
+
// Not picked again for this block, it would be preferred as a previous source of it
|
|
1757
|
+
if (warning.type.code === DownloadByRootErrorCode.NOT_ENOUGH_SIDECARS_RECEIVED) {
|
|
1758
|
+
excludedPeers.add(peerId);
|
|
1759
|
+
}
|
|
1545
1760
|
}
|
|
1546
|
-
// TODO: penalize peer?
|
|
1547
1761
|
}
|
|
1548
1762
|
}
|
|
1549
1763
|
catch (e) {
|
|
1550
1764
|
this.logger.debug("Error downloading in BlockInputSync.fetchBlockInput", { slot, rootHex, attempt: i, peer: peerId, peerClient }, e);
|
|
1551
1765
|
const downloadByRootMetrics = this.metrics?.blockInputSync.downloadByRoot;
|
|
1552
|
-
// TODO: penalize peer?
|
|
1553
1766
|
if (e instanceof DownloadByRootError) {
|
|
1554
1767
|
const errorCode = e.type.code;
|
|
1555
1768
|
downloadByRootMetrics?.error.inc({ code: errorCode, client: peerClient });
|
|
1769
|
+
if (errorCode === DownloadByRootErrorCode.BELOW_FINALIZED_SLOT) {
|
|
1770
|
+
throw e;
|
|
1771
|
+
}
|
|
1772
|
+
// Low as in range sync, not Fatal as peers may serve unverified copies of a root with a bad signature
|
|
1773
|
+
if (errorCode === DownloadByRootErrorCode.MISMATCH_BLOCK_ROOT ||
|
|
1774
|
+
errorCode === DownloadByRootErrorCode.PROPOSAL_SIGNATURE_INVALID ||
|
|
1775
|
+
errorCode === DownloadByRootErrorCode.UNKNOWN_PROPOSER) {
|
|
1776
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, errorCode);
|
|
1777
|
+
}
|
|
1778
|
+
excludedPeers.add(peerId);
|
|
1779
|
+
}
|
|
1780
|
+
else if (e instanceof DataColumnSidecarValidationError) {
|
|
1781
|
+
const errorCode = e.type.code;
|
|
1782
|
+
downloadByRootMetrics?.error.inc({ code: errorCode, client: peerClient });
|
|
1783
|
+
// Not PROPOSAL_SIGNATURE_INVALID or INCORRECT_HEADER_ROOT, which compares signatures too: the spec does not
|
|
1784
|
+
// require verifying the header signature of req/resp columns, so honest peers may serve a bad one
|
|
1785
|
+
switch (errorCode) {
|
|
1786
|
+
case DataColumnSidecarErrorCode.INCORRECT_BLOCK:
|
|
1787
|
+
case DataColumnSidecarErrorCode.INVALID_INDEX:
|
|
1788
|
+
case DataColumnSidecarErrorCode.INCORRECT_CELL_COUNT:
|
|
1789
|
+
case DataColumnSidecarErrorCode.INCORRECT_KZG_COMMITMENTS_COUNT:
|
|
1790
|
+
case DataColumnSidecarErrorCode.INCORRECT_KZG_PROOF_COUNT:
|
|
1791
|
+
case DataColumnSidecarErrorCode.INCLUSION_PROOF_INVALID:
|
|
1792
|
+
case DataColumnSidecarErrorCode.INVALID_KZG_PROOF_BATCH:
|
|
1793
|
+
this.network.reportPeer(peerId, PeerAction.LowToleranceError, errorCode);
|
|
1794
|
+
}
|
|
1556
1795
|
excludedPeers.add(peerId);
|
|
1557
1796
|
}
|
|
1558
1797
|
else if (e instanceof RequestError) {
|
|
@@ -1605,6 +1844,27 @@ export class BlockInputSync {
|
|
|
1605
1844
|
}
|
|
1606
1845
|
} // end while loop over peers
|
|
1607
1846
|
const message = `Error fetching BlockInput with slot=${slot} root=${rootHex} after ${i - 1} attempts.`;
|
|
1847
|
+
if (isPendingBlockInput(cacheItem) &&
|
|
1848
|
+
isBlockInputColumns(cacheItem.blockInput) &&
|
|
1849
|
+
cacheItem.blockInput.hasBlockAndAllData()) {
|
|
1850
|
+
// Peers cannot serve the remaining columns now, half of them is enough to reconstruct the rest
|
|
1851
|
+
this.logger.verbose("BlockInputSync.fetchBlockInput: reconstructing columns peers could not serve", {
|
|
1852
|
+
slot,
|
|
1853
|
+
rootHex,
|
|
1854
|
+
missing: cacheItem.blockInput.getMissingSampledColumnMeta().missing.length,
|
|
1855
|
+
});
|
|
1856
|
+
this.chain.columnReconstructionTracker.triggerColumnReconstruction(cacheItem.blockInput);
|
|
1857
|
+
cacheItem = { ...cacheItem, status: PendingBlockInputStatus.downloaded, timeSyncedSec: Date.now() / 1000 };
|
|
1858
|
+
if (this.pendingBlocks.has(rootHex)) {
|
|
1859
|
+
this.pendingBlocks.set(rootHex, cacheItem);
|
|
1860
|
+
}
|
|
1861
|
+
const result = this.chain.forkChoice.hasBlockHex(cacheItem.blockInput.parentRootHex)
|
|
1862
|
+
? FetchResult.SuccessResolved
|
|
1863
|
+
: FetchResult.SuccessMissingParent;
|
|
1864
|
+
this.metrics?.blockInputSync.fetchTime.observe({ result }, Date.now() / 1000 - fetchStartSec);
|
|
1865
|
+
this.metrics?.blockInputSync.fetchPeers.set({ result }, i - 1);
|
|
1866
|
+
return cacheItem;
|
|
1867
|
+
}
|
|
1608
1868
|
const retryAt = deferredByRateLimit ? this.peerBalancer.getNextRateLimitRetryAt() : null;
|
|
1609
1869
|
if (retryAt !== null) {
|
|
1610
1870
|
throw new UnknownBlockRateLimitedError(`${message} Peers are rate-limited.`, retryAt);
|
|
@@ -1728,10 +1988,12 @@ export class UnknownBlockPeerBalancer {
|
|
|
1728
1988
|
peersMeta;
|
|
1729
1989
|
activeRequests;
|
|
1730
1990
|
rateLimitedUntilByPeer;
|
|
1991
|
+
requestTimesByPeer;
|
|
1731
1992
|
constructor() {
|
|
1732
1993
|
this.peersMeta = new Map();
|
|
1733
1994
|
this.activeRequests = new Map();
|
|
1734
1995
|
this.rateLimitedUntilByPeer = new Map();
|
|
1996
|
+
this.requestTimesByPeer = new Map();
|
|
1735
1997
|
}
|
|
1736
1998
|
/** Trigger on each peer re-status */
|
|
1737
1999
|
onPeerConnected(peerId, syncMeta) {
|
|
@@ -1744,12 +2006,14 @@ export class UnknownBlockPeerBalancer {
|
|
|
1744
2006
|
this.peersMeta.delete(peerId);
|
|
1745
2007
|
this.activeRequests.delete(peerId);
|
|
1746
2008
|
this.rateLimitedUntilByPeer.delete(peerId);
|
|
2009
|
+
this.requestTimesByPeer.delete(peerId);
|
|
1747
2010
|
}
|
|
1748
2011
|
/** Reset all tracked peer state, e.g. when BlockInputSync unsubscribes from the network. */
|
|
1749
2012
|
clear() {
|
|
1750
2013
|
this.peersMeta.clear();
|
|
1751
2014
|
this.activeRequests.clear();
|
|
1752
2015
|
this.rateLimitedUntilByPeer.clear();
|
|
2016
|
+
this.requestTimesByPeer.clear();
|
|
1753
2017
|
}
|
|
1754
2018
|
onRateLimited(peerId, rateLimitedUntilMs) {
|
|
1755
2019
|
this.rateLimitedUntilByPeer.set(peerId, rateLimitedUntilMs);
|
|
@@ -1794,15 +2058,22 @@ export class UnknownBlockPeerBalancer {
|
|
|
1794
2058
|
// 2nd key (tiebreak): prefer peers with least active req
|
|
1795
2059
|
(peerId) => this.activeRequests.get(peerId) ?? 0);
|
|
1796
2060
|
const bestPeerId = sortedEligiblePeers[0];
|
|
1797
|
-
this.onRequest(bestPeerId);
|
|
2061
|
+
this.onRequest(bestPeerId, now);
|
|
1798
2062
|
return this.peersMeta.get(bestPeerId) ?? null;
|
|
1799
2063
|
}
|
|
1800
2064
|
/**
|
|
1801
2065
|
* Consumers don't need to call this method directly, it is called internally by bestPeer*() methods
|
|
1802
2066
|
* make this public for testing
|
|
1803
2067
|
*/
|
|
1804
|
-
onRequest(peerId) {
|
|
2068
|
+
onRequest(peerId, now = Date.now()) {
|
|
1805
2069
|
this.activeRequests.set(peerId, (this.activeRequests.get(peerId) ?? 0) + 1);
|
|
2070
|
+
const requestTimes = (this.requestTimesByPeer.get(peerId) ?? []).filter((time) => now - time < REQUESTS_PER_PEER_WINDOW_MS);
|
|
2071
|
+
requestTimes.push(now);
|
|
2072
|
+
this.requestTimesByPeer.set(peerId, requestTimes);
|
|
2073
|
+
if (requestTimes.length >= MAX_REQUESTS_PER_PEER) {
|
|
2074
|
+
const budgetUntilMs = requestTimes[0] + REQUESTS_PER_PEER_WINDOW_MS;
|
|
2075
|
+
this.onRateLimited(peerId, Math.max(this.rateLimitedUntilByPeer.get(peerId) ?? 0, budgetUntilMs));
|
|
2076
|
+
}
|
|
1806
2077
|
}
|
|
1807
2078
|
/**
|
|
1808
2079
|
* Consumers should call this method when a request is completed for a peer.
|