@aztec/validator-client 0.0.1-commit.6c91f13 → 0.0.1-commit.96bb3f7
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 +256 -0
- package/dest/block_proposal_handler.d.ts +20 -10
- package/dest/block_proposal_handler.d.ts.map +1 -1
- package/dest/block_proposal_handler.js +333 -72
- package/dest/checkpoint_builder.d.ts +70 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +155 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +10 -0
- package/dest/duties/validation_service.d.ts +26 -10
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +51 -21
- package/dest/factory.d.ts +12 -9
- 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/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 +53 -0
- package/dest/validator.d.ts +41 -17
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +299 -77
- package/package.json +16 -12
- package/src/block_proposal_handler.ts +249 -39
- package/src/checkpoint_builder.ts +267 -0
- package/src/config.ts +10 -0
- package/src/duties/validation_service.ts +79 -25
- package/src/factory.ts +16 -11
- package/src/index.ts +2 -0
- 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 +133 -0
- package/src/validator.ts +405 -99
package/dest/validator.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
}
|
|
7
1
|
import { getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
2
|
+
import { BlockNumber } from '@aztec/foundation/branded-types';
|
|
3
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
8
4
|
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
9
6
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
10
7
|
import { sleep } from '@aztec/foundation/sleep';
|
|
11
8
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
12
9
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
13
10
|
import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
|
|
14
11
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
15
|
-
import {
|
|
12
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
16
13
|
import { EventEmitter } from 'events';
|
|
17
14
|
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
18
15
|
import { ValidationService } from './duties/validation_service.js';
|
|
@@ -33,8 +30,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
33
30
|
epochCache;
|
|
34
31
|
p2pClient;
|
|
35
32
|
blockProposalHandler;
|
|
33
|
+
blockSource;
|
|
34
|
+
checkpointsBuilder;
|
|
35
|
+
worldState;
|
|
36
|
+
l1ToL2MessageSource;
|
|
36
37
|
config;
|
|
37
|
-
|
|
38
|
+
blobClient;
|
|
38
39
|
dateProvider;
|
|
39
40
|
tracer;
|
|
40
41
|
validationService;
|
|
@@ -47,8 +48,12 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
47
48
|
lastEpochForCommitteeUpdateLoop;
|
|
48
49
|
epochCacheUpdateLoop;
|
|
49
50
|
proposersOfInvalidBlocks;
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable and we can validate all blocks properly.
|
|
52
|
+
// Tracks slots for which we have successfully validated a block proposal, so we can attest to checkpoint proposals for those slots.
|
|
53
|
+
// eslint-disable-next-line aztec-custom/no-non-primitive-in-collections
|
|
54
|
+
validatedBlockSlots;
|
|
55
|
+
constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
56
|
+
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();
|
|
52
57
|
// Create child logger with fisherman prefix if in fisherman mode
|
|
53
58
|
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
54
59
|
this.tracer = telemetry.getTracer('Validator');
|
|
@@ -102,13 +107,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
102
107
|
this.log.error(`Error updating epoch committee`, err);
|
|
103
108
|
}
|
|
104
109
|
}
|
|
105
|
-
static new(config,
|
|
110
|
+
static new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
|
|
106
111
|
const metrics = new ValidatorMetrics(telemetry);
|
|
107
112
|
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
108
113
|
txsPermitted: !config.disableTransactions
|
|
109
114
|
});
|
|
110
|
-
const blockProposalHandler = new BlockProposalHandler(
|
|
111
|
-
const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config,
|
|
115
|
+
const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
|
|
116
|
+
const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, dateProvider, telemetry);
|
|
112
117
|
return validator;
|
|
113
118
|
}
|
|
114
119
|
getValidatorAddresses() {
|
|
@@ -117,10 +122,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
117
122
|
getBlockProposalHandler() {
|
|
118
123
|
return this.blockProposalHandler;
|
|
119
124
|
}
|
|
120
|
-
// Proxy method for backwards compatibility with tests
|
|
121
|
-
reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
|
|
122
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
123
|
-
}
|
|
124
125
|
signWithAddress(addr, msg) {
|
|
125
126
|
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
126
127
|
}
|
|
@@ -161,41 +162,50 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
161
162
|
if (!this.hasRegisteredHandlers) {
|
|
162
163
|
this.hasRegisteredHandlers = true;
|
|
163
164
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
164
|
-
|
|
165
|
-
this.
|
|
165
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
166
|
+
const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
|
|
167
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
168
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
169
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
170
|
+
// and processed separately via the block handler above.
|
|
171
|
+
const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
172
|
+
this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
|
|
166
173
|
const myAddresses = this.getValidatorAddresses();
|
|
167
174
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
168
175
|
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
169
176
|
}
|
|
170
177
|
}
|
|
171
|
-
|
|
178
|
+
/**
|
|
179
|
+
* Validate a block proposal from a peer.
|
|
180
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
181
|
+
* @returns true if the proposal is valid, false otherwise
|
|
182
|
+
*/ async validateBlockProposal(proposal, proposalSender) {
|
|
172
183
|
const slotNumber = proposal.slotNumber;
|
|
173
184
|
const proposer = proposal.getSender();
|
|
174
185
|
// Reject proposals with invalid signatures
|
|
175
186
|
if (!proposer) {
|
|
176
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
177
|
-
return
|
|
187
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
188
|
+
return false;
|
|
178
189
|
}
|
|
179
|
-
// Check
|
|
190
|
+
// Check if we're in the committee (for metrics purposes)
|
|
180
191
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
181
192
|
const partOfCommittee = inCommittee.length > 0;
|
|
182
193
|
const proposalInfo = {
|
|
183
194
|
...proposal.toBlockInfo(),
|
|
184
195
|
proposer: proposer.toString()
|
|
185
196
|
};
|
|
186
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
197
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
187
198
|
...proposalInfo,
|
|
188
199
|
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
189
200
|
fishermanMode: this.config.fishermanMode || false
|
|
190
201
|
});
|
|
191
|
-
// Reexecute txs if we are part of the committee
|
|
192
|
-
// invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
|
|
202
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
193
203
|
// In fisherman mode, we always reexecute to validate proposals.
|
|
194
204
|
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
|
|
195
|
-
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.
|
|
205
|
+
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
|
|
196
206
|
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
|
|
197
207
|
if (!validationResult.isValid) {
|
|
198
|
-
this.log.warn(`
|
|
208
|
+
this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
199
209
|
const reason = validationResult.reason || 'unknown';
|
|
200
210
|
// Classify failure reason: bad proposal vs node issue
|
|
201
211
|
const badProposalReasons = [
|
|
@@ -208,7 +218,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
208
218
|
if (badProposalReasons.includes(reason)) {
|
|
209
219
|
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
210
220
|
} else {
|
|
211
|
-
// Node issues so we can't
|
|
221
|
+
// Node issues so we can't validate
|
|
212
222
|
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
213
223
|
}
|
|
214
224
|
// Slash invalid block proposals (can happen even when not in committee)
|
|
@@ -216,35 +226,79 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
216
226
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
217
227
|
this.slashInvalidBlock(proposal);
|
|
218
228
|
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
232
|
+
...proposalInfo,
|
|
233
|
+
inCommittee: partOfCommittee,
|
|
234
|
+
fishermanMode: this.config.fishermanMode || false
|
|
235
|
+
});
|
|
236
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable.
|
|
237
|
+
// Track that we successfully validated a block for this slot, so we can attest to checkpoint proposals for it.
|
|
238
|
+
this.validatedBlockSlots.add(slotNumber);
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
243
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
244
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
245
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
246
|
+
*/ async attestToCheckpointProposal(proposal, _proposalSender) {
|
|
247
|
+
const slotNumber = proposal.slotNumber;
|
|
248
|
+
const proposer = proposal.getSender();
|
|
249
|
+
// Reject proposals with invalid signatures
|
|
250
|
+
if (!proposer) {
|
|
251
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
|
|
219
252
|
return undefined;
|
|
220
253
|
}
|
|
221
254
|
// Check that I have any address in current committee before attesting
|
|
255
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
256
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
257
|
+
const proposalInfo = {
|
|
258
|
+
slotNumber,
|
|
259
|
+
archive: proposal.archive.toString(),
|
|
260
|
+
proposer: proposer.toString(),
|
|
261
|
+
txCount: proposal.txHashes.length
|
|
262
|
+
};
|
|
263
|
+
this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
|
|
264
|
+
...proposalInfo,
|
|
265
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
266
|
+
fishermanMode: this.config.fishermanMode || false
|
|
267
|
+
});
|
|
268
|
+
// TODO(palla/mbps): Remove this once checkpoint validation is stable.
|
|
269
|
+
// Check that we have successfully validated a block for this slot before attesting to the checkpoint.
|
|
270
|
+
if (!this.validatedBlockSlots.has(slotNumber)) {
|
|
271
|
+
this.log.warn(`No validated block found for slot ${slotNumber}, refusing to attest to checkpoint`, proposalInfo);
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
|
|
275
|
+
// TODO(palla/mbps): Change default to false once checkpoint validation is stable.
|
|
276
|
+
if (this.config.skipCheckpointProposalValidation !== false) {
|
|
277
|
+
this.log.verbose(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
|
|
278
|
+
} else {
|
|
279
|
+
const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
280
|
+
if (!validationResult.isValid) {
|
|
281
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// Upload blobs to filestore if we can (fire and forget)
|
|
286
|
+
if (this.blobClient.canUpload()) {
|
|
287
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
288
|
+
}
|
|
289
|
+
// Check that I have any address in current committee before attesting
|
|
222
290
|
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
223
291
|
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
224
292
|
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
225
293
|
return undefined;
|
|
226
294
|
}
|
|
227
295
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
228
|
-
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
|
|
296
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
|
|
229
297
|
...proposalInfo,
|
|
230
298
|
inCommittee: partOfCommittee,
|
|
231
299
|
fishermanMode: this.config.fishermanMode || false
|
|
232
300
|
});
|
|
233
301
|
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
234
|
-
// Upload blobs to filestore after successful re-execution (fire-and-forget)
|
|
235
|
-
if (validationResult.reexecutionResult?.block && this.fileStoreBlobUploadClient) {
|
|
236
|
-
void Promise.resolve().then(async ()=>{
|
|
237
|
-
try {
|
|
238
|
-
const blobFields = validationResult.reexecutionResult.block.getCheckpointBlobFields();
|
|
239
|
-
const blobs = getBlobsPerL1Block(blobFields);
|
|
240
|
-
await this.fileStoreBlobUploadClient.saveBlobs(blobs, true);
|
|
241
|
-
this.log.debug(`Uploaded ${blobs.length} blobs to filestore from re-execution`, proposalInfo);
|
|
242
|
-
} catch (err) {
|
|
243
|
-
this.log.warn(`Failed to upload blobs from re-execution`, err);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
// If the above function does not throw an error, then we can attest to the proposal
|
|
248
302
|
// Determine which validators should attest
|
|
249
303
|
let attestors;
|
|
250
304
|
if (partOfCommittee) {
|
|
@@ -261,13 +315,194 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
261
315
|
}
|
|
262
316
|
if (this.config.fishermanMode) {
|
|
263
317
|
// bail out early and don't save attestations to the pool in fisherman mode
|
|
264
|
-
this.log.info(`Creating attestations for
|
|
318
|
+
this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
|
|
265
319
|
...proposalInfo,
|
|
266
320
|
attestors: attestors.map((a)=>a.toString())
|
|
267
321
|
});
|
|
268
322
|
return undefined;
|
|
269
323
|
}
|
|
270
|
-
return this.
|
|
324
|
+
return this.createCheckpointAttestationsFromProposal(proposal, attestors);
|
|
325
|
+
}
|
|
326
|
+
async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
|
|
327
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
|
|
328
|
+
await this.p2pClient.addCheckpointAttestations(attestations);
|
|
329
|
+
return attestations;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
333
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
334
|
+
*/ async validateCheckpointProposal(proposal, proposalInfo) {
|
|
335
|
+
const slot = proposal.slotNumber;
|
|
336
|
+
const timeoutSeconds = 10;
|
|
337
|
+
// Wait for last block to sync by archive
|
|
338
|
+
let lastBlockHeader;
|
|
339
|
+
try {
|
|
340
|
+
lastBlockHeader = await retryUntil(async ()=>{
|
|
341
|
+
await this.blockSource.syncImmediate();
|
|
342
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
343
|
+
}, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
|
|
344
|
+
} catch (err) {
|
|
345
|
+
if (err instanceof TimeoutError) {
|
|
346
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
347
|
+
return {
|
|
348
|
+
isValid: false,
|
|
349
|
+
reason: 'last_block_not_found'
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
353
|
+
return {
|
|
354
|
+
isValid: false,
|
|
355
|
+
reason: 'block_fetch_error'
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
if (!lastBlockHeader) {
|
|
359
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
360
|
+
return {
|
|
361
|
+
isValid: false,
|
|
362
|
+
reason: 'last_block_not_found'
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
// Get the last full block to determine checkpoint number
|
|
366
|
+
const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
|
|
367
|
+
if (!lastBlock) {
|
|
368
|
+
this.log.warn(`Last block ${lastBlockHeader.getBlockNumber()} not found`, proposalInfo);
|
|
369
|
+
return {
|
|
370
|
+
isValid: false,
|
|
371
|
+
reason: 'last_block_not_found'
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
const checkpointNumber = lastBlock.checkpointNumber;
|
|
375
|
+
// Get all full blocks for the slot and checkpoint
|
|
376
|
+
const blocks = await this.getBlocksForSlot(slot, lastBlockHeader, checkpointNumber);
|
|
377
|
+
if (blocks.length === 0) {
|
|
378
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
379
|
+
return {
|
|
380
|
+
isValid: false,
|
|
381
|
+
reason: 'no_blocks_for_slot'
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
385
|
+
...proposalInfo,
|
|
386
|
+
blockNumbers: blocks.map((b)=>b.number)
|
|
387
|
+
});
|
|
388
|
+
// Get checkpoint constants from first block
|
|
389
|
+
const firstBlock = blocks[0];
|
|
390
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
391
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
392
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
393
|
+
// Fork world state at the block before the first block
|
|
394
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
395
|
+
const fork = await this.worldState.fork(parentBlockNumber);
|
|
396
|
+
try {
|
|
397
|
+
// Create checkpoint builder with all existing blocks
|
|
398
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, l1ToL2Messages, fork, blocks);
|
|
399
|
+
// Complete the checkpoint to get computed values
|
|
400
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
401
|
+
// Compare checkpoint header with proposal
|
|
402
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
403
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
404
|
+
...proposalInfo,
|
|
405
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
406
|
+
proposal: proposal.checkpointHeader.toInspect()
|
|
407
|
+
});
|
|
408
|
+
return {
|
|
409
|
+
isValid: false,
|
|
410
|
+
reason: 'checkpoint_header_mismatch'
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
// Compare archive root with proposal
|
|
414
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
415
|
+
this.log.warn(`Archive root mismatch`, {
|
|
416
|
+
...proposalInfo,
|
|
417
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
418
|
+
proposal: proposal.archive.toString()
|
|
419
|
+
});
|
|
420
|
+
return {
|
|
421
|
+
isValid: false,
|
|
422
|
+
reason: 'archive_mismatch'
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
426
|
+
return {
|
|
427
|
+
isValid: true
|
|
428
|
+
};
|
|
429
|
+
} finally{
|
|
430
|
+
await fork.close();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Get all full blocks for a given slot and checkpoint by walking backwards from the last block.
|
|
435
|
+
* Returns blocks in ascending order (earliest to latest).
|
|
436
|
+
* TODO(palla/mbps): Add getL2BlocksForSlot() to L2BlockSource interface for efficiency.
|
|
437
|
+
*/ async getBlocksForSlot(slot, lastBlockHeader, checkpointNumber) {
|
|
438
|
+
const blocks = [];
|
|
439
|
+
let currentHeader = lastBlockHeader;
|
|
440
|
+
const { genesisArchiveRoot } = await this.blockSource.getGenesisValues();
|
|
441
|
+
while(currentHeader.getSlot() === slot){
|
|
442
|
+
const block = await this.blockSource.getL2BlockNew(currentHeader.getBlockNumber());
|
|
443
|
+
if (!block) {
|
|
444
|
+
this.log.warn(`Block ${currentHeader.getBlockNumber()} not found while getting blocks for slot ${slot}`);
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
if (block.checkpointNumber !== checkpointNumber) {
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
blocks.unshift(block);
|
|
451
|
+
const prevArchive = currentHeader.lastArchive.root;
|
|
452
|
+
if (prevArchive.equals(genesisArchiveRoot)) {
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
const prevHeader = await this.blockSource.getBlockHeaderByArchive(prevArchive);
|
|
456
|
+
if (!prevHeader || prevHeader.getSlot() !== slot) {
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
currentHeader = prevHeader;
|
|
460
|
+
}
|
|
461
|
+
return blocks;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Extract checkpoint global variables from a block.
|
|
465
|
+
*/ extractCheckpointConstants(block) {
|
|
466
|
+
const gv = block.header.globalVariables;
|
|
467
|
+
return {
|
|
468
|
+
chainId: gv.chainId,
|
|
469
|
+
version: gv.version,
|
|
470
|
+
slotNumber: gv.slotNumber,
|
|
471
|
+
coinbase: gv.coinbase,
|
|
472
|
+
feeRecipient: gv.feeRecipient,
|
|
473
|
+
gasFees: gv.gasFees
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
478
|
+
*/ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
|
|
479
|
+
try {
|
|
480
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
481
|
+
if (!lastBlockHeader) {
|
|
482
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
// Get the last full block to determine checkpoint number
|
|
486
|
+
const lastBlock = await this.blockSource.getL2BlockNew(lastBlockHeader.getBlockNumber());
|
|
487
|
+
if (!lastBlock) {
|
|
488
|
+
this.log.warn(`Failed to get last block for blob upload`, proposalInfo);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const blocks = await this.getBlocksForSlot(proposal.slotNumber, lastBlockHeader, lastBlock.checkpointNumber);
|
|
492
|
+
if (blocks.length === 0) {
|
|
493
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const blobFields = blocks.flatMap((b)=>b.toBlobFields());
|
|
497
|
+
const blobs = getBlobsPerL1Block(blobFields);
|
|
498
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
499
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
500
|
+
...proposalInfo,
|
|
501
|
+
numBlobs: blobs.length
|
|
502
|
+
});
|
|
503
|
+
} catch (err) {
|
|
504
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
505
|
+
}
|
|
271
506
|
}
|
|
272
507
|
slashInvalidBlock(proposal) {
|
|
273
508
|
const proposer = proposal.getSender();
|
|
@@ -291,25 +526,23 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
291
526
|
}
|
|
292
527
|
]);
|
|
293
528
|
}
|
|
294
|
-
|
|
295
|
-
async createBlockProposal(blockNumber, header, archive, txs, proposerAddress, options) {
|
|
529
|
+
async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options) {
|
|
296
530
|
// TODO(palla/mbps): Prevent double proposals properly
|
|
297
|
-
// if (this.previousProposal?.slotNumber ===
|
|
531
|
+
// if (this.previousProposal?.slotNumber === blockHeader.globalVariables.slotNumber) {
|
|
298
532
|
// this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
|
|
299
533
|
// return Promise.resolve(undefined);
|
|
300
534
|
// }
|
|
301
|
-
this.log.info(`Assembling block proposal for block ${blockNumber} slot ${
|
|
302
|
-
const newProposal = await this.validationService.createBlockProposal(
|
|
535
|
+
this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
|
|
536
|
+
const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
|
|
303
537
|
...options,
|
|
304
538
|
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
305
539
|
});
|
|
306
540
|
this.previousProposal = newProposal;
|
|
307
541
|
return newProposal;
|
|
308
542
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
this.
|
|
312
|
-
return this.createBlockProposal(0, header, archive, txs, proposerAddress, options);
|
|
543
|
+
async createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options) {
|
|
544
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
545
|
+
return await this.validationService.createCheckpointProposal(checkpointHeader, archive, lastBlockInfo, proposerAddress, options);
|
|
313
546
|
}
|
|
314
547
|
async broadcastBlockProposal(proposal) {
|
|
315
548
|
await this.p2pClient.broadcastProposal(proposal);
|
|
@@ -318,23 +551,23 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
318
551
|
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
319
552
|
}
|
|
320
553
|
async collectOwnAttestations(proposal) {
|
|
321
|
-
const slot = proposal.
|
|
554
|
+
const slot = proposal.slotNumber;
|
|
322
555
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
323
556
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
324
557
|
inCommittee
|
|
325
558
|
});
|
|
326
|
-
const attestations = await this.
|
|
559
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
327
560
|
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
328
561
|
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
329
562
|
// due to inactivity for missed attestations.
|
|
330
|
-
void this.p2pClient.
|
|
563
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
|
|
331
564
|
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
332
565
|
});
|
|
333
566
|
return attestations;
|
|
334
567
|
}
|
|
335
568
|
async collectAttestations(proposal, required, deadline) {
|
|
336
|
-
// Wait and poll the p2pClient's attestation pool for this
|
|
337
|
-
const slot = proposal.
|
|
569
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
570
|
+
const slot = proposal.slotNumber;
|
|
338
571
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
339
572
|
if (+deadline < this.dateProvider.now()) {
|
|
340
573
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
@@ -345,13 +578,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
345
578
|
const myAddresses = this.getValidatorAddresses();
|
|
346
579
|
let attestations = [];
|
|
347
580
|
while(true){
|
|
348
|
-
// Filter out attestations with a mismatching
|
|
581
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
349
582
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
350
|
-
const collectedAttestations = (await this.p2pClient.
|
|
351
|
-
if (!attestation.
|
|
352
|
-
this.log.warn(`Received attestation for slot ${slot} with mismatched
|
|
353
|
-
|
|
354
|
-
|
|
583
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
584
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
585
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
|
|
586
|
+
attestationArchive: attestation.archive.toString(),
|
|
587
|
+
proposalArchive: proposal.archive.toString()
|
|
355
588
|
});
|
|
356
589
|
return false;
|
|
357
590
|
}
|
|
@@ -383,11 +616,6 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
383
616
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
384
617
|
}
|
|
385
618
|
}
|
|
386
|
-
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
387
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
388
|
-
await this.p2pClient.addAttestations(attestations);
|
|
389
|
-
return attestations;
|
|
390
|
-
}
|
|
391
619
|
async handleAuthRequest(peer, msg) {
|
|
392
620
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
393
621
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
@@ -407,9 +635,3 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
407
635
|
return authResponse.toBuffer();
|
|
408
636
|
}
|
|
409
637
|
}
|
|
410
|
-
_ts_decorate([
|
|
411
|
-
trackSpan('validator.attestToProposal', (proposal, proposalSender)=>({
|
|
412
|
-
[Attributes.BLOCK_HASH]: proposal.payload.header.hash.toString(),
|
|
413
|
-
[Attributes.PEER_ID]: proposalSender.toString()
|
|
414
|
-
}))
|
|
415
|
-
], ValidatorClient.prototype, "attestToProposal", null);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-client",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.96bb3f7",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -64,17 +64,21 @@
|
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@aztec/blob-client": "0.0.1-commit.
|
|
68
|
-
"@aztec/blob-lib": "0.0.1-commit.
|
|
69
|
-
"@aztec/constants": "0.0.1-commit.
|
|
70
|
-
"@aztec/epoch-cache": "0.0.1-commit.
|
|
71
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
72
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
73
|
-
"@aztec/node-keystore": "0.0.1-commit.
|
|
74
|
-
"@aztec/
|
|
75
|
-
"@aztec/
|
|
76
|
-
"@aztec/
|
|
77
|
-
"@aztec/
|
|
67
|
+
"@aztec/blob-client": "0.0.1-commit.96bb3f7",
|
|
68
|
+
"@aztec/blob-lib": "0.0.1-commit.96bb3f7",
|
|
69
|
+
"@aztec/constants": "0.0.1-commit.96bb3f7",
|
|
70
|
+
"@aztec/epoch-cache": "0.0.1-commit.96bb3f7",
|
|
71
|
+
"@aztec/ethereum": "0.0.1-commit.96bb3f7",
|
|
72
|
+
"@aztec/foundation": "0.0.1-commit.96bb3f7",
|
|
73
|
+
"@aztec/node-keystore": "0.0.1-commit.96bb3f7",
|
|
74
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.96bb3f7",
|
|
75
|
+
"@aztec/p2p": "0.0.1-commit.96bb3f7",
|
|
76
|
+
"@aztec/protocol-contracts": "0.0.1-commit.96bb3f7",
|
|
77
|
+
"@aztec/prover-client": "0.0.1-commit.96bb3f7",
|
|
78
|
+
"@aztec/simulator": "0.0.1-commit.96bb3f7",
|
|
79
|
+
"@aztec/slasher": "0.0.1-commit.96bb3f7",
|
|
80
|
+
"@aztec/stdlib": "0.0.1-commit.96bb3f7",
|
|
81
|
+
"@aztec/telemetry-client": "0.0.1-commit.96bb3f7",
|
|
78
82
|
"koa": "^2.16.1",
|
|
79
83
|
"koa-router": "^13.1.1",
|
|
80
84
|
"tslib": "^2.4.0",
|