@lodestar/beacon-node 1.52.0-dev.0f379f3b22 → 1.52.0-dev.119f1f9fe6

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.
Files changed (272) hide show
  1. package/lib/api/impl/beacon/blocks/index.d.ts.map +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js +18 -5
  3. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  4. package/lib/api/impl/beacon/state/utils.d.ts.map +1 -1
  5. package/lib/api/impl/beacon/state/utils.js +2 -1
  6. package/lib/api/impl/beacon/state/utils.js.map +1 -1
  7. package/lib/api/impl/debug/index.d.ts.map +1 -1
  8. package/lib/api/impl/debug/index.js +6 -2
  9. package/lib/api/impl/debug/index.js.map +1 -1
  10. package/lib/api/impl/validator/index.d.ts.map +1 -1
  11. package/lib/api/impl/validator/index.js +15 -8
  12. package/lib/api/impl/validator/index.js.map +1 -1
  13. package/lib/chain/ColumnReconstructionTracker.d.ts +4 -0
  14. package/lib/chain/ColumnReconstructionTracker.d.ts.map +1 -1
  15. package/lib/chain/ColumnReconstructionTracker.js +4 -0
  16. package/lib/chain/ColumnReconstructionTracker.js.map +1 -1
  17. package/lib/chain/archiveStore/historicalState/getHistoricalState.d.ts.map +1 -1
  18. package/lib/chain/archiveStore/historicalState/getHistoricalState.js +7 -0
  19. package/lib/chain/archiveStore/historicalState/getHistoricalState.js.map +1 -1
  20. package/lib/chain/archiveStore/utils/archiveBlocks.js +1 -1
  21. package/lib/chain/archiveStore/utils/archiveBlocks.js.map +1 -1
  22. package/lib/chain/archiveStore/utils/pruneHistory.d.ts.map +1 -1
  23. package/lib/chain/archiveStore/utils/pruneHistory.js +8 -4
  24. package/lib/chain/archiveStore/utils/pruneHistory.js.map +1 -1
  25. package/lib/chain/blocks/importExecutionPayload.d.ts +1 -1
  26. package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -1
  27. package/lib/chain/blocks/importExecutionPayload.js +9 -3
  28. package/lib/chain/blocks/importExecutionPayload.js.map +1 -1
  29. package/lib/chain/blocks/index.d.ts.map +1 -1
  30. package/lib/chain/blocks/index.js +3 -17
  31. package/lib/chain/blocks/index.js.map +1 -1
  32. package/lib/chain/chain.d.ts +6 -5
  33. package/lib/chain/chain.d.ts.map +1 -1
  34. package/lib/chain/chain.js +71 -25
  35. package/lib/chain/chain.js.map +1 -1
  36. package/lib/chain/errors/attestationError.d.ts +15 -1
  37. package/lib/chain/errors/attestationError.d.ts.map +1 -1
  38. package/lib/chain/errors/attestationError.js +8 -0
  39. package/lib/chain/errors/attestationError.js.map +1 -1
  40. package/lib/chain/errors/executionPayloadBid.d.ts +6 -1
  41. package/lib/chain/errors/executionPayloadBid.d.ts.map +1 -1
  42. package/lib/chain/errors/executionPayloadBid.js +1 -0
  43. package/lib/chain/errors/executionPayloadBid.js.map +1 -1
  44. package/lib/chain/errors/executionPayloadEnvelope.d.ts +55 -0
  45. package/lib/chain/errors/executionPayloadEnvelope.d.ts.map +1 -1
  46. package/lib/chain/errors/executionPayloadEnvelope.js +8 -0
  47. package/lib/chain/errors/executionPayloadEnvelope.js.map +1 -1
  48. package/lib/chain/interface.d.ts +1 -1
  49. package/lib/chain/interface.d.ts.map +1 -1
  50. package/lib/chain/lightClient/index.d.ts +13 -6
  51. package/lib/chain/lightClient/index.d.ts.map +1 -1
  52. package/lib/chain/lightClient/index.js +95 -115
  53. package/lib/chain/lightClient/index.js.map +1 -1
  54. package/lib/chain/prepareNextSlot.d.ts +45 -0
  55. package/lib/chain/prepareNextSlot.d.ts.map +1 -1
  56. package/lib/chain/prepareNextSlot.js +265 -69
  57. package/lib/chain/prepareNextSlot.js.map +1 -1
  58. package/lib/chain/produceBlock/produceBlockBody.d.ts +1 -0
  59. package/lib/chain/produceBlock/produceBlockBody.d.ts.map +1 -1
  60. package/lib/chain/produceBlock/produceBlockBody.js +23 -15
  61. package/lib/chain/produceBlock/produceBlockBody.js.map +1 -1
  62. package/lib/chain/seenCache/seenAttesters.d.ts +1 -0
  63. package/lib/chain/seenCache/seenAttesters.d.ts.map +1 -1
  64. package/lib/chain/seenCache/seenAttesters.js +1 -1
  65. package/lib/chain/seenCache/seenAttesters.js.map +1 -1
  66. package/lib/chain/seenCache/seenBlockAttesters.d.ts +8 -2
  67. package/lib/chain/seenCache/seenBlockAttesters.d.ts.map +1 -1
  68. package/lib/chain/seenCache/seenBlockAttesters.js +43 -6
  69. package/lib/chain/seenCache/seenBlockAttesters.js.map +1 -1
  70. package/lib/chain/validation/aggregateAndProof.d.ts.map +1 -1
  71. package/lib/chain/validation/aggregateAndProof.js +24 -7
  72. package/lib/chain/validation/aggregateAndProof.js.map +1 -1
  73. package/lib/chain/validation/attestation.d.ts.map +1 -1
  74. package/lib/chain/validation/attestation.js +32 -16
  75. package/lib/chain/validation/attestation.js.map +1 -1
  76. package/lib/chain/validation/block.d.ts.map +1 -1
  77. package/lib/chain/validation/block.js +9 -1
  78. package/lib/chain/validation/block.js.map +1 -1
  79. package/lib/chain/validation/executionPayloadBid.d.ts.map +1 -1
  80. package/lib/chain/validation/executionPayloadBid.js +18 -31
  81. package/lib/chain/validation/executionPayloadBid.js.map +1 -1
  82. package/lib/chain/validation/executionPayloadEnvelope.d.ts.map +1 -1
  83. package/lib/chain/validation/executionPayloadEnvelope.js +100 -9
  84. package/lib/chain/validation/executionPayloadEnvelope.js.map +1 -1
  85. package/lib/chain/validation/payloadAttestationMessage.js +9 -0
  86. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  87. package/lib/chain/validation/voluntaryExit.d.ts.map +1 -1
  88. package/lib/chain/validation/voluntaryExit.js +39 -6
  89. package/lib/chain/validation/voluntaryExit.js.map +1 -1
  90. package/lib/chain/validatorMonitor.js +43 -43
  91. package/lib/chain/validatorMonitor.js.map +1 -1
  92. package/lib/execution/engine/blockHash.d.ts +12 -0
  93. package/lib/execution/engine/blockHash.d.ts.map +1 -0
  94. package/lib/execution/engine/blockHash.js +17 -0
  95. package/lib/execution/engine/blockHash.js.map +1 -0
  96. package/lib/execution/engine/http.d.ts +2 -2
  97. package/lib/execution/engine/http.d.ts.map +1 -1
  98. package/lib/execution/engine/http.js +13 -7
  99. package/lib/execution/engine/http.js.map +1 -1
  100. package/lib/execution/engine/interface.d.ts +16 -3
  101. package/lib/execution/engine/interface.d.ts.map +1 -1
  102. package/lib/execution/engine/interface.js +11 -0
  103. package/lib/execution/engine/interface.js.map +1 -1
  104. package/lib/execution/engine/jsonRpcHttpClient.d.ts +2 -0
  105. package/lib/execution/engine/jsonRpcHttpClient.d.ts.map +1 -1
  106. package/lib/execution/engine/jsonRpcHttpClient.js +14 -6
  107. package/lib/execution/engine/jsonRpcHttpClient.js.map +1 -1
  108. package/lib/execution/engine/jsonRpcTransport.d.ts +2 -2
  109. package/lib/execution/engine/jsonRpcTransport.d.ts.map +1 -1
  110. package/lib/execution/engine/jsonRpcTransport.js +4 -4
  111. package/lib/execution/engine/jsonRpcTransport.js.map +1 -1
  112. package/lib/execution/engine/mock.d.ts.map +1 -1
  113. package/lib/execution/engine/mock.js +6 -6
  114. package/lib/execution/engine/mock.js.map +1 -1
  115. package/lib/execution/engine/payloadIdCache.d.ts +6 -1
  116. package/lib/execution/engine/payloadIdCache.d.ts.map +1 -1
  117. package/lib/execution/engine/payloadIdCache.js +7 -2
  118. package/lib/execution/engine/payloadIdCache.js.map +1 -1
  119. package/lib/execution/engine/restHttpClient.d.ts.map +1 -1
  120. package/lib/execution/engine/restHttpClient.js +9 -5
  121. package/lib/execution/engine/restHttpClient.js.map +1 -1
  122. package/lib/execution/engine/restTransport.d.ts +2 -2
  123. package/lib/execution/engine/restTransport.d.ts.map +1 -1
  124. package/lib/execution/engine/restTransport.js +4 -4
  125. package/lib/execution/engine/restTransport.js.map +1 -1
  126. package/lib/execution/engine/signal.d.ts +6 -0
  127. package/lib/execution/engine/signal.d.ts.map +1 -0
  128. package/lib/execution/engine/signal.js +24 -0
  129. package/lib/execution/engine/signal.js.map +1 -0
  130. package/lib/execution/engine/transport.d.ts +2 -2
  131. package/lib/execution/engine/transport.d.ts.map +1 -1
  132. package/lib/index.d.ts +1 -1
  133. package/lib/index.d.ts.map +1 -1
  134. package/lib/index.js +1 -1
  135. package/lib/index.js.map +1 -1
  136. package/lib/metrics/metrics/beacon.d.ts +5 -0
  137. package/lib/metrics/metrics/beacon.d.ts.map +1 -1
  138. package/lib/metrics/metrics/beacon.js +5 -0
  139. package/lib/metrics/metrics/beacon.js.map +1 -1
  140. package/lib/metrics/metrics/lodestar.d.ts +1 -0
  141. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  142. package/lib/metrics/metrics/lodestar.js +5 -0
  143. package/lib/metrics/metrics/lodestar.js.map +1 -1
  144. package/lib/network/gossip/gossipsub.d.ts +1 -0
  145. package/lib/network/gossip/gossipsub.d.ts.map +1 -1
  146. package/lib/network/gossip/gossipsub.js +5 -2
  147. package/lib/network/gossip/gossipsub.js.map +1 -1
  148. package/lib/network/network.d.ts.map +1 -1
  149. package/lib/network/network.js +6 -1
  150. package/lib/network/network.js.map +1 -1
  151. package/lib/network/peers/peerManager.d.ts.map +1 -1
  152. package/lib/network/peers/peerManager.js +22 -2
  153. package/lib/network/peers/peerManager.js.map +1 -1
  154. package/lib/network/peers/utils/prioritizePeers.d.ts +2 -0
  155. package/lib/network/peers/utils/prioritizePeers.d.ts.map +1 -1
  156. package/lib/network/peers/utils/prioritizePeers.js +11 -2
  157. package/lib/network/peers/utils/prioritizePeers.js.map +1 -1
  158. package/lib/network/processor/gossipHandlers.d.ts.map +1 -1
  159. package/lib/network/processor/gossipHandlers.js +15 -5
  160. package/lib/network/processor/gossipHandlers.js.map +1 -1
  161. package/lib/network/processor/index.d.ts.map +1 -1
  162. package/lib/network/processor/index.js +4 -2
  163. package/lib/network/processor/index.js.map +1 -1
  164. package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js +1 -1
  165. package/lib/network/reqresp/handlers/dataColumnSidecarsByRange.js.map +1 -1
  166. package/lib/network/reqresp/handlers/dataColumnSidecarsByRoot.js +1 -1
  167. package/lib/network/reqresp/handlers/dataColumnSidecarsByRoot.js.map +1 -1
  168. package/lib/sync/range/batch.d.ts +10 -0
  169. package/lib/sync/range/batch.d.ts.map +1 -1
  170. package/lib/sync/range/batch.js +29 -0
  171. package/lib/sync/range/batch.js.map +1 -1
  172. package/lib/sync/range/chain.d.ts +17 -2
  173. package/lib/sync/range/chain.d.ts.map +1 -1
  174. package/lib/sync/range/chain.js +60 -20
  175. package/lib/sync/range/chain.js.map +1 -1
  176. package/lib/sync/range/range.d.ts.map +1 -1
  177. package/lib/sync/range/range.js +44 -3
  178. package/lib/sync/range/range.js.map +1 -1
  179. package/lib/sync/range/utils/batches.d.ts +20 -4
  180. package/lib/sync/range/utils/batches.d.ts.map +1 -1
  181. package/lib/sync/range/utils/batches.js +31 -13
  182. package/lib/sync/range/utils/batches.js.map +1 -1
  183. package/lib/sync/range/utils/peerBalancer.d.ts +1 -1
  184. package/lib/sync/range/utils/peerBalancer.d.ts.map +1 -1
  185. package/lib/sync/range/utils/peerBalancer.js +5 -2
  186. package/lib/sync/range/utils/peerBalancer.js.map +1 -1
  187. package/lib/sync/types.d.ts +14 -1
  188. package/lib/sync/types.d.ts.map +1 -1
  189. package/lib/sync/types.js +2 -0
  190. package/lib/sync/types.js.map +1 -1
  191. package/lib/sync/unknownBlock.d.ts +28 -1
  192. package/lib/sync/unknownBlock.d.ts.map +1 -1
  193. package/lib/sync/unknownBlock.js +338 -26
  194. package/lib/sync/unknownBlock.js.map +1 -1
  195. package/lib/sync/utils/downloadByRange.d.ts +22 -1
  196. package/lib/sync/utils/downloadByRange.d.ts.map +1 -1
  197. package/lib/sync/utils/downloadByRange.js +55 -25
  198. package/lib/sync/utils/downloadByRange.js.map +1 -1
  199. package/lib/sync/utils/downloadByRoot.d.ts +24 -2
  200. package/lib/sync/utils/downloadByRoot.d.ts.map +1 -1
  201. package/lib/sync/utils/downloadByRoot.js +48 -5
  202. package/lib/sync/utils/downloadByRoot.js.map +1 -1
  203. package/lib/util/dataColumns.d.ts +1 -1
  204. package/lib/util/dataColumns.d.ts.map +1 -1
  205. package/lib/util/dataColumns.js +4 -1
  206. package/lib/util/dataColumns.js.map +1 -1
  207. package/lib/util/eventLoop.d.ts +3 -3
  208. package/lib/util/eventLoop.d.ts.map +1 -1
  209. package/lib/util/eventLoop.js +3 -3
  210. package/lib/util/eventLoop.js.map +1 -1
  211. package/package.json +27 -27
  212. package/src/api/impl/beacon/blocks/index.ts +20 -5
  213. package/src/api/impl/beacon/state/utils.ts +2 -1
  214. package/src/api/impl/debug/index.ts +6 -2
  215. package/src/api/impl/validator/index.ts +17 -9
  216. package/src/chain/ColumnReconstructionTracker.ts +5 -0
  217. package/src/chain/archiveStore/historicalState/getHistoricalState.ts +9 -0
  218. package/src/chain/archiveStore/utils/archiveBlocks.ts +1 -1
  219. package/src/chain/archiveStore/utils/pruneHistory.ts +9 -4
  220. package/src/chain/blocks/importExecutionPayload.ts +9 -2
  221. package/src/chain/blocks/index.ts +3 -31
  222. package/src/chain/chain.ts +104 -42
  223. package/src/chain/errors/attestationError.ts +11 -1
  224. package/src/chain/errors/executionPayloadBid.ts +6 -0
  225. package/src/chain/errors/executionPayloadEnvelope.ts +58 -0
  226. package/src/chain/interface.ts +1 -1
  227. package/src/chain/lightClient/index.ts +108 -142
  228. package/src/chain/prepareNextSlot.ts +347 -89
  229. package/src/chain/produceBlock/produceBlockBody.ts +25 -16
  230. package/src/chain/seenCache/seenAttesters.ts +1 -1
  231. package/src/chain/seenCache/seenBlockAttesters.ts +48 -6
  232. package/src/chain/validation/aggregateAndProof.ts +24 -9
  233. package/src/chain/validation/attestation.ts +31 -20
  234. package/src/chain/validation/block.ts +9 -1
  235. package/src/chain/validation/executionPayloadBid.ts +21 -33
  236. package/src/chain/validation/executionPayloadEnvelope.ts +115 -8
  237. package/src/chain/validation/payloadAttestationMessage.ts +10 -0
  238. package/src/chain/validation/voluntaryExit.ts +44 -6
  239. package/src/chain/validatorMonitor.ts +43 -43
  240. package/src/execution/engine/blockHash.ts +27 -0
  241. package/src/execution/engine/http.ts +18 -10
  242. package/src/execution/engine/interface.ts +18 -3
  243. package/src/execution/engine/jsonRpcHttpClient.ts +26 -14
  244. package/src/execution/engine/jsonRpcTransport.ts +8 -4
  245. package/src/execution/engine/mock.ts +13 -8
  246. package/src/execution/engine/payloadIdCache.ts +12 -4
  247. package/src/execution/engine/restHttpClient.ts +17 -11
  248. package/src/execution/engine/restTransport.ts +5 -4
  249. package/src/execution/engine/signal.ts +28 -0
  250. package/src/execution/engine/transport.ts +3 -2
  251. package/src/index.ts +6 -1
  252. package/src/metrics/metrics/beacon.ts +9 -0
  253. package/src/metrics/metrics/lodestar.ts +5 -0
  254. package/src/network/gossip/gossipsub.ts +6 -2
  255. package/src/network/network.ts +6 -2
  256. package/src/network/peers/peerManager.ts +26 -2
  257. package/src/network/peers/utils/prioritizePeers.ts +14 -1
  258. package/src/network/processor/gossipHandlers.ts +15 -6
  259. package/src/network/processor/index.ts +7 -2
  260. package/src/network/reqresp/handlers/dataColumnSidecarsByRange.ts +1 -1
  261. package/src/network/reqresp/handlers/dataColumnSidecarsByRoot.ts +1 -1
  262. package/src/sync/range/batch.ts +33 -0
  263. package/src/sync/range/chain.ts +72 -17
  264. package/src/sync/range/range.ts +57 -3
  265. package/src/sync/range/utils/batches.ts +38 -13
  266. package/src/sync/range/utils/peerBalancer.ts +4 -1
  267. package/src/sync/types.ts +13 -0
  268. package/src/sync/unknownBlock.ts +387 -28
  269. package/src/sync/utils/downloadByRange.ts +89 -29
  270. package/src/sync/utils/downloadByRoot.ts +75 -7
  271. package/src/util/dataColumns.ts +4 -1
  272. package/src/util/eventLoop.ts +3 -3
@@ -1,5 +1,5 @@
1
1
  import {ChainForkConfig} from "@lodestar/config";
2
- import {Epoch, Root, Slot} from "@lodestar/types";
2
+ import {Epoch, Root, RootHex, Slot} from "@lodestar/types";
3
3
  import {ErrorAborted, LodestarError, Logger, prettyPrintIndices, toRootHex} from "@lodestar/utils";
4
4
  import {isBlockInputColumns} from "../../chain/blocks/blockInput/blockInput.js";
5
5
  import {BlockInputErrorCode} from "../../chain/blocks/blockInput/errors.js";
@@ -68,6 +68,8 @@ export type SyncChainFns = {
68
68
  onEnd: (err: Error | null, target: ChainTarget | null) => void;
69
69
  /** Deletes an array of BlockInputs from the BlockInputCache */
70
70
  pruneBlockInputs: (blockInputs: IBlockInput[]) => void;
71
+ /** Returns true if the block is known to fork choice */
72
+ hasBlock: (blockRoot: Root) => boolean;
71
73
  };
72
74
 
73
75
  /**
@@ -81,6 +83,15 @@ export type ChainTarget = {
81
83
 
82
84
  export class SyncChainStartError extends Error {}
83
85
 
86
+ export enum SyncChainErrorCode {
87
+ /** The terminal batch has 0 blocks but the target block is not known */
88
+ MISSING_TARGET_BLOCK = "SYNC_CHAIN_ERROR_MISSING_TARGET_BLOCK",
89
+ }
90
+
91
+ type SyncChainErrorType = {code: SyncChainErrorCode.MISSING_TARGET_BLOCK; targetSlot: Slot; targetRoot: RootHex};
92
+
93
+ export class SyncChainError extends LodestarError<SyncChainErrorType> {}
94
+
84
95
  export type SyncChainDebugState = {
85
96
  targetRoot: string | null;
86
97
  targetSlot: number | null;
@@ -136,6 +147,7 @@ export class SyncChain {
136
147
  private readonly reportPeer: SyncChainFns["reportPeer"];
137
148
  private readonly getConnectedPeerSyncMeta: SyncChainFns["getConnectedPeerSyncMeta"];
138
149
  private readonly pruneBlockInputs: SyncChainFns["pruneBlockInputs"];
150
+ private readonly hasBlock: SyncChainFns["hasBlock"];
139
151
 
140
152
  /** AsyncIterable that guarantees processChainSegment is run only at once at anytime */
141
153
  private readonly batchProcessor = new ItTrigger();
@@ -178,6 +190,7 @@ export class SyncChain {
178
190
  this.downloadByRange = fns.downloadByRange;
179
191
  this.reportPeer = fns.reportPeer;
180
192
  this.pruneBlockInputs = fns.pruneBlockInputs;
193
+ this.hasBlock = fns.hasBlock;
181
194
  this.getConnectedPeerSyncMeta = fns.getConnectedPeerSyncMeta;
182
195
  this.config = config;
183
196
  this.clock = clock;
@@ -335,6 +348,32 @@ export class SyncChain {
335
348
 
336
349
  // Returns true if SyncChain has processed all possible blocks with slot <= target.slot
337
350
  if (isSyncChainDone(toArr(this.batches), this.lastEpochWithProcessBlocks, this.target.slot)) {
351
+ // A 0-block terminal batch is only trusted once the target block is known, otherwise a peer returning no
352
+ // blocks for the last range ends the chain short of its target
353
+ const lastAwaitingValidation = toArr(this.batches).findLast(
354
+ (b) => b.state.status === BatchStatus.AwaitingValidation
355
+ );
356
+ if (
357
+ lastAwaitingValidation !== undefined &&
358
+ lastAwaitingValidation.getBlocks().length === 0 &&
359
+ !this.hasBlock(this.target.root)
360
+ ) {
361
+ this.logger.verbose("Terminal 0-block batch without target block", {
362
+ id: this.logId,
363
+ ...lastAwaitingValidation.getMetadata(),
364
+ });
365
+ // Not peer attributable: throws MAX_PROCESSING_ATTEMPTS after MAX_BATCH_PROCESSING_ATTEMPTS without
366
+ // reporting any peer
367
+ lastAwaitingValidation.validationError(
368
+ new SyncChainError({
369
+ code: SyncChainErrorCode.MISSING_TARGET_BLOCK,
370
+ targetSlot: this.target.slot,
371
+ targetRoot: toRootHex(this.target.root),
372
+ })
373
+ );
374
+ this.triggerBatchDownloader();
375
+ continue;
376
+ }
338
377
  break;
339
378
  }
340
379
 
@@ -509,9 +548,12 @@ export class SyncChain {
509
548
  // if last processed epoch is n, we don't want to request batches with epoch > n + MAX_LOOK_AHEAD_EPOCHS
510
549
  // we should have enough batches to process in the buffer: n + 1, ..., n + MAX_LOOK_AHEAD_EPOCHS
511
550
  // let's focus on redownloading these batches first because it may have to reach different peers to get enough sampled columns
551
+ // Note: Don't count batches in the AwaitingValidation state, 0-block batches never advance
552
+ // lastEpochWithProcessBlocks so more than MAX_LOOK_AHEAD_EPOCHS of them in a row would stall sync
553
+ const pendingBatches = batches.filter((batch) => batch.state.status !== BatchStatus.AwaitingValidation);
512
554
  if (
513
- batches.length > 0 &&
514
- Math.max(...batches.map((b) => b.startEpoch)) >= this.lastEpochWithProcessBlocks + MAX_LOOK_AHEAD_EPOCHS
555
+ pendingBatches.length > 0 &&
556
+ Math.max(...pendingBatches.map((b) => b.startEpoch)) >= this.lastEpochWithProcessBlocks + MAX_LOOK_AHEAD_EPOCHS
515
557
  ) {
516
558
  return null;
517
559
  }
@@ -576,6 +618,7 @@ export class SyncChain {
576
618
  }
577
619
  }
578
620
  switch (errCode) {
621
+ case DownloadByRangeErrorCode.DATA_WITHOUT_BLOCKS:
579
622
  case DownloadByRangeErrorCode.EXTRA_BLOCKS:
580
623
  case DownloadByRangeErrorCode.OUT_OF_ORDER_BLOCKS:
581
624
  case DownloadByRangeErrorCode.OUT_OF_RANGE_BLOCKS:
@@ -602,6 +645,14 @@ export class SyncChain {
602
645
  this.scheduleRateLimitBackoffRetry();
603
646
  batch.downloadingRateLimited();
604
647
  this.triggerBatchDownloader();
648
+ } else if (
649
+ res.err instanceof DownloadByRangeError &&
650
+ res.err.type.code === DownloadByRangeErrorCode.INVALID_CACHED_BLOCK
651
+ ) {
652
+ if (res.err.type.blockPeer !== undefined) {
653
+ this.reportPeer(res.err.type.blockPeer, PeerAction.LowToleranceError, res.err.message);
654
+ }
655
+ batch.downloadingInvalidBlock(res.err.type.blockPeer ?? peer.peerId); // Throws after MAX_DOWNLOAD_ATTEMPTS
605
656
  } else {
606
657
  batch.downloadingError(peer.peerId); // Throws after MAX_DOWNLOAD_ATTEMPTS
607
658
  }
@@ -714,39 +765,43 @@ export class SyncChain {
714
765
  } else {
715
766
  this.logger.verbose("Batch process error", logCtx, res.err);
716
767
 
717
- const invalidatePreviousBatches = (): number => {
718
- let invalidatedCount = 0;
768
+ // advanceChain() drops every batch before the last batch with blocks, so the AwaitingValidation batches before
769
+ // `batch` are that batch followed by 0-block batches, see classifyProcessingFault(). With no batch with blocks
770
+ // yet, fall back to the first 0-block batch so a wrong 0-block answer at the start of the chain is retried
771
+ const awaitingValidationBefore = toArr(this.batches).filter(
772
+ (b) => b.startEpoch < batch.startEpoch && b.state.status === BatchStatus.AwaitingValidation
773
+ );
774
+ const parentBatch =
775
+ awaitingValidationBefore.findLast((b) => b.getBlocks().length > 0) ?? awaitingValidationBefore[0];
776
+ // The 0-block batches after `parentBatch` are invalidated with it: an AwaitingValidation batch must not follow a
777
+ // batch that is downloaded again, see validateBatchesStatus()
778
+ const invalidateFromParentBatch = (faultyParentBatch: Batch): void => {
719
779
  for (const pendingBatch of this.batches.values()) {
720
780
  if (
781
+ pendingBatch.startEpoch >= faultyParentBatch.startEpoch &&
721
782
  pendingBatch.startEpoch < batch.startEpoch &&
722
783
  pendingBatch.state.status === BatchStatus.AwaitingValidation
723
784
  ) {
724
785
  this.logger.verbose("Batch validation error", {id: this.logId, ...pendingBatch.getMetadata()});
725
786
  pendingBatch.validationError(res.err);
726
- invalidatedCount++;
727
787
  }
728
788
  }
729
- return invalidatedCount;
730
789
  };
731
790
 
732
- const prevBatch = this.batches.get(batch.startEpoch - EPOCHS_PER_BATCH);
733
- switch (classifyProcessingFault(res.err, batch, prevBatch)) {
791
+ const fault = classifyProcessingFault(res.err, batch, parentBatch);
792
+ switch (fault.kind) {
734
793
  case ProcessingFaultKind.CurrentBatch:
735
794
  batch.processingError(res.err);
736
795
  break;
737
796
 
738
- case ProcessingFaultKind.PreviousBatch:
739
- // If no previous batch was invalidated, it means the error is on this batch
740
- if (invalidatePreviousBatches() === 0) {
741
- batch.processingError(res.err);
742
- } else {
743
- batch.retainForReprocessing();
744
- }
797
+ case ProcessingFaultKind.ParentBatch:
798
+ invalidateFromParentBatch(fault.parentBatch);
799
+ batch.retainForReprocessing();
745
800
  break;
746
801
 
747
802
  case ProcessingFaultKind.Ambiguous:
748
803
  batch.processingError(res.err);
749
- invalidatePreviousBatches();
804
+ invalidateFromParentBatch(fault.parentBatch);
750
805
  break;
751
806
  }
752
807
  }
@@ -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 {BlockError} from "../../chain/errors/index.js";
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())
@@ -408,6 +461,7 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
408
461
  reportPeer: this.reportPeer,
409
462
  getConnectedPeerSyncMeta: this.getConnectedPeerSyncMeta,
410
463
  pruneBlockInputs: this.pruneBlockInputs,
464
+ hasBlock: (blockRoot) => this.chain.forkChoice.hasBlock(blockRoot),
411
465
  onEnd: this.onSyncChainEnd,
412
466
  },
413
467
  {
@@ -125,39 +125,64 @@ export function isSyncChainDone(batches: Batch[], lastEpochWithProcessBlocks: Ep
125
125
  export enum ProcessingFaultKind {
126
126
  /** This batch's own blocks are bad or incomplete, so redownload this batch. */
127
127
  CurrentBatch = "CurrentBatch",
128
- /** This batch is valid, but a previous batch did not deliver the parent. */
129
- PreviousBatch = "PreviousBatch",
130
- /** The fault could be in this batch or a previous batch, so hedge both ways. */
128
+ /** This batch is valid, but the parent batch did not deliver the parent. */
129
+ ParentBatch = "ParentBatch",
130
+ /** The fault could be in this batch or the parent batch, so hedge both ways. */
131
131
  Ambiguous = "Ambiguous",
132
132
  }
133
133
 
134
+ export type ProcessingFault =
135
+ | {kind: ProcessingFaultKind.CurrentBatch}
136
+ | {kind: ProcessingFaultKind.ParentBatch; parentBatch: Batch}
137
+ | {kind: ProcessingFaultKind.Ambiguous; parentBatch: Batch};
138
+
134
139
  /**
135
140
  * Classify a processChainSegment error for a batch so range sync only redownloads
136
141
  * the batch or batches that could actually be at fault.
137
142
  * Note that range sync is supposed to only handle linear chain, for forky condition we'll handle it via backward sync
138
143
  * in BlockInputSync or the upcoming/TODO beacon_blocks_by_head req/resp.
144
+ *
145
+ * `parentBatch` is the last batch with blocks before `batch`, it may be followed by 0-block batches. If no batch with
146
+ * blocks awaits validation, it is the first 0-block batch before `batch`.
147
+ * Epochs with 0 blocks usually happen on devnets, during periods of poor liveness when no proposer gets a block
148
+ * included for whole epochs. A wrong 0-block response only shows up as an unknown parent in `batch`, so with 0-block
149
+ * batches in between the fault is never pinned on `batch` alone: `parentBatch` and the 0-block batches after it are
150
+ * downloaded again.
139
151
  */
140
- export function classifyProcessingFault(err: Error, batch: Batch, prevBatch: Batch | undefined): ProcessingFaultKind {
152
+ export function classifyProcessingFault(err: Error, batch: Batch, parentBatch: Batch | undefined): ProcessingFault {
141
153
  if (!(err instanceof BlockError)) {
142
- return ProcessingFaultKind.CurrentBatch;
154
+ return {kind: ProcessingFaultKind.CurrentBatch};
143
155
  }
144
156
 
145
157
  const {code} = err.type;
146
158
  if (code !== BlockErrorCode.PARENT_BLOCK_UNKNOWN && code !== BlockErrorCode.PARENT_PAYLOAD_UNKNOWN) {
147
- return ProcessingFaultKind.CurrentBatch;
159
+ return {kind: ProcessingFaultKind.CurrentBatch};
160
+ }
161
+
162
+ if (parentBatch === undefined) {
163
+ return {kind: ProcessingFaultKind.CurrentBatch};
148
164
  }
149
165
 
150
166
  const firstErrorSlot = err.signedBlock.message.slot;
151
167
  if (firstErrorSlot === batch.startSlot) {
152
- // note that range sync is for linear chain, so this is clearly an issue of the previous batch
153
- return prevBatch === undefined ? ProcessingFaultKind.CurrentBatch : ProcessingFaultKind.PreviousBatch;
168
+ // note that range sync is for linear chain, so this is clearly an issue of the parent batch
169
+ return {kind: ProcessingFaultKind.ParentBatch, parentBatch};
154
170
  }
155
171
 
156
- const prevTailSlot = batch.startSlot - 1;
157
- const prevHasFullTail =
172
+ // `parentBatch` can be followed by 0-block batches, so its tail is the end of its own range
173
+ const parentTailSlot = parentBatch.startSlot + parentBatch.count - 1;
174
+ const parentHasFullTail =
158
175
  code === BlockErrorCode.PARENT_PAYLOAD_UNKNOWN
159
- ? (prevBatch?.getPayloadEnvelopes()?.get(prevTailSlot)?.hasPayloadEnvelope() ?? false)
160
- : (prevBatch?.getBlocks().some((b) => b.slot === prevTailSlot) ?? false);
176
+ ? (parentBatch.getPayloadEnvelopes()?.get(parentTailSlot)?.hasPayloadEnvelope() ?? false)
177
+ : parentBatch.getBlocks().some((b) => b.slot === parentTailSlot);
161
178
 
162
- return prevHasFullTail ? ProcessingFaultKind.CurrentBatch : ProcessingFaultKind.Ambiguous;
179
+ // With 0-block batches in between, it could be a wrong 0-block batch
180
+ const hasZeroBlockGap = parentTailSlot + 1 < batch.startSlot;
181
+
182
+ // Assumes the tail block / payload delivered by `parentBatch` is canonical. If it is orphaned, `parentBatch` is at
183
+ // fault: `batch` keeps failing until MAX_PROCESSING_ATTEMPTS and a new SyncChain is created.
184
+ if (parentHasFullTail && !hasZeroBlockGap) {
185
+ return {kind: ProcessingFaultKind.CurrentBatch};
186
+ }
187
+ return {kind: ProcessingFaultKind.Ambiguous, parentBatch};
163
188
  }
@@ -52,7 +52,7 @@ export class ChainPeersBalancer {
52
52
 
53
53
  /**
54
54
  * Return the most suitable peer to retry
55
- * Sort peers by (1) less active requests (2) most columns we need.
55
+ * Sort peers by (1) fewer failed attempts for this batch (2) less active requests (3) most columns we need.
56
56
  * Peers that failed this batch or already succeeded for the same request are excluded inside `filterPeers`.
57
57
  */
58
58
  bestPeerToRetryBatch(batch: Batch): PeerSyncMeta | undefined {
@@ -63,9 +63,12 @@ export class ChainPeersBalancer {
63
63
  // TODO(fulu): This is fulu specific and hinders our peer selection PreFulu
64
64
  const pendingDataColumns = columnsRequest?.columns ?? this.custodyConfig.sampledColumns;
65
65
  const eligiblePeers = this.filterPeers(batch, pendingDataColumns, false);
66
+ const triedPeerCounts = batch.getTriedPeerCounts();
66
67
 
67
68
  const sortedBestPeers = sortBy(
68
69
  eligiblePeers,
70
+ // prefer peers not tried for this batch yet
71
+ ({syncInfo}) => triedPeerCounts.get(syncInfo.peerId) ?? 0,
69
72
  ({syncInfo}) => this.activeRequestsByPeer.get(syncInfo.peerId) ?? 0, // prefer peers with least active req
70
73
  ({columns}) => -1 * columns // prefer peers with the most columns
71
74
  );
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;
@@ -161,6 +172,8 @@ export type PendingPayloadRootHex = {
161
172
  export type PendingPayloadEnvelope = {
162
173
  status: PendingPayloadInputStatus.waitingForBlock;
163
174
  envelope: SignedExecutionPayloadEnvelope;
175
+ /** Peer that served the envelope, undefined if it was already cached */
176
+ envelopePeerId?: string;
164
177
  timeAddedSec: number;
165
178
  peerIdStrings: Set<string>;
166
179
  };