@aztec/validator-client 0.0.1-fake-ceab37513c → 0.0.6-commit.a2d1860fe9
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 +521 -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/src/validator.ts
CHANGED
|
@@ -1,29 +1,62 @@
|
|
|
1
|
+
import type { BlobClientInterface } from '@aztec/blob-client/client';
|
|
2
|
+
import { type Blob, getBlobsPerL1Block } from '@aztec/blob-lib';
|
|
1
3
|
import type { EpochCache } from '@aztec/epoch-cache';
|
|
4
|
+
import { validateFeeAssetPriceModifier } from '@aztec/ethereum/contracts';
|
|
5
|
+
import {
|
|
6
|
+
BlockNumber,
|
|
7
|
+
CheckpointNumber,
|
|
8
|
+
EpochNumber,
|
|
9
|
+
IndexWithinCheckpoint,
|
|
10
|
+
SlotNumber,
|
|
11
|
+
} from '@aztec/foundation/branded-types';
|
|
12
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
13
|
+
import { TimeoutError } from '@aztec/foundation/error';
|
|
2
14
|
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
15
|
import type { Signature } from '@aztec/foundation/eth-signature';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
16
|
+
import { type LogData, type Logger, createLogger } from '@aztec/foundation/log';
|
|
17
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
6
18
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
7
19
|
import { sleep } from '@aztec/foundation/sleep';
|
|
8
20
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
9
21
|
import type { KeystoreManager } from '@aztec/node-keystore';
|
|
10
|
-
import type { P2P, PeerId
|
|
22
|
+
import type { DuplicateAttestationInfo, DuplicateProposalInfo, P2P, PeerId } from '@aztec/p2p';
|
|
11
23
|
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
12
24
|
import { OffenseType, WANT_TO_SLASH_EVENT, type Watcher, type WatcherEmitter } from '@aztec/slasher';
|
|
13
25
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
14
|
-
import type { CommitteeAttestationsAndSigners, L2BlockSource } from '@aztec/stdlib/block';
|
|
15
|
-
import
|
|
16
|
-
import type {
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
import type { CommitteeAttestationsAndSigners, L2Block, L2BlockSink, L2BlockSource } from '@aztec/stdlib/block';
|
|
27
|
+
import { getEpochAtSlot, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
|
|
28
|
+
import type {
|
|
29
|
+
CreateCheckpointProposalLastBlockData,
|
|
30
|
+
ITxProvider,
|
|
31
|
+
Validator,
|
|
32
|
+
ValidatorClientFullConfig,
|
|
33
|
+
WorldStateSynchronizer,
|
|
34
|
+
} from '@aztec/stdlib/interfaces/server';
|
|
35
|
+
import { type L1ToL2MessageSource, accumulateCheckpointOutHashes } from '@aztec/stdlib/messaging';
|
|
36
|
+
import {
|
|
37
|
+
type BlockProposal,
|
|
38
|
+
type BlockProposalOptions,
|
|
39
|
+
type CheckpointAttestation,
|
|
40
|
+
CheckpointProposal,
|
|
41
|
+
type CheckpointProposalCore,
|
|
42
|
+
type CheckpointProposalOptions,
|
|
43
|
+
} from '@aztec/stdlib/p2p';
|
|
44
|
+
import type { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
45
|
+
import type { BlockHeader, CheckpointGlobalVariables, Tx } from '@aztec/stdlib/tx';
|
|
19
46
|
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
20
47
|
import { type TelemetryClient, type Tracer, getTelemetryClient } from '@aztec/telemetry-client';
|
|
48
|
+
import { createHASigner } from '@aztec/validator-ha-signer/factory';
|
|
49
|
+
import { DutyType, type SigningContext } from '@aztec/validator-ha-signer/types';
|
|
50
|
+
import type { ValidatorHASigner } from '@aztec/validator-ha-signer/validator-ha-signer';
|
|
21
51
|
|
|
22
52
|
import { EventEmitter } from 'events';
|
|
23
53
|
import type { TypedDataDefinition } from 'viem';
|
|
24
54
|
|
|
25
55
|
import { BlockProposalHandler, type BlockProposalValidationFailureReason } from './block_proposal_handler.js';
|
|
56
|
+
import type { FullNodeCheckpointsBuilder } from './checkpoint_builder.js';
|
|
26
57
|
import { ValidationService } from './duties/validation_service.js';
|
|
58
|
+
import { HAKeyStore } from './key_store/ha_key_store.js';
|
|
59
|
+
import type { ExtendedValidatorKeyStore } from './key_store/interface.js';
|
|
27
60
|
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
28
61
|
import { ValidatorMetrics } from './metrics.js';
|
|
29
62
|
|
|
@@ -44,50 +77,68 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
44
77
|
public readonly tracer: Tracer;
|
|
45
78
|
private validationService: ValidationService;
|
|
46
79
|
private metrics: ValidatorMetrics;
|
|
47
|
-
|
|
80
|
+
private log: Logger;
|
|
48
81
|
// Whether it has already registered handlers on the p2p client
|
|
49
82
|
private hasRegisteredHandlers = false;
|
|
50
83
|
|
|
51
|
-
|
|
52
|
-
private
|
|
84
|
+
/** Tracks the last block proposal we created, to detect duplicate proposal attempts. */
|
|
85
|
+
private lastProposedBlock?: BlockProposal;
|
|
86
|
+
|
|
87
|
+
/** Tracks the last checkpoint proposal we created. */
|
|
88
|
+
private lastProposedCheckpoint?: CheckpointProposal;
|
|
53
89
|
|
|
54
|
-
private lastEpochForCommitteeUpdateLoop:
|
|
90
|
+
private lastEpochForCommitteeUpdateLoop: EpochNumber | undefined;
|
|
55
91
|
private epochCacheUpdateLoop: RunningPromise;
|
|
56
92
|
|
|
57
93
|
private proposersOfInvalidBlocks: Set<string> = new Set();
|
|
58
94
|
|
|
95
|
+
/** Tracks the last checkpoint proposal we attested to, to prevent equivocation. */
|
|
96
|
+
private lastAttestedProposal?: CheckpointProposalCore;
|
|
97
|
+
|
|
59
98
|
protected constructor(
|
|
60
|
-
private keyStore:
|
|
99
|
+
private keyStore: ExtendedValidatorKeyStore,
|
|
61
100
|
private epochCache: EpochCache,
|
|
62
101
|
private p2pClient: P2P,
|
|
63
102
|
private blockProposalHandler: BlockProposalHandler,
|
|
103
|
+
private blockSource: L2BlockSource,
|
|
104
|
+
private checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
105
|
+
private worldState: WorldStateSynchronizer,
|
|
106
|
+
private l1ToL2MessageSource: L1ToL2MessageSource,
|
|
64
107
|
private config: ValidatorClientFullConfig,
|
|
108
|
+
private blobClient: BlobClientInterface,
|
|
109
|
+
private haSigner: ValidatorHASigner | undefined,
|
|
65
110
|
private dateProvider: DateProvider = new DateProvider(),
|
|
66
111
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
67
|
-
|
|
112
|
+
log = createLogger('validator'),
|
|
68
113
|
) {
|
|
69
114
|
super();
|
|
115
|
+
|
|
116
|
+
// Create child logger with fisherman prefix if in fisherman mode
|
|
117
|
+
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
118
|
+
|
|
70
119
|
this.tracer = telemetry.getTracer('Validator');
|
|
71
120
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
72
121
|
|
|
73
|
-
this.validationService = new ValidationService(keyStore);
|
|
122
|
+
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
74
123
|
|
|
75
124
|
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
76
|
-
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
|
|
125
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
77
126
|
|
|
78
127
|
const myAddresses = this.getValidatorAddresses();
|
|
79
128
|
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
80
129
|
}
|
|
81
130
|
|
|
82
|
-
public static validateKeyStoreConfiguration(keyStoreManager: KeystoreManager) {
|
|
131
|
+
public static validateKeyStoreConfiguration(keyStoreManager: KeystoreManager, logger?: Logger) {
|
|
83
132
|
const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
84
133
|
const validatorAddresses = validatorKeyStore.getAddresses();
|
|
85
134
|
// Verify that we can retrieve all required data from the key store
|
|
86
135
|
for (const address of validatorAddresses) {
|
|
87
136
|
// Functions throw if required data is not available
|
|
137
|
+
let coinbase: EthAddress;
|
|
138
|
+
let feeRecipient: AztecAddress;
|
|
88
139
|
try {
|
|
89
|
-
validatorKeyStore.getCoinbaseAddress(address);
|
|
90
|
-
validatorKeyStore.getFeeRecipient(address);
|
|
140
|
+
coinbase = validatorKeyStore.getCoinbaseAddress(address);
|
|
141
|
+
feeRecipient = validatorKeyStore.getFeeRecipient(address);
|
|
91
142
|
} catch (error) {
|
|
92
143
|
throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
|
|
93
144
|
}
|
|
@@ -96,6 +147,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
96
147
|
if (!publisherAddresses.length) {
|
|
97
148
|
throw new Error(`No publisher addresses found for validator address ${address}`);
|
|
98
149
|
}
|
|
150
|
+
logger?.debug(
|
|
151
|
+
`Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map(x => x.toString()).join()}`,
|
|
152
|
+
);
|
|
99
153
|
}
|
|
100
154
|
}
|
|
101
155
|
|
|
@@ -126,15 +180,17 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
126
180
|
}
|
|
127
181
|
}
|
|
128
182
|
|
|
129
|
-
static new(
|
|
183
|
+
static async new(
|
|
130
184
|
config: ValidatorClientFullConfig,
|
|
131
|
-
|
|
185
|
+
checkpointsBuilder: FullNodeCheckpointsBuilder,
|
|
186
|
+
worldState: WorldStateSynchronizer,
|
|
132
187
|
epochCache: EpochCache,
|
|
133
188
|
p2pClient: P2P,
|
|
134
|
-
blockSource: L2BlockSource,
|
|
189
|
+
blockSource: L2BlockSource & L2BlockSink,
|
|
135
190
|
l1ToL2MessageSource: L1ToL2MessageSource,
|
|
136
|
-
txProvider:
|
|
191
|
+
txProvider: ITxProvider,
|
|
137
192
|
keyStoreManager: KeystoreManager,
|
|
193
|
+
blobClient: BlobClientInterface,
|
|
138
194
|
dateProvider: DateProvider = new DateProvider(),
|
|
139
195
|
telemetry: TelemetryClient = getTelemetryClient(),
|
|
140
196
|
) {
|
|
@@ -143,23 +199,45 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
143
199
|
txsPermitted: !config.disableTransactions,
|
|
144
200
|
});
|
|
145
201
|
const blockProposalHandler = new BlockProposalHandler(
|
|
146
|
-
|
|
202
|
+
checkpointsBuilder,
|
|
203
|
+
worldState,
|
|
147
204
|
blockSource,
|
|
148
205
|
l1ToL2MessageSource,
|
|
149
206
|
txProvider,
|
|
150
207
|
blockProposalValidator,
|
|
208
|
+
epochCache,
|
|
151
209
|
config,
|
|
152
210
|
metrics,
|
|
153
211
|
dateProvider,
|
|
154
212
|
telemetry,
|
|
155
213
|
);
|
|
156
214
|
|
|
215
|
+
const nodeKeystoreAdapter = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
216
|
+
let validatorKeyStore: ExtendedValidatorKeyStore = nodeKeystoreAdapter;
|
|
217
|
+
let haSigner: ValidatorHASigner | undefined;
|
|
218
|
+
if (config.haSigningEnabled) {
|
|
219
|
+
// If maxStuckDutiesAgeMs is not explicitly set, compute it from Aztec slot duration
|
|
220
|
+
const haConfig = {
|
|
221
|
+
...config,
|
|
222
|
+
maxStuckDutiesAgeMs: config.maxStuckDutiesAgeMs ?? epochCache.getL1Constants().slotDuration * 2 * 1000,
|
|
223
|
+
};
|
|
224
|
+
const { signer } = await createHASigner(haConfig);
|
|
225
|
+
haSigner = signer;
|
|
226
|
+
validatorKeyStore = new HAKeyStore(nodeKeystoreAdapter, signer);
|
|
227
|
+
}
|
|
228
|
+
|
|
157
229
|
const validator = new ValidatorClient(
|
|
158
|
-
|
|
230
|
+
validatorKeyStore,
|
|
159
231
|
epochCache,
|
|
160
232
|
p2pClient,
|
|
161
233
|
blockProposalHandler,
|
|
234
|
+
blockSource,
|
|
235
|
+
checkpointsBuilder,
|
|
236
|
+
worldState,
|
|
237
|
+
l1ToL2MessageSource,
|
|
162
238
|
config,
|
|
239
|
+
blobClient,
|
|
240
|
+
haSigner,
|
|
163
241
|
dateProvider,
|
|
164
242
|
telemetry,
|
|
165
243
|
);
|
|
@@ -177,18 +255,8 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
177
255
|
return this.blockProposalHandler;
|
|
178
256
|
}
|
|
179
257
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
proposal: BlockProposal,
|
|
183
|
-
blockNumber: number,
|
|
184
|
-
txs: any[],
|
|
185
|
-
l1ToL2Messages: Fr[],
|
|
186
|
-
): Promise<any> {
|
|
187
|
-
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition) {
|
|
191
|
-
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
258
|
+
public signWithAddress(addr: EthAddress, msg: TypedDataDefinition, context: SigningContext) {
|
|
259
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg, context);
|
|
192
260
|
}
|
|
193
261
|
|
|
194
262
|
public getCoinbaseForAttestor(attestor: EthAddress): EthAddress {
|
|
@@ -207,24 +275,43 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
207
275
|
this.config = { ...this.config, ...config };
|
|
208
276
|
}
|
|
209
277
|
|
|
278
|
+
public reloadKeystore(newManager: KeystoreManager): void {
|
|
279
|
+
if (this.config.haSigningEnabled && !this.haSigner) {
|
|
280
|
+
this.log.warn(
|
|
281
|
+
'HA signing is enabled in config but was not initialized at startup. ' +
|
|
282
|
+
'Restart the node to enable HA signing.',
|
|
283
|
+
);
|
|
284
|
+
} else if (!this.config.haSigningEnabled && this.haSigner) {
|
|
285
|
+
this.log.warn(
|
|
286
|
+
'HA signing was disabled via config update but the HA signer is still active. ' +
|
|
287
|
+
'Restart the node to fully disable HA signing.',
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const newAdapter = NodeKeystoreAdapter.fromKeyStoreManager(newManager);
|
|
292
|
+
if (this.haSigner) {
|
|
293
|
+
this.keyStore = new HAKeyStore(newAdapter, this.haSigner);
|
|
294
|
+
} else {
|
|
295
|
+
this.keyStore = newAdapter;
|
|
296
|
+
}
|
|
297
|
+
this.validationService = new ValidationService(this.keyStore, this.log.createChild('validation-service'));
|
|
298
|
+
}
|
|
299
|
+
|
|
210
300
|
public async start() {
|
|
211
301
|
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
212
302
|
this.log.warn(`Validator client already started`);
|
|
213
303
|
return;
|
|
214
304
|
}
|
|
215
305
|
|
|
306
|
+
await this.keyStore.start();
|
|
307
|
+
|
|
216
308
|
await this.registerHandlers();
|
|
217
309
|
|
|
218
310
|
const myAddresses = this.getValidatorAddresses();
|
|
219
311
|
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
312
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
220
313
|
if (inCommittee.length > 0) {
|
|
221
|
-
this.log.info(
|
|
222
|
-
`Started validator with addresses in current validator committee: ${inCommittee
|
|
223
|
-
.map(a => a.toString())
|
|
224
|
-
.join(', ')}`,
|
|
225
|
-
);
|
|
226
|
-
} else {
|
|
227
|
-
this.log.info(`Started validator with addresses: ${myAddresses.map(a => a.toString()).join(', ')}`);
|
|
314
|
+
this.log.info(`Addresses in current validator committee: ${inCommittee.map(a => a.toString()).join(', ')}`);
|
|
228
315
|
}
|
|
229
316
|
this.epochCacheUpdateLoop.start();
|
|
230
317
|
|
|
@@ -233,6 +320,7 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
233
320
|
|
|
234
321
|
public async stop() {
|
|
235
322
|
await this.epochCacheUpdateLoop.stop();
|
|
323
|
+
await this.keyStore.stop();
|
|
236
324
|
}
|
|
237
325
|
|
|
238
326
|
/** Register handlers on the p2p client */
|
|
@@ -241,9 +329,29 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
241
329
|
this.hasRegisteredHandlers = true;
|
|
242
330
|
this.log.debug(`Registering validator handlers for p2p client`);
|
|
243
331
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
332
|
+
// Block proposal handler - validates but does NOT attest (validators only attest to checkpoints)
|
|
333
|
+
const blockHandler = (block: BlockProposal, proposalSender: PeerId): Promise<boolean> =>
|
|
334
|
+
this.validateBlockProposal(block, proposalSender);
|
|
335
|
+
this.p2pClient.registerBlockProposalHandler(blockHandler);
|
|
336
|
+
|
|
337
|
+
// Checkpoint proposal handler - validates and creates attestations
|
|
338
|
+
// The checkpoint is received as CheckpointProposalCore since the lastBlock is extracted
|
|
339
|
+
// and processed separately via the block handler above.
|
|
340
|
+
const checkpointHandler = (
|
|
341
|
+
checkpoint: CheckpointProposalCore,
|
|
342
|
+
proposalSender: PeerId,
|
|
343
|
+
): Promise<CheckpointAttestation[] | undefined> => this.attestToCheckpointProposal(checkpoint, proposalSender);
|
|
344
|
+
this.p2pClient.registerCheckpointProposalHandler(checkpointHandler);
|
|
345
|
+
|
|
346
|
+
// Duplicate proposal handler - triggers slashing for equivocation
|
|
347
|
+
this.p2pClient.registerDuplicateProposalCallback((info: DuplicateProposalInfo) => {
|
|
348
|
+
this.handleDuplicateProposal(info);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// Duplicate attestation handler - triggers slashing for attestation equivocation
|
|
352
|
+
this.p2pClient.registerDuplicateAttestationCallback((info: DuplicateAttestationInfo) => {
|
|
353
|
+
this.handleDuplicateAttestation(info);
|
|
354
|
+
});
|
|
247
355
|
|
|
248
356
|
const myAddresses = this.getValidatorAddresses();
|
|
249
357
|
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
@@ -252,47 +360,86 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
252
360
|
}
|
|
253
361
|
}
|
|
254
362
|
|
|
255
|
-
|
|
256
|
-
|
|
363
|
+
/**
|
|
364
|
+
* Validate a block proposal from a peer.
|
|
365
|
+
* Note: Validators do NOT attest to individual blocks - attestations are only for checkpoint proposals.
|
|
366
|
+
* @returns true if the proposal is valid, false otherwise
|
|
367
|
+
*/
|
|
368
|
+
async validateBlockProposal(proposal: BlockProposal, proposalSender: PeerId): Promise<boolean> {
|
|
369
|
+
const slotNumber = proposal.slotNumber;
|
|
370
|
+
|
|
371
|
+
// Note: During escape hatch, we still want to "validate" proposals for observability,
|
|
372
|
+
// but we intentionally reject them and disable slashing invalid block and attestation flow.
|
|
373
|
+
const escapeHatchOpen = await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber);
|
|
374
|
+
|
|
257
375
|
const proposer = proposal.getSender();
|
|
258
376
|
|
|
259
377
|
// Reject proposals with invalid signatures
|
|
260
378
|
if (!proposer) {
|
|
261
|
-
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
262
|
-
return
|
|
379
|
+
this.log.warn(`Received block proposal with invalid signature for slot ${slotNumber}`);
|
|
380
|
+
return false;
|
|
263
381
|
}
|
|
264
382
|
|
|
265
|
-
//
|
|
383
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
384
|
+
if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
|
|
385
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
386
|
+
proposer: proposer.toString(),
|
|
387
|
+
slotNumber,
|
|
388
|
+
});
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Check if we're in the committee (for metrics purposes)
|
|
266
393
|
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
267
394
|
const partOfCommittee = inCommittee.length > 0;
|
|
268
|
-
const incFailedAttestation = (reason: string) => this.metrics.incFailedAttestations(1, reason, partOfCommittee);
|
|
269
395
|
|
|
270
396
|
const proposalInfo = { ...proposal.toBlockInfo(), proposer: proposer.toString() };
|
|
271
|
-
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
397
|
+
this.log.info(`Received block proposal for slot ${slotNumber}`, {
|
|
272
398
|
...proposalInfo,
|
|
273
399
|
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
400
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
274
401
|
});
|
|
275
402
|
|
|
276
|
-
// Reexecute txs if we are part of the committee
|
|
277
|
-
//
|
|
278
|
-
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } =
|
|
403
|
+
// Reexecute txs if we are part of the committee, or if slashing is enabled, or if we are configured to always reexecute.
|
|
404
|
+
// In fisherman mode, we always reexecute to validate proposals.
|
|
405
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } =
|
|
406
|
+
this.config;
|
|
279
407
|
const shouldReexecute =
|
|
408
|
+
fishermanMode ||
|
|
280
409
|
(slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute) ||
|
|
281
410
|
(partOfCommittee && validatorReexecute) ||
|
|
282
|
-
alwaysReexecuteBlockProposals
|
|
411
|
+
alwaysReexecuteBlockProposals ||
|
|
412
|
+
this.blobClient.canUpload();
|
|
283
413
|
|
|
284
414
|
const validationResult = await this.blockProposalHandler.handleBlockProposal(
|
|
285
415
|
proposal,
|
|
286
416
|
proposalSender,
|
|
287
|
-
!!shouldReexecute,
|
|
417
|
+
!!shouldReexecute && !escapeHatchOpen,
|
|
288
418
|
);
|
|
289
419
|
|
|
290
420
|
if (!validationResult.isValid) {
|
|
291
|
-
this.log.warn(`
|
|
292
|
-
|
|
421
|
+
this.log.warn(`Block proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
422
|
+
|
|
423
|
+
const reason = validationResult.reason || 'unknown';
|
|
424
|
+
// Classify failure reason: bad proposal vs node issue
|
|
425
|
+
const badProposalReasons: BlockProposalValidationFailureReason[] = [
|
|
426
|
+
'invalid_proposal',
|
|
427
|
+
'state_mismatch',
|
|
428
|
+
'failed_txs',
|
|
429
|
+
'in_hash_mismatch',
|
|
430
|
+
'parent_block_wrong_slot',
|
|
431
|
+
];
|
|
432
|
+
|
|
433
|
+
if (badProposalReasons.includes(reason as BlockProposalValidationFailureReason)) {
|
|
434
|
+
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
435
|
+
} else {
|
|
436
|
+
// Node issues so we can't validate
|
|
437
|
+
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
438
|
+
}
|
|
293
439
|
|
|
294
|
-
// Slash invalid block proposals
|
|
440
|
+
// Slash invalid block proposals (can happen even when not in committee)
|
|
295
441
|
if (
|
|
442
|
+
!escapeHatchOpen &&
|
|
296
443
|
validationResult.reason &&
|
|
297
444
|
SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) &&
|
|
298
445
|
slashBroadcastedInvalidBlockPenalty > 0n
|
|
@@ -300,21 +447,360 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
300
447
|
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
301
448
|
this.slashInvalidBlock(proposal);
|
|
302
449
|
}
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
this.log.info(`Validated block proposal for slot ${slotNumber}`, {
|
|
454
|
+
...proposalInfo,
|
|
455
|
+
inCommittee: partOfCommittee,
|
|
456
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
457
|
+
escapeHatchOpen,
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
if (escapeHatchOpen) {
|
|
461
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, rejecting block proposal`, proposalInfo);
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Validate and attest to a checkpoint proposal from a peer.
|
|
470
|
+
* The proposal is received as CheckpointProposalCore (without lastBlock) since
|
|
471
|
+
* the lastBlock is extracted and processed separately via the block handler.
|
|
472
|
+
* @returns Checkpoint attestations if valid, undefined otherwise
|
|
473
|
+
*/
|
|
474
|
+
async attestToCheckpointProposal(
|
|
475
|
+
proposal: CheckpointProposalCore,
|
|
476
|
+
_proposalSender: PeerId,
|
|
477
|
+
): Promise<CheckpointAttestation[] | undefined> {
|
|
478
|
+
const slotNumber = proposal.slotNumber;
|
|
479
|
+
const proposer = proposal.getSender();
|
|
480
|
+
|
|
481
|
+
// If escape hatch is open for this slot's epoch, do not attest.
|
|
482
|
+
if (await this.epochCache.isEscapeHatchOpenAtSlot(slotNumber)) {
|
|
483
|
+
this.log.warn(`Escape hatch open for slot ${slotNumber}, skipping checkpoint attestation handling`);
|
|
484
|
+
return undefined;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Reject proposals with invalid signatures
|
|
488
|
+
if (!proposer) {
|
|
489
|
+
this.log.warn(`Received checkpoint proposal with invalid signature for slot ${slotNumber}`);
|
|
490
|
+
return undefined;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Ignore proposals from ourselves (may happen in HA setups)
|
|
494
|
+
if (this.getValidatorAddresses().some(addr => addr.equals(proposer))) {
|
|
495
|
+
this.log.warn(`Ignoring block proposal from self for slot ${slotNumber}`, {
|
|
496
|
+
proposer: proposer.toString(),
|
|
497
|
+
slotNumber,
|
|
498
|
+
});
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// Validate fee asset price modifier is within allowed range
|
|
503
|
+
if (!validateFeeAssetPriceModifier(proposal.feeAssetPriceModifier)) {
|
|
504
|
+
this.log.warn(
|
|
505
|
+
`Received checkpoint proposal with invalid feeAssetPriceModifier ${proposal.feeAssetPriceModifier} for slot ${slotNumber}`,
|
|
506
|
+
);
|
|
303
507
|
return undefined;
|
|
304
508
|
}
|
|
305
509
|
|
|
306
510
|
// Check that I have any address in current committee before attesting
|
|
307
|
-
|
|
511
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
512
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
513
|
+
|
|
514
|
+
const proposalInfo = {
|
|
515
|
+
slotNumber,
|
|
516
|
+
archive: proposal.archive.toString(),
|
|
517
|
+
proposer: proposer.toString(),
|
|
518
|
+
txCount: proposal.txHashes.length,
|
|
519
|
+
};
|
|
520
|
+
this.log.info(`Received checkpoint proposal for slot ${slotNumber}`, {
|
|
521
|
+
...proposalInfo,
|
|
522
|
+
txHashes: proposal.txHashes.map(t => t.toString()),
|
|
523
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// Validate the checkpoint proposal before attesting (unless skipCheckpointProposalValidation is set)
|
|
527
|
+
if (this.config.skipCheckpointProposalValidation) {
|
|
528
|
+
this.log.warn(`Skipping checkpoint proposal validation for slot ${slotNumber}`, proposalInfo);
|
|
529
|
+
} else {
|
|
530
|
+
const validationResult = await this.validateCheckpointProposal(proposal, proposalInfo);
|
|
531
|
+
if (!validationResult.isValid) {
|
|
532
|
+
this.log.warn(`Checkpoint proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
533
|
+
return undefined;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// Upload blobs to filestore if we can (fire and forget)
|
|
538
|
+
if (this.blobClient.canUpload()) {
|
|
539
|
+
void this.uploadBlobsForCheckpoint(proposal, proposalInfo);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Check that I have any address in current committee before attesting
|
|
543
|
+
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
544
|
+
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
308
545
|
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
309
546
|
return undefined;
|
|
310
547
|
}
|
|
311
548
|
|
|
312
549
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
313
|
-
this.log.info(
|
|
314
|
-
|
|
550
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} checkpoint proposal for slot ${slotNumber}`, {
|
|
551
|
+
...proposalInfo,
|
|
552
|
+
inCommittee: partOfCommittee,
|
|
553
|
+
fishermanMode: this.config.fishermanMode || false,
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
557
|
+
|
|
558
|
+
// Determine which validators should attest
|
|
559
|
+
let attestors: EthAddress[];
|
|
560
|
+
if (partOfCommittee) {
|
|
561
|
+
attestors = inCommittee;
|
|
562
|
+
} else if (this.config.fishermanMode) {
|
|
563
|
+
// In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
|
|
564
|
+
attestors = this.getValidatorAddresses();
|
|
565
|
+
} else {
|
|
566
|
+
attestors = [];
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// Only create attestations if we have attestors
|
|
570
|
+
if (attestors.length === 0) {
|
|
571
|
+
return undefined;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (this.config.fishermanMode) {
|
|
575
|
+
// bail out early and don't save attestations to the pool in fisherman mode
|
|
576
|
+
this.log.info(`Creating checkpoint attestations for slot ${slotNumber}`, {
|
|
577
|
+
...proposalInfo,
|
|
578
|
+
attestors: attestors.map(a => a.toString()),
|
|
579
|
+
});
|
|
580
|
+
return undefined;
|
|
581
|
+
}
|
|
315
582
|
|
|
316
|
-
|
|
317
|
-
|
|
583
|
+
return await this.createCheckpointAttestationsFromProposal(proposal, attestors);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Checks if we should attest to a slot based on equivocation prevention rules.
|
|
588
|
+
* @returns true if we should attest, false if we should skip
|
|
589
|
+
*/
|
|
590
|
+
private shouldAttestToSlot(slotNumber: SlotNumber): boolean {
|
|
591
|
+
// If attestToEquivocatedProposals is true, always allow
|
|
592
|
+
if (this.config.attestToEquivocatedProposals) {
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Check if incoming slot is strictly greater than last attested
|
|
597
|
+
if (this.lastAttestedProposal && slotNumber <= this.lastAttestedProposal.slotNumber) {
|
|
598
|
+
this.log.warn(
|
|
599
|
+
`Refusing to process a proposal for slot ${slotNumber} given we already attested to a proposal for slot ${this.lastAttestedProposal.slotNumber}`,
|
|
600
|
+
);
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
return true;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
private async createCheckpointAttestationsFromProposal(
|
|
608
|
+
proposal: CheckpointProposalCore,
|
|
609
|
+
attestors: EthAddress[] = [],
|
|
610
|
+
): Promise<CheckpointAttestation[] | undefined> {
|
|
611
|
+
// Equivocation check: must happen right before signing to minimize the race window
|
|
612
|
+
if (!this.shouldAttestToSlot(proposal.slotNumber)) {
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
const attestations = await this.validationService.attestToCheckpointProposal(proposal, attestors);
|
|
617
|
+
|
|
618
|
+
// Track the proposal we attested to (to prevent equivocation)
|
|
619
|
+
this.lastAttestedProposal = proposal;
|
|
620
|
+
|
|
621
|
+
await this.p2pClient.addOwnCheckpointAttestations(attestations);
|
|
622
|
+
return attestations;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Validates a checkpoint proposal by building the full checkpoint and comparing it with the proposal.
|
|
627
|
+
* @returns Validation result with isValid flag and reason if invalid.
|
|
628
|
+
*/
|
|
629
|
+
private async validateCheckpointProposal(
|
|
630
|
+
proposal: CheckpointProposalCore,
|
|
631
|
+
proposalInfo: LogData,
|
|
632
|
+
): Promise<{ isValid: true } | { isValid: false; reason: string }> {
|
|
633
|
+
const slot = proposal.slotNumber;
|
|
634
|
+
|
|
635
|
+
// Timeout block syncing at the start of the next slot
|
|
636
|
+
const config = this.checkpointsBuilder.getConfig();
|
|
637
|
+
const nextSlotTimestampSeconds = Number(getTimestampForSlot(SlotNumber(slot + 1), config));
|
|
638
|
+
const timeoutSeconds = Math.max(1, nextSlotTimestampSeconds - Math.floor(this.dateProvider.now() / 1000));
|
|
639
|
+
|
|
640
|
+
// Wait for last block to sync by archive
|
|
641
|
+
let lastBlockHeader: BlockHeader | undefined;
|
|
642
|
+
try {
|
|
643
|
+
lastBlockHeader = await retryUntil(
|
|
644
|
+
async () => {
|
|
645
|
+
await this.blockSource.syncImmediate();
|
|
646
|
+
return this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
647
|
+
},
|
|
648
|
+
`waiting for block with archive ${proposal.archive.toString()} for slot ${slot}`,
|
|
649
|
+
timeoutSeconds,
|
|
650
|
+
0.5,
|
|
651
|
+
);
|
|
652
|
+
} catch (err) {
|
|
653
|
+
if (err instanceof TimeoutError) {
|
|
654
|
+
this.log.warn(`Timed out waiting for block with archive matching checkpoint proposal`, proposalInfo);
|
|
655
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
656
|
+
}
|
|
657
|
+
this.log.error(`Error fetching last block for checkpoint proposal`, err, proposalInfo);
|
|
658
|
+
return { isValid: false, reason: 'block_fetch_error' };
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (!lastBlockHeader) {
|
|
662
|
+
this.log.warn(`Last block not found for checkpoint proposal`, proposalInfo);
|
|
663
|
+
return { isValid: false, reason: 'last_block_not_found' };
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// Get all full blocks for the slot and checkpoint
|
|
667
|
+
const blocks = await this.blockSource.getBlocksForSlot(slot);
|
|
668
|
+
if (blocks.length === 0) {
|
|
669
|
+
this.log.warn(`No blocks found for slot ${slot}`, proposalInfo);
|
|
670
|
+
return { isValid: false, reason: 'no_blocks_for_slot' };
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// Ensure the last block for this slot matches the archive in the checkpoint proposal
|
|
674
|
+
if (!blocks.at(-1)?.archive.root.equals(proposal.archive)) {
|
|
675
|
+
this.log.warn(`Last block archive mismatch for checkpoint proposal`, proposalInfo);
|
|
676
|
+
return { isValid: false, reason: 'last_block_archive_mismatch' };
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
this.log.debug(`Found ${blocks.length} blocks for slot ${slot}`, {
|
|
680
|
+
...proposalInfo,
|
|
681
|
+
blockNumbers: blocks.map(b => b.number),
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
// Get checkpoint constants from first block
|
|
685
|
+
const firstBlock = blocks[0];
|
|
686
|
+
const constants = this.extractCheckpointConstants(firstBlock);
|
|
687
|
+
const checkpointNumber = firstBlock.checkpointNumber;
|
|
688
|
+
|
|
689
|
+
// Get L1-to-L2 messages for this checkpoint
|
|
690
|
+
const l1ToL2Messages = await this.l1ToL2MessageSource.getL1ToL2Messages(checkpointNumber);
|
|
691
|
+
|
|
692
|
+
// Collect the out hashes of all the checkpoints before this one in the same epoch
|
|
693
|
+
const epoch = getEpochAtSlot(slot, this.epochCache.getL1Constants());
|
|
694
|
+
const previousCheckpointOutHashes = (await this.blockSource.getCheckpointsDataForEpoch(epoch))
|
|
695
|
+
.filter(c => c.checkpointNumber < checkpointNumber)
|
|
696
|
+
.map(c => c.checkpointOutHash);
|
|
697
|
+
|
|
698
|
+
// Fork world state at the block before the first block
|
|
699
|
+
const parentBlockNumber = BlockNumber(firstBlock.number - 1);
|
|
700
|
+
const fork = await this.worldState.fork(parentBlockNumber);
|
|
701
|
+
|
|
702
|
+
try {
|
|
703
|
+
// Create checkpoint builder with all existing blocks
|
|
704
|
+
const checkpointBuilder = await this.checkpointsBuilder.openCheckpoint(
|
|
705
|
+
checkpointNumber,
|
|
706
|
+
constants,
|
|
707
|
+
proposal.feeAssetPriceModifier,
|
|
708
|
+
l1ToL2Messages,
|
|
709
|
+
previousCheckpointOutHashes,
|
|
710
|
+
fork,
|
|
711
|
+
blocks,
|
|
712
|
+
this.log.getBindings(),
|
|
713
|
+
);
|
|
714
|
+
|
|
715
|
+
// Complete the checkpoint to get computed values
|
|
716
|
+
const computedCheckpoint = await checkpointBuilder.completeCheckpoint();
|
|
717
|
+
|
|
718
|
+
// Compare checkpoint header with proposal
|
|
719
|
+
if (!computedCheckpoint.header.equals(proposal.checkpointHeader)) {
|
|
720
|
+
this.log.warn(`Checkpoint header mismatch`, {
|
|
721
|
+
...proposalInfo,
|
|
722
|
+
computed: computedCheckpoint.header.toInspect(),
|
|
723
|
+
proposal: proposal.checkpointHeader.toInspect(),
|
|
724
|
+
});
|
|
725
|
+
return { isValid: false, reason: 'checkpoint_header_mismatch' };
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// Compare archive root with proposal
|
|
729
|
+
if (!computedCheckpoint.archive.root.equals(proposal.archive)) {
|
|
730
|
+
this.log.warn(`Archive root mismatch`, {
|
|
731
|
+
...proposalInfo,
|
|
732
|
+
computed: computedCheckpoint.archive.root.toString(),
|
|
733
|
+
proposal: proposal.archive.toString(),
|
|
734
|
+
});
|
|
735
|
+
return { isValid: false, reason: 'archive_mismatch' };
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// Check that the accumulated epoch out hash matches the value in the proposal.
|
|
739
|
+
// The epoch out hash is the accumulated hash of all checkpoint out hashes in the epoch.
|
|
740
|
+
const checkpointOutHash = computedCheckpoint.getCheckpointOutHash();
|
|
741
|
+
const computedEpochOutHash = accumulateCheckpointOutHashes([...previousCheckpointOutHashes, checkpointOutHash]);
|
|
742
|
+
const proposalEpochOutHash = proposal.checkpointHeader.epochOutHash;
|
|
743
|
+
if (!computedEpochOutHash.equals(proposalEpochOutHash)) {
|
|
744
|
+
this.log.warn(`Epoch out hash mismatch`, {
|
|
745
|
+
proposalEpochOutHash: proposalEpochOutHash.toString(),
|
|
746
|
+
computedEpochOutHash: computedEpochOutHash.toString(),
|
|
747
|
+
checkpointOutHash: checkpointOutHash.toString(),
|
|
748
|
+
previousCheckpointOutHashes: previousCheckpointOutHashes.map(h => h.toString()),
|
|
749
|
+
...proposalInfo,
|
|
750
|
+
});
|
|
751
|
+
return { isValid: false, reason: 'out_hash_mismatch' };
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
this.log.verbose(`Checkpoint proposal validation successful for slot ${slot}`, proposalInfo);
|
|
755
|
+
return { isValid: true };
|
|
756
|
+
} finally {
|
|
757
|
+
await fork.close();
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* Extract checkpoint global variables from a block.
|
|
763
|
+
*/
|
|
764
|
+
private extractCheckpointConstants(block: L2Block): CheckpointGlobalVariables {
|
|
765
|
+
const gv = block.header.globalVariables;
|
|
766
|
+
return {
|
|
767
|
+
chainId: gv.chainId,
|
|
768
|
+
version: gv.version,
|
|
769
|
+
slotNumber: gv.slotNumber,
|
|
770
|
+
timestamp: gv.timestamp,
|
|
771
|
+
coinbase: gv.coinbase,
|
|
772
|
+
feeRecipient: gv.feeRecipient,
|
|
773
|
+
gasFees: gv.gasFees,
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Uploads blobs for a checkpoint to the filestore (fire and forget).
|
|
779
|
+
*/
|
|
780
|
+
protected async uploadBlobsForCheckpoint(proposal: CheckpointProposalCore, proposalInfo: LogData): Promise<void> {
|
|
781
|
+
try {
|
|
782
|
+
const lastBlockHeader = await this.blockSource.getBlockHeaderByArchive(proposal.archive);
|
|
783
|
+
if (!lastBlockHeader) {
|
|
784
|
+
this.log.warn(`Failed to get last block header for blob upload`, proposalInfo);
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const blocks = await this.blockSource.getBlocksForSlot(proposal.slotNumber);
|
|
789
|
+
if (blocks.length === 0) {
|
|
790
|
+
this.log.warn(`No blocks found for blob upload`, proposalInfo);
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const blobFields = blocks.flatMap(b => b.toBlobFields());
|
|
795
|
+
const blobs: Blob[] = await getBlobsPerL1Block(blobFields);
|
|
796
|
+
await this.blobClient.sendBlobsToFilestore(blobs);
|
|
797
|
+
this.log.debug(`Uploaded ${blobs.length} blobs to filestore for checkpoint at slot ${proposal.slotNumber}`, {
|
|
798
|
+
...proposalInfo,
|
|
799
|
+
numBlobs: blobs.length,
|
|
800
|
+
});
|
|
801
|
+
} catch (err) {
|
|
802
|
+
this.log.warn(`Failed to upload blobs for checkpoint: ${err}`, proposalInfo);
|
|
803
|
+
}
|
|
318
804
|
}
|
|
319
805
|
|
|
320
806
|
private slashInvalidBlock(proposal: BlockProposal) {
|
|
@@ -339,34 +825,130 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
339
825
|
validator: proposer,
|
|
340
826
|
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
341
827
|
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
342
|
-
epochOrSlot: proposal.slotNumber
|
|
828
|
+
epochOrSlot: BigInt(proposal.slotNumber),
|
|
829
|
+
},
|
|
830
|
+
]);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* Handle detection of a duplicate proposal (equivocation).
|
|
835
|
+
* Emits a slash event when a proposer sends multiple proposals for the same position.
|
|
836
|
+
*/
|
|
837
|
+
private handleDuplicateProposal(info: DuplicateProposalInfo): void {
|
|
838
|
+
const { slot, proposer, type } = info;
|
|
839
|
+
|
|
840
|
+
this.log.warn(`Triggering slash event for duplicate ${type} proposal from ${proposer.toString()} at slot ${slot}`, {
|
|
841
|
+
proposer: proposer.toString(),
|
|
842
|
+
slot,
|
|
843
|
+
type,
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
// Emit slash event
|
|
847
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
848
|
+
{
|
|
849
|
+
validator: proposer,
|
|
850
|
+
amount: this.config.slashDuplicateProposalPenalty,
|
|
851
|
+
offenseType: OffenseType.DUPLICATE_PROPOSAL,
|
|
852
|
+
epochOrSlot: BigInt(slot),
|
|
853
|
+
},
|
|
854
|
+
]);
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Handle detection of a duplicate attestation (equivocation).
|
|
859
|
+
* Emits a slash event when an attester signs attestations for different proposals at the same slot.
|
|
860
|
+
*/
|
|
861
|
+
private handleDuplicateAttestation(info: DuplicateAttestationInfo): void {
|
|
862
|
+
const { slot, attester } = info;
|
|
863
|
+
|
|
864
|
+
this.log.warn(`Triggering slash event for duplicate attestation from ${attester.toString()} at slot ${slot}`, {
|
|
865
|
+
attester: attester.toString(),
|
|
866
|
+
slot,
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
870
|
+
{
|
|
871
|
+
validator: attester,
|
|
872
|
+
amount: this.config.slashDuplicateAttestationPenalty,
|
|
873
|
+
offenseType: OffenseType.DUPLICATE_ATTESTATION,
|
|
874
|
+
epochOrSlot: BigInt(slot),
|
|
343
875
|
},
|
|
344
876
|
]);
|
|
345
877
|
}
|
|
346
878
|
|
|
347
879
|
async createBlockProposal(
|
|
348
|
-
|
|
349
|
-
|
|
880
|
+
blockHeader: BlockHeader,
|
|
881
|
+
indexWithinCheckpoint: IndexWithinCheckpoint,
|
|
882
|
+
inHash: Fr,
|
|
350
883
|
archive: Fr,
|
|
351
|
-
stateReference: StateReference,
|
|
352
884
|
txs: Tx[],
|
|
353
885
|
proposerAddress: EthAddress | undefined,
|
|
354
|
-
options: BlockProposalOptions,
|
|
355
|
-
): Promise<BlockProposal
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
886
|
+
options: BlockProposalOptions = {},
|
|
887
|
+
): Promise<BlockProposal> {
|
|
888
|
+
// Validate that we're not creating a proposal for an older or equal position
|
|
889
|
+
if (this.lastProposedBlock) {
|
|
890
|
+
const lastSlot = this.lastProposedBlock.slotNumber;
|
|
891
|
+
const lastIndex = this.lastProposedBlock.indexWithinCheckpoint;
|
|
892
|
+
const newSlot = blockHeader.globalVariables.slotNumber;
|
|
893
|
+
|
|
894
|
+
if (newSlot < lastSlot || (newSlot === lastSlot && indexWithinCheckpoint <= lastIndex)) {
|
|
895
|
+
throw new Error(
|
|
896
|
+
`Cannot create block proposal for slot ${newSlot} index ${indexWithinCheckpoint}: ` +
|
|
897
|
+
`already proposed block for slot ${lastSlot} index ${lastIndex}`,
|
|
898
|
+
);
|
|
899
|
+
}
|
|
359
900
|
}
|
|
360
901
|
|
|
902
|
+
this.log.info(
|
|
903
|
+
`Assembling block proposal for block ${blockHeader.globalVariables.blockNumber} slot ${blockHeader.globalVariables.slotNumber}`,
|
|
904
|
+
);
|
|
361
905
|
const newProposal = await this.validationService.createBlockProposal(
|
|
362
|
-
|
|
906
|
+
blockHeader,
|
|
907
|
+
indexWithinCheckpoint,
|
|
908
|
+
inHash,
|
|
363
909
|
archive,
|
|
364
|
-
stateReference,
|
|
365
910
|
txs,
|
|
366
911
|
proposerAddress,
|
|
912
|
+
{
|
|
913
|
+
...options,
|
|
914
|
+
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal,
|
|
915
|
+
},
|
|
916
|
+
);
|
|
917
|
+
this.lastProposedBlock = newProposal;
|
|
918
|
+
return newProposal;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
async createCheckpointProposal(
|
|
922
|
+
checkpointHeader: CheckpointHeader,
|
|
923
|
+
archive: Fr,
|
|
924
|
+
feeAssetPriceModifier: bigint,
|
|
925
|
+
lastBlockInfo: CreateCheckpointProposalLastBlockData | undefined,
|
|
926
|
+
proposerAddress: EthAddress | undefined,
|
|
927
|
+
options: CheckpointProposalOptions = {},
|
|
928
|
+
): Promise<CheckpointProposal> {
|
|
929
|
+
// Validate that we're not creating a proposal for an older or equal slot
|
|
930
|
+
if (this.lastProposedCheckpoint) {
|
|
931
|
+
const lastSlot = this.lastProposedCheckpoint.slotNumber;
|
|
932
|
+
const newSlot = checkpointHeader.slotNumber;
|
|
933
|
+
|
|
934
|
+
if (newSlot <= lastSlot) {
|
|
935
|
+
throw new Error(
|
|
936
|
+
`Cannot create checkpoint proposal for slot ${newSlot}: ` +
|
|
937
|
+
`already proposed checkpoint for slot ${lastSlot}`,
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
this.log.info(`Assembling checkpoint proposal for slot ${checkpointHeader.slotNumber}`);
|
|
943
|
+
const newProposal = await this.validationService.createCheckpointProposal(
|
|
944
|
+
checkpointHeader,
|
|
945
|
+
archive,
|
|
946
|
+
feeAssetPriceModifier,
|
|
947
|
+
lastBlockInfo,
|
|
948
|
+
proposerAddress,
|
|
367
949
|
options,
|
|
368
950
|
);
|
|
369
|
-
this.
|
|
951
|
+
this.lastProposedCheckpoint = newProposal;
|
|
370
952
|
return newProposal;
|
|
371
953
|
}
|
|
372
954
|
|
|
@@ -376,21 +958,39 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
376
958
|
|
|
377
959
|
async signAttestationsAndSigners(
|
|
378
960
|
attestationsAndSigners: CommitteeAttestationsAndSigners,
|
|
379
|
-
proposer: EthAddress
|
|
961
|
+
proposer: EthAddress,
|
|
962
|
+
slot: SlotNumber,
|
|
963
|
+
blockNumber: BlockNumber | CheckpointNumber,
|
|
380
964
|
): Promise<Signature> {
|
|
381
|
-
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
965
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer, slot, blockNumber);
|
|
382
966
|
}
|
|
383
967
|
|
|
384
|
-
async collectOwnAttestations(proposal:
|
|
385
|
-
const slot = proposal.
|
|
968
|
+
async collectOwnAttestations(proposal: CheckpointProposal): Promise<CheckpointAttestation[]> {
|
|
969
|
+
const slot = proposal.slotNumber;
|
|
386
970
|
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
387
971
|
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, { inCommittee });
|
|
388
|
-
|
|
972
|
+
const attestations = await this.createCheckpointAttestationsFromProposal(proposal, inCommittee);
|
|
973
|
+
|
|
974
|
+
if (!attestations) {
|
|
975
|
+
return [];
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
979
|
+
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
980
|
+
// due to inactivity for missed attestations.
|
|
981
|
+
void this.p2pClient.broadcastCheckpointAttestations(attestations).catch(err => {
|
|
982
|
+
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
983
|
+
});
|
|
984
|
+
return attestations;
|
|
389
985
|
}
|
|
390
986
|
|
|
391
|
-
async collectAttestations(
|
|
392
|
-
|
|
393
|
-
|
|
987
|
+
async collectAttestations(
|
|
988
|
+
proposal: CheckpointProposal,
|
|
989
|
+
required: number,
|
|
990
|
+
deadline: Date,
|
|
991
|
+
): Promise<CheckpointAttestation[]> {
|
|
992
|
+
// Wait and poll the p2pClient's attestation pool for this checkpoint until we have enough attestations
|
|
993
|
+
const slot = proposal.slotNumber;
|
|
394
994
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
395
995
|
|
|
396
996
|
if (+deadline < this.dateProvider.now()) {
|
|
@@ -405,16 +1005,16 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
405
1005
|
const proposalId = proposal.archive.toString();
|
|
406
1006
|
const myAddresses = this.getValidatorAddresses();
|
|
407
1007
|
|
|
408
|
-
let attestations:
|
|
1008
|
+
let attestations: CheckpointAttestation[] = [];
|
|
409
1009
|
while (true) {
|
|
410
|
-
// Filter out attestations with a mismatching
|
|
1010
|
+
// Filter out attestations with a mismatching archive. This should NOT happen since we have verified
|
|
411
1011
|
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
412
|
-
const collectedAttestations = (await this.p2pClient.
|
|
1012
|
+
const collectedAttestations = (await this.p2pClient.getCheckpointAttestationsForSlot(slot, proposalId)).filter(
|
|
413
1013
|
attestation => {
|
|
414
|
-
if (!attestation.
|
|
1014
|
+
if (!attestation.archive.equals(proposal.archive)) {
|
|
415
1015
|
this.log.warn(
|
|
416
|
-
`Received attestation for slot ${slot} with mismatched
|
|
417
|
-
{
|
|
1016
|
+
`Received attestation for slot ${slot} with mismatched archive from ${attestation.getSender()?.toString()}`,
|
|
1017
|
+
{ attestationArchive: attestation.archive.toString(), proposalArchive: proposal.archive.toString() },
|
|
418
1018
|
);
|
|
419
1019
|
return false;
|
|
420
1020
|
}
|
|
@@ -450,20 +1050,11 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
450
1050
|
throw new AttestationTimeoutError(attestations.length, required, slot);
|
|
451
1051
|
}
|
|
452
1052
|
|
|
453
|
-
this.log.debug(`Collected ${attestations.length} attestations so far`);
|
|
1053
|
+
this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
|
|
454
1054
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
455
1055
|
}
|
|
456
1056
|
}
|
|
457
1057
|
|
|
458
|
-
private async createBlockAttestationsFromProposal(
|
|
459
|
-
proposal: BlockProposal,
|
|
460
|
-
attestors: EthAddress[] = [],
|
|
461
|
-
): Promise<BlockAttestation[]> {
|
|
462
|
-
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
463
|
-
await this.p2pClient.addAttestations(attestations);
|
|
464
|
-
return attestations;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
1058
|
private async handleAuthRequest(peer: PeerId, msg: Buffer): Promise<Buffer> {
|
|
468
1059
|
const authRequest = AuthRequest.fromBuffer(msg);
|
|
469
1060
|
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch(_ => undefined);
|
|
@@ -482,7 +1073,9 @@ export class ValidatorClient extends (EventEmitter as new () => WatcherEmitter)
|
|
|
482
1073
|
}
|
|
483
1074
|
|
|
484
1075
|
const payloadToSign = authRequest.getPayloadToSign();
|
|
485
|
-
|
|
1076
|
+
// AUTH_REQUEST doesn't require HA protection - multiple signatures are safe
|
|
1077
|
+
const context: SigningContext = { dutyType: DutyType.AUTH_REQUEST };
|
|
1078
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign, context);
|
|
486
1079
|
const authResponse = new AuthResponse(statusMessage, signature);
|
|
487
1080
|
return authResponse.toBuffer();
|
|
488
1081
|
}
|