@aztec/validator-client 0.0.0-test.1 → 0.0.1-commit.b655e406
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 +286 -0
- package/dest/config.d.ts +2 -13
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +31 -7
- package/dest/duties/validation_service.d.ts +16 -8
- package/dest/duties/validation_service.d.ts.map +1 -1
- package/dest/duties/validation_service.js +35 -11
- package/dest/factory.d.ts +21 -4
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +13 -6
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- package/dest/key_store/index.d.ts +2 -0
- 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 +54 -5
- 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 +40 -10
- 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 +67 -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 +11 -4
- package/dest/metrics.d.ts.map +1 -1
- package/dest/metrics.js +52 -15
- package/dest/validator.d.ts +48 -61
- package/dest/validator.d.ts.map +1 -1
- package/dest/validator.js +262 -165
- package/package.json +25 -19
- package/src/block_proposal_handler.ts +343 -0
- package/src/config.ts +42 -22
- package/src/duties/validation_service.ts +69 -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 +381 -233
- 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,146 @@
|
|
|
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
|
log;
|
|
32
|
+
tracer;
|
|
21
33
|
validationService;
|
|
22
34
|
metrics;
|
|
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.log = log, this.hasRegisteredHandlers = false, this.proposersOfInvalidBlocks = new Set();
|
|
44
|
+
this.tracer = telemetry.getTracer('Validator');
|
|
32
45
|
this.metrics = new ValidatorMetrics(telemetry);
|
|
33
|
-
this.validationService = new ValidationService(keyStore);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
this.validationService = new ValidationService(keyStore, log.createChild('validation-service'));
|
|
47
|
+
// Refresh epoch cache every second to trigger alert if participation in committee changes
|
|
48
|
+
this.epochCacheUpdateLoop = new RunningPromise(this.handleEpochCommitteeUpdate.bind(this), log, 1000);
|
|
49
|
+
const myAddresses = this.getValidatorAddresses();
|
|
50
|
+
this.log.verbose(`Initialized validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
51
|
+
}
|
|
52
|
+
static validateKeyStoreConfiguration(keyStoreManager, logger) {
|
|
53
|
+
const validatorKeyStore = NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager);
|
|
54
|
+
const validatorAddresses = validatorKeyStore.getAddresses();
|
|
55
|
+
// Verify that we can retrieve all required data from the key store
|
|
56
|
+
for (const address of validatorAddresses){
|
|
57
|
+
// Functions throw if required data is not available
|
|
58
|
+
let coinbase;
|
|
59
|
+
let feeRecipient;
|
|
60
|
+
try {
|
|
61
|
+
coinbase = validatorKeyStore.getCoinbaseAddress(address);
|
|
62
|
+
feeRecipient = validatorKeyStore.getFeeRecipient(address);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
throw new Error(`Failed to retrieve required data for validator address ${address}, error: ${error}`);
|
|
44
65
|
}
|
|
45
|
-
|
|
46
|
-
|
|
66
|
+
const publisherAddresses = validatorKeyStore.getPublisherAddresses(address);
|
|
67
|
+
if (!publisherAddresses.length) {
|
|
68
|
+
throw new Error(`No publisher addresses found for validator address ${address}`);
|
|
69
|
+
}
|
|
70
|
+
logger?.debug(`Validator ${address.toString()} configured with coinbase ${coinbase.toString()}, feeRecipient ${feeRecipient.toString()} and publishers ${publisherAddresses.map((x)=>x.toString()).join()}`);
|
|
71
|
+
}
|
|
47
72
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
73
|
+
async handleEpochCommitteeUpdate() {
|
|
74
|
+
try {
|
|
75
|
+
const { committee, epoch } = await this.epochCache.getCommittee('next');
|
|
76
|
+
if (!committee) {
|
|
77
|
+
this.log.trace(`No committee found for slot`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (epoch !== this.lastEpochForCommitteeUpdateLoop) {
|
|
81
|
+
const me = this.getValidatorAddresses();
|
|
82
|
+
const committeeSet = new Set(committee.map((v)=>v.toString()));
|
|
83
|
+
const inCommittee = me.filter((a)=>committeeSet.has(a.toString()));
|
|
84
|
+
if (inCommittee.length > 0) {
|
|
85
|
+
this.log.info(`Validators ${inCommittee.map((a)=>a.toString()).join(',')} are on the validator committee for epoch ${epoch}`);
|
|
86
|
+
} else {
|
|
87
|
+
this.log.verbose(`Validators ${me.map((a)=>a.toString()).join(', ')} are not on the validator committee for epoch ${epoch}`);
|
|
88
|
+
}
|
|
89
|
+
this.lastEpochForCommitteeUpdateLoop = epoch;
|
|
90
|
+
}
|
|
91
|
+
} catch (err) {
|
|
92
|
+
this.log.error(`Error updating epoch committee`, err);
|
|
51
93
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
|
|
94
|
+
}
|
|
95
|
+
static new(config, blockBuilder, epochCache, p2pClient, blockSource, l1ToL2MessageSource, txProvider, keyStoreManager, dateProvider = new DateProvider(), telemetry = getTelemetryClient()) {
|
|
96
|
+
const metrics = new ValidatorMetrics(telemetry);
|
|
97
|
+
const blockProposalValidator = new BlockProposalValidator(epochCache, {
|
|
98
|
+
txsPermitted: !config.disableTransactions
|
|
99
|
+
});
|
|
100
|
+
const blockProposalHandler = new BlockProposalHandler(blockBuilder, blockSource, l1ToL2MessageSource, txProvider, blockProposalValidator, config, metrics, dateProvider, telemetry);
|
|
101
|
+
const validator = new ValidatorClient(NodeKeystoreAdapter.fromKeyStoreManager(keyStoreManager), epochCache, p2pClient, blockProposalHandler, config, dateProvider, telemetry);
|
|
56
102
|
return validator;
|
|
57
103
|
}
|
|
104
|
+
getValidatorAddresses() {
|
|
105
|
+
return this.keyStore.getAddresses().filter((addr)=>!this.config.disabledValidators.some((disabled)=>disabled.equals(addr)));
|
|
106
|
+
}
|
|
107
|
+
getBlockProposalHandler() {
|
|
108
|
+
return this.blockProposalHandler;
|
|
109
|
+
}
|
|
110
|
+
// Proxy method for backwards compatibility with tests
|
|
111
|
+
reExecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages) {
|
|
112
|
+
return this.blockProposalHandler.reexecuteTransactions(proposal, blockNumber, txs, l1ToL2Messages);
|
|
113
|
+
}
|
|
114
|
+
signWithAddress(addr, msg) {
|
|
115
|
+
return this.keyStore.signTypedDataWithAddress(addr, msg);
|
|
116
|
+
}
|
|
117
|
+
getCoinbaseForAttestor(attestor) {
|
|
118
|
+
return this.keyStore.getCoinbaseAddress(attestor);
|
|
119
|
+
}
|
|
120
|
+
getFeeRecipientForAttestor(attestor) {
|
|
121
|
+
return this.keyStore.getFeeRecipient(attestor);
|
|
122
|
+
}
|
|
123
|
+
getConfig() {
|
|
124
|
+
return this.config;
|
|
125
|
+
}
|
|
126
|
+
updateConfig(config) {
|
|
127
|
+
this.config = {
|
|
128
|
+
...this.config,
|
|
129
|
+
...config
|
|
130
|
+
};
|
|
131
|
+
}
|
|
58
132
|
async start() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
133
|
+
if (this.epochCacheUpdateLoop.isRunning()) {
|
|
134
|
+
this.log.warn(`Validator client already started`);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
await this.registerHandlers();
|
|
138
|
+
const myAddresses = this.getValidatorAddresses();
|
|
139
|
+
const inCommittee = await this.epochCache.filterInCommittee('now', myAddresses);
|
|
140
|
+
if (inCommittee.length > 0) {
|
|
141
|
+
this.log.info(`Started validator with addresses in current validator committee: ${inCommittee.map((a)=>a.toString()).join(', ')}`);
|
|
65
142
|
} else {
|
|
66
|
-
this.log.info(`Started validator with
|
|
143
|
+
this.log.info(`Started validator with addresses: ${myAddresses.map((a)=>a.toString()).join(', ')}`);
|
|
67
144
|
}
|
|
68
145
|
this.epochCacheUpdateLoop.start();
|
|
69
146
|
return Promise.resolve();
|
|
@@ -71,155 +148,159 @@ import { ValidatorMetrics } from './metrics.js';
|
|
|
71
148
|
async stop() {
|
|
72
149
|
await this.epochCacheUpdateLoop.stop();
|
|
73
150
|
}
|
|
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;
|
|
151
|
+
/** Register handlers on the p2p client */ async registerHandlers() {
|
|
152
|
+
if (!this.hasRegisteredHandlers) {
|
|
153
|
+
this.hasRegisteredHandlers = true;
|
|
154
|
+
this.log.debug(`Registering validator handlers for p2p client`);
|
|
155
|
+
const handler = (block, proposalSender)=>this.attestToProposal(block, proposalSender);
|
|
156
|
+
this.p2pClient.registerBlockProposalHandler(handler);
|
|
157
|
+
const myAddresses = this.getValidatorAddresses();
|
|
158
|
+
this.p2pClient.registerThisValidatorAddresses(myAddresses);
|
|
159
|
+
await this.p2pClient.addReqRespSubProtocol(ReqRespSubProtocol.AUTH, this.handleAuthRequest.bind(this));
|
|
101
160
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
161
|
+
}
|
|
162
|
+
async attestToProposal(proposal, proposalSender) {
|
|
163
|
+
const slotNumber = proposal.slotNumber.toBigInt();
|
|
164
|
+
const proposer = proposal.getSender();
|
|
165
|
+
// Reject proposals with invalid signatures
|
|
166
|
+
if (!proposer) {
|
|
167
|
+
this.log.warn(`Received proposal with invalid signature for slot ${slotNumber}`);
|
|
106
168
|
return undefined;
|
|
107
169
|
}
|
|
108
|
-
// Check that
|
|
109
|
-
this.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
170
|
+
// Check that I have any address in current committee before attesting
|
|
171
|
+
const inCommittee = await this.epochCache.filterInCommittee(slotNumber, this.getValidatorAddresses());
|
|
172
|
+
const partOfCommittee = inCommittee.length > 0;
|
|
173
|
+
const proposalInfo = {
|
|
174
|
+
...proposal.toBlockInfo(),
|
|
175
|
+
proposer: proposer.toString()
|
|
176
|
+
};
|
|
177
|
+
this.log.info(`Received proposal for slot ${slotNumber}`, {
|
|
178
|
+
...proposalInfo,
|
|
179
|
+
txHashes: proposal.txHashes.map((t)=>t.toString())
|
|
180
|
+
});
|
|
181
|
+
// Reexecute txs if we are part of the committee so we can attest, or if slashing is enabled so we can slash
|
|
182
|
+
// invalid proposals even when not in the committee, or if we are configured to always reexecute for monitoring purposes.
|
|
183
|
+
const { validatorReexecute, slashBroadcastedInvalidBlockPenalty, alwaysReexecuteBlockProposals } = this.config;
|
|
184
|
+
const shouldReexecute = slashBroadcastedInvalidBlockPenalty > 0n && validatorReexecute || partOfCommittee && validatorReexecute || alwaysReexecuteBlockProposals;
|
|
185
|
+
const validationResult = await this.blockProposalHandler.handleBlockProposal(proposal, proposalSender, !!shouldReexecute);
|
|
186
|
+
if (!validationResult.isValid) {
|
|
187
|
+
this.log.warn(`Proposal validation failed: ${validationResult.reason}`, proposalInfo);
|
|
188
|
+
const reason = validationResult.reason || 'unknown';
|
|
189
|
+
// Classify failure reason: bad proposal vs node issue
|
|
190
|
+
const badProposalReasons = [
|
|
191
|
+
'invalid_proposal',
|
|
192
|
+
'state_mismatch',
|
|
193
|
+
'failed_txs',
|
|
194
|
+
'in_hash_mismatch',
|
|
195
|
+
'parent_block_wrong_slot'
|
|
196
|
+
];
|
|
197
|
+
if (badProposalReasons.includes(reason)) {
|
|
198
|
+
this.metrics.incFailedAttestationsBadProposal(1, reason, partOfCommittee);
|
|
120
199
|
} else {
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
200
|
+
// Node issues so we can't attest
|
|
201
|
+
this.metrics.incFailedAttestationsNodeIssue(1, reason, partOfCommittee);
|
|
202
|
+
}
|
|
203
|
+
// Slash invalid block proposals (can happen even when not in committee)
|
|
204
|
+
if (validationResult.reason && SLASHABLE_BLOCK_PROPOSAL_VALIDATION_RESULT.includes(validationResult.reason) && slashBroadcastedInvalidBlockPenalty > 0n) {
|
|
205
|
+
this.log.warn(`Slashing proposer for invalid block proposal`, proposalInfo);
|
|
206
|
+
this.slashInvalidBlock(proposal);
|
|
124
207
|
}
|
|
125
208
|
return undefined;
|
|
126
209
|
}
|
|
210
|
+
// Check that I have any address in current committee before attesting
|
|
211
|
+
if (!partOfCommittee) {
|
|
212
|
+
this.log.verbose(`No validator in the current committee, skipping attestation`, proposalInfo);
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
127
215
|
// Provided all of the above checks pass, we can attest to the proposal
|
|
128
216
|
this.log.info(`Attesting to proposal for slot ${slotNumber}`, proposalInfo);
|
|
217
|
+
this.metrics.incSuccessfulAttestations(inCommittee.length);
|
|
129
218
|
// If the above function does not throw an error, then we can attest to the proposal
|
|
130
|
-
return this.
|
|
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
|
|
150
|
-
});
|
|
151
|
-
stopTimer();
|
|
152
|
-
this.log.verbose(`Transaction re-execution complete`);
|
|
153
|
-
if (numFailedTxs > 0) {
|
|
154
|
-
await this.metrics.recordFailedReexecution(proposal);
|
|
155
|
-
throw new ReExFailedTxsError(numFailedTxs);
|
|
156
|
-
}
|
|
157
|
-
if (block.body.txEffects.length !== txHashes.length) {
|
|
158
|
-
await this.metrics.recordFailedReexecution(proposal);
|
|
159
|
-
throw new ReExTimeoutError();
|
|
160
|
-
}
|
|
161
|
-
// This function will throw an error if state updates do not match
|
|
162
|
-
if (!block.archive.root.equals(proposal.archive)) {
|
|
163
|
-
await this.metrics.recordFailedReexecution(proposal);
|
|
164
|
-
throw new ReExStateMismatchError();
|
|
165
|
-
}
|
|
219
|
+
return this.createBlockAttestationsFromProposal(proposal, inCommittee);
|
|
166
220
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
* @param proposal - The proposal to attest to
|
|
174
|
-
*/ async ensureTransactionsAreAvailable(proposal) {
|
|
175
|
-
const txHashes = proposal.payload.txHashes;
|
|
176
|
-
const transactionStatuses = await Promise.all(txHashes.map((txHash)=>this.p2pClient.getTxStatus(txHash)));
|
|
177
|
-
const missingTxs = txHashes.filter((_, index)=>![
|
|
178
|
-
'pending',
|
|
179
|
-
'mined'
|
|
180
|
-
].includes(transactionStatuses[index] ?? ''));
|
|
181
|
-
if (missingTxs.length === 0) {
|
|
182
|
-
return; // All transactions are available
|
|
221
|
+
slashInvalidBlock(proposal) {
|
|
222
|
+
const proposer = proposal.getSender();
|
|
223
|
+
// Skip if signature is invalid (shouldn't happen since we validate earlier)
|
|
224
|
+
if (!proposer) {
|
|
225
|
+
this.log.warn(`Cannot slash proposal with invalid signature`);
|
|
226
|
+
return;
|
|
183
227
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
228
|
+
// Trim the set if it's too big.
|
|
229
|
+
if (this.proposersOfInvalidBlocks.size > MAX_PROPOSERS_OF_INVALID_BLOCKS) {
|
|
230
|
+
// remove oldest proposer. `values` is guaranteed to be in insertion order.
|
|
231
|
+
this.proposersOfInvalidBlocks.delete(this.proposersOfInvalidBlocks.values().next().value);
|
|
188
232
|
}
|
|
233
|
+
this.proposersOfInvalidBlocks.add(proposer.toString());
|
|
234
|
+
this.emit(WANT_TO_SLASH_EVENT, [
|
|
235
|
+
{
|
|
236
|
+
validator: proposer,
|
|
237
|
+
amount: this.config.slashBroadcastedInvalidBlockPenalty,
|
|
238
|
+
offenseType: OffenseType.BROADCASTED_INVALID_BLOCK_PROPOSAL,
|
|
239
|
+
epochOrSlot: proposal.slotNumber.toBigInt()
|
|
240
|
+
}
|
|
241
|
+
]);
|
|
189
242
|
}
|
|
190
|
-
async createBlockProposal(header, archive, txs) {
|
|
191
|
-
if (this.previousProposal?.slotNumber.equals(header.
|
|
243
|
+
async createBlockProposal(blockNumber, header, archive, stateReference, txs, proposerAddress, options) {
|
|
244
|
+
if (this.previousProposal?.slotNumber.equals(header.slotNumber)) {
|
|
192
245
|
this.log.verbose(`Already made a proposal for the same slot, skipping proposal`);
|
|
193
246
|
return Promise.resolve(undefined);
|
|
194
247
|
}
|
|
195
|
-
const newProposal = await this.validationService.createBlockProposal(header, archive, txs
|
|
248
|
+
const newProposal = await this.validationService.createBlockProposal(header, archive, stateReference, txs, proposerAddress, {
|
|
249
|
+
...options,
|
|
250
|
+
broadcastInvalidBlockProposal: this.config.broadcastInvalidBlockProposal
|
|
251
|
+
});
|
|
196
252
|
this.previousProposal = newProposal;
|
|
197
253
|
return newProposal;
|
|
198
254
|
}
|
|
199
|
-
broadcastBlockProposal(proposal) {
|
|
200
|
-
this.p2pClient.broadcastProposal(proposal);
|
|
255
|
+
async broadcastBlockProposal(proposal) {
|
|
256
|
+
await this.p2pClient.broadcastProposal(proposal);
|
|
257
|
+
}
|
|
258
|
+
async signAttestationsAndSigners(attestationsAndSigners, proposer) {
|
|
259
|
+
return await this.validationService.signAttestationsAndSigners(attestationsAndSigners, proposer);
|
|
260
|
+
}
|
|
261
|
+
async collectOwnAttestations(proposal) {
|
|
262
|
+
const slot = proposal.payload.header.slotNumber.toBigInt();
|
|
263
|
+
const inCommittee = await this.epochCache.filterInCommittee(slot, this.getValidatorAddresses());
|
|
264
|
+
this.log.debug(`Collecting ${inCommittee.length} self-attestations for slot ${slot}`, {
|
|
265
|
+
inCommittee
|
|
266
|
+
});
|
|
267
|
+
return this.createBlockAttestationsFromProposal(proposal, inCommittee);
|
|
201
268
|
}
|
|
202
|
-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/7962)
|
|
203
269
|
async collectAttestations(proposal, required, deadline) {
|
|
204
270
|
// Wait and poll the p2pClient's attestation pool for this block until we have enough attestations
|
|
205
|
-
const slot = proposal.payload.header.
|
|
271
|
+
const slot = proposal.payload.header.slotNumber.toBigInt();
|
|
206
272
|
this.log.debug(`Collecting ${required} attestations for slot ${slot} with deadline ${deadline.toISOString()}`);
|
|
207
273
|
if (+deadline < this.dateProvider.now()) {
|
|
208
274
|
this.log.error(`Deadline ${deadline.toISOString()} for collecting ${required} attestations for slot ${slot} is in the past`);
|
|
209
|
-
throw new AttestationTimeoutError(required, slot);
|
|
275
|
+
throw new AttestationTimeoutError(0, required, slot);
|
|
210
276
|
}
|
|
277
|
+
await this.collectOwnAttestations(proposal);
|
|
211
278
|
const proposalId = proposal.archive.toString();
|
|
212
|
-
const
|
|
279
|
+
const myAddresses = this.getValidatorAddresses();
|
|
213
280
|
let attestations = [];
|
|
214
281
|
while(true){
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
282
|
+
// Filter out attestations with a mismatching payload. This should NOT happen since we have verified
|
|
283
|
+
// the proposer signature (ie our own) before accepting the attestation into the pool via the p2p client.
|
|
284
|
+
const collectedAttestations = (await this.p2pClient.getAttestationsForSlot(slot, proposalId)).filter((attestation)=>{
|
|
285
|
+
if (!attestation.payload.equals(proposal.payload)) {
|
|
286
|
+
this.log.warn(`Received attestation for slot ${slot} with mismatched payload from ${attestation.getSender()?.toString()}`, {
|
|
287
|
+
attestationPayload: attestation.payload,
|
|
288
|
+
proposalPayload: proposal.payload
|
|
289
|
+
});
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
});
|
|
294
|
+
// Log new attestations we collected
|
|
295
|
+
const oldSenders = attestations.map((attestation)=>attestation.getSender());
|
|
220
296
|
for (const collected of collectedAttestations){
|
|
221
|
-
const collectedSender =
|
|
222
|
-
|
|
297
|
+
const collectedSender = collected.getSender();
|
|
298
|
+
// Skip attestations with invalid signatures
|
|
299
|
+
if (!collectedSender) {
|
|
300
|
+
this.log.warn(`Skipping attestation with invalid signature for slot ${slot}`);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (!myAddresses.some((address)=>address.equals(collectedSender)) && !oldSenders.some((sender)=>sender?.equals(collectedSender))) {
|
|
223
304
|
this.log.debug(`Received attestation for slot ${slot} from ${collectedSender.toString()}`);
|
|
224
305
|
}
|
|
225
306
|
}
|
|
@@ -230,17 +311,33 @@ import { ValidatorMetrics } from './metrics.js';
|
|
|
230
311
|
}
|
|
231
312
|
if (+deadline < this.dateProvider.now()) {
|
|
232
313
|
this.log.error(`Timeout ${deadline.toISOString()} waiting for ${required} attestations for slot ${slot}`);
|
|
233
|
-
throw new AttestationTimeoutError(required, slot);
|
|
314
|
+
throw new AttestationTimeoutError(attestations.length, required, slot);
|
|
234
315
|
}
|
|
235
|
-
this.log.debug(`Collected ${attestations.length} attestations so far`);
|
|
316
|
+
this.log.debug(`Collected ${attestations.length} of ${required} attestations so far`);
|
|
236
317
|
await sleep(this.config.attestationPollingIntervalMs);
|
|
237
318
|
}
|
|
238
319
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
}
|
|
244
|
-
|
|
320
|
+
async createBlockAttestationsFromProposal(proposal, attestors = []) {
|
|
321
|
+
const attestations = await this.validationService.attestToProposal(proposal, attestors);
|
|
322
|
+
await this.p2pClient.addAttestations(attestations);
|
|
323
|
+
return attestations;
|
|
324
|
+
}
|
|
325
|
+
async handleAuthRequest(peer, msg) {
|
|
326
|
+
const authRequest = AuthRequest.fromBuffer(msg);
|
|
327
|
+
const statusMessage = await this.p2pClient.handleAuthRequestFromPeer(authRequest, peer).catch((_)=>undefined);
|
|
328
|
+
if (statusMessage === undefined) {
|
|
329
|
+
return Buffer.alloc(0);
|
|
330
|
+
}
|
|
331
|
+
// Find a validator address that is in the set
|
|
332
|
+
const allRegisteredValidators = await this.epochCache.getRegisteredValidators();
|
|
333
|
+
const addressToUse = this.getValidatorAddresses().find((address)=>allRegisteredValidators.find((v)=>v.equals(address)) !== undefined);
|
|
334
|
+
if (addressToUse === undefined) {
|
|
335
|
+
// We don't have a registered address
|
|
336
|
+
return Buffer.alloc(0);
|
|
337
|
+
}
|
|
338
|
+
const payloadToSign = authRequest.getPayloadToSign();
|
|
339
|
+
const signature = await this.keyStore.signMessageWithAddress(addressToUse, payloadToSign);
|
|
340
|
+
const authResponse = new AuthResponse(statusMessage, signature);
|
|
341
|
+
return authResponse.toBuffer();
|
|
245
342
|
}
|
|
246
343
|
}
|
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.b655e406",
|
|
4
4
|
"main": "dest/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -21,8 +21,6 @@
|
|
|
21
21
|
"build": "yarn clean && tsc -b",
|
|
22
22
|
"build:dev": "tsc -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.b655e406",
|
|
68
|
+
"@aztec/epoch-cache": "0.0.1-commit.b655e406",
|
|
69
|
+
"@aztec/ethereum": "0.0.1-commit.b655e406",
|
|
70
|
+
"@aztec/foundation": "0.0.1-commit.b655e406",
|
|
71
|
+
"@aztec/node-keystore": "0.0.1-commit.b655e406",
|
|
72
|
+
"@aztec/p2p": "0.0.1-commit.b655e406",
|
|
73
|
+
"@aztec/prover-client": "0.0.1-commit.b655e406",
|
|
74
|
+
"@aztec/slasher": "0.0.1-commit.b655e406",
|
|
75
|
+
"@aztec/stdlib": "0.0.1-commit.b655e406",
|
|
76
|
+
"@aztec/telemetry-client": "0.0.1-commit.b655e406",
|
|
77
|
+
"koa": "^2.16.1",
|
|
78
|
+
"koa-router": "^13.1.1",
|
|
73
79
|
"tslib": "^2.4.0",
|
|
74
|
-
"viem": "2.
|
|
80
|
+
"viem": "npm:@spalladino/viem@2.38.2-eip7594.0"
|
|
75
81
|
},
|
|
76
82
|
"devDependencies": {
|
|
77
|
-
"@jest/globals": "^
|
|
78
|
-
"@types/jest": "^
|
|
79
|
-
"@types/node": "^
|
|
80
|
-
"jest": "^
|
|
81
|
-
"jest-mock-extended": "^
|
|
83
|
+
"@jest/globals": "^30.0.0",
|
|
84
|
+
"@types/jest": "^30.0.0",
|
|
85
|
+
"@types/node": "^22.15.17",
|
|
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
|
}
|