@lodestar/beacon-node 1.51.0-dev.5a82190416 → 1.51.0-dev.6375077e13

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 (46) hide show
  1. package/lib/chain/archiveStore/archiveStore.d.ts.map +1 -1
  2. package/lib/chain/archiveStore/archiveStore.js +3 -4
  3. package/lib/chain/archiveStore/archiveStore.js.map +1 -1
  4. package/lib/chain/archiveStore/utils/pruneHistory.d.ts +9 -2
  5. package/lib/chain/archiveStore/utils/pruneHistory.d.ts.map +1 -1
  6. package/lib/chain/archiveStore/utils/pruneHistory.js +12 -4
  7. package/lib/chain/archiveStore/utils/pruneHistory.js.map +1 -1
  8. package/lib/chain/blocks/verifyBlocksSignatures.d.ts.map +1 -1
  9. package/lib/chain/blocks/verifyBlocksSignatures.js +3 -0
  10. package/lib/chain/blocks/verifyBlocksSignatures.js.map +1 -1
  11. package/lib/chain/bls/multithread/index.d.ts +1 -1
  12. package/lib/chain/bls/multithread/index.d.ts.map +1 -1
  13. package/lib/chain/bls/multithread/index.js +37 -11
  14. package/lib/chain/bls/multithread/index.js.map +1 -1
  15. package/lib/chain/validation/aggregateAndProof.js +1 -1
  16. package/lib/chain/validation/aggregateAndProof.js.map +1 -1
  17. package/lib/chain/validation/attestation.js +1 -1
  18. package/lib/chain/validation/attestation.js.map +1 -1
  19. package/lib/chain/validation/syncCommittee.d.ts.map +1 -1
  20. package/lib/chain/validation/syncCommittee.js +16 -10
  21. package/lib/chain/validation/syncCommittee.js.map +1 -1
  22. package/lib/chain/validation/syncCommitteeContributionAndProof.d.ts.map +1 -1
  23. package/lib/chain/validation/syncCommitteeContributionAndProof.js +11 -0
  24. package/lib/chain/validation/syncCommitteeContributionAndProof.js.map +1 -1
  25. package/lib/network/reqresp/handlers/beaconBlocksByHead.js +1 -1
  26. package/lib/network/reqresp/handlers/beaconBlocksByHead.js.map +1 -1
  27. package/lib/sync/sync.d.ts +8 -0
  28. package/lib/sync/sync.d.ts.map +1 -1
  29. package/lib/sync/sync.js +42 -22
  30. package/lib/sync/sync.js.map +1 -1
  31. package/lib/util/bitArray.d.ts +6 -6
  32. package/lib/util/bitArray.d.ts.map +1 -1
  33. package/lib/util/bitArray.js +25 -50
  34. package/lib/util/bitArray.js.map +1 -1
  35. package/package.json +17 -17
  36. package/src/chain/archiveStore/archiveStore.ts +5 -4
  37. package/src/chain/archiveStore/utils/pruneHistory.ts +16 -6
  38. package/src/chain/blocks/verifyBlocksSignatures.ts +3 -0
  39. package/src/chain/bls/multithread/index.ts +34 -14
  40. package/src/chain/validation/aggregateAndProof.ts +1 -1
  41. package/src/chain/validation/attestation.ts +1 -1
  42. package/src/chain/validation/syncCommittee.ts +31 -15
  43. package/src/chain/validation/syncCommitteeContributionAndProof.ts +13 -0
  44. package/src/network/reqresp/handlers/beaconBlocksByHead.ts +1 -1
  45. package/src/sync/sync.ts +43 -22
  46. package/src/util/bitArray.ts +22 -43
@@ -54,6 +54,9 @@ export async function verifyBlocksSignatures(
54
54
  // attestations already buffer up to `MAX_BUFFER_WAIT_MS`
55
55
  {priority: true}
56
56
  );
57
+ // Handlers are attached after the loop, which yields. A rejection in between, e.g. the verifier closing,
58
+ // must not be unhandled. `rejectFirstInvalidResolveAllValid()` still observes it.
59
+ isValidPromises[i].catch(() => {});
57
60
 
58
61
  // getBlockSignatureSets() takes 45ms in benchmarks for 2022Q2 mainnet blocks (100 sigs). When syncing a 32 blocks
59
62
  // segments it will block the event loop for 1400 ms, which is too much. This call will allow the event loop to
@@ -7,7 +7,7 @@ import {Worker, spawn} from "@chainsafe/threads";
7
7
  self = undefined;
8
8
 
9
9
  import {ISignatureSet} from "@lodestar/state-transition";
10
- import {Logger} from "@lodestar/utils";
10
+ import {Logger, defer} from "@lodestar/utils";
11
11
  import {Metrics} from "../../../metrics/index.js";
12
12
  import {LinkedList} from "../../../util/array.js";
13
13
  import {callInNextEventLoop} from "../../../util/eventLoop.js";
@@ -94,7 +94,7 @@ type WorkerStatus =
94
94
  | {code: WorkerStatusCode.initializing; initPromise: Promise<WorkerApi>}
95
95
  | {code: WorkerStatusCode.initializationError; error: Error}
96
96
  | {code: WorkerStatusCode.idle; workerApi: WorkerApi}
97
- | {code: WorkerStatusCode.running; workerApi: WorkerApi};
97
+ | {code: WorkerStatusCode.running; workerApi: WorkerApi; abort: () => void};
98
98
 
99
99
  type WorkerDescriptor = {
100
100
  worker: Worker;
@@ -126,7 +126,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
126
126
  timeout: NodeJS.Timeout;
127
127
  } | null = null;
128
128
  private blsVerifyAllMultiThread: boolean;
129
- private closed = false;
129
+ private closePromise: Promise<void> | undefined;
130
130
  private workersBusy = 0;
131
131
 
132
132
  constructor(options: BlsMultiThreadWorkerPoolOptions, modules: BlsMultiThreadWorkerPoolModules) {
@@ -149,6 +149,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
149
149
 
150
150
  canAcceptWork(): boolean {
151
151
  return (
152
+ this.closePromise === undefined &&
152
153
  this.workersBusy < blsPoolSize &&
153
154
  // TODO: Should also bound the jobs queue?
154
155
  this.jobs.length < MAX_JOBS_CAN_ACCEPT_WORK
@@ -156,6 +157,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
156
157
  }
157
158
 
158
159
  async verifySignatureSets(sets: ISignatureSet[], opts: VerifySignatureOpts = {}): Promise<boolean> {
160
+ if (this.closePromise !== undefined) throw new QueueError({code: QueueErrorCode.QUEUE_ABORTED});
159
161
  this.metrics?.bls.aggregatedPubkeys.inc(getAggregatedPubkeysCount(sets));
160
162
  this.metrics?.blsThreadPool.totalSigSets.inc(sets.length);
161
163
  if (opts.priority) {
@@ -208,6 +210,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
208
210
  message: Uint8Array,
209
211
  opts: Omit<VerifySignatureOpts, "verifyOnMainThread"> = {}
210
212
  ): Promise<boolean[]> {
213
+ if (this.closePromise !== undefined) throw new QueueError({code: QueueErrorCode.QUEUE_ABORTED});
211
214
  const promises: Promise<boolean[]>[] = [];
212
215
  for (const setsChunk of chunkSameMessageSignatureSets(sets)) {
213
216
  promises.push(
@@ -229,9 +232,14 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
229
232
  return results.flat();
230
233
  }
231
234
 
232
- async close(): Promise<void> {
235
+ close(): Promise<void> {
236
+ if (this.closePromise !== undefined) return this.closePromise;
233
237
  if (this.bufferedJobs) {
234
238
  clearTimeout(this.bufferedJobs.timeout);
239
+ for (const jobs of [this.bufferedJobs.jobs, this.bufferedJobs.prioritizedJobs]) {
240
+ for (const job of jobs) job.reject(new QueueError({code: QueueErrorCode.QUEUE_ABORTED}));
241
+ }
242
+ this.bufferedJobs = null;
235
243
  }
236
244
 
237
245
  // Abort all jobs
@@ -241,15 +249,21 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
241
249
  this.jobs.clear();
242
250
 
243
251
  // Terminate all workers. await to ensure no workers are left hanging
244
- await Promise.all(
252
+ this.closePromise = Promise.all(
245
253
  Array.from(this.workers.entries()).map(([id, worker]) =>
246
254
  // NOTE: 'threads' has not yet updated types, and NodeJS complains with
247
255
  // [DEP0132] DeprecationWarning: Passing a callback to worker.terminate() is deprecated. It returns a Promise instead.
248
- (worker.worker.terminate() as unknown as Promise<void>).catch((e: Error) => {
249
- this.logger.error("Error terminating worker", {id}, e);
250
- })
256
+ (worker.worker.terminate() as unknown as Promise<void>)
257
+ .catch((e: Error) => {
258
+ this.logger.error("Error terminating worker", {id}, e);
259
+ })
260
+ .finally(() => {
261
+ // threads does not settle outstanding RPC calls when its worker exits.
262
+ if (worker.status.code === WorkerStatusCode.running) worker.status.abort();
263
+ })
251
264
  )
252
- );
265
+ ).then(() => {});
266
+ return this.closePromise;
253
267
  }
254
268
 
255
269
  private createWorkers(poolSize: number): WorkerDescriptor[] {
@@ -295,7 +309,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
295
309
  * Register BLS work to be done eventually in a worker
296
310
  */
297
311
  private queueBlsWork(job: JobQueueItem): void {
298
- if (this.closed) {
312
+ if (this.closePromise !== undefined) {
299
313
  throw new QueueError({code: QueueErrorCode.QUEUE_ABORTED});
300
314
  }
301
315
 
@@ -354,7 +368,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
354
368
  * Potentially submit jobs to an idle worker, only if there's a worker and jobs
355
369
  */
356
370
  private runJob = async (): Promise<void> => {
357
- if (this.closed) {
371
+ if (this.closePromise !== undefined) {
358
372
  return;
359
373
  }
360
374
 
@@ -375,7 +389,12 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
375
389
  // Maybe it's not useful since all data referenced in jobs is likely referenced by others
376
390
 
377
391
  const workerApi = worker.status.workerApi;
378
- worker.status = {code: WorkerStatusCode.running, workerApi};
392
+ const completion = defer<BlsWorkResult>();
393
+ worker.status = {
394
+ code: WorkerStatusCode.running,
395
+ workerApi,
396
+ abort: () => completion.reject(new QueueError({code: QueueErrorCode.QUEUE_ABORTED})),
397
+ };
379
398
  this.workersBusy++;
380
399
  const jobsStarted: JobQueueItem[] = [];
381
400
  let completedJobsCount = 0;
@@ -441,7 +460,8 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
441
460
  // If worker communication or result processing throws, all unresolved jobs are rejected.
442
461
 
443
462
  const [jobStartSec, jobStartNs] = process.hrtime();
444
- const workResult = await workerApi.verifyManySignatureSets(workReqs);
463
+ void workerApi.verifyManySignatureSets(workReqs).then(completion.resolve, completion.reject);
464
+ const workResult = await completion.promise;
445
465
  const [jobEndSec, jobEndNs] = process.hrtime();
446
466
  const {workerId, batchRetries, batchSigsSuccess, verificationCalls, workerStartTime, workerEndTime, results} =
447
467
  workResult;
@@ -485,7 +505,7 @@ export class BlsMultiThreadWorkerPool implements IBlsVerifier {
485
505
  }
486
506
  } catch (e) {
487
507
  // Worker communications should never reject
488
- if (!this.closed) {
508
+ if (this.closePromise === undefined) {
489
509
  this.logger.error("BlsMultiThreadWorkerPool error", {}, e as Error);
490
510
  }
491
511
  // Reject jobs that were not already completed
@@ -72,7 +72,7 @@ async function validateAggregateAndProof(
72
72
  const attSlot = attData.slot;
73
73
 
74
74
  let committeeIndex: number | null;
75
- if (ForkSeq[fork] >= ForkSeq.gloas) {
75
+ if (ForkSeq[fork] >= ForkSeq.gloas && chain.config.getForkSeq(attSlot) >= ForkSeq.gloas) {
76
76
  // [REJECT] `aggregate.data.index < 2`.
77
77
  if (attData.index >= 2) {
78
78
  throw new AttestationError(GossipAction.REJECT, {
@@ -291,7 +291,7 @@ async function validateAttestationNoSignatureCheck(
291
291
  // api or first time validation of a gossip attestation
292
292
  committeeIndex = attestationOrCache.attestation.committeeIndex;
293
293
 
294
- if (isForkPostGloas(fork)) {
294
+ if (isForkPostGloas(fork) && isForkPostGloas(chain.config.getForkName(attSlot))) {
295
295
  // [REJECT] `attestation.data.index < 2`.
296
296
  if (attData.index >= 2) {
297
297
  throw new AttestationError(GossipAction.REJECT, {
@@ -1,7 +1,8 @@
1
1
  import {SYNC_COMMITTEE_SUBNET_COUNT, SYNC_COMMITTEE_SUBNET_SIZE} from "@lodestar/params";
2
2
  import {IBeaconStateView, isStatePostAltair} from "@lodestar/state-transition";
3
- import {SubnetID, altair} from "@lodestar/types";
3
+ import {RootHex, Slot, SubnetID, ValidatorIndex, altair} from "@lodestar/types";
4
4
  import {toRootHex} from "@lodestar/utils";
5
+ import {Metrics} from "../../metrics/metrics.js";
5
6
  import {GossipAction, SyncCommitteeError, SyncCommitteeErrorCode} from "../errors/index.js";
6
7
  import {IBeaconChain} from "../interface.js";
7
8
  import {getSyncCommitteeSignatureSet} from "./signatureSets/index.js";
@@ -32,6 +33,33 @@ export async function validateGossipSyncCommittee(
32
33
 
33
34
  // [IGNORE] There has been no other valid sync committee signature for the declared slot for the validator referenced
34
35
  // by sync_committee_signature.validator_index.
36
+ assertSyncCommitteeMessageNotKnown(chain, slot, subnet, validatorIndex, messageRoot, chain.metrics);
37
+
38
+ // [REJECT] The subnet_id is valid for the given validator, i.e. subnet_id in compute_subnets_for_sync_committee(state, sync_committee_signature.validator_index).
39
+ // Note this validation implies the validator is part of the broader current sync committee along with the correct subcommittee.
40
+ // > Checked in validateGossipSyncCommitteeExceptSig()
41
+
42
+ // [REJECT] The signature is valid for the message beacon_block_root for the validator referenced by validator_index.
43
+ await validateSyncCommitteeSigOnly(chain, headState, syncCommittee);
44
+
45
+ // Re-check after the await: concurrent messages from the same validator all pass the check above.
46
+ // Metrics were already counted by the first check.
47
+ assertSyncCommitteeMessageNotKnown(chain, slot, subnet, validatorIndex, messageRoot, null);
48
+
49
+ // Register this valid item as seen
50
+ chain.seenSyncCommitteeMessages.add(slot, subnet, validatorIndex, messageRoot);
51
+
52
+ return {indicesInSubcommittee};
53
+ }
54
+
55
+ function assertSyncCommitteeMessageNotKnown(
56
+ chain: IBeaconChain,
57
+ slot: Slot,
58
+ subnet: SubnetID,
59
+ validatorIndex: ValidatorIndex,
60
+ messageRoot: RootHex,
61
+ metrics: Metrics | null
62
+ ): void {
35
63
  const prevRoot = chain.seenSyncCommitteeMessages.get(slot, subnet, validatorIndex);
36
64
  if (prevRoot) {
37
65
  let shouldIgnore = false;
@@ -39,9 +67,9 @@ export async function validateGossipSyncCommittee(
39
67
  shouldIgnore = true;
40
68
  } else {
41
69
  const headRoot = chain.forkChoice.getHeadRoot();
42
- chain.metrics?.gossipSyncCommittee.equivocationCount.inc();
70
+ metrics?.gossipSyncCommittee.equivocationCount.inc();
43
71
  if (messageRoot === headRoot) {
44
- chain.metrics?.gossipSyncCommittee.equivocationToHeadCount.inc();
72
+ metrics?.gossipSyncCommittee.equivocationToHeadCount.inc();
45
73
  } else {
46
74
  shouldIgnore = true;
47
75
  }
@@ -57,18 +85,6 @@ export async function validateGossipSyncCommittee(
57
85
  });
58
86
  }
59
87
  }
60
-
61
- // [REJECT] The subnet_id is valid for the given validator, i.e. subnet_id in compute_subnets_for_sync_committee(state, sync_committee_signature.validator_index).
62
- // Note this validation implies the validator is part of the broader current sync committee along with the correct subcommittee.
63
- // > Checked in validateGossipSyncCommitteeExceptSig()
64
-
65
- // [REJECT] The signature is valid for the message beacon_block_root for the validator referenced by validator_index.
66
- await validateSyncCommitteeSigOnly(chain, headState, syncCommittee);
67
-
68
- // Register this valid item as seen
69
- chain.seenSyncCommitteeMessages.add(slot, subnet, validatorIndex, messageRoot);
70
-
71
- return {indicesInSubcommittee};
72
88
  }
73
89
 
74
90
  export async function validateApiSyncCommittee(
@@ -92,6 +92,19 @@ export async function validateSyncCommitteeGossipContributionAndProof(
92
92
  });
93
93
  }
94
94
 
95
+ // Re-check after the await: concurrent contributions from the same aggregator all pass the checks above
96
+ if (chain.seenContributionAndProof.isAggregatorKnown(slot, subcommitteeIndex, aggregatorIndex)) {
97
+ throw new SyncCommitteeError(GossipAction.IGNORE, {
98
+ code: SyncCommitteeErrorCode.SYNC_COMMITTEE_AGGREGATOR_ALREADY_KNOWN,
99
+ });
100
+ }
101
+
102
+ if (!skipValidationKnownParticipants && chain.seenContributionAndProof.participantsKnown(contribution)) {
103
+ throw new SyncCommitteeError(GossipAction.IGNORE, {
104
+ code: SyncCommitteeErrorCode.SYNC_COMMITTEE_PARTICIPANTS_ALREADY_KNOWN,
105
+ });
106
+ }
107
+
95
108
  // no need to add to seenSyncCommittteeContributionCache here, gossip handler will do that
96
109
  chain.seenContributionAndProof.add(contributionAndProof, syncCommitteeParticipantIndices.length);
97
110
 
@@ -23,7 +23,7 @@ export async function* onBeaconBlocksByHead(
23
23
  let blockRootHex = requestedRootHex;
24
24
  const minimumRequestEpoch = Math.max(
25
25
  GENESIS_EPOCH,
26
- chain.clock.currentEpoch - chain.config.MIN_EPOCHS_FOR_BLOCK_REQUESTS
26
+ chain.clock.currentEpoch - chain.config.getMinEpochsForBlockRequests(chain.clock.currentEpoch)
27
27
  );
28
28
  const minimumRequestSlot = computeStartSlotAtEpoch(minimumRequestEpoch);
29
29
 
package/src/sync/sync.ts CHANGED
@@ -33,6 +33,8 @@ export class BeaconSync implements IBeaconSync {
33
33
  /** For metrics only */
34
34
  private readonly peerSyncType = new Map<string, PeerSyncType>();
35
35
  private readonly slotImportTolerance: Slot;
36
+ /** The requested subscription state, set while a change is being applied */
37
+ private pendingGossipSubscribed: boolean | undefined;
36
38
 
37
39
  constructor(opts: SyncOptions, modules: SyncModules) {
38
40
  const {config, chain, db, metrics, network, logger} = modules;
@@ -89,6 +91,7 @@ export class BeaconSync implements IBeaconSync {
89
91
  }
90
92
 
91
93
  close(): void {
94
+ this.pendingGossipSubscribed = undefined;
92
95
  this.network.events.off(NetworkEvent.peerConnected, this.addPeer);
93
96
  this.network.events.off(NetworkEvent.peerDisconnected, this.removePeer);
94
97
  this.chain.clock.off(ClockEvent.epoch, this.onClockEpoch);
@@ -296,17 +299,7 @@ export class BeaconSync implements IBeaconSync {
296
299
  if (state === SyncState.Synced && this.chain.clock.currentEpoch >= MIN_EPOCH_TO_START_GOSSIP) {
297
300
  this.chain.forkChoice.resumeFastConfirmation();
298
301
 
299
- if (!this.network.isSubscribedToGossipCoreTopics()) {
300
- this.network
301
- .subscribeGossipCoreTopics()
302
- .then(() => {
303
- this.metrics?.syncSwitchGossipSubscriptions.inc({action: "subscribed"});
304
- this.logger.info("Subscribed gossip core topics");
305
- })
306
- .catch((e) => {
307
- this.logger.error("Error subscribing to gossip core topics", {}, e);
308
- });
309
- }
302
+ this.setGossipSubscription(true);
310
303
 
311
304
  // also start searching for unknown blocks
312
305
  if (!this.unknownBlockSync.isSubscribedToNetwork()) {
@@ -322,17 +315,8 @@ export class BeaconSync implements IBeaconSync {
322
315
  // Same debounce as gossip: transient blips keep the rule running, only a real gap pauses it
323
316
  this.chain.forkChoice.pauseFastConfirmation();
324
317
 
325
- if (this.network.isSubscribedToGossipCoreTopics()) {
326
- this.logger.warn(`Node sync has fallen behind by ${syncDiff} slots`);
327
- this.network
328
- .unsubscribeGossipCoreTopics()
329
- .then(() => {
330
- this.metrics?.syncSwitchGossipSubscriptions.inc({action: "unsubscribed"});
331
- this.logger.info("Un-subscribed gossip core topics");
332
- })
333
- .catch((e) => {
334
- this.logger.error("Error unsubscribing to gossip core topics", {}, e);
335
- });
318
+ if (this.setGossipSubscription(false)) {
319
+ this.logger.warn("Node sync has fallen behind", {slots: syncDiff});
336
320
  }
337
321
 
338
322
  // also stop searching for unknown blocks
@@ -344,6 +328,43 @@ export class BeaconSync implements IBeaconSync {
344
328
  }
345
329
  };
346
330
 
331
+ private setGossipSubscription(subscribe: boolean): boolean {
332
+ if ((this.pendingGossipSubscribed ?? this.network.isSubscribedToGossipCoreTopics()) === subscribe) return false;
333
+ const applying = this.pendingGossipSubscribed !== undefined;
334
+ this.pendingGossipSubscribed = subscribe;
335
+ if (!applying) void this.applyGossipSubscription();
336
+ return true;
337
+ }
338
+
339
+ /**
340
+ * Applies the latest requested state one request at a time. Network skips a subscribe issued while an unsubscribe is
341
+ * in flight, so opposite requests must not overlap.
342
+ */
343
+ private async applyGossipSubscription(): Promise<void> {
344
+ for (;;) {
345
+ const subscribe = this.pendingGossipSubscribed;
346
+ if (subscribe === undefined || subscribe === this.network.isSubscribedToGossipCoreTopics()) break;
347
+ try {
348
+ await (subscribe ? this.network.subscribeGossipCoreTopics() : this.network.unsubscribeGossipCoreTopics());
349
+ } catch (e) {
350
+ this.logger.error(
351
+ subscribe ? "Error subscribing to gossip core topics" : "Error unsubscribing to gossip core topics",
352
+ {},
353
+ e as Error
354
+ );
355
+ // Leave a retry of the same request to the next sync state update
356
+ if (this.pendingGossipSubscribed === subscribe) break;
357
+ continue;
358
+ }
359
+ if (this.pendingGossipSubscribed === undefined || this.network.isSubscribedToGossipCoreTopics() !== subscribe) {
360
+ break;
361
+ }
362
+ this.metrics?.syncSwitchGossipSubscriptions.inc({action: subscribe ? "subscribed" : "unsubscribed"});
363
+ this.logger.info(subscribe ? "Subscribed gossip core topics" : "Un-subscribed gossip core topics");
364
+ }
365
+ this.pendingGossipSubscribed = undefined;
366
+ }
367
+
347
368
  private onClockEpoch = (): void => {
348
369
  // If a node witness the genesis event consider starting gossip
349
370
  // Also, ensure that updateSyncState is run at least once per epoch.
@@ -22,63 +22,42 @@ export enum IntersectResult {
22
22
  * | 11110000 | 00001111 | A exclude B |
23
23
  * | 11111100 | 00111111 | A intersect B |
24
24
  *
25
- * For all bytes in BitArray:
26
- * - equals = MAYBE ONLY equals
27
- * - excludes = MUST ONLY equals
28
- * - superset = MUST superset MAYBE equal
29
- * - subset = MUST subset MAYBE equal
30
- * - intersect = any other condition
25
+ * For all bytes in BitArray, bytes missing from the shorter array count as zero:
26
+ * - equals = neither has a bit the other lacks
27
+ * - superset = only A has bits B lacks
28
+ * - subset = only B has bits A lacks
29
+ * - exclude = both have bits the other lacks, none shared
30
+ * - intersect = both have bits the other lacks, some shared
31
31
  */
32
32
  export function intersectUint8Arrays(aUA: Uint8Array, bUA: Uint8Array): IntersectResult {
33
- const len = aUA.length;
33
+ const len = Math.max(aUA.length, bUA.length);
34
34
 
35
- let someEquals = false;
36
- let someExcludes = false;
37
- let someSuperset = false;
38
- let someSubset = false;
35
+ let aHasExtra = false;
36
+ let bHasExtra = false;
37
+ let hasShared = false;
39
38
 
40
39
  for (let i = 0; i < len; i++) {
41
- const a = aUA[i];
42
- const b = bUA[i];
40
+ const a = aUA[i] ?? 0;
41
+ const b = bUA[i] ?? 0;
43
42
 
44
- if (a === 0 && b === 0) {
45
- // zero, skip
46
- } else if (a === b) {
47
- // A equals B
48
- someEquals = true;
49
- } else if ((a & b) === 0) {
50
- // A excludes B
51
- someExcludes = true;
52
- } else if ((a & b) === b) {
53
- // A superset B
54
- if (someSubset) return IntersectResult.Intersect;
55
- someSuperset = true;
56
- } else if ((a & b) === a) {
57
- // A subset B
58
- if (someSuperset) return IntersectResult.Intersect;
59
- someSubset = true;
60
- } else {
61
- // A diff B
62
- return IntersectResult.Intersect;
63
- }
43
+ if ((a & b) !== 0) hasShared = true;
44
+ if ((a & ~b) !== 0) aHasExtra = true;
45
+ if ((b & ~a) !== 0) bHasExtra = true;
46
+
47
+ if (aHasExtra && bHasExtra && hasShared) return IntersectResult.Intersect;
64
48
  }
65
49
 
66
- // equals = MAYBE ONLY equals
67
- if (!someExcludes && !someSuperset && !someSubset) return IntersectResult.Equal;
68
- // excludes = MUST ONLY equals
69
- if (!someEquals && someExcludes && !someSuperset && !someSubset) return IntersectResult.Exclusive;
70
- // superset = MUST superset MAYBE equal
71
- if (!someExcludes && someSuperset && !someSubset) return IntersectResult.Superset;
72
- // subset = MUST subset MAYBE equal
73
- if (!someExcludes && !someSuperset && someSubset) return IntersectResult.Subset;
74
- // intersect = any other condition
75
- return IntersectResult.Intersect;
50
+ if (!aHasExtra && !bHasExtra) return IntersectResult.Equal;
51
+ if (!bHasExtra) return IntersectResult.Superset;
52
+ if (!aHasExtra) return IntersectResult.Subset;
53
+ return hasShared ? IntersectResult.Intersect : IntersectResult.Exclusive;
76
54
  }
77
55
 
78
56
  /**
79
57
  * Check if first BitArray is equal to or superset of the second
80
58
  */
81
59
  export function isSuperSetOrEqual(superSet: BitArray, toCheck: BitArray): boolean {
60
+ if (superSet.bitLen !== toCheck.bitLen) return false;
82
61
  const intersectionResult = intersectUint8Arrays(superSet.uint8Array, toCheck.uint8Array);
83
62
  return intersectionResult === IntersectResult.Superset || intersectionResult === IntersectResult.Equal;
84
63
  }