@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.023c3e5

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 (78) hide show
  1. package/README.md +282 -0
  2. package/dest/block_proposal_handler.d.ts +63 -0
  3. package/dest/block_proposal_handler.d.ts.map +1 -0
  4. package/dest/block_proposal_handler.js +546 -0
  5. package/dest/checkpoint_builder.d.ts +66 -0
  6. package/dest/checkpoint_builder.d.ts.map +1 -0
  7. package/dest/checkpoint_builder.js +173 -0
  8. package/dest/config.d.ts +3 -14
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +41 -8
  11. package/dest/duties/validation_service.d.ts +50 -13
  12. package/dest/duties/validation_service.d.ts.map +1 -1
  13. package/dest/duties/validation_service.js +123 -17
  14. package/dest/factory.d.ts +28 -6
  15. package/dest/factory.d.ts.map +1 -1
  16. package/dest/factory.js +13 -6
  17. package/dest/index.d.ts +6 -2
  18. package/dest/index.d.ts.map +1 -1
  19. package/dest/index.js +5 -1
  20. package/dest/key_store/ha_key_store.d.ts +99 -0
  21. package/dest/key_store/ha_key_store.d.ts.map +1 -0
  22. package/dest/key_store/ha_key_store.js +208 -0
  23. package/dest/key_store/index.d.ts +4 -1
  24. package/dest/key_store/index.d.ts.map +1 -1
  25. package/dest/key_store/index.js +3 -0
  26. package/dest/key_store/interface.d.ts +85 -6
  27. package/dest/key_store/interface.d.ts.map +1 -1
  28. package/dest/key_store/interface.js +3 -3
  29. package/dest/key_store/local_key_store.d.ts +46 -11
  30. package/dest/key_store/local_key_store.d.ts.map +1 -1
  31. package/dest/key_store/local_key_store.js +68 -17
  32. package/dest/key_store/node_keystore_adapter.d.ts +151 -0
  33. package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
  34. package/dest/key_store/node_keystore_adapter.js +330 -0
  35. package/dest/key_store/web3signer_key_store.d.ts +66 -0
  36. package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
  37. package/dest/key_store/web3signer_key_store.js +156 -0
  38. package/dest/metrics.d.ts +13 -5
  39. package/dest/metrics.d.ts.map +1 -1
  40. package/dest/metrics.js +63 -22
  41. package/dest/tx_validator/index.d.ts +3 -0
  42. package/dest/tx_validator/index.d.ts.map +1 -0
  43. package/dest/tx_validator/index.js +2 -0
  44. package/dest/tx_validator/nullifier_cache.d.ts +14 -0
  45. package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
  46. package/dest/tx_validator/nullifier_cache.js +24 -0
  47. package/dest/tx_validator/tx_validator_factory.d.ts +19 -0
  48. package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
  49. package/dest/tx_validator/tx_validator_factory.js +54 -0
  50. package/dest/validator.d.ts +73 -58
  51. package/dest/validator.d.ts.map +1 -1
  52. package/dest/validator.js +559 -166
  53. package/package.json +37 -21
  54. package/src/block_proposal_handler.ts +555 -0
  55. package/src/checkpoint_builder.ts +314 -0
  56. package/src/config.ts +52 -22
  57. package/src/duties/validation_service.ts +193 -19
  58. package/src/factory.ts +65 -11
  59. package/src/index.ts +5 -1
  60. package/src/key_store/ha_key_store.ts +269 -0
  61. package/src/key_store/index.ts +3 -0
  62. package/src/key_store/interface.ts +100 -5
  63. package/src/key_store/local_key_store.ts +77 -18
  64. package/src/key_store/node_keystore_adapter.ts +398 -0
  65. package/src/key_store/web3signer_key_store.ts +205 -0
  66. package/src/metrics.ts +86 -23
  67. package/src/tx_validator/index.ts +2 -0
  68. package/src/tx_validator/nullifier_cache.ts +30 -0
  69. package/src/tx_validator/tx_validator_factory.ts +154 -0
  70. package/src/validator.ts +757 -221
  71. package/dest/errors/index.d.ts +0 -2
  72. package/dest/errors/index.d.ts.map +0 -1
  73. package/dest/errors/index.js +0 -1
  74. package/dest/errors/validator.error.d.ts +0 -29
  75. package/dest/errors/validator.error.d.ts.map +0 -1
  76. package/dest/errors/validator.error.js +0 -45
  77. package/src/errors/index.ts +0 -1
  78. package/src/errors/validator.error.ts +0 -55
@@ -0,0 +1,314 @@
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { merge, pick } from '@aztec/foundation/collection';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
5
+ import { bufferToHex } from '@aztec/foundation/string';
6
+ import { DateProvider, elapsed } from '@aztec/foundation/timer';
7
+ import { getDefaultAllowedSetupFunctions } from '@aztec/p2p/msg_validators';
8
+ import { LightweightCheckpointBuilder } from '@aztec/prover-client/light';
9
+ import {
10
+ GuardedMerkleTreeOperations,
11
+ PublicContractsDB,
12
+ PublicProcessor,
13
+ createPublicTxSimulatorForBlockBuilding,
14
+ } from '@aztec/simulator/server';
15
+ import { L2Block } from '@aztec/stdlib/block';
16
+ import { Checkpoint } from '@aztec/stdlib/checkpoint';
17
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
18
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
19
+ import { Gas } from '@aztec/stdlib/gas';
20
+ import {
21
+ type BuildBlockInCheckpointResult,
22
+ type FullNodeBlockBuilderConfig,
23
+ FullNodeBlockBuilderConfigKeys,
24
+ type ICheckpointBlockBuilder,
25
+ type ICheckpointsBuilder,
26
+ type MerkleTreeWriteOperations,
27
+ NoValidTxsError,
28
+ type PublicProcessorLimits,
29
+ type WorldStateSynchronizer,
30
+ } from '@aztec/stdlib/interfaces/server';
31
+ import { MerkleTreeId } from '@aztec/stdlib/trees';
32
+ import { type CheckpointGlobalVariables, GlobalVariables, StateReference, Tx } from '@aztec/stdlib/tx';
33
+ import { type TelemetryClient, getTelemetryClient } from '@aztec/telemetry-client';
34
+
35
+ import { createValidatorForBlockBuilding } from './tx_validator/tx_validator_factory.js';
36
+
37
+ // Re-export for backward compatibility
38
+ export type { BuildBlockInCheckpointResult } from '@aztec/stdlib/interfaces/server';
39
+
40
+ /**
41
+ * Builder for a single checkpoint. Handles building blocks within the checkpoint
42
+ * and completing it.
43
+ */
44
+ export class CheckpointBuilder implements ICheckpointBlockBuilder {
45
+ private log: Logger;
46
+
47
+ constructor(
48
+ private checkpointBuilder: LightweightCheckpointBuilder,
49
+ private fork: MerkleTreeWriteOperations,
50
+ private config: FullNodeBlockBuilderConfig,
51
+ private contractDataSource: ContractDataSource,
52
+ private dateProvider: DateProvider,
53
+ private telemetryClient: TelemetryClient,
54
+ bindings?: LoggerBindings,
55
+ ) {
56
+ this.log = createLogger('checkpoint-builder', {
57
+ ...bindings,
58
+ instanceId: `checkpoint-${checkpointBuilder.checkpointNumber}`,
59
+ });
60
+ }
61
+
62
+ getConstantData(): CheckpointGlobalVariables {
63
+ return this.checkpointBuilder.constants;
64
+ }
65
+
66
+ /**
67
+ * Builds a single block within this checkpoint.
68
+ */
69
+ async buildBlock(
70
+ pendingTxs: Iterable<Tx> | AsyncIterable<Tx>,
71
+ blockNumber: BlockNumber,
72
+ timestamp: bigint,
73
+ opts: PublicProcessorLimits & { expectedEndState?: StateReference } = {},
74
+ ): Promise<BuildBlockInCheckpointResult> {
75
+ const slot = this.checkpointBuilder.constants.slotNumber;
76
+
77
+ this.log.verbose(`Building block ${blockNumber} for slot ${slot} within checkpoint`, {
78
+ slot,
79
+ blockNumber,
80
+ ...opts,
81
+ currentTime: new Date(this.dateProvider.now()),
82
+ });
83
+
84
+ const constants = this.checkpointBuilder.constants;
85
+ const globalVariables = GlobalVariables.from({
86
+ chainId: constants.chainId,
87
+ version: constants.version,
88
+ blockNumber,
89
+ slotNumber: constants.slotNumber,
90
+ timestamp,
91
+ coinbase: constants.coinbase,
92
+ feeRecipient: constants.feeRecipient,
93
+ gasFees: constants.gasFees,
94
+ });
95
+ const { processor, validator } = await this.makeBlockBuilderDeps(globalVariables, this.fork);
96
+
97
+ const [publicProcessorDuration, [processedTxs, failedTxs, usedTxs, _, usedTxBlobFields]] = await elapsed(() =>
98
+ processor.process(pendingTxs, opts, validator),
99
+ );
100
+
101
+ // Throw if we didn't collect a single valid tx and we're not allowed to build empty blocks
102
+ // (only the first block in a checkpoint can be empty)
103
+ if (processedTxs.length === 0 && this.checkpointBuilder.getBlockCount() > 0) {
104
+ throw new NoValidTxsError(failedTxs);
105
+ }
106
+
107
+ // Add block to checkpoint
108
+ const block = await this.checkpointBuilder.addBlock(globalVariables, processedTxs, {
109
+ expectedEndState: opts.expectedEndState,
110
+ });
111
+
112
+ // How much public gas was processed
113
+ const publicGas = processedTxs.reduce((acc, tx) => acc.add(tx.gasUsed.publicGas), Gas.empty());
114
+
115
+ this.log.debug('Built block within checkpoint', {
116
+ header: block.header.toInspect(),
117
+ processedTxs: processedTxs.map(tx => tx.hash.toString()),
118
+ failedTxs: failedTxs.map(tx => tx.tx.txHash.toString()),
119
+ });
120
+
121
+ return {
122
+ block,
123
+ publicGas,
124
+ publicProcessorDuration,
125
+ numTxs: processedTxs.length,
126
+ failedTxs,
127
+ usedTxs,
128
+ usedTxBlobFields,
129
+ };
130
+ }
131
+
132
+ /** Completes the checkpoint and returns it. */
133
+ async completeCheckpoint(): Promise<Checkpoint> {
134
+ const checkpoint = await this.checkpointBuilder.completeCheckpoint();
135
+
136
+ this.log.verbose(`Completed checkpoint ${checkpoint.number}`, {
137
+ checkpointNumber: checkpoint.number,
138
+ numBlocks: checkpoint.blocks.length,
139
+ archiveRoot: checkpoint.archive.root.toString(),
140
+ });
141
+
142
+ return checkpoint;
143
+ }
144
+
145
+ /** Gets the checkpoint currently in progress. */
146
+ getCheckpoint(): Promise<Checkpoint> {
147
+ return this.checkpointBuilder.clone().completeCheckpoint();
148
+ }
149
+
150
+ protected async makeBlockBuilderDeps(globalVariables: GlobalVariables, fork: MerkleTreeWriteOperations) {
151
+ const txPublicSetupAllowList = this.config.txPublicSetupAllowList ?? (await getDefaultAllowedSetupFunctions());
152
+ const contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
153
+ const guardedFork = new GuardedMerkleTreeOperations(fork);
154
+
155
+ const bindings = this.log.getBindings();
156
+ const publicTxSimulator = createPublicTxSimulatorForBlockBuilding(
157
+ guardedFork,
158
+ contractsDB,
159
+ globalVariables,
160
+ this.telemetryClient,
161
+ bindings,
162
+ );
163
+
164
+ const processor = new PublicProcessor(
165
+ globalVariables,
166
+ guardedFork,
167
+ contractsDB,
168
+ publicTxSimulator,
169
+ this.dateProvider,
170
+ this.telemetryClient,
171
+ createLogger('simulator:public-processor', bindings),
172
+ this.config,
173
+ );
174
+
175
+ const validator = createValidatorForBlockBuilding(
176
+ fork,
177
+ this.contractDataSource,
178
+ globalVariables,
179
+ txPublicSetupAllowList,
180
+ this.log.getBindings(),
181
+ );
182
+
183
+ return {
184
+ processor,
185
+ validator,
186
+ };
187
+ }
188
+ }
189
+
190
+ /** Factory for creating checkpoint builders. */
191
+ export class FullNodeCheckpointsBuilder implements ICheckpointsBuilder {
192
+ private log: Logger;
193
+
194
+ constructor(
195
+ private config: FullNodeBlockBuilderConfig & Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration'>,
196
+ private worldState: WorldStateSynchronizer,
197
+ private contractDataSource: ContractDataSource,
198
+ private dateProvider: DateProvider,
199
+ private telemetryClient: TelemetryClient = getTelemetryClient(),
200
+ ) {
201
+ this.log = createLogger('checkpoint-builder');
202
+ }
203
+
204
+ public getConfig(): FullNodeBlockBuilderConfig {
205
+ return this.config;
206
+ }
207
+
208
+ public updateConfig(config: Partial<FullNodeBlockBuilderConfig>) {
209
+ this.config = merge(this.config, pick(config, ...FullNodeBlockBuilderConfigKeys));
210
+ }
211
+
212
+ /**
213
+ * Starts a new checkpoint and returns a CheckpointBuilder to build blocks within it.
214
+ */
215
+ async startCheckpoint(
216
+ checkpointNumber: CheckpointNumber,
217
+ constants: CheckpointGlobalVariables,
218
+ l1ToL2Messages: Fr[],
219
+ previousCheckpointOutHashes: Fr[],
220
+ fork: MerkleTreeWriteOperations,
221
+ bindings?: LoggerBindings,
222
+ ): Promise<CheckpointBuilder> {
223
+ const stateReference = await fork.getStateReference();
224
+ const archiveTree = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
225
+
226
+ this.log.verbose(`Building new checkpoint ${checkpointNumber}`, {
227
+ checkpointNumber,
228
+ msgCount: l1ToL2Messages.length,
229
+ initialStateReference: stateReference.toInspect(),
230
+ initialArchiveRoot: bufferToHex(archiveTree.root),
231
+ constants,
232
+ });
233
+
234
+ const lightweightBuilder = await LightweightCheckpointBuilder.startNewCheckpoint(
235
+ checkpointNumber,
236
+ constants,
237
+ l1ToL2Messages,
238
+ previousCheckpointOutHashes,
239
+ fork,
240
+ bindings,
241
+ );
242
+
243
+ return new CheckpointBuilder(
244
+ lightweightBuilder,
245
+ fork,
246
+ this.config,
247
+ this.contractDataSource,
248
+ this.dateProvider,
249
+ this.telemetryClient,
250
+ bindings,
251
+ );
252
+ }
253
+
254
+ /**
255
+ * Opens a checkpoint, either starting fresh or resuming from existing blocks.
256
+ */
257
+ async openCheckpoint(
258
+ checkpointNumber: CheckpointNumber,
259
+ constants: CheckpointGlobalVariables,
260
+ l1ToL2Messages: Fr[],
261
+ previousCheckpointOutHashes: Fr[],
262
+ fork: MerkleTreeWriteOperations,
263
+ existingBlocks: L2Block[] = [],
264
+ bindings?: LoggerBindings,
265
+ ): Promise<CheckpointBuilder> {
266
+ const stateReference = await fork.getStateReference();
267
+ const archiveTree = await fork.getTreeInfo(MerkleTreeId.ARCHIVE);
268
+
269
+ if (existingBlocks.length === 0) {
270
+ return this.startCheckpoint(
271
+ checkpointNumber,
272
+ constants,
273
+ l1ToL2Messages,
274
+ previousCheckpointOutHashes,
275
+ fork,
276
+ bindings,
277
+ );
278
+ }
279
+
280
+ this.log.verbose(`Resuming checkpoint ${checkpointNumber} with ${existingBlocks.length} existing blocks`, {
281
+ checkpointNumber,
282
+ msgCount: l1ToL2Messages.length,
283
+ existingBlockCount: existingBlocks.length,
284
+ initialStateReference: stateReference.toInspect(),
285
+ initialArchiveRoot: bufferToHex(archiveTree.root),
286
+ constants,
287
+ });
288
+
289
+ const lightweightBuilder = await LightweightCheckpointBuilder.resumeCheckpoint(
290
+ checkpointNumber,
291
+ constants,
292
+ l1ToL2Messages,
293
+ previousCheckpointOutHashes,
294
+ fork,
295
+ existingBlocks,
296
+ bindings,
297
+ );
298
+
299
+ return new CheckpointBuilder(
300
+ lightweightBuilder,
301
+ fork,
302
+ this.config,
303
+ this.contractDataSource,
304
+ this.dateProvider,
305
+ this.telemetryClient,
306
+ bindings,
307
+ );
308
+ }
309
+
310
+ /** Returns a fork of the world state at the given block number. */
311
+ getFork(blockNumber: BlockNumber): Promise<MerkleTreeWriteOperations> {
312
+ return this.worldState.fork(blockNumber);
313
+ }
314
+ }
package/src/config.ts CHANGED
@@ -1,38 +1,48 @@
1
- import { NULL_KEY } from '@aztec/ethereum';
2
1
  import {
3
2
  type ConfigMappingsType,
4
3
  booleanConfigHelper,
5
4
  getConfigFromMappings,
6
5
  numberConfigHelper,
6
+ secretValueConfigHelper,
7
7
  } from '@aztec/foundation/config';
8
+ import { EthAddress } from '@aztec/foundation/eth-address';
9
+ import type { ValidatorClientConfig } from '@aztec/stdlib/interfaces/server';
10
+ import { validatorHASignerConfigMappings } from '@aztec/validator-ha-signer/config';
8
11
 
9
- /**
10
- * The Validator Configuration
11
- */
12
- export interface ValidatorClientConfig {
13
- /** The private key of the validator participating in attestation duties */
14
- validatorPrivateKey?: string;
15
-
16
- /** Do not run the validator */
17
- disableValidator: boolean;
18
-
19
- /** Interval between polling for new attestations from peers */
20
- attestationPollingIntervalMs: number;
21
-
22
- /** Re-execute transactions before attesting */
23
- validatorReexecute: boolean;
24
- }
12
+ export type { ValidatorClientConfig };
25
13
 
26
14
  export const validatorClientConfigMappings: ConfigMappingsType<ValidatorClientConfig> = {
27
- validatorPrivateKey: {
28
- env: 'VALIDATOR_PRIVATE_KEY',
29
- parseEnv: (val: string) => (val ? `0x${val.replace('0x', '')}` : NULL_KEY),
30
- description: 'The private key of the validator participating in attestation duties',
15
+ validatorPrivateKeys: {
16
+ env: 'VALIDATOR_PRIVATE_KEYS',
17
+ description: 'List of private keys of the validators participating in attestation duties',
18
+ ...secretValueConfigHelper<`0x${string}`[]>(val =>
19
+ val ? val.split(',').map<`0x${string}`>(key => `0x${key.replace('0x', '')}`) : [],
20
+ ),
21
+ fallback: ['VALIDATOR_PRIVATE_KEY'],
22
+ },
23
+ validatorAddresses: {
24
+ env: 'VALIDATOR_ADDRESSES',
25
+ description: 'List of addresses of the validators to use with remote signers',
26
+ parseEnv: (val: string) =>
27
+ val
28
+ .split(',')
29
+ .filter(address => address && address.trim().length > 0)
30
+ .map(address => EthAddress.fromString(address.trim())),
31
+ defaultValue: [],
31
32
  },
32
33
  disableValidator: {
33
34
  env: 'VALIDATOR_DISABLED',
34
35
  description: 'Do not run the validator',
35
- ...booleanConfigHelper(),
36
+ ...booleanConfigHelper(false),
37
+ },
38
+ disabledValidators: {
39
+ description: 'Temporarily disable these specific validator addresses',
40
+ parseEnv: (val: string) =>
41
+ val
42
+ .split(',')
43
+ .filter(address => address && address.trim().length > 0)
44
+ .map(address => EthAddress.fromString(address.trim())),
45
+ defaultValue: [],
36
46
  },
37
47
  attestationPollingIntervalMs: {
38
48
  env: 'VALIDATOR_ATTESTATIONS_POLLING_INTERVAL_MS',
@@ -44,6 +54,26 @@ export const validatorClientConfigMappings: ConfigMappingsType<ValidatorClientCo
44
54
  description: 'Re-execute transactions before attesting',
45
55
  ...booleanConfigHelper(true),
46
56
  },
57
+ alwaysReexecuteBlockProposals: {
58
+ description:
59
+ 'Whether to always reexecute block proposals, even for non-validator nodes (useful for monitoring network status).',
60
+ defaultValue: true,
61
+ },
62
+ fishermanMode: {
63
+ env: 'FISHERMAN_MODE',
64
+ description:
65
+ 'Whether to run in fisherman mode: validates all proposals and attestations but does not broadcast attestations or participate in consensus.',
66
+ ...booleanConfigHelper(false),
67
+ },
68
+ skipCheckpointProposalValidation: {
69
+ description: 'Skip checkpoint proposal validation and always attest (default: false)',
70
+ defaultValue: false,
71
+ },
72
+ skipPushProposedBlocksToArchiver: {
73
+ description: 'Skip pushing re-executed blocks to archiver (default: false)',
74
+ defaultValue: false,
75
+ },
76
+ ...validatorHASignerConfigMappings,
47
77
  };
48
78
 
49
79
  /**
@@ -1,45 +1,219 @@
1
+ import {
2
+ BlockNumber,
3
+ type CheckpointNumber,
4
+ IndexWithinCheckpoint,
5
+ type SlotNumber,
6
+ } from '@aztec/foundation/branded-types';
1
7
  import { Buffer32 } from '@aztec/foundation/buffer';
2
- import { keccak256 } from '@aztec/foundation/crypto';
3
- import type { Fr } from '@aztec/foundation/fields';
4
- import { BlockAttestation, BlockProposal, ConsensusPayload, SignatureDomainSeparator } from '@aztec/stdlib/p2p';
5
- import type { BlockHeader, TxHash } from '@aztec/stdlib/tx';
8
+ import { keccak256 } from '@aztec/foundation/crypto/keccak';
9
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
+ import type { EthAddress } from '@aztec/foundation/eth-address';
11
+ import type { Signature } from '@aztec/foundation/eth-signature';
12
+ import { createLogger } from '@aztec/foundation/log';
13
+ import type { CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
14
+ import type { CreateCheckpointProposalLastBlockData } from '@aztec/stdlib/interfaces/server';
15
+ import {
16
+ BlockProposal,
17
+ type BlockProposalOptions,
18
+ CheckpointAttestation,
19
+ CheckpointProposal,
20
+ type CheckpointProposalCore,
21
+ type CheckpointProposalOptions,
22
+ ConsensusPayload,
23
+ SignatureDomainSeparator,
24
+ } from '@aztec/stdlib/p2p';
25
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
26
+ import type { BlockHeader, Tx } from '@aztec/stdlib/tx';
27
+ import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
28
+ import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
6
29
 
7
30
  import type { ValidatorKeyStore } from '../key_store/interface.js';
8
31
 
9
32
  export class ValidationService {
10
- constructor(private keyStore: ValidatorKeyStore) {}
33
+ constructor(
34
+ private keyStore: ValidatorKeyStore,
35
+ private log = createLogger('validator:validation-service'),
36
+ ) {}
11
37
 
12
38
  /**
13
39
  * Create a block proposal with the given header, archive, and transactions
14
40
  *
15
- * @param header - The block header
41
+ * @param blockHeader - The block header
42
+ * @param blockIndexWithinCheckpoint - The block index within checkpoint for HA signing context
43
+ * @param inHash - Hash of L1 to L2 messages for this checkpoint
16
44
  * @param archive - The archive of the current block
17
- * @param txs - TxHash[] ordered list of transactions
45
+ * @param txs - Ordered list of transactions (Tx[])
46
+ * @param proposerAttesterAddress - The address of the proposer/attester, or undefined
47
+ * @param options - Block proposal options (including broadcastInvalidBlockProposal for testing)
18
48
  *
19
- * @returns A block proposal signing the above information (not the current implementation!!!)
49
+ * @returns A block proposal signing the above information
50
+ * @throws DutyAlreadySignedError if HA signer indicates duty already signed by another node
51
+ * @throws SlashingProtectionError if attempting to sign different data for same slot
20
52
  */
21
- createBlockProposal(header: BlockHeader, archive: Fr, txs: TxHash[]): Promise<BlockProposal> {
22
- const payloadSigner = (payload: Buffer32) => this.keyStore.signMessage(payload);
53
+ public createBlockProposal(
54
+ blockHeader: BlockHeader,
55
+ blockIndexWithinCheckpoint: IndexWithinCheckpoint,
56
+ inHash: Fr,
57
+ archive: Fr,
58
+ txs: Tx[],
59
+ proposerAttesterAddress: EthAddress | undefined,
60
+ options: BlockProposalOptions,
61
+ ): Promise<BlockProposal> {
62
+ // For testing: change the new archive to trigger state_mismatch validation failure
63
+ if (options.broadcastInvalidBlockProposal) {
64
+ archive = Fr.random();
65
+ this.log.warn(`Creating INVALID block proposal for slot ${blockHeader.globalVariables.slotNumber}`);
66
+ }
23
67
 
24
- return BlockProposal.createProposalFromSigner(new ConsensusPayload(header, archive, txs), payloadSigner);
68
+ // Create a signer that uses the appropriate address
69
+ const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
70
+ const payloadSigner = (payload: Buffer32, context: SigningContext) =>
71
+ this.keyStore.signMessageWithAddress(address, payload, context);
72
+
73
+ return BlockProposal.createProposalFromSigner(
74
+ blockHeader,
75
+ blockIndexWithinCheckpoint,
76
+ inHash,
77
+ archive,
78
+ txs.map(tx => tx.getTxHash()),
79
+ options.publishFullTxs ? txs : undefined,
80
+ payloadSigner,
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Create a checkpoint proposal with the last block header and checkpoint header
86
+ *
87
+ * @param checkpointHeader - The checkpoint header containing aggregated data
88
+ * @param archive - The archive of the checkpoint
89
+ * @param lastBlockInfo - Info about the last block (header, index, txs) or undefined
90
+ * @param proposerAttesterAddress - The address of the proposer
91
+ * @param options - Checkpoint proposal options
92
+ *
93
+ * @returns A checkpoint proposal signing the above information
94
+ */
95
+ public createCheckpointProposal(
96
+ checkpointHeader: CheckpointHeader,
97
+ archive: Fr,
98
+ lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
99
+ proposerAttesterAddress: EthAddress | undefined,
100
+ options: CheckpointProposalOptions,
101
+ ): Promise<CheckpointProposal> {
102
+ // For testing: change the archive to trigger state_mismatch validation failure
103
+ if (options.broadcastInvalidCheckpointProposal) {
104
+ archive = Fr.random();
105
+ this.log.warn(`Creating INVALID checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
106
+ }
107
+
108
+ // Create a signer that takes payload and context, and uses the appropriate address
109
+ const payloadSigner = (payload: Buffer32, context: SigningContext) => {
110
+ const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
111
+ return this.keyStore.signMessageWithAddress(address, payload, context);
112
+ };
113
+
114
+ // Last block to include in the proposal
115
+ const lastBlock = lastBlockInfo && {
116
+ blockHeader: lastBlockInfo.blockHeader,
117
+ indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
118
+ txHashes: lastBlockInfo.txs.map(tx => tx.getTxHash()),
119
+ txs: options.publishFullTxs ? lastBlockInfo.txs : undefined,
120
+ };
121
+
122
+ return CheckpointProposal.createProposalFromSigner(checkpointHeader, archive, lastBlock, payloadSigner);
25
123
  }
26
124
 
27
125
  /**
28
- * Attest to the given block proposal constructed by the current sequencer
126
+ * Attest with selection of validators to the given checkpoint proposal
29
127
  *
30
128
  * NOTE: This is just a blind signing.
31
129
  * We assume that the proposal is valid and DA guarantees have been checked previously.
32
130
  *
33
- * @param proposal - The proposal to attest to
34
- * @returns attestation
131
+ * @param proposal - The checkpoint proposal (core version without lastBlock) to attest to
132
+ * @param attestors - The validators to attest with
133
+ * @returns checkpoint attestations
134
+ */
135
+ async attestToCheckpointProposal(
136
+ proposal: CheckpointProposalCore,
137
+ attestors: EthAddress[],
138
+ ): Promise<CheckpointAttestation[]> {
139
+ // Create the attestation payload from the checkpoint proposal
140
+ const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive);
141
+ const buf = Buffer32.fromBuffer(
142
+ keccak256(payload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation)),
143
+ );
144
+
145
+ // TODO(spy/ha): Use checkpointNumber instead of blockNumber once CheckpointHeader includes it.
146
+ // Currently using lastBlock.blockNumber as a proxy for checkpoint identification in HA signing.
147
+ // blockNumber is NOT used for the primary key so it's safe to use here.
148
+ // See CheckpointHeader TODO and SigningContext types documentation.
149
+ let blockNumber: BlockNumber;
150
+ try {
151
+ blockNumber = proposal.blockNumber;
152
+ } catch {
153
+ // Checkpoint proposal may not have lastBlock, use 0 as fallback
154
+ blockNumber = BlockNumber(0);
155
+ }
156
+ const context: SigningContext = {
157
+ slot: proposal.slotNumber,
158
+ blockNumber,
159
+ dutyType: DutyType.ATTESTATION,
160
+ };
161
+
162
+ // Sign each attestor in parallel, catching HA errors per-attestor
163
+ const results = await Promise.allSettled(
164
+ attestors.map(async attestor => {
165
+ const sig = await this.keyStore.signMessageWithAddress(attestor, buf, context);
166
+ // return new BlockAttestation(proposal.payload, sig, proposal.signature);
167
+ return new CheckpointAttestation(payload, sig, proposal.signature);
168
+ }),
169
+ );
170
+
171
+ const attestations: CheckpointAttestation[] = [];
172
+ for (let i = 0; i < results.length; i++) {
173
+ const result = results[i];
174
+ if (result.status === 'fulfilled') {
175
+ attestations.push(result.value);
176
+ } else {
177
+ const error = result.reason;
178
+ if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
179
+ this.log.info(
180
+ `Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
181
+ );
182
+ // Continue with remaining attestors
183
+ } else {
184
+ throw error;
185
+ }
186
+ }
187
+ }
188
+
189
+ return attestations;
190
+ }
191
+
192
+ /**
193
+ * Sign attestations and signers payload
194
+ * @param attestationsAndSigners - The attestations and signers to sign
195
+ * @param proposer - The proposer address to sign with
196
+ * @param slot - The slot number for HA signing context
197
+ * @param blockNumber - The block or checkpoint number for HA signing context
198
+ * @returns signature
199
+ * @throws DutyAlreadySignedError if already signed by another HA node
200
+ * @throws SlashingProtectionError if attempting to sign different data for same slot
35
201
  */
36
- async attestToProposal(proposal: BlockProposal): Promise<BlockAttestation> {
37
- // TODO(https://github.com/AztecProtocol/aztec-packages/issues/7961): check that the current validator is correct
202
+ signAttestationsAndSigners(
203
+ attestationsAndSigners: CommitteeAttestationsAndSigners,
204
+ proposer: EthAddress,
205
+ slot: SlotNumber,
206
+ blockNumber: BlockNumber | CheckpointNumber,
207
+ ): Promise<Signature> {
208
+ const context: SigningContext = {
209
+ slot,
210
+ blockNumber,
211
+ dutyType: DutyType.ATTESTATIONS_AND_SIGNERS,
212
+ };
38
213
 
39
214
  const buf = Buffer32.fromBuffer(
40
- keccak256(await proposal.payload.getPayloadToSign(SignatureDomainSeparator.blockAttestation)),
215
+ keccak256(attestationsAndSigners.getPayloadToSign(SignatureDomainSeparator.attestationsAndSigners)),
41
216
  );
42
- const sig = await this.keyStore.signMessage(buf);
43
- return new BlockAttestation(proposal.payload, sig);
217
+ return this.keyStore.signMessageWithAddress(proposer, buf, context);
44
218
  }
45
219
  }