@aztec/validator-client 0.0.1-fake-ceab37513c → 0.0.2-commit.217f559981
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 +285 -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 +339 -93
- package/dest/checkpoint_builder.d.ts +66 -0
- package/dest/checkpoint_builder.d.ts.map +1 -0
- package/dest/checkpoint_builder.js +175 -0
- package/dest/config.d.ts +1 -1
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +20 -7
- package/dest/duties/validation_service.d.ts +47 -15
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +119 -30
- 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 +13 -10
- package/dest/metrics.d.ts +8 -5
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +48 -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 +19 -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 +77 -22
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +524 -72
- package/package.json +24 -14
- package/src/block_proposal_handler.ts +265 -73
- package/src/checkpoint_builder.ts +321 -0
- package/src/config.ts +20 -6
- package/src/duties/validation_service.ts +173 -37
- 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 +22 -12
- package/src/metrics.ts +67 -36
- 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 +154 -0
- package/src/validator.ts +697 -104
package/dest/validator.js
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
|
+
import { getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
2
|
+
import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
|
|
3
|
+
import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
|
|
4
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
1
5
|
import { createLogger } from '@aztec/foundation/log';
|
|
6
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
2
7
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
3
8
|
import { sleep } from '@aztec/foundation/sleep';
|
|
4
9
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
5
10
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
6
11
|
import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
|
|
12
|
+
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
13
|
+
import { accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
|
|
7
14
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
8
15
|
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
16
|
+
import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
17
|
+
import { DutyType } from '@aztec/validator-ha-signer/types';
|
|
9
18
|
import { EventEmitter } from 'events';
|
|
10
19
|
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
11
20
|
import { ValidationService } from './duties/validation_service.js';
|
|
21
|
+
import { HAKeyStore } from './key_store/ha_key_store.js';
|
|
12
22
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
13
23
|
import { ValidatorMetrics } from './metrics.js';
|
|
14
24
|
// We maintain a set of proposers who have proposed invalid blocks.
|
|
@@ -26,38 +36,49 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
26
36
|
epochCache;
|
|
27
37
|
p2pClient;
|
|
28
38
|
blockProposalHandler;
|
|
39
|
+
blockSource;
|
|
40
|
+
checkpointsBuilder;
|
|
41
|
+
worldState;
|
|
42
|
+
l1ToL2MessageSource;
|
|
29
43
|
config;
|
|
44
|
+
blobClient;
|
|
45
|
+
haSigner;
|
|
30
46
|
dateProvider;
|
|
31
|
-
log;
|
|
32
47
|
tracer;
|
|
33
48
|
validationService;
|
|
34
49
|
metrics;
|
|
50
|
+
log;
|
|
35
51
|
// Whether it has already registered handlers on the p2p client
|
|
36
52
|
hasRegisteredHandlers;
|
|
37
|
-
|
|
38
|
-
|
|
53
|
+
/** Tracks the last block proposal we created, to detect duplicate proposal attempts. */ lastProposedBlock;
|
|
54
|
+
/** Tracks the last checkpoint proposal we created. */ lastProposedCheckpoint;
|
|
39
55
|
lastEpochForCommitteeUpdateLoop;
|
|
40
56
|
epochCacheUpdateLoop;
|
|
41
57
|
proposersOfInvalidBlocks;
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
/** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */ lastAttestedProposal;
|
|
59
|
+
constructor(keyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
60
|
+
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.haSigner = haSigner, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
|
|
61
|
+
// Create child logger with fisherman prefix if in fisherman mode
|
|
62
|
+
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
44
63
|
this.tracer = telemetry.getTracer('Validator');
|
|
45
64
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
46
|
-
this.validationService = new ValidationService(keyStore);
|
|
65
|
+
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
47
66
|
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
48
|
-
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
|
|
67
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
49
68
|
const myAddresses = this.getValidatorAddresses();
|
|
50
69
|
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
51
70
|
}
|
|
52
|
-
static validateKeyStoreConfiguration(keyStoreManager) {
|
|
71
|
+
static validateKeyStoreConfiguration(keyStoreManager, logger) {
|
|
53
72
|
const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
54
73
|
const validatorAddresses = validatorKeyStore.getAddresses();
|
|
55
74
|
// Verify that we can retrieve all required data from the key store
|
|
56
75
|
for (const address of validatorAddresses){
|
|
57
76
|
// Functions throw if required data is not available
|
|
77
|
+
let coinbase;
|
|
78
|
+
let feeRecipient;
|
|
58
79
|
try {
|
|
59
|
-
validatorKeyStore.getCoinbaseAddress(address);
|
|
60
|
-
validatorKeyStore.getFeeRecipient(address);
|
|
80
|
+
coinbase = validatorKeyStore.getCoinbaseAddress(address);
|
|
81
|
+
feeRecipient = validatorKeyStore.getFeeRecipient(address);
|
|
61
82
|
} catch (error) {
|
|
62
83
|
throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
|
|
63
84
|
}
|
|
@@ -65,6 +86,7 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
65
86
|
if (!publisherAddresses.length) {
|
|
66
87
|
throw new Error(`No publisher addresses found for validator address ${address}`);
|
|
67
88
|
}
|
|
89
|
+
logger?.debug(`Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map((x)=>x.toString()).join()}`);
|
|
68
90
|
}
|
|
69
91
|
}
|
|
70
92
|
async handleEpochCommitteeUpdate() {
|
|
@@ -89,13 +111,29 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
89
111
|
this.log.error(`Error updating epoch committee`, err);
|
|
90
112
|
}
|
|
91
113
|
}
|
|
92
|
-
static new(config,
|
|
114
|
+
static async new(config, checkpointsBuilder, worldState, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, blobClient, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
|
|
93
115
|
const metrics = new ValidatorMetrics(telemetry);
|
|
94
116
|
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
95
117
|
txsPermitted: !config.disableTransactions
|
|
96
118
|
});
|
|
97
|
-
const blockProposalHandler = new BlockProposalHandler(
|
|
98
|
-
const
|
|
119
|
+
const blockProposalHandler = new BlockProposalHandler(checkpointsBuilder, worldState, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, epochCache, config, metrics, dateProvider, telemetry);
|
|
120
|
+
const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
121
|
+
let validatorKeyStore = nodeKeystoreAdapter;
|
|
122
|
+
let haSigner;
|
|
123
|
+
if (config.haSigningEnabled) {
|
|
124
|
+
// If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
|
|
125
|
+
const haConfig = {
|
|
126
|
+
...config,
|
|
127
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000
|
|
128
|
+
};
|
|
129
|
+
const { signer } = await createHASigner(haConfig, {
|
|
130
|
+
telemetryClient: telemetry,
|
|
131
|
+
dateProvider
|
|
132
|
+
});
|
|
133
|
+
haSigner = signer;
|
|
134
|
+
validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, signer);
|
|
135
|
+
}
|
|
136
|
+
const validator = new ValidatorClient(validatorKeyStore, epochCache, p2pClient, blockProposalHandler, blockSource, checkpointsBuilder, worldState, l1ToL2MessageSource, config, blobClient, haSigner, dateProvider, telemetry);
|
|
99
137
|
return validator;
|
|
100
138
|
}
|
|
101
139
|
getValidatorAddresses() {
|
|
@@ -104,12 +142,8 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
104
142
|
getBlockProposalHandler() {
|
|
105
143
|
return this.blockProposalHandler;
|
|
106
144
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
110
|
-
}
|
|
111
|
-
signWithAddress(addr, msg) {
|
|
112
|
-
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
145
|
+
signWithAddress(addr, msg, context) {
|
|
146
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
113
147
|
}
|
|
114
148
|
getCoinbaseForAttestor(attestor) {
|
|
115
149
|
return this.keyStore.getCoinbaseAddress(attestor);
|
|
@@ -126,81 +160,429 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
126
160
|
...config
|
|
127
161
|
};
|
|
128
162
|
}
|
|
163
|
+
reloadKeystore(newManager) {
|
|
164
|
+
if (this.config.haSigningEnabled && !this.haSigner) {
|
|
165
|
+
this.log.warn('HA signing is enabled in config but was not initialized at startup. ' + 'Restart the node to enable HA signing.');
|
|
166
|
+
} else if (!this.config.haSigningEnabled && this.haSigner) {
|
|
167
|
+
this.log.warn('HA signing was disabled via config update but the HA signer is still active. ' + 'Restart the node to fully disable HA signing.');
|
|
168
|
+
}
|
|
169
|
+
const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
|
|
170
|
+
if (this.haSigner) {
|
|
171
|
+
this.keyStore = new HAKeyStore(newAdapter, this.haSigner);
|
|
172
|
+
} else {
|
|
173
|
+
this.keyStore = newAdapter;
|
|
174
|
+
}
|
|
175
|
+
this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
|
|
176
|
+
}
|
|
129
177
|
async start() {
|
|
130
178
|
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
131
179
|
this.log.warn(`Validator client already started`);
|
|
132
180
|
return;
|
|
133
181
|
}
|
|
182
|
+
await this.keyStore.start();
|
|
134
183
|
await this.registerHandlers();
|
|
135
184
|
const myAddresses = this.getValidatorAddresses();
|
|
136
185
|
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
186
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
137
187
|
if (inCommittee.length > 0) {
|
|
138
|
-
this.log.info(`
|
|
139
|
-
} else {
|
|
140
|
-
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
188
|
+
this.log.info(`Addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
|
|
141
189
|
}
|
|
142
190
|
this.epochCacheUpdateLoop.start();
|
|
143
191
|
return Promise.resolve();
|
|
144
192
|
}
|
|
145
193
|
async stop() {
|
|
146
194
|
await this.epochCacheUpdateLoop.stop();
|
|
195
|
+
await this.keyStore.stop();
|
|
147
196
|
}
|
|
148
197
|
/** Register handlers on the p2p client */ async registerHandlers() {
|
|
149
198
|
if (!this.hasRegisteredHandlers) {
|
|
150
199
|
this.hasRegisteredHandlers = true;
|
|
151
200
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
152
|
-
|
|
153
|
-
this.
|
|
201
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
202
|
+
const blockHandler = (block, proposalSender)=>this.validateBlockProposal(block, proposalSender);
|
|
203
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
204
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
205
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
206
|
+
// and processed separately via the block handler above.
|
|
207
|
+
const checkpointHandler = (checkpoint, proposalSender)=>this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
208
|
+
this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
|
|
209
|
+
// Duplicate proposal handler - triggers slashing for equivocation
|
|
210
|
+
this.p2pClient.registerDuplicateProposalCallback((info)=>{
|
|
211
|
+
this.handleDuplicateProposal(info);
|
|
212
|
+
});
|
|
213
|
+
// Duplicate attestation handler - triggers slashing for attestation equivocation
|
|
214
|
+
this.p2pClient.registerDuplicateAttestationCallback((info)=>{
|
|
215
|
+
this.handleDuplicateAttestation(info);
|
|
216
|
+
});
|
|
154
217
|
const myAddresses = this.getValidatorAddresses();
|
|
155
218
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
156
219
|
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
157
220
|
}
|
|
158
221
|
}
|
|
159
|
-
|
|
160
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Validate a block proposal from a peer.
|
|
224
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
225
|
+
* @returns true if the proposal is valid, false otherwise
|
|
226
|
+
*/ async validateBlockProposal(proposal, proposalSender) {
|
|
227
|
+
const slotNumber = proposal.slotNumber;
|
|
228
|
+
// Note: During escape hatch, we still want to "validate" proposals for observability,
|
|
229
|
+
// but we intentionally reject them and disable slashing invalid block and attestation flow.
|
|
230
|
+
const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
|
|
161
231
|
const proposer = proposal.getSender();
|
|
162
232
|
// Reject proposals with invalid signatures
|
|
163
233
|
if (!proposer) {
|
|
164
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
165
|
-
return
|
|
234
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
235
|
+
return false;
|
|
166
236
|
}
|
|
167
|
-
//
|
|
237
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
238
|
+
if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
|
|
239
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
240
|
+
proposer: proposer.toString(),
|
|
241
|
+
slotNumber
|
|
242
|
+
});
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
// Check if we're in the committee (for metrics purposes)
|
|
168
246
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
169
247
|
const partOfCommittee = inCommittee.length > 0;
|
|
170
|
-
const incFailedAttestation = (reason)=>this.metrics.incFailedAttestations(1, reason, partOfCommittee);
|
|
171
248
|
const proposalInfo = {
|
|
172
249
|
...proposal.toBlockInfo(),
|
|
173
250
|
proposer: proposer.toString()
|
|
174
251
|
};
|
|
175
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
252
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
176
253
|
...proposalInfo,
|
|
177
|
-
txHashes: proposal.txHashes.map((t)=>t.toString())
|
|
254
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
255
|
+
fishermanMode: this.config.fishermanMode || false
|
|
178
256
|
});
|
|
179
|
-
// Reexecute txs if we are part of the committee
|
|
180
|
-
//
|
|
181
|
-
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
|
|
182
|
-
const shouldReexecute = slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
|
|
183
|
-
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
|
|
257
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
258
|
+
// In fisherman mode, we always reexecute to validate proposals.
|
|
259
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
|
|
260
|
+
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals || this.blobClient.canUpload();
|
|
261
|
+
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute && !escapeHatchOpen);
|
|
184
262
|
if (!validationResult.isValid) {
|
|
185
|
-
this.log.warn(`
|
|
186
|
-
|
|
187
|
-
//
|
|
188
|
-
|
|
263
|
+
this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
264
|
+
const reason = validationResult.reason || 'unknown';
|
|
265
|
+
// Classify failure reason: bad proposal vs node issue
|
|
266
|
+
const badProposalReasons = [
|
|
267
|
+
'invalid_proposal',
|
|
268
|
+
'state_mismatch',
|
|
269
|
+
'failed_txs',
|
|
270
|
+
'in_hash_mismatch',
|
|
271
|
+
'parent_block_wrong_slot'
|
|
272
|
+
];
|
|
273
|
+
if (badProposalReasons.includes(reason)) {
|
|
274
|
+
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
275
|
+
} else {
|
|
276
|
+
// Node issues so we can't validate
|
|
277
|
+
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
278
|
+
}
|
|
279
|
+
// Slash invalid block proposals (can happen even when not in committee)
|
|
280
|
+
if (!escapeHatchOpen && validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
189
281
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
190
282
|
this.slashInvalidBlock(proposal);
|
|
191
283
|
}
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
287
|
+
...proposalInfo,
|
|
288
|
+
inCommittee: partOfCommittee,
|
|
289
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
290
|
+
escapeHatchOpen
|
|
291
|
+
});
|
|
292
|
+
if (escapeHatchOpen) {
|
|
293
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
300
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
301
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
302
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
303
|
+
*/ async attestToCheckpointProposal(proposal, _proposalSender) {
|
|
304
|
+
const slotNumber = proposal.slotNumber;
|
|
305
|
+
const proposer = proposal.getSender();
|
|
306
|
+
// If escape hatch is open for this slot's epoch, do not attest.
|
|
307
|
+
if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
|
|
308
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
// Reject proposals with invalid signatures
|
|
312
|
+
if (!proposer) {
|
|
313
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
317
|
+
if (this.getValidatorAddresses().some((addr)=>addr.equals(proposer))) {
|
|
318
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
319
|
+
proposer: proposer.toString(),
|
|
320
|
+
slotNumber
|
|
321
|
+
});
|
|
322
|
+
return undefined;
|
|
323
|
+
}
|
|
324
|
+
// Validate fee asset price modifier is within allowed range
|
|
325
|
+
if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
|
|
326
|
+
this.log.warn(`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${slotNumber}`);
|
|
192
327
|
return undefined;
|
|
193
328
|
}
|
|
194
329
|
// Check that I have any address in current committee before attesting
|
|
195
|
-
|
|
330
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
331
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
332
|
+
const proposalInfo = {
|
|
333
|
+
slotNumber,
|
|
334
|
+
archive: proposal.archive.toString(),
|
|
335
|
+
proposer: proposer.toString(),
|
|
336
|
+
txCount: proposal.txHashes.length
|
|
337
|
+
};
|
|
338
|
+
this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
|
|
339
|
+
...proposalInfo,
|
|
340
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
341
|
+
fishermanMode: this.config.fishermanMode || false
|
|
342
|
+
});
|
|
343
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
|
|
344
|
+
if (this.config.skipCheckpointProposalValidation) {
|
|
345
|
+
this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
|
|
346
|
+
} else {
|
|
347
|
+
const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
348
|
+
if (!validationResult.isValid) {
|
|
349
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// Upload blobs to filestore if we can (fire and forget)
|
|
354
|
+
if (this.blobClient.canUpload()) {
|
|
355
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
356
|
+
}
|
|
357
|
+
// Check that I have any address in current committee before attesting
|
|
358
|
+
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
359
|
+
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
196
360
|
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
197
361
|
return undefined;
|
|
198
362
|
}
|
|
199
363
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
200
|
-
this.log.info(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
364
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
|
|
365
|
+
...proposalInfo,
|
|
366
|
+
inCommittee: partOfCommittee,
|
|
367
|
+
fishermanMode: this.config.fishermanMode || false
|
|
368
|
+
});
|
|
369
|
+
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
370
|
+
// Determine which validators should attest
|
|
371
|
+
let attestors;
|
|
372
|
+
if (partOfCommittee) {
|
|
373
|
+
attestors = inCommittee;
|
|
374
|
+
} else if (this.config.fishermanMode) {
|
|
375
|
+
// In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
|
|
376
|
+
attestors = this.getValidatorAddresses();
|
|
377
|
+
} else {
|
|
378
|
+
attestors = [];
|
|
379
|
+
}
|
|
380
|
+
// Only create attestations if we have attestors
|
|
381
|
+
if (attestors.length === 0) {
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
if (this.config.fishermanMode) {
|
|
385
|
+
// bail out early and don't save attestations to the pool in fisherman mode
|
|
386
|
+
this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
|
|
387
|
+
...proposalInfo,
|
|
388
|
+
attestors: attestors.map((a)=>a.toString())
|
|
389
|
+
});
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Checks if we should attest to a slot based on equivocation prevention rules.
|
|
396
|
+
* @returns true if we should attest, false if we should skip
|
|
397
|
+
*/ shouldAttestToSlot(slotNumber) {
|
|
398
|
+
// If attestToEquivocatedProposals is true, always allow
|
|
399
|
+
if (this.config.attestToEquivocatedProposals) {
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
// Check if incoming slot is strictly greater than last attested
|
|
403
|
+
if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
|
|
404
|
+
this.log.warn(`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`);
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
async createCheckpointAttestationsFromProposal(proposal, attestors = []) {
|
|
410
|
+
// Equivocation check: must happen right before signing to minimize the race window
|
|
411
|
+
if (!this.shouldAttestToSlot(proposal.slotNumber)) {
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
|
|
415
|
+
// Track the proposal we attested to (to prevent equivocation)
|
|
416
|
+
this.lastAttestedProposal = proposal;
|
|
417
|
+
await this.p2pClient.addOwnCheckpointAttestations(attestations);
|
|
418
|
+
return attestations;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
422
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
423
|
+
*/ async validateCheckpointProposal(proposal, proposalInfo) {
|
|
424
|
+
const slot = proposal.slotNumber;
|
|
425
|
+
// Timeout block syncing at the start of the next slot
|
|
426
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
427
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
428
|
+
const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
|
|
429
|
+
// Wait for last block to sync by archive
|
|
430
|
+
let lastBlockHeader;
|
|
431
|
+
try {
|
|
432
|
+
lastBlockHeader = await retryUntil(async ()=>{
|
|
433
|
+
await this.blockSource.syncImmediate();
|
|
434
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
435
|
+
}, `waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`, timeoutSeconds, 0.5);
|
|
436
|
+
} catch (err) {
|
|
437
|
+
if (err instanceof TimeoutError) {
|
|
438
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
439
|
+
return {
|
|
440
|
+
isValid: false,
|
|
441
|
+
reason: 'last_block_not_found'
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
445
|
+
return {
|
|
446
|
+
isValid: false,
|
|
447
|
+
reason: 'block_fetch_error'
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
if (!lastBlockHeader) {
|
|
451
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
452
|
+
return {
|
|
453
|
+
isValid: false,
|
|
454
|
+
reason: 'last_block_not_found'
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
// Get all full blocks for the slot and checkpoint
|
|
458
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
459
|
+
if (blocks.length === 0) {
|
|
460
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
461
|
+
return {
|
|
462
|
+
isValid: false,
|
|
463
|
+
reason: 'no_blocks_for_slot'
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
// Ensure the last block for this slot matches the archive in the checkpoint proposal
|
|
467
|
+
if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
|
|
468
|
+
this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
|
|
469
|
+
return {
|
|
470
|
+
isValid: false,
|
|
471
|
+
reason: 'last_block_archive_mismatch'
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
475
|
+
...proposalInfo,
|
|
476
|
+
blockNumbers: blocks.map((b)=>b.number)
|
|
477
|
+
});
|
|
478
|
+
// Get checkpoint constants from first block
|
|
479
|
+
const firstBlock = blocks[0];
|
|
480
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
481
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
482
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
483
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
484
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
485
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
486
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch)).filter((c)=>c.checkpointNumber < checkpointNumber).map((c)=>c.checkpointOutHash);
|
|
487
|
+
// Fork world state at the block before the first block
|
|
488
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
489
|
+
const fork = await this.worldState.fork(parentBlockNumber);
|
|
490
|
+
try {
|
|
491
|
+
// Create checkpoint builder with all existing blocks
|
|
492
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(checkpointNumber, constants, proposal.feeAssetPriceModifier, l1ToL2Messages, previousCheckpointOutHashes, fork, blocks, this.log.getBindings());
|
|
493
|
+
// Complete the checkpoint to get computed values
|
|
494
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
495
|
+
// Compare checkpoint header with proposal
|
|
496
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
497
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
498
|
+
...proposalInfo,
|
|
499
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
500
|
+
proposal: proposal.checkpointHeader.toInspect()
|
|
501
|
+
});
|
|
502
|
+
return {
|
|
503
|
+
isValid: false,
|
|
504
|
+
reason: 'checkpoint_header_mismatch'
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
// Compare archive root with proposal
|
|
508
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
509
|
+
this.log.warn(`Archive root mismatch`, {
|
|
510
|
+
...proposalInfo,
|
|
511
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
512
|
+
proposal: proposal.archive.toString()
|
|
513
|
+
});
|
|
514
|
+
return {
|
|
515
|
+
isValid: false,
|
|
516
|
+
reason: 'archive_mismatch'
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
520
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
521
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
522
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([
|
|
523
|
+
...previousCheckpointOutHashes,
|
|
524
|
+
checkpointOutHash
|
|
525
|
+
]);
|
|
526
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
527
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
528
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
529
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
530
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
531
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
532
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map((h)=>h.toString()),
|
|
533
|
+
...proposalInfo
|
|
534
|
+
});
|
|
535
|
+
return {
|
|
536
|
+
isValid: false,
|
|
537
|
+
reason: 'out_hash_mismatch'
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
541
|
+
return {
|
|
542
|
+
isValid: true
|
|
543
|
+
};
|
|
544
|
+
} finally{
|
|
545
|
+
await fork.close();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Extract checkpoint global variables from a block.
|
|
550
|
+
*/ extractCheckpointConstants(block) {
|
|
551
|
+
const gv = block.header.globalVariables;
|
|
552
|
+
return {
|
|
553
|
+
chainId: gv.chainId,
|
|
554
|
+
version: gv.version,
|
|
555
|
+
slotNumber: gv.slotNumber,
|
|
556
|
+
timestamp: gv.timestamp,
|
|
557
|
+
coinbase: gv.coinbase,
|
|
558
|
+
feeRecipient: gv.feeRecipient,
|
|
559
|
+
gasFees: gv.gasFees
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
564
|
+
*/ async uploadBlobsForCheckpoint(proposal, proposalInfo) {
|
|
565
|
+
try {
|
|
566
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
567
|
+
if (!lastBlockHeader) {
|
|
568
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
572
|
+
if (blocks.length === 0) {
|
|
573
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const blobFields = blocks.flatMap((b)=>b.toBlobFields());
|
|
577
|
+
const blobs = await getBlobsPerL1Block(blobFields);
|
|
578
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
579
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
580
|
+
...proposalInfo,
|
|
581
|
+
numBlobs: blobs.length
|
|
582
|
+
});
|
|
583
|
+
} catch (err) {
|
|
584
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
585
|
+
}
|
|
204
586
|
}
|
|
205
587
|
slashInvalidBlock(proposal) {
|
|
206
588
|
const proposer = proposal.getSender();
|
|
@@ -220,36 +602,107 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
220
602
|
validator: proposer,
|
|
221
603
|
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
222
604
|
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
223
|
-
epochOrSlot: proposal.slotNumber
|
|
605
|
+
epochOrSlot: BigInt(proposal.slotNumber)
|
|
606
|
+
}
|
|
607
|
+
]);
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Handle detection of a duplicate proposal (equivocation).
|
|
611
|
+
* Emits a slash event when a proposer sends multiple proposals for the same position.
|
|
612
|
+
*/ handleDuplicateProposal(info) {
|
|
613
|
+
const { slot, proposer, type } = info;
|
|
614
|
+
this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
|
|
615
|
+
proposer: proposer.toString(),
|
|
616
|
+
slot,
|
|
617
|
+
type
|
|
618
|
+
});
|
|
619
|
+
// Emit slash event
|
|
620
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
621
|
+
{
|
|
622
|
+
validator: proposer,
|
|
623
|
+
amount: this.config.slashDuplicateProposalPenalty,
|
|
624
|
+
offenseType: OffenseType.DUPLICATE_PROPOSAL,
|
|
625
|
+
epochOrSlot: BigInt(slot)
|
|
224
626
|
}
|
|
225
627
|
]);
|
|
226
628
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
629
|
+
/**
|
|
630
|
+
* Handle detection of a duplicate attestation (equivocation).
|
|
631
|
+
* Emits a slash event when an attester signs attestations for different proposals at the same slot.
|
|
632
|
+
*/ handleDuplicateAttestation(info) {
|
|
633
|
+
const { slot, attester } = info;
|
|
634
|
+
this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
|
|
635
|
+
attester: attester.toString(),
|
|
636
|
+
slot
|
|
637
|
+
});
|
|
638
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
639
|
+
{
|
|
640
|
+
validator: attester,
|
|
641
|
+
amount: this.config.slashDuplicateAttestationPenalty,
|
|
642
|
+
offenseType: OffenseType.DUPLICATE_ATTESTATION,
|
|
643
|
+
epochOrSlot: BigInt(slot)
|
|
644
|
+
}
|
|
645
|
+
]);
|
|
646
|
+
}
|
|
647
|
+
async createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, options = {}) {
|
|
648
|
+
// Validate that we're not creating a proposal for an older or equal position
|
|
649
|
+
if (this.lastProposedBlock) {
|
|
650
|
+
const lastSlot = this.lastProposedBlock.slotNumber;
|
|
651
|
+
const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
|
|
652
|
+
const newSlot = blockHeader.globalVariables.slotNumber;
|
|
653
|
+
if (newSlot < lastSlot || newSlot === lastSlot && indexWithinCheckpoint <= lastIndex) {
|
|
654
|
+
throw new Error(`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` + `already proposed block for slot ${lastSlot} index ${lastIndex}`);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
this.log.info(`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`);
|
|
658
|
+
const newProposal = await this.validationService.createBlockProposal(blockHeader, indexWithinCheckpoint, inHash, archive, txs, proposerAddress, {
|
|
659
|
+
...options,
|
|
660
|
+
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
661
|
+
});
|
|
662
|
+
this.lastProposedBlock = newProposal;
|
|
663
|
+
return newProposal;
|
|
664
|
+
}
|
|
665
|
+
async createCheckpointProposal(checkpointHeader, archive, feeAssetPriceModifier, lastBlockInfo, proposerAddress, options = {}) {
|
|
666
|
+
// Validate that we're not creating a proposal for an older or equal slot
|
|
667
|
+
if (this.lastProposedCheckpoint) {
|
|
668
|
+
const lastSlot = this.lastProposedCheckpoint.slotNumber;
|
|
669
|
+
const newSlot = checkpointHeader.slotNumber;
|
|
670
|
+
if (newSlot <= lastSlot) {
|
|
671
|
+
throw new Error(`Cannot create checkpoint proposal for slot ${newSlot}: ` + `already proposed checkpoint for slot ${lastSlot}`);
|
|
672
|
+
}
|
|
231
673
|
}
|
|
232
|
-
|
|
233
|
-
this.
|
|
674
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
675
|
+
const newProposal = await this.validationService.createCheckpointProposal(checkpointHeader, archive, feeAssetPriceModifier, lastBlockInfo, proposerAddress, options);
|
|
676
|
+
this.lastProposedCheckpoint = newProposal;
|
|
234
677
|
return newProposal;
|
|
235
678
|
}
|
|
236
679
|
async broadcastBlockProposal(proposal) {
|
|
237
680
|
await this.p2pClient.broadcastProposal(proposal);
|
|
238
681
|
}
|
|
239
|
-
async signAttestationsAndSigners(attestationsAndSigners, proposer) {
|
|
240
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
682
|
+
async signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber) {
|
|
683
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
|
|
241
684
|
}
|
|
242
685
|
async collectOwnAttestations(proposal) {
|
|
243
|
-
const slot = proposal.
|
|
686
|
+
const slot = proposal.slotNumber;
|
|
244
687
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
245
688
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
246
689
|
inCommittee
|
|
247
690
|
});
|
|
248
|
-
|
|
691
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
692
|
+
if (!attestations) {
|
|
693
|
+
return [];
|
|
694
|
+
}
|
|
695
|
+
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
696
|
+
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
697
|
+
// due to inactivity for missed attestations.
|
|
698
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch((err)=>{
|
|
699
|
+
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
700
|
+
});
|
|
701
|
+
return attestations;
|
|
249
702
|
}
|
|
250
703
|
async collectAttestations(proposal, required, deadline) {
|
|
251
|
-
// Wait and poll the p2pClient's attestation pool for this
|
|
252
|
-
const slot = proposal.
|
|
704
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
705
|
+
const slot = proposal.slotNumber;
|
|
253
706
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
254
707
|
if (+deadline < this.dateProvider.now()) {
|
|
255
708
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
@@ -260,13 +713,13 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
260
713
|
const myAddresses = this.getValidatorAddresses();
|
|
261
714
|
let attestations = [];
|
|
262
715
|
while(true){
|
|
263
|
-
// Filter out attestations with a mismatching
|
|
716
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
264
717
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
265
|
-
const collectedAttestations = (await this.p2pClient.
|
|
266
|
-
if (!attestation.
|
|
267
|
-
this.log.warn(`Received attestation for slot ${slot} with mismatched
|
|
268
|
-
|
|
269
|
-
|
|
718
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
719
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
720
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`, {
|
|
721
|
+
attestationArchive: attestation.archive.toString(),
|
|
722
|
+
proposalArchive: proposal.archive.toString()
|
|
270
723
|
});
|
|
271
724
|
return false;
|
|
272
725
|
}
|
|
@@ -294,15 +747,10 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
294
747
|
this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
|
|
295
748
|
throw new AttestationTimeoutError(attestations.length, required, slot);
|
|
296
749
|
}
|
|
297
|
-
this.log.debug(`Collected ${attestations.length} attestations so far`);
|
|
750
|
+
this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
|
|
298
751
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
299
752
|
}
|
|
300
753
|
}
|
|
301
|
-
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
302
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
303
|
-
await this.p2pClient.addAttestations(attestations);
|
|
304
|
-
return attestations;
|
|
305
|
-
}
|
|
306
754
|
async handleAuthRequest(peer, msg) {
|
|
307
755
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
308
756
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
@@ -317,7 +765,11 @@ const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
|
317
765
|
return Buffer.alloc(0);
|
|
318
766
|
}
|
|
319
767
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
320
|
-
|
|
768
|
+
// AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
|
|
769
|
+
const context = {
|
|
770
|
+
dutyType: DutyType.AUTH_REQUEST
|
|
771
|
+
};
|
|
772
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
|
|
321
773
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
322
774
|
return authResponse.toBuffer();
|
|
323
775
|
}
|