@aztec/validator-client 0.0.1-commit.b655e406 → 0.0.1-commit.bf2612ae
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 +282 -0
- package/dest/block_proposal_handler.d.ts +24 -13
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +349 -89
- package/dest/checkpoint_builder.d.ts +67 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +160 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +17 -7
- 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 +113 -31
- package/dest/factory.d.ts +13 -8
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +2 -2
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -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 -11
- 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 +1 -1
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +8 -33
- package/dest/tx_validator/index.d.ts +3 -0
- package/dest/tx_validator/index.d.ts.map +1 -0
- package/dest/tx_validator/index.js +2 -0
- package/dest/tx_validator/nullifier_cache.d.ts +14 -0
- package/dest/tx_validator/nullifier_cache.d.ts.map +1 -0
- package/dest/tx_validator/nullifier_cache.js +24 -0
- package/dest/tx_validator/tx_validator_factory.d.ts +18 -0
- package/dest/tx_validator/tx_validator_factory.d.ts.map +1 -0
- package/dest/tx_validator/tx_validator_factory.js +54 -0
- package/dest/validator.d.ts +49 -20
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +373 -63
- package/package.json +24 -14
- package/src/block_proposal_handler.ts +277 -66
- package/src/checkpoint_builder.ts +284 -0
- package/src/config.ts +17 -6
- package/src/duties/validation_service.ts +157 -38
- package/src/factory.ts +17 -8
- package/src/index.ts +2 -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 +7 -34
- package/src/tx_validator/index.ts +2 -0
- package/src/tx_validator/nullifier_cache.ts +30 -0
- package/src/tx_validator/tx_validator_factory.ts +135 -0
- package/src/validator.ts +499 -95
package/dest/validator.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
+
import { getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
1
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
2
6
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
3
7
|
import { sleep } from '@aztec/foundation/sleep';
|
|
4
8
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
9
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
6
10
|
import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
|
|
11
|
+
import { getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
12
|
+
import { accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
|
|
7
13
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
8
14
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
15
|
+
import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
16
|
+
import { DutyType } from '@aztec/validator-ha-signer/types';
|
|
9
17
|
import { EventEmitter } from 'events';
|
|
10
18
|
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
11
19
|
import { ValidationService } from './duties/validation_service.js';
|
|
20
|
+
import { HAKeyStore } from './key_store/ha_key_store.js';
|
|
12
21
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
13
22
|
import { ValidatorMetrics } from './metrics.js';
|
|
14
23
|
// We maintain a set of proposers who have proposed invalid blocks.
|
|
@@ -26,12 +35,17 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
26
35
|
epochCache;
|
|
27
36
|
p2pClient;
|
|
28
37
|
blockProposalHandler;
|
|
38
|
+
blockSource;
|
|
39
|
+
checkpointsBuilder;
|
|
40
|
+
worldState;
|
|
41
|
+
l1ToL2MessageSource;
|
|
29
42
|
config;
|
|
43
|
+
blobClient;
|
|
30
44
|
dateProvider;
|
|
31
|
-
log;
|
|
32
45
|
tracer;
|
|
33
46
|
validationService;
|
|
34
47
|
metrics;
|
|
48
|
+
log;
|
|
35
49
|
// Whether it has already registered handlers on the p2p client
|
|
36
50
|
hasRegisteredHandlers;
|
|
37
51
|
// Used to check if we are sending the same proposal twice
|
|
@@ -39,13 +53,19 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
39
53
|
lastEpochForCommitteeUpdateLoop;
|
|
40
54
|
epochCacheUpdateLoop;
|
|
41
55
|
proposersOfInvalidBlocks;
|
|
42
|
-
|
|
43
|
-
|
|
56
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
|
|
57
|
+
// Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
|
|
58
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
59
|
+
validatedBlockSlots;
|
|
60
|
+
constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
61
|
+
super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.blockSource = blockSource, this.checkpointsBuilder = checkpointsBuilder, this.worldState = worldState, this.l1ToL2MessageSource = l1ToL2MessageSource, this.config = config, this.blobClient = blobClient, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set(), this.validatedBlockSlots = new Set();
|
|
62
|
+
// Create child logger with fisherman prefix if in fisherman mode
|
|
63
|
+
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
44
64
|
this.tracer = telemetry.getTracer('Validator');
|
|
45
65
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
46
|
-
this.validationService = new ValidationService(keyStore, log.createChild('validation-service'));
|
|
66
|
+
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
47
67
|
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
48
|
-
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
|
|
68
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
49
69
|
const myAddresses = this.getValidatorAddresses();
|
|
50
70
|
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
51
71
|
}
|
|
@@ -92,13 +112,23 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
92
112
|
this.log.error(`Error updating epoch committee`, err);
|
|
93
113
|
}
|
|
94
114
|
}
|
|
95
|
-
static new(config,
|
|
115
|
+
static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
|
|
96
116
|
const metrics = new ValidatorMetrics(telemetry);
|
|
97
117
|
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
98
118
|
txsPermitted: !config.disableTransactions
|
|
99
119
|
});
|
|
100
|
-
const blockProposalHandler = new BlockProposalHandler(
|
|
101
|
-
|
|
120
|
+
const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
|
|
121
|
+
let validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
122
|
+
if (config.haSigningEnabled) {
|
|
123
|
+
// If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
|
|
124
|
+
const haConfig = {
|
|
125
|
+
...config,
|
|
126
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
|
|
127
|
+
};
|
|
128
|
+
const { signer } = await createHASigner(haConfig);
|
|
129
|
+
validatorKeyStore = new HAKeyStore(validatorKeyStore, signer);
|
|
130
|
+
}
|
|
131
|
+
const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
|
|
102
132
|
return validator;
|
|
103
133
|
}
|
|
104
134
|
getValidatorAddresses() {
|
|
@@ -107,12 +137,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
107
137
|
getBlockProposalHandler() {
|
|
108
138
|
return this.blockProposalHandler;
|
|
109
139
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
113
|
-
}
|
|
114
|
-
signWithAddress(addr, msg) {
|
|
115
|
-
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
140
|
+
signWithAddress(addr, msg, context) {
|
|
141
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
116
142
|
}
|
|
117
143
|
getCoinbaseForAttestor(attestor) {
|
|
118
144
|
return this.keyStore.getCoinbaseAddress(attestor);
|
|
@@ -134,57 +160,72 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
134
160
|
this.log.warn(`Validator client already started`);
|
|
135
161
|
return;
|
|
136
162
|
}
|
|
163
|
+
await this.keyStore.start();
|
|
137
164
|
await this.registerHandlers();
|
|
138
165
|
const myAddresses = this.getValidatorAddresses();
|
|
139
166
|
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
167
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
140
168
|
if (inCommittee.length > 0) {
|
|
141
|
-
this.log.info(`
|
|
142
|
-
} else {
|
|
143
|
-
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
169
|
+
this.log.info(`Addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
|
|
144
170
|
}
|
|
145
171
|
this.epochCacheUpdateLoop.start();
|
|
146
172
|
return Promise.resolve();
|
|
147
173
|
}
|
|
148
174
|
async stop() {
|
|
149
175
|
await this.epochCacheUpdateLoop.stop();
|
|
176
|
+
await this.keyStore.stop();
|
|
150
177
|
}
|
|
151
178
|
/** Register handlers on the p2p client */ async registerHandlers() {
|
|
152
179
|
if (!this.hasRegisteredHandlers) {
|
|
153
180
|
this.hasRegisteredHandlers = true;
|
|
154
181
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
155
|
-
|
|
156
|
-
this.
|
|
182
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
183
|
+
const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
|
|
184
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
185
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
186
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
187
|
+
// and processed separately via the block handler above.
|
|
188
|
+
const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
189
|
+
this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
|
|
157
190
|
const myAddresses = this.getValidatorAddresses();
|
|
158
191
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
159
192
|
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
160
193
|
}
|
|
161
194
|
}
|
|
162
|
-
|
|
163
|
-
|
|
195
|
+
/**
|
|
196
|
+
* Validate a block proposal from a peer.
|
|
197
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
198
|
+
* @returns true if the proposal is valid, false otherwise
|
|
199
|
+
*/ async validateBlockProposal(proposal, proposalSender) {
|
|
200
|
+
const slotNumber = proposal.slotNumber;
|
|
201
|
+
// Note: During escape hatch, we still want to "validate" proposals for observability,
|
|
202
|
+
// but we intentionally reject them and disable slashing invalid block and attestation flow.
|
|
203
|
+
const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
|
|
164
204
|
const proposer = proposal.getSender();
|
|
165
205
|
// Reject proposals with invalid signatures
|
|
166
206
|
if (!proposer) {
|
|
167
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
168
|
-
return
|
|
207
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
208
|
+
return false;
|
|
169
209
|
}
|
|
170
|
-
// Check
|
|
210
|
+
// Check if we're in the committee (for metrics purposes)
|
|
171
211
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
172
212
|
const partOfCommittee = inCommittee.length > 0;
|
|
173
213
|
const proposalInfo = {
|
|
174
214
|
...proposal.toBlockInfo(),
|
|
175
215
|
proposer: proposer.toString()
|
|
176
216
|
};
|
|
177
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
217
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
178
218
|
...proposalInfo,
|
|
179
|
-
txHashes: proposal.txHashes.map((t)=>t.toString())
|
|
219
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
220
|
+
fishermanMode: this.config.fishermanMode || false
|
|
180
221
|
});
|
|
181
|
-
// Reexecute txs if we are part of the committee
|
|
182
|
-
//
|
|
183
|
-
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
|
|
184
|
-
const shouldReexecute = slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
|
|
185
|
-
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
|
|
222
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
223
|
+
// In fisherman mode, we always reexecute to validate proposals.
|
|
224
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
|
|
225
|
+
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
|
|
226
|
+
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
|
|
186
227
|
if (!validationResult.isValid) {
|
|
187
|
-
this.log.warn(`
|
|
228
|
+
this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
188
229
|
const reason = validationResult.reason || 'unknown';
|
|
189
230
|
// Classify failure reason: bad proposal vs node issue
|
|
190
231
|
const badProposalReasons = [
|
|
@@ -197,26 +238,283 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
197
238
|
if (badProposalReasons.includes(reason)) {
|
|
198
239
|
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
199
240
|
} else {
|
|
200
|
-
// Node issues so we can't
|
|
241
|
+
// Node issues so we can't validate
|
|
201
242
|
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
202
243
|
}
|
|
203
244
|
// Slash invalid block proposals (can happen even when not in committee)
|
|
204
|
-
if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
245
|
+
if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
205
246
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
206
247
|
this.slashInvalidBlock(proposal);
|
|
207
248
|
}
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
252
|
+
...proposalInfo,
|
|
253
|
+
inCommittee: partOfCommittee,
|
|
254
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
255
|
+
escapeHatchOpen
|
|
256
|
+
});
|
|
257
|
+
if (escapeHatchOpen) {
|
|
258
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable.
|
|
262
|
+
// Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
|
|
263
|
+
this.validatedBlockSlots.add(slotNumber);
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
268
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
269
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
270
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
271
|
+
*/ async attestToCheckpointProposal(proposal, _proposalSender) {
|
|
272
|
+
const slotNumber = proposal.slotNumber;
|
|
273
|
+
const proposer = proposal.getSender();
|
|
274
|
+
// If escape hatch is open for this slot's epoch, do not attest.
|
|
275
|
+
if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
|
|
276
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
// Reject proposals with invalid signatures
|
|
280
|
+
if (!proposer) {
|
|
281
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
|
|
208
282
|
return undefined;
|
|
209
283
|
}
|
|
210
284
|
// Check that I have any address in current committee before attesting
|
|
211
|
-
|
|
285
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
286
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
287
|
+
const proposalInfo = {
|
|
288
|
+
slotNumber,
|
|
289
|
+
archive: proposal.archive.toString(),
|
|
290
|
+
proposer: proposer.toString(),
|
|
291
|
+
txCount: proposal.txHashes.length
|
|
292
|
+
};
|
|
293
|
+
this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
|
|
294
|
+
...proposalInfo,
|
|
295
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
296
|
+
fishermanMode: this.config.fishermanMode || false
|
|
297
|
+
});
|
|
298
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable.
|
|
299
|
+
// Check that we have successfully validated a block for this slot before attesting to the checkpoint.
|
|
300
|
+
if (!this.validatedBlockSlots.has(slotNumber)) {
|
|
301
|
+
this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
|
|
305
|
+
// TODO(palla/mbps): Change default to false once checkpoint validation is stable.
|
|
306
|
+
if (this.config.skipCheckpointProposalValidation !== false) {
|
|
307
|
+
this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
|
|
308
|
+
} else {
|
|
309
|
+
const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
310
|
+
if (!validationResult.isValid) {
|
|
311
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
// Upload blobs to filestore if we can (fire and forget)
|
|
316
|
+
if (this.blobClient.canUpload()) {
|
|
317
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
318
|
+
}
|
|
319
|
+
// Check that I have any address in current committee before attesting
|
|
320
|
+
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
321
|
+
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
212
322
|
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
213
323
|
return undefined;
|
|
214
324
|
}
|
|
215
325
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
216
|
-
this.log.info(
|
|
326
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
|
|
327
|
+
...proposalInfo,
|
|
328
|
+
inCommittee: partOfCommittee,
|
|
329
|
+
fishermanMode: this.config.fishermanMode || false
|
|
330
|
+
});
|
|
217
331
|
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
218
|
-
//
|
|
219
|
-
|
|
332
|
+
// Determine which validators should attest
|
|
333
|
+
let attestors;
|
|
334
|
+
if (partOfCommittee) {
|
|
335
|
+
attestors = inCommittee;
|
|
336
|
+
} else if (this.config.fishermanMode) {
|
|
337
|
+
// In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
|
|
338
|
+
attestors = this.getValidatorAddresses();
|
|
339
|
+
} else {
|
|
340
|
+
attestors = [];
|
|
341
|
+
}
|
|
342
|
+
// Only create attestations if we have attestors
|
|
343
|
+
if (attestors.length === 0) {
|
|
344
|
+
return undefined;
|
|
345
|
+
}
|
|
346
|
+
if (this.config.fishermanMode) {
|
|
347
|
+
// bail out early and don't save attestations to the pool in fisherman mode
|
|
348
|
+
this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
|
|
349
|
+
...proposalInfo,
|
|
350
|
+
attestors: attestors.map((a)=>a.toString())
|
|
351
|
+
});
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
return this.createCheckpointAttestationsFromProposal(proposal, attestors);
|
|
355
|
+
}
|
|
356
|
+
async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
|
|
357
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
|
|
358
|
+
await this.p2pClient.addCheckpointAttestations(attestations);
|
|
359
|
+
return attestations;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
363
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
364
|
+
*/ async validateCheckpointProposal(proposal, proposalInfo) {
|
|
365
|
+
const slot = proposal.slotNumber;
|
|
366
|
+
const timeoutSeconds = 10;
|
|
367
|
+
// Wait for last block to sync by archive
|
|
368
|
+
let lastBlockHeader;
|
|
369
|
+
try {
|
|
370
|
+
lastBlockHeader = await retryUntil(async ()=>{
|
|
371
|
+
await this.blockSource.syncImmediate();
|
|
372
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
373
|
+
}, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
|
|
374
|
+
} catch (err) {
|
|
375
|
+
if (err instanceof TimeoutError) {
|
|
376
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
377
|
+
return {
|
|
378
|
+
isValid: false,
|
|
379
|
+
reason: 'last_block_not_found'
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
383
|
+
return {
|
|
384
|
+
isValid: false,
|
|
385
|
+
reason: 'block_fetch_error'
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
if (!lastBlockHeader) {
|
|
389
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
390
|
+
return {
|
|
391
|
+
isValid: false,
|
|
392
|
+
reason: 'last_block_not_found'
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
// Get all full blocks for the slot and checkpoint
|
|
396
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
397
|
+
if (blocks.length === 0) {
|
|
398
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
399
|
+
return {
|
|
400
|
+
isValid: false,
|
|
401
|
+
reason: 'no_blocks_for_slot'
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
405
|
+
...proposalInfo,
|
|
406
|
+
blockNumbers: blocks.map((b)=>b.number)
|
|
407
|
+
});
|
|
408
|
+
// Get checkpoint constants from first block
|
|
409
|
+
const firstBlock = blocks[0];
|
|
410
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
411
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
412
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
413
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
414
|
+
// Compute the previous checkpoint out hashes for the epoch.
|
|
415
|
+
// TODO: There can be a more efficient way to get the previous checkpoint out hashes without having to fetch the
|
|
416
|
+
// actual checkpoints and the blocks/txs in them.
|
|
417
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
418
|
+
const previousCheckpoints = (await this.blockSource.getCheckpointsForEpoch(epoch)).filter((b)=>b.number < checkpointNumber).sort((a, b)=>a.number - b.number);
|
|
419
|
+
const previousCheckpointOutHashes = previousCheckpoints.map((c)=>c.getCheckpointOutHash());
|
|
420
|
+
// Fork world state at the block before the first block
|
|
421
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
422
|
+
const fork = await this.worldState.fork(parentBlockNumber);
|
|
423
|
+
try {
|
|
424
|
+
// Create checkpoint builder with all existing blocks
|
|
425
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks);
|
|
426
|
+
// Complete the checkpoint to get computed values
|
|
427
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
428
|
+
// Compare checkpoint header with proposal
|
|
429
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
430
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
431
|
+
...proposalInfo,
|
|
432
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
433
|
+
proposal: proposal.checkpointHeader.toInspect()
|
|
434
|
+
});
|
|
435
|
+
return {
|
|
436
|
+
isValid: false,
|
|
437
|
+
reason: 'checkpoint_header_mismatch'
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
// Compare archive root with proposal
|
|
441
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
442
|
+
this.log.warn(`Archive root mismatch`, {
|
|
443
|
+
...proposalInfo,
|
|
444
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
445
|
+
proposal: proposal.archive.toString()
|
|
446
|
+
});
|
|
447
|
+
return {
|
|
448
|
+
isValid: false,
|
|
449
|
+
reason: 'archive_mismatch'
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
453
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
454
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
455
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([
|
|
456
|
+
...previousCheckpointOutHashes,
|
|
457
|
+
checkpointOutHash
|
|
458
|
+
]);
|
|
459
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
460
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
461
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
462
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
463
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
464
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
465
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map((h)=>h.toString()),
|
|
466
|
+
...proposalInfo
|
|
467
|
+
});
|
|
468
|
+
return {
|
|
469
|
+
isValid: false,
|
|
470
|
+
reason: 'out_hash_mismatch'
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
474
|
+
return {
|
|
475
|
+
isValid: true
|
|
476
|
+
};
|
|
477
|
+
} finally{
|
|
478
|
+
await fork.close();
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Extract checkpoint global variables from a block.
|
|
483
|
+
*/ extractCheckpointConstants(block) {
|
|
484
|
+
const gv = block.header.globalVariables;
|
|
485
|
+
return {
|
|
486
|
+
chainId: gv.chainId,
|
|
487
|
+
version: gv.version,
|
|
488
|
+
slotNumber: gv.slotNumber,
|
|
489
|
+
coinbase: gv.coinbase,
|
|
490
|
+
feeRecipient: gv.feeRecipient,
|
|
491
|
+
gasFees: gv.gasFees
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
496
|
+
*/ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
|
|
497
|
+
try {
|
|
498
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
499
|
+
if (!lastBlockHeader) {
|
|
500
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
504
|
+
if (blocks.length === 0) {
|
|
505
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const blobFields = blocks.flatMap((b)=>b.toBlobFields());
|
|
509
|
+
const blobs = getBlobsPerL1Block(blobFields);
|
|
510
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
511
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
512
|
+
...proposalInfo,
|
|
513
|
+
numBlobs: blobs.length
|
|
514
|
+
});
|
|
515
|
+
} catch (err) {
|
|
516
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
517
|
+
}
|
|
220
518
|
}
|
|
221
519
|
slashInvalidBlock(proposal) {
|
|
222
520
|
const proposer = proposal.getSender();
|
|
@@ -236,39 +534,52 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
236
534
|
validator: proposer,
|
|
237
535
|
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
238
536
|
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
239
|
-
epochOrSlot: proposal.slotNumber
|
|
537
|
+
epochOrSlot: BigInt(proposal.slotNumber)
|
|
240
538
|
}
|
|
241
539
|
]);
|
|
242
540
|
}
|
|
243
|
-
async createBlockProposal(
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
541
|
+
async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
|
|
542
|
+
// TODO(palla/mbps): Prevent double proposals properly
|
|
543
|
+
// if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
|
|
544
|
+
// this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
|
|
545
|
+
// return Promise.resolve(undefined);
|
|
546
|
+
// }
|
|
547
|
+
this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
|
|
548
|
+
const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
|
|
249
549
|
...options,
|
|
250
550
|
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
251
551
|
});
|
|
252
552
|
this.previousProposal = newProposal;
|
|
253
553
|
return newProposal;
|
|
254
554
|
}
|
|
555
|
+
async createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options = {}) {
|
|
556
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
557
|
+
return await this.validationService.createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options);
|
|
558
|
+
}
|
|
255
559
|
async broadcastBlockProposal(proposal) {
|
|
256
560
|
await this.p2pClient.broadcastProposal(proposal);
|
|
257
561
|
}
|
|
258
|
-
async signAttestationsAndSigners(attestationsAndSigners, proposer) {
|
|
259
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
562
|
+
async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber) {
|
|
563
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
|
|
260
564
|
}
|
|
261
565
|
async collectOwnAttestations(proposal) {
|
|
262
|
-
const slot = proposal.
|
|
566
|
+
const slot = proposal.slotNumber;
|
|
263
567
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
264
568
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
265
569
|
inCommittee
|
|
266
570
|
});
|
|
267
|
-
|
|
571
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
572
|
+
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
573
|
+
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
574
|
+
// due to inactivity for missed attestations.
|
|
575
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
|
|
576
|
+
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
577
|
+
});
|
|
578
|
+
return attestations;
|
|
268
579
|
}
|
|
269
580
|
async collectAttestations(proposal, required, deadline) {
|
|
270
|
-
// Wait and poll the p2pClient's attestation pool for this
|
|
271
|
-
const slot = proposal.
|
|
581
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
582
|
+
const slot = proposal.slotNumber;
|
|
272
583
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
273
584
|
if (+deadline < this.dateProvider.now()) {
|
|
274
585
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
@@ -279,13 +590,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
279
590
|
const myAddresses = this.getValidatorAddresses();
|
|
280
591
|
let attestations = [];
|
|
281
592
|
while(true){
|
|
282
|
-
// Filter out attestations with a mismatching
|
|
593
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
283
594
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
284
|
-
const collectedAttestations = (await this.p2pClient.
|
|
285
|
-
if (!attestation.
|
|
286
|
-
this.log.warn(`Received attestation for slot ${slot} with mismatched
|
|
287
|
-
|
|
288
|
-
|
|
595
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
596
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
597
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
|
|
598
|
+
attestationArchive: attestation.archive.toString(),
|
|
599
|
+
proposalArchive: proposal.archive.toString()
|
|
289
600
|
});
|
|
290
601
|
return false;
|
|
291
602
|
}
|
|
@@ -317,11 +628,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
317
628
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
318
629
|
}
|
|
319
630
|
}
|
|
320
|
-
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
321
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
322
|
-
await this.p2pClient.addAttestations(attestations);
|
|
323
|
-
return attestations;
|
|
324
|
-
}
|
|
325
631
|
async handleAuthRequest(peer, msg) {
|
|
326
632
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
327
633
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
@@ -336,7 +642,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
336
642
|
return Buffer.alloc(0);
|
|
337
643
|
}
|
|
338
644
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
339
|
-
|
|
645
|
+
// AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
|
|
646
|
+
const context = {
|
|
647
|
+
dutyType: DutyType.AUTH_REQUEST
|
|
648
|
+
};
|
|
649
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
|
|
340
650
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
341
651
|
return authResponse.toBuffer();
|
|
342
652
|
}
|