@aztec/validator-client 0.0.1-commit.fce3e4f → 0.0.1-commit.ffe5b04ea
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +327 -0
- package/dest/block_proposal_handler.d.ts +25 -14
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +366 -105
- package/dest/checkpoint_builder.d.ts +76 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +228 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +37 -8
- package/dest/duties/validation_service.d.ts +42 -13
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +105 -28
- package/dest/factory.d.ts +13 -8
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +4 -3
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/key_store/ha_key_store.d.ts +99 -0
- package/dest/key_store/ha_key_store.d.ts.map +1 -0
- package/dest/key_store/ha_key_store.js +208 -0
- package/dest/key_store/index.d.ts +2 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +1 -0
- package/dest/key_store/interface.d.ts +36 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/local_key_store.d.ts +10 -5
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +9 -5
- package/dest/key_store/node_keystore_adapter.d.ts +18 -5
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -1
- package/dest/key_store/node_keystore_adapter.js +18 -4
- package/dest/key_store/web3signer_key_store.d.ts +10 -5
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -1
- package/dest/key_store/web3signer_key_store.js +9 -5
- package/dest/metrics.d.ts +12 -3
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +46 -30
- package/dest/validator.d.ts +76 -21
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +478 -57
- package/package.json +23 -13
- package/src/block_proposal_handler.ts +288 -75
- package/src/checkpoint_builder.ts +390 -0
- package/src/config.ts +36 -7
- package/src/duties/validation_service.ts +156 -33
- package/src/factory.ts +18 -8
- package/src/index.ts +1 -0
- package/src/key_store/ha_key_store.ts +269 -0
- package/src/key_store/index.ts +1 -0
- package/src/key_store/interface.ts +44 -5
- package/src/key_store/local_key_store.ts +14 -5
- package/src/key_store/node_keystore_adapter.ts +28 -5
- package/src/key_store/web3signer_key_store.ts +18 -5
- package/src/metrics.ts +63 -33
- package/src/validator.ts +640 -85
|
@@ -1,19 +1,31 @@
|
|
|
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';
|
|
8
|
+
import { keccak256 } from '@aztec/foundation/crypto/keccak';
|
|
9
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
10
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
4
11
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
5
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
6
12
|
import { createLogger } from '@aztec/foundation/log';
|
|
7
13
|
import type { CommitteeAttestationsAndSigners } from '@aztec/stdlib/block';
|
|
14
|
+
import type { CreateCheckpointProposalLastBlockData } from '@aztec/stdlib/interfaces/server';
|
|
8
15
|
import {
|
|
9
|
-
BlockAttestation,
|
|
10
16
|
BlockProposal,
|
|
11
17
|
type BlockProposalOptions,
|
|
18
|
+
CheckpointAttestation,
|
|
19
|
+
CheckpointProposal,
|
|
20
|
+
type CheckpointProposalCore,
|
|
21
|
+
type CheckpointProposalOptions,
|
|
12
22
|
ConsensusPayload,
|
|
13
23
|
SignatureDomainSeparator,
|
|
14
24
|
} from '@aztec/stdlib/p2p';
|
|
15
25
|
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
16
|
-
import type { Tx } from '@aztec/stdlib/tx';
|
|
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';
|
|
17
29
|
|
|
18
30
|
import type { ValidatorKeyStore } from '../key_store/interface.js';
|
|
19
31
|
|
|
@@ -26,72 +38,183 @@ export class ValidationService {
|
|
|
26
38
|
/**
|
|
27
39
|
* Create a block proposal with the given header, archive, and transactions
|
|
28
40
|
*
|
|
29
|
-
* @param
|
|
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
|
|
30
44
|
* @param archive - The archive of the current block
|
|
31
|
-
* @param txs -
|
|
45
|
+
* @param txs - Ordered list of transactions (Tx[])
|
|
46
|
+
* @param proposerAttesterAddress - The address of the proposer/attester, or undefined
|
|
32
47
|
* @param options - Block proposal options (including broadcastInvalidBlockProposal for testing)
|
|
33
48
|
*
|
|
34
|
-
* @returns A block proposal signing the above information
|
|
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
|
|
35
52
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
53
|
+
public createBlockProposal(
|
|
54
|
+
blockHeader: BlockHeader,
|
|
55
|
+
blockIndexWithinCheckpoint: IndexWithinCheckpoint,
|
|
56
|
+
inHash: Fr,
|
|
38
57
|
archive: Fr,
|
|
39
58
|
txs: Tx[],
|
|
40
59
|
proposerAttesterAddress: EthAddress | undefined,
|
|
41
60
|
options: BlockProposalOptions,
|
|
42
61
|
): Promise<BlockProposal> {
|
|
43
|
-
let payloadSigner: (payload: Buffer32) => Promise<Signature>;
|
|
44
|
-
if (proposerAttesterAddress !== undefined) {
|
|
45
|
-
payloadSigner = (payload: Buffer32) => this.keyStore.signMessageWithAddress(proposerAttesterAddress, payload);
|
|
46
|
-
} else {
|
|
47
|
-
// if there is no proposer attester address, just use the first signer
|
|
48
|
-
const signer = this.keyStore.getAddress(0);
|
|
49
|
-
payloadSigner = (payload: Buffer32) => this.keyStore.signMessageWithAddress(signer, payload);
|
|
50
|
-
}
|
|
51
|
-
// TODO: check if this is calculated earlier / can not be recomputed
|
|
52
|
-
const txHashes = await Promise.all(txs.map(tx => tx.getTxHash()));
|
|
53
|
-
|
|
54
62
|
// For testing: change the new archive to trigger state_mismatch validation failure
|
|
55
63
|
if (options.broadcastInvalidBlockProposal) {
|
|
56
64
|
archive = Fr.random();
|
|
57
|
-
this.log.warn(`Creating INVALID block proposal for slot ${
|
|
65
|
+
this.log.warn(`Creating INVALID block proposal for slot ${blockHeader.globalVariables.slotNumber}`);
|
|
58
66
|
}
|
|
59
67
|
|
|
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
|
+
|
|
60
73
|
return BlockProposal.createProposalFromSigner(
|
|
61
|
-
|
|
62
|
-
|
|
74
|
+
blockHeader,
|
|
75
|
+
blockIndexWithinCheckpoint,
|
|
76
|
+
inHash,
|
|
77
|
+
archive,
|
|
78
|
+
txs.map(tx => tx.getTxHash()),
|
|
63
79
|
options.publishFullTxs ? txs : undefined,
|
|
64
80
|
payloadSigner,
|
|
65
81
|
);
|
|
66
82
|
}
|
|
67
83
|
|
|
68
84
|
/**
|
|
69
|
-
*
|
|
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
|
+
feeAssetPriceModifier: bigint,
|
|
99
|
+
lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
|
|
100
|
+
proposerAttesterAddress: EthAddress | undefined,
|
|
101
|
+
options: CheckpointProposalOptions,
|
|
102
|
+
): Promise<CheckpointProposal> {
|
|
103
|
+
// For testing: change the archive to trigger state_mismatch validation failure
|
|
104
|
+
if (options.broadcastInvalidCheckpointProposal) {
|
|
105
|
+
archive = Fr.random();
|
|
106
|
+
this.log.warn(`Creating INVALID checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Create a signer that takes payload and context, and uses the appropriate address
|
|
110
|
+
const payloadSigner = (payload: Buffer32, context: SigningContext) => {
|
|
111
|
+
const address = proposerAttesterAddress ?? this.keyStore.getAddress(0);
|
|
112
|
+
return this.keyStore.signMessageWithAddress(address, payload, context);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Last block to include in the proposal
|
|
116
|
+
const lastBlock = lastBlockInfo && {
|
|
117
|
+
blockHeader: lastBlockInfo.blockHeader,
|
|
118
|
+
indexWithinCheckpoint: lastBlockInfo.indexWithinCheckpoint,
|
|
119
|
+
txHashes: lastBlockInfo.txs.map(tx => tx.getTxHash()),
|
|
120
|
+
txs: options.publishFullTxs ? lastBlockInfo.txs : undefined,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return CheckpointProposal.createProposalFromSigner(
|
|
124
|
+
checkpointHeader,
|
|
125
|
+
archive,
|
|
126
|
+
feeAssetPriceModifier,
|
|
127
|
+
lastBlock,
|
|
128
|
+
payloadSigner,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Attest with selection of validators to the given checkpoint proposal
|
|
70
134
|
*
|
|
71
135
|
* NOTE: This is just a blind signing.
|
|
72
136
|
* We assume that the proposal is valid and DA guarantees have been checked previously.
|
|
73
137
|
*
|
|
74
|
-
* @param proposal - The proposal to attest to
|
|
138
|
+
* @param proposal - The checkpoint proposal (core version without lastBlock) to attest to
|
|
75
139
|
* @param attestors - The validators to attest with
|
|
76
|
-
* @returns attestations
|
|
140
|
+
* @returns checkpoint attestations
|
|
77
141
|
*/
|
|
78
|
-
async
|
|
142
|
+
async attestToCheckpointProposal(
|
|
143
|
+
proposal: CheckpointProposalCore,
|
|
144
|
+
attestors: EthAddress[],
|
|
145
|
+
): Promise<CheckpointAttestation[]> {
|
|
146
|
+
// Create the attestation payload from the checkpoint proposal
|
|
147
|
+
const payload = new ConsensusPayload(proposal.checkpointHeader, proposal.archive, proposal.feeAssetPriceModifier);
|
|
79
148
|
const buf = Buffer32.fromBuffer(
|
|
80
|
-
keccak256(
|
|
149
|
+
keccak256(payload.getPayloadToSign(SignatureDomainSeparator.checkpointAttestation)),
|
|
81
150
|
);
|
|
82
|
-
|
|
83
|
-
|
|
151
|
+
|
|
152
|
+
// TODO(spy/ha): Use checkpointNumber instead of blockNumber once CheckpointHeader includes it.
|
|
153
|
+
// CheckpointProposalCore doesn't have lastBlock info, so use 0 as a proxy.
|
|
154
|
+
// blockNumber is NOT used for the primary key so it's safe to use here.
|
|
155
|
+
// See CheckpointHeader TODO and SigningContext types documentation.
|
|
156
|
+
const blockNumber = BlockNumber(0);
|
|
157
|
+
const context: SigningContext = {
|
|
158
|
+
slot: proposal.slotNumber,
|
|
159
|
+
blockNumber,
|
|
160
|
+
dutyType: DutyType.ATTESTATION,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Sign each attestor in parallel, catching HA errors per-attestor
|
|
164
|
+
const results = await Promise.allSettled(
|
|
165
|
+
attestors.map(async attestor => {
|
|
166
|
+
const sig = await this.keyStore.signMessageWithAddress(attestor, buf, context);
|
|
167
|
+
// return new BlockAttestation(proposal.payload, sig, proposal.signature);
|
|
168
|
+
return new CheckpointAttestation(payload, sig, proposal.signature);
|
|
169
|
+
}),
|
|
84
170
|
);
|
|
85
|
-
|
|
171
|
+
|
|
172
|
+
const attestations: CheckpointAttestation[] = [];
|
|
173
|
+
for (let i = 0; i < results.length; i++) {
|
|
174
|
+
const result = results[i];
|
|
175
|
+
if (result.status === 'fulfilled') {
|
|
176
|
+
attestations.push(result.value);
|
|
177
|
+
} else {
|
|
178
|
+
const error = result.reason;
|
|
179
|
+
if (error instanceof DutyAlreadySignedError || error instanceof SlashingProtectionError) {
|
|
180
|
+
this.log.info(
|
|
181
|
+
`Attestation for slot ${proposal.slotNumber} by ${attestors[i]} already signed by another High-Availability node`,
|
|
182
|
+
);
|
|
183
|
+
// Continue with remaining attestors
|
|
184
|
+
} else {
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return attestations;
|
|
86
191
|
}
|
|
87
192
|
|
|
88
|
-
|
|
193
|
+
/**
|
|
194
|
+
* Sign attestations and signers payload
|
|
195
|
+
* @param attestationsAndSigners - The attestations and signers to sign
|
|
196
|
+
* @param proposer - The proposer address to sign with
|
|
197
|
+
* @param slot - The slot number for HA signing context
|
|
198
|
+
* @param blockNumber - The block or checkpoint number for HA signing context
|
|
199
|
+
* @returns signature
|
|
200
|
+
* @throws DutyAlreadySignedError if already signed by another HA node
|
|
201
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot
|
|
202
|
+
*/
|
|
203
|
+
signAttestationsAndSigners(
|
|
89
204
|
attestationsAndSigners: CommitteeAttestationsAndSigners,
|
|
90
205
|
proposer: EthAddress,
|
|
206
|
+
slot: SlotNumber,
|
|
207
|
+
blockNumber: BlockNumber | CheckpointNumber,
|
|
91
208
|
): Promise<Signature> {
|
|
209
|
+
const context: SigningContext = {
|
|
210
|
+
slot,
|
|
211
|
+
blockNumber,
|
|
212
|
+
dutyType: DutyType.ATTESTATIONS_AND_SIGNERS,
|
|
213
|
+
};
|
|
214
|
+
|
|
92
215
|
const buf = Buffer32.fromBuffer(
|
|
93
216
|
keccak256(attestationsAndSigners.getPayloadToSign(SignatureDomainSeparator.attestationsAndSigners)),
|
|
94
217
|
);
|
|
95
|
-
return
|
|
218
|
+
return this.keyStore.signMessageWithAddress(proposer, buf, context);
|
|
96
219
|
}
|
|
97
220
|
}
|
package/src/factory.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
+
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
1
2
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
2
3
|
import type { DateProvider } from '@aztec/foundation/timer';
|
|
3
4
|
import type { KeystoreManager } from '@aztec/node-keystore';
|
|
4
5
|
import { BlockProposalValidator, type P2PClient } from '@aztec/p2p';
|
|
5
|
-
import type { L2BlockSource } from '@aztec/stdlib/block';
|
|
6
|
-
import type {
|
|
6
|
+
import type { L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
7
|
+
import type { ValidatorClientFullConfig, WorldStateSynchronizer } from '@aztec/stdlib/interfaces/server';
|
|
7
8
|
import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
|
|
8
9
|
import type { TelemetryClient } from '@aztec/telemetry-client';
|
|
9
10
|
|
|
10
11
|
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
12
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
11
13
|
import { ValidatorMetrics } from './metrics.js';
|
|
12
14
|
import { ValidatorClient } from './validator.js';
|
|
13
15
|
|
|
14
16
|
export function createBlockProposalHandler(
|
|
15
17
|
config: ValidatorClientFullConfig,
|
|
16
18
|
deps: {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
checkpointsBuilder: FullNodeCheckpointsBuilder;
|
|
20
|
+
worldState: WorldStateSynchronizer;
|
|
21
|
+
blockSource: L2BlockSource & L2BlockSink;
|
|
19
22
|
l1ToL2MessageSource: L1ToL2MessageSource;
|
|
20
23
|
p2pClient: P2PClient;
|
|
21
24
|
epochCache: EpochCache;
|
|
@@ -26,13 +29,16 @@ export function createBlockProposalHandler(
|
|
|
26
29
|
const metrics = new ValidatorMetrics(deps.telemetry);
|
|
27
30
|
const blockProposalValidator = new BlockProposalValidator(deps.epochCache, {
|
|
28
31
|
txsPermitted: !config.disableTransactions,
|
|
32
|
+
maxTxsPerBlock: config.validateMaxTxsPerBlock,
|
|
29
33
|
});
|
|
30
34
|
return new BlockProposalHandler(
|
|
31
|
-
deps.
|
|
35
|
+
deps.checkpointsBuilder,
|
|
36
|
+
deps.worldState,
|
|
32
37
|
deps.blockSource,
|
|
33
38
|
deps.l1ToL2MessageSource,
|
|
34
39
|
deps.p2pClient.getTxProvider(),
|
|
35
40
|
blockProposalValidator,
|
|
41
|
+
deps.epochCache,
|
|
36
42
|
config,
|
|
37
43
|
metrics,
|
|
38
44
|
deps.dateProvider,
|
|
@@ -43,14 +49,16 @@ export function createBlockProposalHandler(
|
|
|
43
49
|
export function createValidatorClient(
|
|
44
50
|
config: ValidatorClientFullConfig,
|
|
45
51
|
deps: {
|
|
46
|
-
|
|
52
|
+
checkpointsBuilder: FullNodeCheckpointsBuilder;
|
|
53
|
+
worldState: WorldStateSynchronizer;
|
|
47
54
|
p2pClient: P2PClient;
|
|
48
|
-
blockSource: L2BlockSource;
|
|
55
|
+
blockSource: L2BlockSource & L2BlockSink;
|
|
49
56
|
l1ToL2MessageSource: L1ToL2MessageSource;
|
|
50
57
|
telemetry: TelemetryClient;
|
|
51
58
|
dateProvider: DateProvider;
|
|
52
59
|
epochCache: EpochCache;
|
|
53
60
|
keyStoreManager: KeystoreManager | undefined;
|
|
61
|
+
blobClient: BlobClientInterface;
|
|
54
62
|
},
|
|
55
63
|
) {
|
|
56
64
|
if (config.disableValidator || !deps.keyStoreManager) {
|
|
@@ -60,13 +68,15 @@ export function createValidatorClient(
|
|
|
60
68
|
const txProvider = deps.p2pClient.getTxProvider();
|
|
61
69
|
return ValidatorClient.new(
|
|
62
70
|
config,
|
|
63
|
-
deps.
|
|
71
|
+
deps.checkpointsBuilder,
|
|
72
|
+
deps.worldState,
|
|
64
73
|
deps.epochCache,
|
|
65
74
|
deps.p2pClient,
|
|
66
75
|
deps.blockSource,
|
|
67
76
|
deps.l1ToL2MessageSource,
|
|
68
77
|
txProvider,
|
|
69
78
|
deps.keyStoreManager,
|
|
79
|
+
deps.blobClient,
|
|
70
80
|
deps.dateProvider,
|
|
71
81
|
deps.telemetry,
|
|
72
82
|
);
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High Availability Key Store
|
|
3
|
+
*
|
|
4
|
+
* A ValidatorKeyStore wrapper that adds slashing protection for HA validator setups.
|
|
5
|
+
* When multiple validator nodes are running, only one node will sign for a given duty.
|
|
6
|
+
*/
|
|
7
|
+
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
8
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
9
|
+
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
10
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
11
|
+
import type { EthRemoteSignerConfig } from '@aztec/node-keystore';
|
|
12
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
13
|
+
import { DutyAlreadySignedError, SlashingProtectionError } from '@aztec/validator-ha-signer/errors';
|
|
14
|
+
import {
|
|
15
|
+
type HAProtectedSigningContext,
|
|
16
|
+
type SigningContext,
|
|
17
|
+
isHAProtectedContext,
|
|
18
|
+
} from '@aztec/validator-ha-signer/types';
|
|
19
|
+
import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
|
|
20
|
+
|
|
21
|
+
import { type TypedDataDefinition, hashTypedData } from 'viem';
|
|
22
|
+
|
|
23
|
+
import type { ExtendedValidatorKeyStore } from './interface.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* High Availability Key Store
|
|
27
|
+
*
|
|
28
|
+
* Wraps a base ExtendedValidatorKeyStore and ValidatorHASigner to provide
|
|
29
|
+
* HA-protected signing operations (when context is provided).
|
|
30
|
+
*
|
|
31
|
+
* The extended interface methods (getAttesterAddresses, getCoinbaseAddress, etc.)
|
|
32
|
+
* are pure pass-through since they don't require HA coordination.
|
|
33
|
+
*
|
|
34
|
+
* Usage:
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const baseKeyStore = NodeKeystoreAdapter.fromPrivateKeys(privateKeys);
|
|
37
|
+
* const haSigner = new ValidatorHASigner(db, config);
|
|
38
|
+
* const haKeyStore = new HAKeyStore(baseKeyStore, haSigner);
|
|
39
|
+
*
|
|
40
|
+
* // Without context - signs directly (no HA protection)
|
|
41
|
+
* const sig = await haKeyStore.signMessageWithAddress(addr, msg);
|
|
42
|
+
*
|
|
43
|
+
* // With context - HA protected, throws DutyAlreadySignedError if already signed
|
|
44
|
+
* const result = await haKeyStore.signMessageWithAddress(addr, msg, {
|
|
45
|
+
* slot: 100n,
|
|
46
|
+
* blockNumber: 50n,
|
|
47
|
+
* dutyType: DutyType.BLOCK_PROPOSAL,
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export class HAKeyStore implements ExtendedValidatorKeyStore {
|
|
52
|
+
private readonly log = createLogger('ha-key-store');
|
|
53
|
+
|
|
54
|
+
constructor(
|
|
55
|
+
private readonly baseKeyStore: ExtendedValidatorKeyStore,
|
|
56
|
+
private readonly haSigner: ValidatorHASigner,
|
|
57
|
+
) {
|
|
58
|
+
this.log.info('HAKeyStore initialized', {
|
|
59
|
+
nodeId: haSigner.nodeId,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Sign typed data with all addresses.
|
|
65
|
+
* Coordinates across nodes to prevent double-signing for most duty types.
|
|
66
|
+
* AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
|
|
67
|
+
* Returns only signatures that were successfully claimed by this node.
|
|
68
|
+
*/
|
|
69
|
+
async signTypedData(typedData: TypedDataDefinition, context: SigningContext): Promise<Signature[]> {
|
|
70
|
+
// no need for HA protection on auth request and txs signatures
|
|
71
|
+
if (!isHAProtectedContext(context)) {
|
|
72
|
+
return this.baseKeyStore.signTypedData(typedData, context);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Sign each address with HA protection
|
|
76
|
+
const addresses = this.getAddresses();
|
|
77
|
+
const results = await Promise.allSettled(
|
|
78
|
+
addresses.map(addr => this.signTypedDataWithAddress(addr, typedData, context)),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Filter out failures (already signed by other nodes or other errors)
|
|
82
|
+
return results
|
|
83
|
+
.filter((result): result is PromiseFulfilledResult<Signature> => {
|
|
84
|
+
if (result.status === 'fulfilled') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
// Log expected HA errors (already signed) at debug level
|
|
88
|
+
if (result.reason instanceof DutyAlreadySignedError) {
|
|
89
|
+
this.log.debug(`Duty already signed by another node`, {
|
|
90
|
+
dutyType: context.dutyType,
|
|
91
|
+
slot: context.slot,
|
|
92
|
+
signedByNode: result.reason.signedByNode,
|
|
93
|
+
});
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
// Re-throw unexpected errors
|
|
97
|
+
throw result.reason;
|
|
98
|
+
})
|
|
99
|
+
.map(result => result.value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Sign a message with all addresses.
|
|
104
|
+
* Coordinates across nodes to prevent double-signing for most duty types.
|
|
105
|
+
* AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
|
|
106
|
+
* Returns only signatures that were successfully claimed by this node.
|
|
107
|
+
*/
|
|
108
|
+
async signMessage(message: Buffer32, context: SigningContext): Promise<Signature[]> {
|
|
109
|
+
// no need for HA protection on auth request and txs signatures
|
|
110
|
+
if (!isHAProtectedContext(context)) {
|
|
111
|
+
return this.baseKeyStore.signMessage(message, context);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Sign each address with HA protection
|
|
115
|
+
const addresses = this.getAddresses();
|
|
116
|
+
const results = await Promise.allSettled(
|
|
117
|
+
addresses.map(addr => this.signMessageWithAddress(addr, message, context)),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
// Filter out failures (already signed by other nodes or other errors)
|
|
121
|
+
return results
|
|
122
|
+
.filter((result): result is PromiseFulfilledResult<Signature> => {
|
|
123
|
+
if (result.status === 'fulfilled') {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
// Log expected HA errors (already signed) at debug level
|
|
127
|
+
if (result.reason instanceof DutyAlreadySignedError) {
|
|
128
|
+
this.log.debug(`Duty already signed by another node`, {
|
|
129
|
+
dutyType: context.dutyType,
|
|
130
|
+
slot: context.slot,
|
|
131
|
+
signedByNode: result.reason.signedByNode,
|
|
132
|
+
});
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
// Re-throw unexpected errors
|
|
136
|
+
throw result.reason;
|
|
137
|
+
})
|
|
138
|
+
.map(result => result.value);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Sign typed data with a specific address.
|
|
143
|
+
* Coordinates across nodes to prevent double-signing for most duty types.
|
|
144
|
+
* AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
|
|
145
|
+
* @throws DutyAlreadySignedError if the duty was already signed by another node
|
|
146
|
+
* @throws SlashingProtectionError if attempting to sign different data for the same slot
|
|
147
|
+
*/
|
|
148
|
+
async signTypedDataWithAddress(
|
|
149
|
+
address: EthAddress,
|
|
150
|
+
typedData: TypedDataDefinition,
|
|
151
|
+
context: SigningContext,
|
|
152
|
+
): Promise<Signature> {
|
|
153
|
+
// AUTH_REQUEST and TXS bypass HA protection - multiple signatures are safe
|
|
154
|
+
if (!isHAProtectedContext(context)) {
|
|
155
|
+
return this.baseKeyStore.signTypedDataWithAddress(address, typedData, context);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Compute signing root from typed data for HA tracking
|
|
159
|
+
const digest = hashTypedData(typedData);
|
|
160
|
+
const messageHash = Buffer32.fromString(digest);
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
return await this.haSigner.signWithProtection(address, messageHash, context, () =>
|
|
164
|
+
this.baseKeyStore.signTypedDataWithAddress(address, typedData, context),
|
|
165
|
+
);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
this.processSigningError(error, context);
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Sign a message with a specific address.
|
|
174
|
+
* Coordinates across nodes to prevent double-signing for most duty types.
|
|
175
|
+
* AUTH_REQUEST and TXS duties bypass HA protection since signing multiple times is safe.
|
|
176
|
+
* @throws DutyAlreadySignedError if the duty was already signed by another node
|
|
177
|
+
* @throws SlashingProtectionError if attempting to sign different data for the same slot
|
|
178
|
+
*/
|
|
179
|
+
async signMessageWithAddress(address: EthAddress, message: Buffer32, context: SigningContext): Promise<Signature> {
|
|
180
|
+
// no need for HA protection on auth request and txs signatures
|
|
181
|
+
if (!isHAProtectedContext(context)) {
|
|
182
|
+
return this.baseKeyStore.signMessageWithAddress(address, message, context);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
return await this.haSigner.signWithProtection(address, message, context, messageHash =>
|
|
187
|
+
this.baseKeyStore.signMessageWithAddress(address, messageHash, context),
|
|
188
|
+
);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
this.processSigningError(error, context);
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
196
|
+
// pass-through methods (no HA logic needed)
|
|
197
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
198
|
+
|
|
199
|
+
getAddress(index: number): EthAddress {
|
|
200
|
+
return this.baseKeyStore.getAddress(index);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
getAddresses(): EthAddress[] {
|
|
204
|
+
return this.baseKeyStore.getAddresses();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
getAttesterAddresses(): EthAddress[] {
|
|
208
|
+
return this.baseKeyStore.getAttesterAddresses();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
getCoinbaseAddress(attesterAddress: EthAddress): EthAddress {
|
|
212
|
+
return this.baseKeyStore.getCoinbaseAddress(attesterAddress);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getPublisherAddresses(attesterAddress: EthAddress): EthAddress[] {
|
|
216
|
+
return this.baseKeyStore.getPublisherAddresses(attesterAddress);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
getFeeRecipient(attesterAddress: EthAddress): AztecAddress {
|
|
220
|
+
return this.baseKeyStore.getFeeRecipient(attesterAddress);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
getRemoteSignerConfig(attesterAddress: EthAddress): EthRemoteSignerConfig | undefined {
|
|
224
|
+
return this.baseKeyStore.getRemoteSignerConfig(attesterAddress);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Process signing errors from the HA signer.
|
|
229
|
+
* Logs expected HA errors (already signed) at appropriate levels.
|
|
230
|
+
* Re-throws unexpected errors.
|
|
231
|
+
*/
|
|
232
|
+
private processSigningError(error: unknown, context: HAProtectedSigningContext) {
|
|
233
|
+
if (error instanceof DutyAlreadySignedError) {
|
|
234
|
+
this.log.debug(`Duty already signed by another node with the same payload`, {
|
|
235
|
+
dutyType: context.dutyType,
|
|
236
|
+
slot: context.slot,
|
|
237
|
+
signedByNode: error.signedByNode,
|
|
238
|
+
});
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (error instanceof SlashingProtectionError) {
|
|
243
|
+
this.log.warn(`Duty already signed by another node with different payload`, {
|
|
244
|
+
dutyType: context.dutyType,
|
|
245
|
+
slot: context.slot,
|
|
246
|
+
existingMessageHash: error.existingMessageHash,
|
|
247
|
+
attemptedMessageHash: error.attemptedMessageHash,
|
|
248
|
+
});
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Re-throw errors
|
|
253
|
+
throw error;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Start the high-availability key store
|
|
258
|
+
*/
|
|
259
|
+
public async start() {
|
|
260
|
+
await this.haSigner.start();
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Stop the high-availability key store
|
|
265
|
+
*/
|
|
266
|
+
public async stop() {
|
|
267
|
+
await this.haSigner.stop();
|
|
268
|
+
}
|
|
269
|
+
}
|
package/src/key_store/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
|
3
3
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
4
|
import type { EthRemoteSignerConfig } from '@aztec/node-keystore';
|
|
5
5
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
6
|
+
import type { SigningContext } from '@aztec/validator-ha-signer/types';
|
|
6
7
|
|
|
7
8
|
import type { TypedDataDefinition } from 'viem';
|
|
8
9
|
|
|
@@ -26,17 +27,45 @@ export interface ValidatorKeyStore {
|
|
|
26
27
|
*/
|
|
27
28
|
getAddresses(): EthAddress[];
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Sign typed data with all keystore private keys
|
|
32
|
+
* @param typedData - The complete EIP-712 typed data structure
|
|
33
|
+
* @param context - Signing context for HA slashing protection
|
|
34
|
+
* @returns signatures (when context provided with HA, only successfully claimed signatures are returned)
|
|
35
|
+
*/
|
|
36
|
+
signTypedData(typedData: TypedDataDefinition, context: SigningContext): Promise<Signature[]>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sign typed data with a specific address's private key
|
|
40
|
+
* @param address - The address of the signer to use
|
|
41
|
+
* @param typedData - The complete EIP-712 typed data structure
|
|
42
|
+
* @param context - Signing context for HA slashing protection
|
|
43
|
+
* @returns signature
|
|
44
|
+
*/
|
|
45
|
+
signTypedDataWithAddress(
|
|
46
|
+
address: EthAddress,
|
|
47
|
+
typedData: TypedDataDefinition,
|
|
48
|
+
context: SigningContext,
|
|
49
|
+
): Promise<Signature>;
|
|
50
|
+
|
|
31
51
|
/**
|
|
32
52
|
* Flavor of sign message that followed EIP-712 eth signed message prefix
|
|
33
53
|
* Note: this is only required when we are using ecdsa signatures over secp256k1
|
|
34
54
|
*
|
|
35
55
|
* @param message - The message to sign.
|
|
36
|
-
* @
|
|
56
|
+
* @param context - Signing context for HA slashing protection
|
|
57
|
+
* @returns The signatures (when context provided with HA, only successfully claimed signatures are returned).
|
|
37
58
|
*/
|
|
38
|
-
signMessage(message: Buffer32): Promise<Signature[]>;
|
|
39
|
-
|
|
59
|
+
signMessage(message: Buffer32, context: SigningContext): Promise<Signature[]>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Sign a message with a specific address's private key
|
|
63
|
+
* @param address - The address of the signer to use
|
|
64
|
+
* @param message - The message to sign
|
|
65
|
+
* @param context - Signing context for HA slashing protection
|
|
66
|
+
* @returns signature
|
|
67
|
+
*/
|
|
68
|
+
signMessageWithAddress(address: EthAddress, message: Buffer32, context: SigningContext): Promise<Signature>;
|
|
40
69
|
}
|
|
41
70
|
|
|
42
71
|
/**
|
|
@@ -79,4 +108,14 @@ export interface ExtendedValidatorKeyStore extends ValidatorKeyStore {
|
|
|
79
108
|
* @returns the remote signer configuration or undefined
|
|
80
109
|
*/
|
|
81
110
|
getRemoteSignerConfig(attesterAddress: EthAddress): EthRemoteSignerConfig | undefined;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Start the key store
|
|
114
|
+
*/
|
|
115
|
+
start(): Promise<void>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Stop the key store
|
|
119
|
+
*/
|
|
120
|
+
stop(): Promise<void>;
|
|
82
121
|
}
|