@lodestar/beacon-node 1.47.0-dev.16cb8ec36c → 1.47.0-dev.1c8babbe6e

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 (175) hide show
  1. package/lib/api/impl/beacon/blocks/index.js +1 -1
  2. package/lib/api/impl/beacon/blocks/index.js.map +1 -1
  3. package/lib/api/impl/beacon/pool/index.d.ts.map +1 -1
  4. package/lib/api/impl/beacon/pool/index.js +8 -1
  5. package/lib/api/impl/beacon/pool/index.js.map +1 -1
  6. package/lib/api/impl/beacon/state/utils.d.ts +2 -1
  7. package/lib/api/impl/beacon/state/utils.d.ts.map +1 -1
  8. package/lib/api/impl/beacon/state/utils.js.map +1 -1
  9. package/lib/api/impl/events/index.d.ts.map +1 -1
  10. package/lib/api/impl/events/index.js +2 -0
  11. package/lib/api/impl/events/index.js.map +1 -1
  12. package/lib/api/rest/base.d.ts.map +1 -1
  13. package/lib/api/rest/base.js +7 -0
  14. package/lib/api/rest/base.js.map +1 -1
  15. package/lib/chain/blocks/importExecutionPayload.d.ts +3 -2
  16. package/lib/chain/blocks/importExecutionPayload.d.ts.map +1 -1
  17. package/lib/chain/blocks/importExecutionPayload.js +11 -9
  18. package/lib/chain/blocks/importExecutionPayload.js.map +1 -1
  19. package/lib/chain/blocks/index.d.ts.map +1 -1
  20. package/lib/chain/blocks/index.js +14 -6
  21. package/lib/chain/blocks/index.js.map +1 -1
  22. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.d.ts.map +1 -1
  23. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.js +22 -12
  24. package/lib/chain/blocks/verifyBlocksStateTransitionOnly.js.map +1 -1
  25. package/lib/chain/blocks/verifyExecutionPayloadEnvelope.d.ts +2 -2
  26. package/lib/chain/blocks/verifyExecutionPayloadEnvelope.d.ts.map +1 -1
  27. package/lib/chain/blocks/verifyExecutionPayloadEnvelope.js +2 -2
  28. package/lib/chain/blocks/verifyExecutionPayloadEnvelope.js.map +1 -1
  29. package/lib/chain/bls/index.d.ts +1 -1
  30. package/lib/chain/bls/index.d.ts.map +1 -1
  31. package/lib/chain/bls/interface.d.ts +5 -5
  32. package/lib/chain/bls/interface.d.ts.map +1 -1
  33. package/lib/chain/bls/multithread/index.d.ts +5 -11
  34. package/lib/chain/bls/multithread/index.d.ts.map +1 -1
  35. package/lib/chain/bls/multithread/index.js +116 -101
  36. package/lib/chain/bls/multithread/index.js.map +1 -1
  37. package/lib/chain/bls/multithread/jobItem.d.ts +5 -19
  38. package/lib/chain/bls/multithread/jobItem.d.ts.map +1 -1
  39. package/lib/chain/bls/multithread/jobItem.js +10 -65
  40. package/lib/chain/bls/multithread/jobItem.js.map +1 -1
  41. package/lib/chain/bls/multithread/types.d.ts +29 -8
  42. package/lib/chain/bls/multithread/types.d.ts.map +1 -1
  43. package/lib/chain/bls/multithread/types.js +12 -0
  44. package/lib/chain/bls/multithread/types.js.map +1 -1
  45. package/lib/chain/bls/multithread/utils.d.ts +4 -0
  46. package/lib/chain/bls/multithread/utils.d.ts.map +1 -1
  47. package/lib/chain/bls/multithread/utils.js +10 -0
  48. package/lib/chain/bls/multithread/utils.js.map +1 -1
  49. package/lib/chain/bls/multithread/worker.js +49 -23
  50. package/lib/chain/bls/multithread/worker.js.map +1 -1
  51. package/lib/chain/bls/singleThread.d.ts +4 -10
  52. package/lib/chain/bls/singleThread.d.ts.map +1 -1
  53. package/lib/chain/bls/singleThread.js +8 -45
  54. package/lib/chain/bls/singleThread.js.map +1 -1
  55. package/lib/chain/bls/utils.d.ts +4 -4
  56. package/lib/chain/bls/utils.d.ts.map +1 -1
  57. package/lib/chain/bls/utils.js +18 -19
  58. package/lib/chain/bls/utils.js.map +1 -1
  59. package/lib/chain/chain.d.ts +2 -1
  60. package/lib/chain/chain.d.ts.map +1 -1
  61. package/lib/chain/chain.js +5 -3
  62. package/lib/chain/chain.js.map +1 -1
  63. package/lib/chain/errors/blockError.d.ts +1 -0
  64. package/lib/chain/errors/blockError.d.ts.map +1 -1
  65. package/lib/chain/errors/blockError.js +6 -1
  66. package/lib/chain/errors/blockError.js.map +1 -1
  67. package/lib/chain/interface.d.ts +2 -1
  68. package/lib/chain/interface.d.ts.map +1 -1
  69. package/lib/chain/interface.js.map +1 -1
  70. package/lib/chain/opPools/aggregatedAttestationPool.js +1 -1
  71. package/lib/chain/opPools/aggregatedAttestationPool.js.map +1 -1
  72. package/lib/chain/opPools/attestationPool.d.ts +1 -1
  73. package/lib/chain/opPools/attestationPool.d.ts.map +1 -1
  74. package/lib/chain/opPools/attestationPool.js +1 -1
  75. package/lib/chain/opPools/attestationPool.js.map +1 -1
  76. package/lib/chain/opPools/payloadAttestationPool.js +1 -1
  77. package/lib/chain/opPools/payloadAttestationPool.js.map +1 -1
  78. package/lib/chain/opPools/syncCommitteeMessagePool.js +1 -1
  79. package/lib/chain/opPools/syncCommitteeMessagePool.js.map +1 -1
  80. package/lib/chain/opPools/syncContributionAndProofPool.js +1 -1
  81. package/lib/chain/opPools/syncContributionAndProofPool.js.map +1 -1
  82. package/lib/chain/opPools/utils.d.ts +1 -1
  83. package/lib/chain/opPools/utils.d.ts.map +1 -1
  84. package/lib/chain/opPools/utils.js +1 -1
  85. package/lib/chain/opPools/utils.js.map +1 -1
  86. package/lib/chain/validation/attestation.d.ts.map +1 -1
  87. package/lib/chain/validation/attestation.js +1 -4
  88. package/lib/chain/validation/attestation.js.map +1 -1
  89. package/lib/chain/validation/attesterSlashing.d.ts.map +1 -1
  90. package/lib/chain/validation/attesterSlashing.js +1 -1
  91. package/lib/chain/validation/attesterSlashing.js.map +1 -1
  92. package/lib/chain/validation/executionPayloadBid.d.ts.map +1 -1
  93. package/lib/chain/validation/executionPayloadBid.js +1 -2
  94. package/lib/chain/validation/executionPayloadBid.js.map +1 -1
  95. package/lib/chain/validation/executionPayloadEnvelope.js +1 -1
  96. package/lib/chain/validation/executionPayloadEnvelope.js.map +1 -1
  97. package/lib/chain/validation/payloadAttestationMessage.js +2 -9
  98. package/lib/chain/validation/payloadAttestationMessage.js.map +1 -1
  99. package/lib/chain/validation/proposerPreferences.js +2 -2
  100. package/lib/chain/validation/proposerPreferences.js.map +1 -1
  101. package/lib/chain/validation/proposerSlashing.js +1 -1
  102. package/lib/chain/validation/proposerSlashing.js.map +1 -1
  103. package/lib/metrics/metrics/lodestar.d.ts +38 -18
  104. package/lib/metrics/metrics/lodestar.d.ts.map +1 -1
  105. package/lib/metrics/metrics/lodestar.js +80 -50
  106. package/lib/metrics/metrics/lodestar.js.map +1 -1
  107. package/lib/network/gossip/topic.d.ts +13 -0
  108. package/lib/network/gossip/topic.d.ts.map +1 -1
  109. package/lib/network/gossip/topic.js +37 -0
  110. package/lib/network/gossip/topic.js.map +1 -1
  111. package/lib/network/network.d.ts.map +1 -1
  112. package/lib/network/network.js +22 -43
  113. package/lib/network/network.js.map +1 -1
  114. package/lib/network/util.d.ts +0 -1
  115. package/lib/network/util.d.ts.map +1 -1
  116. package/lib/network/util.js +0 -4
  117. package/lib/network/util.js.map +1 -1
  118. package/lib/node/nodejs.d.ts +2 -1
  119. package/lib/node/nodejs.d.ts.map +1 -1
  120. package/lib/node/nodejs.js.map +1 -1
  121. package/lib/sync/range/batch.d.ts +23 -5
  122. package/lib/sync/range/batch.d.ts.map +1 -1
  123. package/lib/sync/range/batch.js +61 -22
  124. package/lib/sync/range/batch.js.map +1 -1
  125. package/lib/sync/range/chain.d.ts +2 -1
  126. package/lib/sync/range/chain.d.ts.map +1 -1
  127. package/lib/sync/range/chain.js +23 -10
  128. package/lib/sync/range/chain.js.map +1 -1
  129. package/package.json +18 -18
  130. package/src/api/impl/beacon/blocks/index.ts +1 -1
  131. package/src/api/impl/beacon/pool/index.ts +7 -1
  132. package/src/api/impl/beacon/state/utils.ts +2 -1
  133. package/src/api/impl/events/index.ts +3 -1
  134. package/src/api/rest/base.ts +7 -0
  135. package/src/chain/blocks/importExecutionPayload.ts +12 -11
  136. package/src/chain/blocks/index.ts +18 -6
  137. package/src/chain/blocks/verifyBlocksStateTransitionOnly.ts +25 -16
  138. package/src/chain/blocks/verifyExecutionPayloadEnvelope.ts +1 -9
  139. package/src/chain/bls/index.ts +1 -1
  140. package/src/chain/bls/interface.ts +7 -3
  141. package/src/chain/bls/multithread/index.ts +148 -120
  142. package/src/chain/bls/multithread/jobItem.ts +12 -82
  143. package/src/chain/bls/multithread/types.ts +33 -8
  144. package/src/chain/bls/multithread/utils.ts +11 -0
  145. package/src/chain/bls/multithread/worker.ts +85 -27
  146. package/src/chain/bls/singleThread.ts +11 -55
  147. package/src/chain/bls/utils.ts +23 -24
  148. package/src/chain/chain.ts +6 -4
  149. package/src/chain/errors/blockError.ts +8 -2
  150. package/src/chain/interface.ts +2 -1
  151. package/src/chain/opPools/aggregatedAttestationPool.ts +1 -1
  152. package/src/chain/opPools/attestationPool.ts +1 -1
  153. package/src/chain/opPools/payloadAttestationPool.ts +1 -1
  154. package/src/chain/opPools/syncCommitteeMessagePool.ts +1 -1
  155. package/src/chain/opPools/syncContributionAndProofPool.ts +1 -1
  156. package/src/chain/opPools/utils.ts +1 -1
  157. package/src/chain/validation/attestation.ts +1 -7
  158. package/src/chain/validation/attesterSlashing.ts +1 -8
  159. package/src/chain/validation/executionPayloadBid.ts +1 -2
  160. package/src/chain/validation/executionPayloadEnvelope.ts +0 -1
  161. package/src/chain/validation/payloadAttestationMessage.ts +3 -11
  162. package/src/chain/validation/proposerPreferences.ts +3 -3
  163. package/src/chain/validation/proposerSlashing.ts +1 -1
  164. package/src/metrics/metrics/lodestar.ts +85 -51
  165. package/src/network/gossip/topic.ts +38 -0
  166. package/src/network/network.ts +27 -55
  167. package/src/network/util.ts +0 -5
  168. package/src/node/nodejs.ts +2 -1
  169. package/src/sync/range/batch.ts +83 -28
  170. package/src/sync/range/chain.ts +26 -11
  171. package/lib/chain/bls/maybeBatch.d.ts +0 -12
  172. package/lib/chain/bls/maybeBatch.d.ts.map +0 -1
  173. package/lib/chain/bls/maybeBatch.js +0 -35
  174. package/lib/chain/bls/maybeBatch.js.map +0 -1
  175. package/src/chain/bls/maybeBatch.ts +0 -45
@@ -17,3 +17,14 @@ export function chunkifyMaximizeChunkSize<T>(arr: T[], minPerChunk: number): T[]
17
17
 
18
18
  return arrArr;
19
19
  }
20
+
21
+ /**
22
+ * Splits an array into chunks that do not exceed a maximum size.
23
+ */
24
+ export function chunkifyMaxChunkSize<T>(arr: T[], maxPerChunk: number): T[][] {
25
+ const chunks: T[][] = [];
26
+ for (let i = 0; i < arr.length; i += maxPerChunk) {
27
+ chunks.push(arr.slice(i, i + maxPerChunk));
28
+ }
29
+ return chunks;
30
+ }
@@ -1,8 +1,20 @@
1
1
  import worker from "node:worker_threads";
2
- import {PublicKey} from "@chainsafe/blst";
2
+ import {
3
+ type BlsSignatureSet,
4
+ verifySignatureSets,
5
+ verifySignatureSetsSameMessage,
6
+ } from "@chainsafe/lodestar-z/bls-verifier";
3
7
  import {expose} from "@chainsafe/threads/worker";
4
- import {SignatureSetDeserialized, verifySignatureSetsMaybeBatch} from "../maybeBatch.js";
5
- import {BlsWorkReq, BlsWorkResult, SerializedSet, WorkResult, WorkResultCode, WorkerData} from "./types.js";
8
+ import {
9
+ BlsWorkReq,
10
+ BlsWorkResult,
11
+ JobQueueItemType,
12
+ VerificationCall,
13
+ VerificationCallOperation,
14
+ WorkResult,
15
+ WorkResultCode,
16
+ WorkerData,
17
+ } from "./types.js";
6
18
  import {chunkifyMaximizeChunkSize} from "./utils.js";
7
19
 
8
20
  /**
@@ -27,23 +39,45 @@ expose({
27
39
 
28
40
  function verifyManySignatureSets(workReqArr: BlsWorkReq[]): BlsWorkResult {
29
41
  const [startSec, startNs] = process.hrtime();
30
- const results: WorkResult<boolean>[] = [];
42
+ const results: WorkResult<boolean[]>[] = [];
43
+ const verificationCalls: VerificationCall[] = [];
31
44
  let batchRetries = 0;
32
45
  let batchSigsSuccess = 0;
33
46
 
34
47
  // If there are multiple batchable sets attempt batch verification with them
35
- const batchableSets: {idx: number; sets: SignatureSetDeserialized[]}[] = [];
36
- const nonBatchableSets: {idx: number; sets: SignatureSetDeserialized[]}[] = [];
48
+ const batchableSets: {idx: number; sets: BlsSignatureSet[]}[] = [];
49
+ const nonBatchableSets: {
50
+ idx: number;
51
+ sets: BlsSignatureSet[];
52
+ operation: VerificationCallOperation.single | VerificationCallOperation.fallback;
53
+ }[] = [];
37
54
 
38
55
  // Split sets between batchable and non-batchable preserving their original index in the req array
39
56
  for (let i = 0; i < workReqArr.length; i++) {
40
57
  const workReq = workReqArr[i];
41
- const sets = workReq.sets.map(deserializeSet);
42
-
43
- if (workReq.opts.batchable) {
44
- batchableSets.push({idx: i, sets});
45
- } else {
46
- nonBatchableSets.push({idx: i, sets});
58
+ switch (workReq.type) {
59
+ case JobQueueItemType.default: {
60
+ const {sets} = workReq;
61
+ if (workReq.opts.batchable) {
62
+ batchableSets.push({idx: i, sets});
63
+ } else {
64
+ nonBatchableSets.push({idx: i, sets, operation: VerificationCallOperation.single});
65
+ }
66
+ break;
67
+ }
68
+ case JobQueueItemType.sameMessage:
69
+ try {
70
+ const result = recordVerificationCall(
71
+ verificationCalls,
72
+ VerificationCallOperation.sameMessage,
73
+ workReq.sets.length,
74
+ () => verifySignatureSetsSameMessage(workReq.sets, workReq.message)
75
+ );
76
+ results[i] = {code: WorkResultCode.success, result};
77
+ } catch (e) {
78
+ results[i] = {code: WorkResultCode.error, error: e as Error};
79
+ }
80
+ break;
47
81
  }
48
82
  }
49
83
 
@@ -52,7 +86,7 @@ function verifyManySignatureSets(workReqArr: BlsWorkReq[]): BlsWorkResult {
52
86
  const batchableChunks = chunkifyMaximizeChunkSize(batchableSets, BATCHABLE_MIN_PER_CHUNK);
53
87
 
54
88
  for (const batchableChunk of batchableChunks) {
55
- const allSets: SignatureSetDeserialized[] = [];
89
+ const allSets: BlsSignatureSet[] = [];
56
90
  for (const {sets} of batchableChunk) {
57
91
  for (const set of sets) {
58
92
  allSets.push(set);
@@ -61,33 +95,49 @@ function verifyManySignatureSets(workReqArr: BlsWorkReq[]): BlsWorkResult {
61
95
 
62
96
  try {
63
97
  // Attempt to verify multiple sets at once
64
- const isValid = verifySignatureSetsMaybeBatch(allSets);
98
+ const isValid = recordVerificationCall(verificationCalls, VerificationCallOperation.batch, allSets.length, () =>
99
+ verifySignatureSets(allSets)
100
+ );
65
101
 
66
102
  if (isValid) {
67
103
  // The entire batch is valid, return success to all
68
104
  for (const {idx, sets} of batchableChunk) {
69
105
  batchSigsSuccess += sets.length;
70
- results[idx] = {code: WorkResultCode.success, result: isValid};
106
+ results[idx] = {code: WorkResultCode.success, result: [isValid]};
71
107
  }
72
108
  } else {
73
109
  batchRetries++;
74
110
  // Re-verify all sigs
75
- nonBatchableSets.push(...batchableChunk);
111
+ nonBatchableSets.push(
112
+ ...batchableChunk.map(({idx, sets}) => ({
113
+ idx,
114
+ sets,
115
+ operation: VerificationCallOperation.fallback as const,
116
+ }))
117
+ );
76
118
  }
77
119
  } catch (_e) {
78
120
  // TODO: Ignore this error expecting that the same error will happen when re-verifying the set individually
79
- // It's not ideal but '@chainsafe/blst' may throw errors on some conditions
121
+ // It's not ideal but the BLS implementation may throw errors on some conditions
80
122
  batchRetries++;
81
123
  // Re-verify all sigs
82
- nonBatchableSets.push(...batchableChunk);
124
+ nonBatchableSets.push(
125
+ ...batchableChunk.map(({idx, sets}) => ({
126
+ idx,
127
+ sets,
128
+ operation: VerificationCallOperation.fallback as const,
129
+ }))
130
+ );
83
131
  }
84
132
  }
85
133
  }
86
134
 
87
- for (const {idx, sets} of nonBatchableSets) {
135
+ for (const {idx, sets, operation} of nonBatchableSets) {
88
136
  try {
89
- const isValid = verifySignatureSetsMaybeBatch(sets);
90
- results[idx] = {code: WorkResultCode.success, result: isValid};
137
+ const isValid = recordVerificationCall(verificationCalls, operation, sets.length, () =>
138
+ verifySignatureSets(sets)
139
+ );
140
+ results[idx] = {code: WorkResultCode.success, result: [isValid]};
91
141
  } catch (e) {
92
142
  results[idx] = {code: WorkResultCode.error, error: e as Error};
93
143
  }
@@ -99,16 +149,24 @@ function verifyManySignatureSets(workReqArr: BlsWorkReq[]): BlsWorkResult {
99
149
  workerId,
100
150
  batchRetries,
101
151
  batchSigsSuccess,
152
+ verificationCalls,
102
153
  workerStartTime: [startSec, startNs],
103
154
  workerEndTime: [workerEndSec, workerEndNs],
104
155
  results,
105
156
  };
106
157
  }
107
158
 
108
- function deserializeSet(set: SerializedSet): SignatureSetDeserialized {
109
- return {
110
- publicKey: PublicKey.fromBytes(set.publicKey),
111
- message: set.message,
112
- signature: set.signature,
113
- };
159
+ function recordVerificationCall<T>(
160
+ calls: VerificationCall[],
161
+ operation: VerificationCallOperation,
162
+ signatureSets: number,
163
+ verify: () => T
164
+ ): T {
165
+ const startTime = process.hrtime();
166
+ try {
167
+ return verify();
168
+ } finally {
169
+ const [seconds, nanoseconds] = process.hrtime(startTime);
170
+ calls.push({operation, signatureSets, duration: seconds + nanoseconds / 1e9});
171
+ }
114
172
  }
@@ -1,31 +1,20 @@
1
- import {PublicKey, Signature, aggregatePublicKeys, aggregateSignatures, verify} from "@chainsafe/blst";
2
- import {ISignatureSet, PubkeyCache} from "@lodestar/state-transition";
1
+ import {verifySignatureSetsSameMessage} from "@chainsafe/lodestar-z/bls-verifier";
2
+ import {ISignatureSet} from "@lodestar/state-transition";
3
3
  import {Metrics} from "../../metrics/index.js";
4
- import {IBlsVerifier} from "./interface.js";
5
- import {verifySignatureSetsMaybeBatch} from "./maybeBatch.js";
6
- import {getAggregatedPubkey, getAggregatedPubkeysCount} from "./utils.js";
4
+ import {IBlsVerifier, SameMessageSignatureSet} from "./interface.js";
5
+ import {chunkSameMessageSignatureSets, getAggregatedPubkeysCount, verifySignatureSetsInBatches} from "./utils.js";
7
6
 
8
7
  export class BlsSingleThreadVerifier implements IBlsVerifier {
9
8
  private readonly metrics: Metrics | null;
10
- private readonly pubkeyCache: PubkeyCache;
11
-
12
- constructor({metrics = null, pubkeyCache}: {metrics: Metrics | null; pubkeyCache: PubkeyCache}) {
9
+ constructor({metrics = null}: {metrics: Metrics | null}) {
13
10
  this.metrics = metrics;
14
- this.pubkeyCache = pubkeyCache;
15
11
  }
16
12
 
17
13
  async verifySignatureSets(sets: ISignatureSet[]): Promise<boolean> {
18
14
  this.metrics?.bls.aggregatedPubkeys.inc(getAggregatedPubkeysCount(sets));
19
15
 
20
- const setsAggregated = sets.map((set) => ({
21
- publicKey: getAggregatedPubkey(set, this.pubkeyCache, this.metrics),
22
- message: set.signingRoot,
23
- signature: set.signature,
24
- }));
25
-
26
- // Count time after aggregating
27
16
  const timer = this.metrics?.blsThreadPool.mainThreadDurationInThreadPool.startTimer();
28
- const isValid = verifySignatureSetsMaybeBatch(setsAggregated);
17
+ const isValid = verifySignatureSetsInBatches(sets);
29
18
 
30
19
  // Don't use a try/catch, only count run without exceptions
31
20
  if (timer) {
@@ -35,46 +24,13 @@ export class BlsSingleThreadVerifier implements IBlsVerifier {
35
24
  return isValid;
36
25
  }
37
26
 
38
- async verifySignatureSetsSameMessage(
39
- sets: {publicKey: PublicKey; signature: Uint8Array}[],
40
- message: Uint8Array
41
- ): Promise<boolean[]> {
27
+ async verifySignatureSetsSameMessage(sets: SameMessageSignatureSet[], message: Uint8Array): Promise<boolean[]> {
42
28
  const timer = this.metrics?.blsThreadPool.mainThreadDurationInThreadPool.startTimer();
43
- const pubkey = aggregatePublicKeys(sets.map((set) => set.publicKey));
44
- let isAllValid = true;
45
- // validate signature = true
46
- const signatures = sets.map((set) => {
47
- try {
48
- return Signature.fromBytes(set.signature, true);
49
- } catch (_) {
50
- // at least one set has malformed signature
51
- isAllValid = false;
52
- return null;
53
- }
54
- });
55
-
56
- if (isAllValid) {
57
- const signature = aggregateSignatures(signatures as Signature[]);
58
- isAllValid = verify(message, pubkey, signature);
59
- }
60
-
61
- let result: boolean[];
62
- if (isAllValid) {
63
- result = sets.map(() => true);
64
- } else {
65
- result = sets.map((set, i) => {
66
- const sig = signatures[i];
67
- if (sig === null) {
68
- return false;
69
- }
70
- return verify(message, set.publicKey, sig);
71
- });
29
+ const result: boolean[] = [];
30
+ for (const setsChunk of chunkSameMessageSignatureSets(sets)) {
31
+ result.push(...verifySignatureSetsSameMessage(setsChunk, message));
72
32
  }
73
-
74
- if (timer) {
75
- timer();
76
- }
77
-
33
+ timer?.();
78
34
  return result;
79
35
  }
80
36
 
@@ -1,33 +1,32 @@
1
- import {PublicKey, aggregatePublicKeys} from "@chainsafe/blst";
2
- import {ISignatureSet, PubkeyCache, SignatureSetType} from "@lodestar/state-transition";
3
- import {Metrics} from "../../metrics/metrics.js";
1
+ import {
2
+ BLS_VERIFIER_MAX_BATCH_SIZE,
3
+ BLS_VERIFIER_MAX_SAME_MESSAGE_BATCH_SIZE,
4
+ verifySignatureSets,
5
+ } from "@chainsafe/lodestar-z/bls-verifier";
6
+ import {ISignatureSet, SignatureSetType, toBlsSignatureSet} from "@lodestar/state-transition";
7
+ import {SameMessageSignatureSet} from "./interface.js";
4
8
 
5
- export function getAggregatedPubkey(
6
- signatureSet: ISignatureSet,
7
- pubkeyCache: PubkeyCache,
8
- metrics: Metrics | null = null
9
- ): PublicKey {
10
- switch (signatureSet.type) {
11
- case SignatureSetType.single:
12
- return signatureSet.pubkey;
9
+ export function verifySignatureSetsInBatches(signatureSets: ISignatureSet[]): boolean {
10
+ if (signatureSets.length === 0) {
11
+ return false;
12
+ }
13
13
 
14
- case SignatureSetType.indexed: {
15
- return pubkeyCache.getOrThrow(signatureSet.index);
14
+ for (let start = 0; start < signatureSets.length; start += BLS_VERIFIER_MAX_BATCH_SIZE) {
15
+ const sets = signatureSets.slice(start, start + BLS_VERIFIER_MAX_BATCH_SIZE).map(toBlsSignatureSet);
16
+ if (!verifySignatureSets(sets)) {
17
+ return false;
16
18
  }
19
+ }
17
20
 
18
- case SignatureSetType.aggregate: {
19
- const timer = metrics?.blsThreadPool.pubkeysAggregationMainThreadDuration.startTimer();
20
- const pubkeys = signatureSet.indices.map((i) => {
21
- return pubkeyCache.getOrThrow(i);
22
- });
23
- const aggregated = aggregatePublicKeys(pubkeys);
24
- timer?.();
25
- return aggregated;
26
- }
21
+ return true;
22
+ }
27
23
 
28
- default:
29
- throw Error("Unknown signature set type");
24
+ export function chunkSameMessageSignatureSets(sets: SameMessageSignatureSet[]): SameMessageSignatureSet[][] {
25
+ const chunks: SameMessageSignatureSet[][] = [];
26
+ for (let start = 0; start < sets.length; start += BLS_VERIFIER_MAX_SAME_MESSAGE_BATCH_SIZE) {
27
+ chunks.push(sets.slice(start, start + BLS_VERIFIER_MAX_SAME_MESSAGE_BATCH_SIZE));
30
28
  }
29
+ return chunks;
31
30
  }
32
31
 
33
32
  export function getAggregatedPubkeysCount(signatureSets: ISignatureSet[]): number {
@@ -1,5 +1,6 @@
1
1
  import path from "node:path";
2
2
  import {PrivateKey} from "@libp2p/interface";
3
+ import {type PubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
3
4
  import {Type} from "@chainsafe/ssz";
4
5
  import {routes} from "@lodestar/api";
5
6
  import {BeaconConfig} from "@lodestar/config";
@@ -25,7 +26,6 @@ import {
25
26
  EffectiveBalanceIncrements,
26
27
  EpochShuffling,
27
28
  IBeaconStateView,
28
- PubkeyCache,
29
29
  computeEndSlotAtEpoch,
30
30
  computeEpochAtSlot,
31
31
  computeStartSlotAtEpoch,
@@ -307,8 +307,8 @@ export class BeaconChain implements IBeaconChain {
307
307
  const emitter = new ChainEventEmitter();
308
308
  // by default, verify signatures on both main threads and worker threads
309
309
  const bls = opts.blsVerifyAllMainThread
310
- ? new BlsSingleThreadVerifier({metrics, pubkeyCache})
311
- : new BlsMultiThreadWorkerPool(opts, {logger, metrics, pubkeyCache});
310
+ ? new BlsSingleThreadVerifier({metrics})
311
+ : new BlsMultiThreadWorkerPool(opts, {logger, metrics});
312
312
 
313
313
  if (!clock) clock = new Clock({config, genesisTime: this.genesisTime, signal});
314
314
 
@@ -1090,7 +1090,7 @@ export class BeaconChain implements IBeaconChain {
1090
1090
  RegenCaller.produceBlock
1091
1091
  );
1092
1092
  const proposerIndex = state.getBeaconProposer(slot);
1093
- const proposerPubKey = this.pubkeyCache.getOrThrow(proposerIndex).toBytes();
1093
+ const proposerPubKey = this.pubkeyCache.getPubkeyBytesOrThrow(proposerIndex);
1094
1094
 
1095
1095
  const {body, produceResult, executionPayloadValue, shouldOverrideBuilder} = await produceBlockBody.call(
1096
1096
  this,
@@ -1577,6 +1577,8 @@ export class BeaconChain implements IBeaconChain {
1577
1577
  // syncContributionAndProofPool tracks metrics on its own
1578
1578
  metrics.opPool.blsToExecutionChangePoolSize.set(this.opPool.blsToExecutionChangeSize);
1579
1579
  metrics.chain.blacklistedBlocks.set(this.blacklistedBlocks.size);
1580
+ metrics.bls.pubkeyCacheSize.set(this.pubkeyCache.size);
1581
+ metrics.bls.pubkeyCacheCapacity.set(this.pubkeyCache.capacity);
1580
1582
 
1581
1583
  const headState = this.getHeadState();
1582
1584
  if (isStatePostElectra(headState)) {
@@ -136,7 +136,7 @@ export type BlockErrorType =
136
136
  | {code: BlockErrorCode.NON_LINEAR_PARENT_ROOTS}
137
137
  | {code: BlockErrorCode.NON_LINEAR_SLOTS}
138
138
  | {code: BlockErrorCode.ENVELOPE_BLOCK_ROOT_MISMATCH; envelopeBlockRoot: RootHex; blockRoot: RootHex}
139
- | {code: BlockErrorCode.PER_BLOCK_PROCESSING_ERROR; error: Error}
139
+ | {code: BlockErrorCode.PER_BLOCK_PROCESSING_ERROR; blockRoot: RootHex; error: Error}
140
140
  | {code: BlockErrorCode.BEACON_CHAIN_ERROR; error: Error}
141
141
  | {code: BlockErrorCode.KNOWN_BAD_BLOCK}
142
142
  | {code: BlockErrorCode.BLACKLISTED_BLOCK}
@@ -184,13 +184,19 @@ export function isBlockErrorAborted(e: unknown): e is BlockError {
184
184
  export function renderBlockErrorType(type: BlockErrorType): Record<string, string | number | null> {
185
185
  switch (type.code) {
186
186
  case BlockErrorCode.PRESTATE_MISSING:
187
- case BlockErrorCode.PER_BLOCK_PROCESSING_ERROR:
188
187
  case BlockErrorCode.BEACON_CHAIN_ERROR:
189
188
  return {
190
189
  code: type.code,
191
190
  error: type.error.message,
192
191
  };
193
192
 
193
+ case BlockErrorCode.PER_BLOCK_PROCESSING_ERROR:
194
+ return {
195
+ code: type.code,
196
+ blockRoot: type.blockRoot,
197
+ error: type.error.message,
198
+ };
199
+
194
200
  case BlockErrorCode.INVALID_SIGNATURE:
195
201
  return {
196
202
  code: type.code,
@@ -1,7 +1,8 @@
1
+ import {type PubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
1
2
  import {Type} from "@chainsafe/ssz";
2
3
  import {BeaconConfig} from "@lodestar/config";
3
4
  import {CheckpointWithHex, IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
4
- import {EpochShuffling, IBeaconStateView, PubkeyCache} from "@lodestar/state-transition";
5
+ import {EpochShuffling, IBeaconStateView} from "@lodestar/state-transition";
5
6
  import {
6
7
  BeaconBlock,
7
8
  BlindedBeaconBlock,
@@ -1,4 +1,4 @@
1
- import {Signature, aggregateSignatures} from "@chainsafe/blst";
1
+ import {Signature, aggregateSignatures} from "@chainsafe/lodestar-z/blst";
2
2
  import {BitArray} from "@chainsafe/ssz";
3
3
  import {BeaconConfig} from "@lodestar/config";
4
4
  import {IForkChoice} from "@lodestar/fork-choice";
@@ -1,4 +1,4 @@
1
- import {Signature, aggregateSignatures} from "@chainsafe/blst";
1
+ import {Signature, aggregateSignatures} from "@chainsafe/lodestar-z/blst";
2
2
  import {BitArray} from "@chainsafe/ssz";
3
3
  import {ChainForkConfig} from "@lodestar/config";
4
4
  import {MAX_COMMITTEES_PER_SLOT, isForkPostElectra} from "@lodestar/params";
@@ -1,4 +1,4 @@
1
- import {Signature, aggregateSignatures} from "@chainsafe/blst";
1
+ import {Signature, aggregateSignatures} from "@chainsafe/lodestar-z/blst";
2
2
  import {BitArray} from "@chainsafe/ssz";
3
3
  import {ChainForkConfig} from "@lodestar/config";
4
4
  import {MAX_COMMITTEES_PER_SLOT, MAX_PAYLOAD_ATTESTATIONS, PTC_SIZE} from "@lodestar/params";
@@ -1,4 +1,4 @@
1
- import {Signature, aggregateSignatures} from "@chainsafe/blst";
1
+ import {Signature, aggregateSignatures} from "@chainsafe/lodestar-z/blst";
2
2
  import {BitArray} from "@chainsafe/ssz";
3
3
  import {ChainForkConfig} from "@lodestar/config";
4
4
  import {SYNC_COMMITTEE_SIZE, SYNC_COMMITTEE_SUBNET_COUNT} from "@lodestar/params";
@@ -1,4 +1,4 @@
1
- import {Signature, aggregateSignatures} from "@chainsafe/blst";
1
+ import {Signature, aggregateSignatures} from "@chainsafe/lodestar-z/blst";
2
2
  import {BitArray} from "@chainsafe/ssz";
3
3
  import {ChainForkConfig} from "@lodestar/config";
4
4
  import {SYNC_COMMITTEE_SIZE, SYNC_COMMITTEE_SUBNET_SIZE} from "@lodestar/params";
@@ -1,4 +1,4 @@
1
- import {Signature} from "@chainsafe/blst";
1
+ import {Signature} from "@chainsafe/lodestar-z/blst";
2
2
  import {BLS_WITHDRAWAL_PREFIX} from "@lodestar/params";
3
3
  import {IBeaconStateView} from "@lodestar/state-transition";
4
4
  import {Slot, capella} from "@lodestar/types";
@@ -142,13 +142,7 @@ export async function validateGossipAttestationsSameAttData(
142
142
  const batchableBls = signatureSets.length >= chain.opts.minSameMessageSignatureSetsToBatch;
143
143
  if (batchableBls) {
144
144
  // all signature sets should have same signing root since we filtered in network processor
145
- signatureValids = await chain.bls.verifySignatureSetsSameMessage(
146
- signatureSets.map((set) => {
147
- const publicKey = chain.pubkeyCache.getOrThrow(set.index);
148
- return {publicKey, signature: set.signature};
149
- }),
150
- signatureSets[0].signingRoot
151
- );
145
+ signatureValids = await chain.bls.verifySignatureSetsSameMessage(signatureSets, signatureSets[0].signingRoot);
152
146
  } else {
153
147
  // don't want to block the main thread if there are too few signatures
154
148
  signatureValids = await Promise.all(
@@ -44,14 +44,7 @@ export async function validateAttesterSlashing(
44
44
  // [REJECT] All of the conditions within process_attester_slashing pass validation.
45
45
  try {
46
46
  // verifySignature = false, verified in batch below
47
- assertValidAttesterSlashing(
48
- chain.config,
49
- chain.pubkeyCache,
50
- state.slot,
51
- state.validatorCount,
52
- attesterSlashing,
53
- false
54
- );
47
+ assertValidAttesterSlashing(chain.config, state.slot, state.validatorCount, attesterSlashing, false);
55
48
  } catch (e) {
56
49
  throw new AttesterSlashingError(GossipAction.REJECT, {
57
50
  code: AttesterSlashingErrorCode.INVALID,
@@ -1,4 +1,3 @@
1
- import {PublicKey} from "@chainsafe/blst";
2
1
  import {IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
3
2
  import {PAYLOAD_BUILDER_VERSION} from "@lodestar/params";
4
3
  import {
@@ -345,7 +344,7 @@ async function validateExecutionPayloadBid(
345
344
 
346
345
  // [REJECT] `signed_execution_payload_bid.signature` is valid with respect to the `bid.builder_index`.
347
346
  const signatureSet = createSingleSignatureSetFromComponents(
348
- PublicKey.fromBytes(builder.pubkey),
347
+ builder.pubkey,
349
348
  getExecutionPayloadBidSigningRoot(chain.config, bid),
350
349
  signedExecutionPayloadBid.signature
351
350
  );
@@ -170,7 +170,6 @@ async function validateExecutionPayloadEnvelope(
170
170
  // by `verify_execution_payload_envelope_signature`.
171
171
  const signatureSet = getExecutionPayloadEnvelopeSignatureSet(
172
172
  chain.config,
173
- chain.pubkeyCache,
174
173
  blockState,
175
174
  executionPayloadEnvelope,
176
175
  payloadInput.proposerIndex
@@ -1,5 +1,5 @@
1
1
  import {
2
- createSingleSignatureSetFromComponents,
2
+ createIndexedSignatureSetFromComponents,
3
3
  getPayloadAttestationDataSigningRoot,
4
4
  isStatePostGloas,
5
5
  } from "@lodestar/state-transition";
@@ -111,16 +111,8 @@ async function validatePayloadAttestationMessage(
111
111
  }
112
112
 
113
113
  // [REJECT] `payload_attestation_message.signature` is valid with respect to the validator's public key.
114
- const validatorPubkey = chain.pubkeyCache.get(validatorIndex);
115
- if (!validatorPubkey) {
116
- throw new PayloadAttestationError(GossipAction.REJECT, {
117
- code: PayloadAttestationErrorCode.INVALID_ATTESTER,
118
- attesterIndex: validatorIndex,
119
- });
120
- }
121
-
122
- const signatureSet = createSingleSignatureSetFromComponents(
123
- validatorPubkey,
114
+ const signatureSet = createIndexedSignatureSetFromComponents(
115
+ validatorIndex,
124
116
  getPayloadAttestationDataSigningRoot(chain.config, data),
125
117
  payloadAttestationMessage.signature
126
118
  );
@@ -3,7 +3,7 @@ import {GENESIS_EPOCH, GENESIS_SLOT, MIN_SEED_LOOKAHEAD, SLOTS_PER_EPOCH, isFork
3
3
  import {
4
4
  computeEpochAtSlot,
5
5
  computeStartSlotAtEpoch,
6
- createSingleSignatureSetFromComponents,
6
+ createIndexedSignatureSetFromComponents,
7
7
  getProposerPreferencesSigningRoot,
8
8
  } from "@lodestar/state-transition";
9
9
  import {Epoch, Slot, ValidatorIndex, gloas} from "@lodestar/types";
@@ -139,8 +139,8 @@ export async function validateGossipProposerPreferences(
139
139
  }
140
140
 
141
141
  // [REJECT] `signed_proposer_preferences.signature` is valid with respect to the validator's public key.
142
- const signatureSet = createSingleSignatureSetFromComponents(
143
- chain.pubkeyCache.getOrThrow(validatorIndex),
142
+ const signatureSet = createIndexedSignatureSetFromComponents(
143
+ validatorIndex,
144
144
  getProposerPreferencesSigningRoot(chain.config, preferences),
145
145
  signedProposerPreferences.signature
146
146
  );
@@ -37,7 +37,7 @@ async function validateProposerSlashing(
37
37
  try {
38
38
  const proposer = state.getValidator(proposerSlashing.signedHeader1.message.proposerIndex);
39
39
  // verifySignature = false, verified in batch below
40
- assertValidProposerSlashing(chain.config, chain.pubkeyCache, state.slot, proposerSlashing, proposer, false);
40
+ assertValidProposerSlashing(chain.config, state.slot, proposerSlashing, proposer, false);
41
41
  } catch (e) {
42
42
  throw new ProposerSlashingError(GossipAction.REJECT, {
43
43
  code: ProposerSlashingErrorCode.INVALID,