@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.5476d83
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/dest/block_proposal_handler.d.ts +52 -0
- package/dest/block_proposal_handler.d.ts.map +1 -0
- package/dest/block_proposal_handler.js +290 -0
- package/dest/config.d.ts +3 -14
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +36 -7
- package/dest/duties/validation_service.d.ts +17 -9
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +34 -11
- package/dest/factory.d.ts +22 -5
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +13 -6
- package/dest/index.d.ts +4 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- package/dest/key_store/index.d.ts +3 -1
- package/dest/key_store/index.d.ts.map +1 -1
- package/dest/key_store/index.js +2 -0
- package/dest/key_store/interface.d.ts +55 -6
- package/dest/key_store/interface.d.ts.map +1 -1
- package/dest/key_store/interface.js +3 -3
- package/dest/key_store/local_key_store.d.ts +41 -11
- package/dest/key_store/local_key_store.d.ts.map +1 -1
- package/dest/key_store/local_key_store.js +63 -16
- package/dest/key_store/node_keystore_adapter.d.ts +138 -0
- package/dest/key_store/node_keystore_adapter.d.ts.map +1 -0
- package/dest/key_store/node_keystore_adapter.js +316 -0
- package/dest/key_store/web3signer_key_store.d.ts +61 -0
- package/dest/key_store/web3signer_key_store.d.ts.map +1 -0
- package/dest/key_store/web3signer_key_store.js +152 -0
- package/dest/metrics.d.ts +12 -5
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +52 -15
- package/dest/validator.d.ts +50 -63
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +306 -172
- package/package.json +27 -21
- package/src/block_proposal_handler.ts +341 -0
- package/src/config.ts +48 -22
- package/src/duties/validation_service.ts +66 -14
- package/src/factory.ts +56 -11
- package/src/index.ts +3 -1
- package/src/key_store/index.ts +2 -0
- package/src/key_store/interface.ts +61 -5
- package/src/key_store/local_key_store.ts +67 -17
- package/src/key_store/node_keystore_adapter.ts +375 -0
- package/src/key_store/web3signer_key_store.ts +192 -0
- package/src/metrics.ts +68 -17
- package/src/validator.ts +416 -230
- package/dest/errors/index.d.ts +0 -2
- package/dest/errors/index.d.ts.map +0 -1
- package/dest/errors/index.js +0 -1
- package/dest/errors/validator.error.d.ts +0 -29
- package/dest/errors/validator.error.d.ts.map +0 -1
- package/dest/errors/validator.error.js +0 -45
- package/src/errors/index.ts +0 -1
- package/src/errors/validator.error.ts +0 -55
package/dest/validator.js
CHANGED
|
@@ -1,69 +1,147 @@
|
|
|
1
|
-
import { Buffer32 } from '@aztec/foundation/buffer';
|
|
2
1
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
2
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
4
3
|
import { sleep } from '@aztec/foundation/sleep';
|
|
5
4
|
import { DateProvider } from '@aztec/foundation/timer';
|
|
6
|
-
import { BlockProposalValidator } from '@aztec/p2p
|
|
7
|
-
import {
|
|
5
|
+
import { AuthRequest, AuthResponse, BlockProposalValidator, ReqRespSubProtocol } from '@aztec/p2p';
|
|
6
|
+
import { OffenseType, WANT_TO_SLASH_EVENT } from '@aztec/slasher';
|
|
7
|
+
import { AttestationTimeoutError } from '@aztec/stdlib/validators';
|
|
8
|
+
import { getTelemetryClient } from '@aztec/telemetry-client';
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import { BlockProposalHandler } from './block_proposal_handler.js';
|
|
8
11
|
import { ValidationService } from './duties/validation_service.js';
|
|
9
|
-
import {
|
|
10
|
-
import { LocalKeyStore } from './key_store/local_key_store.js';
|
|
12
|
+
import { NodeKeystoreAdapter } from './key_store/node_keystore_adapter.js';
|
|
11
13
|
import { ValidatorMetrics } from './metrics.js';
|
|
14
|
+
// We maintain a set of proposers who have proposed invalid blocks.
|
|
15
|
+
// Just cap the set to avoid unbounded growth.
|
|
16
|
+
const MAX_PROPOSERS_OF_INVALID_BLOCKS = 1000;
|
|
17
|
+
// What errors from the block proposal handler result in slashing
|
|
18
|
+
const SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT = [
|
|
19
|
+
'state_mismatch',
|
|
20
|
+
'failed_txs'
|
|
21
|
+
];
|
|
12
22
|
/**
|
|
13
23
|
* Validator Client
|
|
14
|
-
*/ export class ValidatorClient extends
|
|
24
|
+
*/ export class ValidatorClient extends EventEmitter {
|
|
15
25
|
keyStore;
|
|
16
26
|
epochCache;
|
|
17
27
|
p2pClient;
|
|
28
|
+
blockProposalHandler;
|
|
18
29
|
config;
|
|
19
30
|
dateProvider;
|
|
20
|
-
|
|
31
|
+
tracer;
|
|
21
32
|
validationService;
|
|
22
33
|
metrics;
|
|
34
|
+
log;
|
|
35
|
+
// Whether it has already registered handlers on the p2p client
|
|
36
|
+
hasRegisteredHandlers;
|
|
23
37
|
// Used to check if we are sending the same proposal twice
|
|
24
38
|
previousProposal;
|
|
25
|
-
|
|
26
|
-
blockBuilder;
|
|
39
|
+
lastEpochForCommitteeUpdateLoop;
|
|
27
40
|
epochCacheUpdateLoop;
|
|
28
|
-
|
|
29
|
-
constructor(keyStore, epochCache, p2pClient, config, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
30
|
-
|
|
31
|
-
|
|
41
|
+
proposersOfInvalidBlocks;
|
|
42
|
+
constructor(keyStore, epochCache, p2pClient, blockProposalHandler, config, dateProvider = new DateProvider(), telemetry = getTelemetryClient(), log = createLogger('validator')){
|
|
43
|
+
super(), this.keyStore = keyStore, this.epochCache = epochCache, this.p2pClient = p2pClient, this.blockProposalHandler = blockProposalHandler, this.config = config, this.dateProvider = dateProvider, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
|
|
44
|
+
// Create child logger with fisherman prefix if in fisherman mode
|
|
45
|
+
this.log = config.fishermanMode ? log.createChild('[FISHERMAN]') : log;
|
|
46
|
+
this.tracer = telemetry.getTracer('Validator');
|
|
32
47
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
33
|
-
this.validationService = new ValidationService(keyStore);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
this.validationService = new ValidationService(keyStore, this.log.createChild('validation-service'));
|
|
49
|
+
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
50
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), this.log, 1000);
|
|
51
|
+
const myAddresses = this.getValidatorAddresses();
|
|
52
|
+
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
53
|
+
}
|
|
54
|
+
static validateKeyStoreConfiguration(keyStoreManager, logger) {
|
|
55
|
+
const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
56
|
+
const validatorAddresses = validatorKeyStore.getAddresses();
|
|
57
|
+
// Verify that we can retrieve all required data from the key store
|
|
58
|
+
for (const address of validatorAddresses){
|
|
59
|
+
// Functions throw if required data is not available
|
|
60
|
+
let coinbase;
|
|
61
|
+
let feeRecipient;
|
|
62
|
+
try {
|
|
63
|
+
coinbase = validatorKeyStore.getCoinbaseAddress(address);
|
|
64
|
+
feeRecipient = validatorKeyStore.getFeeRecipient(address);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
|
|
44
67
|
}
|
|
45
|
-
|
|
46
|
-
|
|
68
|
+
const publisherAddresses = validatorKeyStore.getPublisherAddresses(address);
|
|
69
|
+
if (!publisherAddresses.length) {
|
|
70
|
+
throw new Error(`No publisher addresses found for validator address ${address}`);
|
|
71
|
+
}
|
|
72
|
+
logger?.debug(`Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map((x)=>x.toString()).join()}`);
|
|
73
|
+
}
|
|
47
74
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
75
|
+
async handleEpochCommitteeUpdate() {
|
|
76
|
+
try {
|
|
77
|
+
const { committee, epoch } = await this.epochCache.getCommittee('next');
|
|
78
|
+
if (!committee) {
|
|
79
|
+
this.log.trace(`No committee found for slot`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
|
|
83
|
+
const me = this.getValidatorAddresses();
|
|
84
|
+
const committeeSet = new Set(committee.map((v)=>v.toString()));
|
|
85
|
+
const inCommittee = me.filter((a)=>committeeSet.has(a.toString()));
|
|
86
|
+
if (inCommittee.length > 0) {
|
|
87
|
+
this.log.info(`Validators ${inCommittee.map((a)=>a.toString()).join(',')} are on the validator committee for epoch ${epoch}`);
|
|
88
|
+
} else {
|
|
89
|
+
this.log.verbose(`Validators ${me.map((a)=>a.toString()).join(', ')} are not on the validator committee for epoch ${epoch}`);
|
|
90
|
+
}
|
|
91
|
+
this.lastEpochForCommitteeUpdateLoop = epoch;
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
this.log.error(`Error updating epoch committee`, err);
|
|
51
95
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
96
|
+
}
|
|
97
|
+
static new(config, blockBuilder, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
|
|
98
|
+
const metrics = new ValidatorMetrics(telemetry);
|
|
99
|
+
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
100
|
+
txsPermitted: !config.disableTransactions
|
|
101
|
+
});
|
|
102
|
+
const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
|
|
103
|
+
const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config, dateProvider, telemetry);
|
|
56
104
|
return validator;
|
|
57
105
|
}
|
|
106
|
+
getValidatorAddresses() {
|
|
107
|
+
return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
|
|
108
|
+
}
|
|
109
|
+
getBlockProposalHandler() {
|
|
110
|
+
return this.blockProposalHandler;
|
|
111
|
+
}
|
|
112
|
+
// Proxy method for backwards compatibility with tests
|
|
113
|
+
reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
|
|
114
|
+
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
115
|
+
}
|
|
116
|
+
signWithAddress(addr, msg) {
|
|
117
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
118
|
+
}
|
|
119
|
+
getCoinbaseForAttestor(attestor) {
|
|
120
|
+
return this.keyStore.getCoinbaseAddress(attestor);
|
|
121
|
+
}
|
|
122
|
+
getFeeRecipientForAttestor(attestor) {
|
|
123
|
+
return this.keyStore.getFeeRecipient(attestor);
|
|
124
|
+
}
|
|
125
|
+
getConfig() {
|
|
126
|
+
return this.config;
|
|
127
|
+
}
|
|
128
|
+
updateConfig(config) {
|
|
129
|
+
this.config = {
|
|
130
|
+
...this.config,
|
|
131
|
+
...config
|
|
132
|
+
};
|
|
133
|
+
}
|
|
58
134
|
async start() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
135
|
+
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
136
|
+
this.log.warn(`Validator client already started`);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
await this.registerHandlers();
|
|
140
|
+
const myAddresses = this.getValidatorAddresses();
|
|
141
|
+
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
142
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
143
|
+
if (inCommittee.length > 0) {
|
|
144
|
+
this.log.info(`Addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
|
|
67
145
|
}
|
|
68
146
|
this.epochCacheUpdateLoop.start();
|
|
69
147
|
return Promise.resolve();
|
|
@@ -71,155 +149,195 @@ import { ValidatorMetrics } from './metrics.js';
|
|
|
71
149
|
async stop() {
|
|
72
150
|
await this.epochCacheUpdateLoop.stop();
|
|
73
151
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
* We reuse the sequencer's block building functionality for re-execution
|
|
84
|
-
*/ registerBlockBuilder(blockBuilder) {
|
|
85
|
-
this.blockBuilder = blockBuilder;
|
|
86
|
-
}
|
|
87
|
-
async attestToProposal(proposal) {
|
|
88
|
-
const slotNumber = proposal.slotNumber.toNumber();
|
|
89
|
-
const proposalInfo = {
|
|
90
|
-
slotNumber,
|
|
91
|
-
blockNumber: proposal.payload.header.globalVariables.blockNumber.toNumber(),
|
|
92
|
-
archive: proposal.payload.archive.toString(),
|
|
93
|
-
txCount: proposal.payload.txHashes.length,
|
|
94
|
-
txHashes: proposal.payload.txHashes.map((txHash)=>txHash.toString())
|
|
95
|
-
};
|
|
96
|
-
this.log.verbose(`Received request to attest for slot ${slotNumber}`);
|
|
97
|
-
// Check that I am in the committee
|
|
98
|
-
if (!await this.epochCache.isInCommittee(this.keyStore.getAddress())) {
|
|
99
|
-
this.log.verbose(`Not in the committee, skipping attestation`);
|
|
100
|
-
return undefined;
|
|
152
|
+
/** Register handlers on the p2p client */ async registerHandlers() {
|
|
153
|
+
if (!this.hasRegisteredHandlers) {
|
|
154
|
+
this.hasRegisteredHandlers = true;
|
|
155
|
+
this.log.debug(`Registering validator handlers for p2p client`);
|
|
156
|
+
const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
|
|
157
|
+
this.p2pClient.registerBlockProposalHandler(handler);
|
|
158
|
+
const myAddresses = this.getValidatorAddresses();
|
|
159
|
+
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
160
|
+
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
101
161
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
162
|
+
}
|
|
163
|
+
async attestToProposal(proposal, proposalSender) {
|
|
164
|
+
const slotNumber = proposal.slotNumber;
|
|
165
|
+
const proposer = proposal.getSender();
|
|
166
|
+
// Reject proposals with invalid signatures
|
|
167
|
+
if (!proposer) {
|
|
168
|
+
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
106
169
|
return undefined;
|
|
107
170
|
}
|
|
108
|
-
// Check that
|
|
109
|
-
this.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
171
|
+
// Check that I have any address in current committee before attesting
|
|
172
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
173
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
174
|
+
const proposalInfo = {
|
|
175
|
+
...proposal.toBlockInfo(),
|
|
176
|
+
proposer: proposer.toString()
|
|
177
|
+
};
|
|
178
|
+
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
179
|
+
...proposalInfo,
|
|
180
|
+
txHashes: proposal.txHashes.map((t)=>t.toString()),
|
|
181
|
+
fishermanMode: this.config.fishermanMode || false
|
|
182
|
+
});
|
|
183
|
+
// Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
|
|
184
|
+
// invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
|
|
185
|
+
// In fisherman mode, we always reexecute to validate proposals.
|
|
186
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals, fishermanMode } = this.config;
|
|
187
|
+
const shouldReexecute = fishermanMode || slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
|
|
188
|
+
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
|
|
189
|
+
if (!validationResult.isValid) {
|
|
190
|
+
this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
191
|
+
const reason = validationResult.reason || 'unknown';
|
|
192
|
+
// Classify failure reason: bad proposal vs node issue
|
|
193
|
+
const badProposalReasons = [
|
|
194
|
+
'invalid_proposal',
|
|
195
|
+
'state_mismatch',
|
|
196
|
+
'failed_txs',
|
|
197
|
+
'in_hash_mismatch',
|
|
198
|
+
'parent_block_wrong_slot'
|
|
199
|
+
];
|
|
200
|
+
if (badProposalReasons.includes(reason)) {
|
|
201
|
+
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
120
202
|
} else {
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
this.log.error(`Failed to attest to proposal`, error, proposalInfo);
|
|
203
|
+
// Node issues so we can't attest
|
|
204
|
+
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
124
205
|
}
|
|
206
|
+
// Slash invalid block proposals (can happen even when not in committee)
|
|
207
|
+
if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
208
|
+
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
209
|
+
this.slashInvalidBlock(proposal);
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
// Check that I have any address in current committee before attesting
|
|
214
|
+
// In fisherman mode, we still create attestations for validation even if not in committee
|
|
215
|
+
if (!partOfCommittee && !this.config.fishermanMode) {
|
|
216
|
+
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
125
217
|
return undefined;
|
|
126
218
|
}
|
|
127
219
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
128
|
-
this.log.info(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Re-execute the transactions in the proposal and check that the state updates match the header state
|
|
134
|
-
* @param proposal - The proposal to re-execute
|
|
135
|
-
*/ async reExecuteTransactions(proposal) {
|
|
136
|
-
const { header, txHashes } = proposal.payload;
|
|
137
|
-
const txs = (await Promise.all(txHashes.map((tx)=>this.p2pClient.getTxByHash(tx)))).filter((tx)=>tx !== undefined);
|
|
138
|
-
// If we cannot request all of the transactions, then we should fail
|
|
139
|
-
if (txs.length !== txHashes.length) {
|
|
140
|
-
throw new TransactionsNotAvailableError(txHashes);
|
|
141
|
-
}
|
|
142
|
-
// Assertion: This check will fail if re-execution is not enabled
|
|
143
|
-
if (this.blockBuilder === undefined) {
|
|
144
|
-
throw new BlockBuilderNotProvidedError();
|
|
145
|
-
}
|
|
146
|
-
// Use the sequencer's block building logic to re-execute the transactions
|
|
147
|
-
const stopTimer = this.metrics.reExecutionTimer();
|
|
148
|
-
const { block, numFailedTxs } = await this.blockBuilder(txs, header.globalVariables, {
|
|
149
|
-
validateOnly: true
|
|
220
|
+
this.log.info(`${partOfCommittee ? 'Attesting to' : 'Validated'} proposal for slot ${slotNumber}`, {
|
|
221
|
+
...proposalInfo,
|
|
222
|
+
inCommittee: partOfCommittee,
|
|
223
|
+
fishermanMode: this.config.fishermanMode || false
|
|
150
224
|
});
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
225
|
+
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
226
|
+
// If the above function does not throw an error, then we can attest to the proposal
|
|
227
|
+
// Determine which validators should attest
|
|
228
|
+
let attestors;
|
|
229
|
+
if (partOfCommittee) {
|
|
230
|
+
attestors = inCommittee;
|
|
231
|
+
} else if (this.config.fishermanMode) {
|
|
232
|
+
// In fisherman mode, create attestations for validation purposes even if not in committee. These won't be broadcast.
|
|
233
|
+
attestors = this.getValidatorAddresses();
|
|
234
|
+
} else {
|
|
235
|
+
attestors = [];
|
|
236
|
+
}
|
|
237
|
+
// Only create attestations if we have attestors
|
|
238
|
+
if (attestors.length === 0) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
if (this.config.fishermanMode) {
|
|
242
|
+
// bail out early and don't save attestations to the pool in fisherman mode
|
|
243
|
+
this.log.info(`Creating attestations for proposal for slot ${slotNumber}`, {
|
|
244
|
+
...proposalInfo,
|
|
245
|
+
attestors: attestors.map((a)=>a.toString())
|
|
246
|
+
});
|
|
247
|
+
return undefined;
|
|
248
|
+
}
|
|
249
|
+
return this.createBlockAttestationsFromProposal(proposal, attestors);
|
|
250
|
+
}
|
|
251
|
+
slashInvalidBlock(proposal) {
|
|
252
|
+
const proposer = proposal.getSender();
|
|
253
|
+
// Skip if signature is invalid (shouldn't happen since we validate earlier)
|
|
254
|
+
if (!proposer) {
|
|
255
|
+
this.log.warn(`Cannot slash proposal with invalid signature`);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
// Trim the set if it's too big.
|
|
259
|
+
if (this.proposersOfInvalidBlocks.size > MAX_PROPOSERS_OF_INVALID_BLOCKS) {
|
|
260
|
+
// remove oldest proposer. `values` is guaranteed to be in insertion order.
|
|
261
|
+
this.proposersOfInvalidBlocks.delete(this.proposersOfInvalidBlocks.values().next().value);
|
|
262
|
+
}
|
|
263
|
+
this.proposersOfInvalidBlocks.add(proposer.toString());
|
|
264
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
265
|
+
{
|
|
266
|
+
validator: proposer,
|
|
267
|
+
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
268
|
+
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
269
|
+
epochOrSlot: BigInt(proposal.slotNumber)
|
|
270
|
+
}
|
|
271
|
+
]);
|
|
272
|
+
}
|
|
273
|
+
async createBlockProposal(blockNumber, header, archive, txs, proposerAddress, options) {
|
|
274
|
+
if (this.previousProposal?.slotNumber === header.slotNumber) {
|
|
192
275
|
this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
|
|
193
276
|
return Promise.resolve(undefined);
|
|
194
277
|
}
|
|
195
|
-
const newProposal = await this.validationService.createBlockProposal(header, archive, txs
|
|
278
|
+
const newProposal = await this.validationService.createBlockProposal(header, archive, txs, proposerAddress, {
|
|
279
|
+
...options,
|
|
280
|
+
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
281
|
+
});
|
|
196
282
|
this.previousProposal = newProposal;
|
|
197
283
|
return newProposal;
|
|
198
284
|
}
|
|
199
|
-
broadcastBlockProposal(proposal) {
|
|
200
|
-
this.p2pClient.broadcastProposal(proposal);
|
|
285
|
+
async broadcastBlockProposal(proposal) {
|
|
286
|
+
await this.p2pClient.broadcastProposal(proposal);
|
|
287
|
+
}
|
|
288
|
+
async signAttestationsAndSigners(attestationsAndSigners, proposer) {
|
|
289
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
290
|
+
}
|
|
291
|
+
async collectOwnAttestations(proposal) {
|
|
292
|
+
const slot = proposal.payload.header.slotNumber;
|
|
293
|
+
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
294
|
+
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
295
|
+
inCommittee
|
|
296
|
+
});
|
|
297
|
+
const attestations = await this.createBlockAttestationsFromProposal(proposal, inCommittee);
|
|
298
|
+
// We broadcast our own attestations to our peers so, in case our block does not get mined on L1,
|
|
299
|
+
// other nodes can see that our validators did attest to this block proposal, and do not slash us
|
|
300
|
+
// due to inactivity for missed attestations.
|
|
301
|
+
void this.p2pClient.broadcastAttestations(attestations).catch((err)=>{
|
|
302
|
+
this.log.error(`Failed to broadcast self-attestations for slot ${slot}`, err);
|
|
303
|
+
});
|
|
304
|
+
return attestations;
|
|
201
305
|
}
|
|
202
|
-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962)
|
|
203
306
|
async collectAttestations(proposal, required, deadline) {
|
|
204
307
|
// Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
|
|
205
|
-
const slot = proposal.payload.header.
|
|
308
|
+
const slot = proposal.payload.header.slotNumber;
|
|
206
309
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
207
310
|
if (+deadline < this.dateProvider.now()) {
|
|
208
311
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
209
|
-
throw new AttestationTimeoutError(required, slot);
|
|
312
|
+
throw new AttestationTimeoutError(0, required, slot);
|
|
210
313
|
}
|
|
314
|
+
await this.collectOwnAttestations(proposal);
|
|
211
315
|
const proposalId = proposal.archive.toString();
|
|
212
|
-
const
|
|
316
|
+
const myAddresses = this.getValidatorAddresses();
|
|
213
317
|
let attestations = [];
|
|
214
318
|
while(true){
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
319
|
+
// Filter out attestations with a mismatching payload. This should NOT happen since we have verified
|
|
320
|
+
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
321
|
+
const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
322
|
+
if (!attestation.payload.equals(proposal.payload)) {
|
|
323
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
|
|
324
|
+
attestationPayload: attestation.payload,
|
|
325
|
+
proposalPayload: proposal.payload
|
|
326
|
+
});
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
return true;
|
|
330
|
+
});
|
|
331
|
+
// Log new attestations we collected
|
|
332
|
+
const oldSenders = attestations.map((attestation)=>attestation.getSender());
|
|
220
333
|
for (const collected of collectedAttestations){
|
|
221
|
-
const collectedSender =
|
|
222
|
-
|
|
334
|
+
const collectedSender = collected.getSender();
|
|
335
|
+
// Skip attestations with invalid signatures
|
|
336
|
+
if (!collectedSender) {
|
|
337
|
+
this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (!myAddresses.some((address)=>address.equals(collectedSender)) && !oldSenders.some((sender)=>sender?.equals(collectedSender))) {
|
|
223
341
|
this.log.debug(`Received attestation for slot ${slot} from ${collectedSender.toString()}`);
|
|
224
342
|
}
|
|
225
343
|
}
|
|
@@ -230,17 +348,33 @@ import { ValidatorMetrics } from './metrics.js';
|
|
|
230
348
|
}
|
|
231
349
|
if (+deadline < this.dateProvider.now()) {
|
|
232
350
|
this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
|
|
233
|
-
throw new AttestationTimeoutError(required, slot);
|
|
351
|
+
throw new AttestationTimeoutError(attestations.length, required, slot);
|
|
234
352
|
}
|
|
235
|
-
this.log.debug(`Collected ${attestations.length} attestations so far`);
|
|
353
|
+
this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
|
|
236
354
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
237
355
|
}
|
|
238
356
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
}
|
|
244
|
-
|
|
357
|
+
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
358
|
+
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
359
|
+
await this.p2pClient.addAttestations(attestations);
|
|
360
|
+
return attestations;
|
|
361
|
+
}
|
|
362
|
+
async handleAuthRequest(peer, msg) {
|
|
363
|
+
const authRequest = AuthRequest.fromBuffer(msg);
|
|
364
|
+
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
365
|
+
if (statusMessage === undefined) {
|
|
366
|
+
return Buffer.alloc(0);
|
|
367
|
+
}
|
|
368
|
+
// Find a validator address that is in the set
|
|
369
|
+
const allRegisteredValidators = await this.epochCache.getRegisteredValidators();
|
|
370
|
+
const addressToUse = this.getValidatorAddresses().find((address)=>allRegisteredValidators.find((v)=>v.equals(address)) !== undefined);
|
|
371
|
+
if (addressToUse === undefined) {
|
|
372
|
+
// We don't have a registered address
|
|
373
|
+
return Buffer.alloc(0);
|
|
374
|
+
}
|
|
375
|
+
const payloadToSign = authRequest.getPayloadToSign();
|
|
376
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
|
|
377
|
+
const authResponse = new AuthResponse(statusMessage, signature);
|
|
378
|
+
return authResponse.toBuffer();
|
|
245
379
|
}
|
|
246
380
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/validator-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1-commit.5476d83",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -18,11 +18,9 @@
|
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"start": "node --no-warnings ./dest/bin",
|
|
21
|
-
"build": "yarn clean &&
|
|
22
|
-
"build:dev": "
|
|
21
|
+
"build": "yarn clean && tsgo -b",
|
|
22
|
+
"build:dev": "tsgo -b --watch",
|
|
23
23
|
"clean": "rm -rf ./dest .tsbuildinfo",
|
|
24
|
-
"formatting": "run -T prettier --check ./src && run -T eslint ./src",
|
|
25
|
-
"formatting:fix": "run -T eslint --fix ./src && run -T prettier -w ./src",
|
|
26
24
|
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
|
|
27
25
|
},
|
|
28
26
|
"inherits": [
|
|
@@ -59,28 +57,36 @@
|
|
|
59
57
|
"testTimeout": 120000,
|
|
60
58
|
"setupFiles": [
|
|
61
59
|
"../../foundation/src/jest/setup.mjs"
|
|
60
|
+
],
|
|
61
|
+
"testEnvironment": "../../foundation/src/jest/env.mjs",
|
|
62
|
+
"setupFilesAfterEnv": [
|
|
63
|
+
"../../foundation/src/jest/setupAfterEnv.mjs"
|
|
62
64
|
]
|
|
63
65
|
},
|
|
64
66
|
"dependencies": {
|
|
65
|
-
"@aztec/
|
|
66
|
-
"@aztec/
|
|
67
|
-
"@aztec/
|
|
68
|
-
"@aztec/
|
|
69
|
-
"@aztec/
|
|
70
|
-
"@aztec/
|
|
71
|
-
"
|
|
72
|
-
"
|
|
67
|
+
"@aztec/constants": "0.0.1-commit.5476d83",
|
|
68
|
+
"@aztec/epoch-cache": "0.0.1-commit.5476d83",
|
|
69
|
+
"@aztec/ethereum": "0.0.1-commit.5476d83",
|
|
70
|
+
"@aztec/foundation": "0.0.1-commit.5476d83",
|
|
71
|
+
"@aztec/node-keystore": "0.0.1-commit.5476d83",
|
|
72
|
+
"@aztec/p2p": "0.0.1-commit.5476d83",
|
|
73
|
+
"@aztec/slasher": "0.0.1-commit.5476d83",
|
|
74
|
+
"@aztec/stdlib": "0.0.1-commit.5476d83",
|
|
75
|
+
"@aztec/telemetry-client": "0.0.1-commit.5476d83",
|
|
76
|
+
"koa": "^2.16.1",
|
|
77
|
+
"koa-router": "^13.1.1",
|
|
73
78
|
"tslib": "^2.4.0",
|
|
74
|
-
"viem": "2.
|
|
79
|
+
"viem": "npm:@aztec/viem@2.38.2"
|
|
75
80
|
},
|
|
76
81
|
"devDependencies": {
|
|
77
|
-
"@jest/globals": "^
|
|
78
|
-
"@types/jest": "^
|
|
79
|
-
"@types/node": "^
|
|
80
|
-
"
|
|
81
|
-
"jest
|
|
82
|
+
"@jest/globals": "^30.0.0",
|
|
83
|
+
"@types/jest": "^30.0.0",
|
|
84
|
+
"@types/node": "^22.15.17",
|
|
85
|
+
"@typescript/native-preview": "7.0.0-dev.20251126.1",
|
|
86
|
+
"jest": "^30.0.0",
|
|
87
|
+
"jest-mock-extended": "^4.0.0",
|
|
82
88
|
"ts-node": "^10.9.1",
|
|
83
|
-
"typescript": "^5.
|
|
89
|
+
"typescript": "^5.3.3"
|
|
84
90
|
},
|
|
85
91
|
"files": [
|
|
86
92
|
"dest",
|
|
@@ -89,6 +95,6 @@
|
|
|
89
95
|
],
|
|
90
96
|
"types": "./dest/index.d.ts",
|
|
91
97
|
"engines": {
|
|
92
|
-
"node": ">=
|
|
98
|
+
"node": ">=20.10"
|
|
93
99
|
}
|
|
94
100
|
}
|