-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
validator.ts
309 lines (273 loc) Β· 12.1 KB
/
validator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import {DatabaseApiOptions} from "@lodestar/db";
import {BLSPubkey, ssz} from "@lodestar/types";
import {createBeaconConfig, BeaconConfig} from "@lodestar/config";
import {Genesis} from "@lodestar/types/phase0";
import {Logger} from "@lodestar/utils";
import {getClient, Api, routes, ApiError} from "@lodestar/api";
import {toHexString} from "@chainsafe/ssz";
import {computeEpochAtSlot, getCurrentSlot} from "@lodestar/state-transition";
import {Clock, IClock} from "./util/clock.js";
import {waitForGenesis} from "./genesis.js";
import {BlockProposingService} from "./services/block.js";
import {AttestationService} from "./services/attestation.js";
import {IndicesService} from "./services/indices.js";
import {SyncCommitteeService} from "./services/syncCommittee.js";
import {pollPrepareBeaconProposer, pollBuilderValidatorRegistration} from "./services/prepareBeaconProposer.js";
import {Interchange, InterchangeFormatVersion, ISlashingProtection} from "./slashingProtection/index.js";
import {assertEqualParams, getLoggerVc, NotEqualParamsError} from "./util/index.js";
import {ChainHeaderTracker} from "./services/chainHeaderTracker.js";
import {ValidatorEventEmitter} from "./services/emitter.js";
import {ValidatorStore, Signer, ValidatorProposerConfig} from "./services/validatorStore.js";
import {ProcessShutdownCallback, PubkeyHex} from "./types.js";
import {BeaconHealth, Metrics} from "./metrics.js";
import {MetaDataRepository} from "./repositories/metaDataRepository.js";
import {DoppelgangerService} from "./services/doppelgangerService.js";
export type ValidatorOptions = {
slashingProtection: ISlashingProtection;
dbOps: DatabaseApiOptions;
api: Api | string | string[];
signers: Signer[];
logger: Logger;
processShutdownCallback: ProcessShutdownCallback;
abortController: AbortController;
afterBlockDelaySlotFraction?: number;
scAfterBlockDelaySlotFraction?: number;
disableAttestationGrouping?: boolean;
doppelgangerProtectionEnabled?: boolean;
closed?: boolean;
valProposerConfig?: ValidatorProposerConfig;
distributed?: boolean;
};
// TODO: Extend the timeout, and let it be customizable
/// The global timeout for HTTP requests to the beacon node.
// const HTTP_TIMEOUT_MS = 12 * 1000;
enum Status {
running,
closed,
}
/**
* Main class for the Validator client.
*/
export class Validator {
readonly validatorStore: ValidatorStore;
private readonly slashingProtection: ISlashingProtection;
private readonly blockProposingService: BlockProposingService;
private readonly attestationService: AttestationService;
private readonly syncCommitteeService: SyncCommitteeService;
private readonly config: BeaconConfig;
private readonly api: Api;
private readonly clock: IClock;
private readonly chainHeaderTracker: ChainHeaderTracker;
private readonly logger: Logger;
private state: Status;
private readonly controller: AbortController;
constructor(opts: ValidatorOptions, readonly genesis: Genesis, metrics: Metrics | null = null) {
const {dbOps, logger, slashingProtection, signers, valProposerConfig} = opts;
const config = createBeaconConfig(dbOps.config, genesis.genesisValidatorsRoot);
this.controller = opts.abortController;
const clock = new Clock(config, logger, {genesisTime: Number(genesis.genesisTime)});
const loggerVc = getLoggerVc(logger, clock);
let api: Api;
if (typeof opts.api === "string" || Array.isArray(opts.api)) {
// This new api instance can make do with default timeout as a faster timeout is
// not necessary since this instance won't be used for validator duties
api = getClient(
{
urls: typeof opts.api === "string" ? [opts.api] : opts.api,
// Validator would need the beacon to respond within the slot
// See https://github.com/ChainSafe/lodestar/issues/5315 for rationale
timeoutMs: config.SECONDS_PER_SLOT * 1000,
getAbortSignal: () => this.controller.signal,
},
{config, logger, metrics: metrics?.restApiClient}
);
} else {
api = opts.api;
}
const indicesService = new IndicesService(logger, api, metrics);
const doppelgangerService = opts.doppelgangerProtectionEnabled
? new DoppelgangerService(logger, clock, api, indicesService, opts.processShutdownCallback, metrics)
: null;
const validatorStore = new ValidatorStore(
config,
slashingProtection,
indicesService,
doppelgangerService,
metrics,
signers,
valProposerConfig,
genesis.genesisValidatorsRoot
);
pollPrepareBeaconProposer(config, loggerVc, api, clock, validatorStore, metrics);
pollBuilderValidatorRegistration(config, loggerVc, api, clock, validatorStore, metrics);
const emitter = new ValidatorEventEmitter();
// Validator event emitter can have more than 10 listeners in a normal course of operation
// We set infinity to prevent MaxListenersExceededWarning which get logged when listeners > 10
emitter.setMaxListeners(Infinity);
const chainHeaderTracker = new ChainHeaderTracker(logger, api, emitter);
this.blockProposingService = new BlockProposingService(config, loggerVc, api, clock, validatorStore, metrics);
this.attestationService = new AttestationService(
loggerVc,
api,
clock,
validatorStore,
emitter,
chainHeaderTracker,
metrics,
{
afterBlockDelaySlotFraction: opts.afterBlockDelaySlotFraction,
disableAttestationGrouping: opts.disableAttestationGrouping || opts.distributed,
distributedAggregationSelection: opts.distributed,
}
);
this.syncCommitteeService = new SyncCommitteeService(
config,
loggerVc,
api,
clock,
validatorStore,
emitter,
chainHeaderTracker,
metrics,
{
scAfterBlockDelaySlotFraction: opts.scAfterBlockDelaySlotFraction,
distributedAggregationSelection: opts.distributed,
}
);
this.config = config;
this.logger = logger;
this.api = api;
this.clock = clock;
this.validatorStore = validatorStore;
this.chainHeaderTracker = chainHeaderTracker;
this.slashingProtection = slashingProtection;
if (metrics) {
opts.dbOps.controller.setMetrics(metrics.db);
}
if (opts.closed) {
this.state = Status.closed;
} else {
// "start" the validator
// Instantiates block and attestation services and runs them once the chain has been started.
this.state = Status.running;
this.clock.start(this.controller.signal);
this.chainHeaderTracker.start(this.controller.signal);
if (metrics) {
this.clock.runEverySlot(() =>
this.fetchBeaconHealth()
.then((health) => metrics.beaconHealth.set(health))
.catch((e) => this.logger.error("Error on fetchBeaconHealth", {}, e))
);
}
}
}
get isRunning(): boolean {
return this.state === Status.running;
}
/** Waits for genesis and genesis time */
static async initializeFromBeaconNode(opts: ValidatorOptions, metrics?: Metrics | null): Promise<Validator> {
const {config} = opts.dbOps;
const {logger} = opts;
let api: Api;
if (typeof opts.api === "string" || Array.isArray(opts.api)) {
const urls = typeof opts.api === "string" ? [opts.api] : opts.api;
// This new api instance can make do with default timeout as a faster timeout is
// not necessary since this instance won't be used for validator duties
api = getClient({urls, getAbortSignal: () => opts.abortController.signal}, {config, logger});
} else {
api = opts.api;
}
const genesis = await waitForGenesis(api, opts.logger, opts.abortController.signal);
logger.info("Genesis fetched from the beacon node");
const res = await api.config.getSpec();
ApiError.assert(res, "Can not fetch spec from beacon node");
assertEqualParams(config, res.response.data);
logger.info("Verified connected beacon node and validator have same the config");
await assertEqualGenesis(opts, genesis);
logger.info("Verified connected beacon node and validator have the same genesisValidatorRoot");
return new Validator(opts, genesis, metrics);
}
removeDutiesForKey(pubkey: PubkeyHex): void {
this.blockProposingService.removeDutiesForKey(pubkey);
this.attestationService.removeDutiesForKey(pubkey);
this.syncCommitteeService.removeDutiesForKey(pubkey);
}
/**
* Stops all validator functions.
*/
async close(): Promise<void> {
if (this.state === Status.closed) return;
this.controller.abort();
this.state = Status.closed;
}
async importInterchange(interchange: Interchange): Promise<void> {
return this.slashingProtection.importInterchange(interchange, this.genesis.genesisValidatorsRoot);
}
async exportInterchange(pubkeys: BLSPubkey[], formatVersion: InterchangeFormatVersion): Promise<Interchange> {
return this.slashingProtection.exportInterchange(this.genesis.genesisValidatorsRoot, pubkeys, formatVersion);
}
/**
* Perform a voluntary exit for the given validator by its key.
*/
async voluntaryExit(publicKey: string, exitEpoch?: number): Promise<void> {
const res = await this.api.beacon.getStateValidators("head", {id: [publicKey]});
ApiError.assert(res, "Can not fetch state validators from beacon node");
const stateValidators = res.response.data;
const stateValidator = stateValidators[0];
if (stateValidator === undefined) {
throw new Error(`Validator pubkey ${publicKey} not found in state`);
}
if (exitEpoch === undefined) {
exitEpoch = computeEpochAtSlot(getCurrentSlot(this.config, this.clock.genesisTime));
}
const signedVoluntaryExit = await this.validatorStore.signVoluntaryExit(publicKey, stateValidator.index, exitEpoch);
ApiError.assert(await this.api.beacon.submitPoolVoluntaryExit(signedVoluntaryExit));
this.logger.info(`Submitted voluntary exit for ${publicKey} to the network`);
}
private async fetchBeaconHealth(): Promise<BeaconHealth> {
try {
const {status: healthCode} = await this.api.node.getHealth();
// API always returns http status codes
// Need to find a way to return a custom enum type
if ((healthCode as unknown as routes.node.NodeHealth) === routes.node.NodeHealth.READY) return BeaconHealth.READY;
if ((healthCode as unknown as routes.node.NodeHealth) === routes.node.NodeHealth.SYNCING)
return BeaconHealth.SYNCING;
if ((healthCode as unknown as routes.node.NodeHealth) === routes.node.NodeHealth.NOT_INITIALIZED_OR_ISSUES)
return BeaconHealth.NOT_INITIALIZED_OR_ISSUES;
else return BeaconHealth.UNKNOWN;
} catch (e) {
// TODO: Filter by network error type
return BeaconHealth.ERROR;
}
}
}
/** Assert the same genesisValidatorRoot and genesisTime */
async function assertEqualGenesis(opts: ValidatorOptions, genesis: Genesis): Promise<void> {
const nodeGenesisValidatorRoot = genesis.genesisValidatorsRoot;
const metaDataRepository = new MetaDataRepository(opts.dbOps);
const genesisValidatorsRoot = await metaDataRepository.getGenesisValidatorsRoot();
if (genesisValidatorsRoot) {
if (!ssz.Root.equals(genesisValidatorsRoot, nodeGenesisValidatorRoot)) {
// this happens when the existing validator db served another network before
opts.logger.error("Not the same genesisValidatorRoot", {
expected: toHexString(nodeGenesisValidatorRoot),
actual: toHexString(genesisValidatorsRoot),
});
throw new NotEqualParamsError("Not the same genesisValidatorRoot");
}
} else {
await metaDataRepository.setGenesisValidatorsRoot(nodeGenesisValidatorRoot);
opts.logger.info("Persisted genesisValidatorRoot", toHexString(nodeGenesisValidatorRoot));
}
const nodeGenesisTime = genesis.genesisTime;
const genesisTime = await metaDataRepository.getGenesisTime();
if (genesisTime !== null) {
if (genesisTime !== nodeGenesisTime) {
opts.logger.error("Not the same genesisTime", {expected: nodeGenesisTime, actual: genesisTime});
throw new NotEqualParamsError("Not the same genesisTime");
}
} else {
await metaDataRepository.setGenesisTime(nodeGenesisTime);
opts.logger.info("Persisted genesisTime", nodeGenesisTime);
}
}