/
validator.ts
282 lines (251 loc) Β· 10.2 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
import {IDatabaseApiOptions} from "@lodestar/db";
import {BLSPubkey, ssz} from "@lodestar/types";
import {createIBeaconConfig, IBeaconConfig} from "@lodestar/config";
import {Genesis} from "@lodestar/types/phase0";
import {ILogger} from "@lodestar/utils";
import {getClient, Api} 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} from "./services/validatorStore.js";
import {ProcessShutdownCallback, PubkeyHex} from "./types.js";
import {Metrics} from "./metrics.js";
import {MetaDataRepository} from "./repositories/metaDataRepository.js";
import {DoppelgangerService} from "./services/doppelgangerService.js";
export const defaultOptions = {
defaultFeeRecipient: "0x0000000000000000000000000000000000000000",
defaultGasLimit: 30_000_000,
};
export type ValidatorOptions = {
slashingProtection: ISlashingProtection;
dbOps: IDatabaseApiOptions;
api: Api | string;
signers: Signer[];
logger: ILogger;
processShutdownCallback: ProcessShutdownCallback;
afterBlockDelaySlotFraction?: number;
graffiti?: string;
defaultFeeRecipient?: string;
strictFeeRecipientCheck?: boolean;
doppelgangerProtectionEnabled?: boolean;
closed?: boolean;
gasLimit?: number;
builder: {enabled?: 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: IBeaconConfig;
private readonly api: Api;
private readonly clock: IClock;
private readonly chainHeaderTracker: ChainHeaderTracker;
private readonly logger: ILogger;
private state: Status;
private readonly controller: AbortController;
constructor(opts: ValidatorOptions, readonly genesis: Genesis, metrics: Metrics | null = null) {
const {
dbOps,
logger,
slashingProtection,
signers,
graffiti,
defaultFeeRecipient,
strictFeeRecipientCheck,
gasLimit,
builder,
} = opts;
const config = createIBeaconConfig(dbOps.config, genesis.genesisValidatorsRoot);
this.controller = new AbortController();
const clock = new Clock(config, logger, {genesisTime: Number(genesis.genesisTime)});
const loggerVc = getLoggerVc(logger, clock);
const api =
typeof opts.api === "string"
? getClient(
{
baseUrl: opts.api,
// Validator would need the beacon to respond within the slot
timeoutMs: config.SECONDS_PER_SLOT * 1000,
getAbortSignal: () => this.controller.signal,
},
{config, logger, metrics: metrics?.restApiClient}
)
: 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,
defaultFeeRecipient ?? defaultOptions.defaultFeeRecipient,
gasLimit ?? defaultOptions.defaultGasLimit
);
pollPrepareBeaconProposer(config, loggerVc, api, clock, validatorStore, metrics);
if (builder.enabled) {
pollBuilderValidatorRegistration(config, loggerVc, api, clock, validatorStore, metrics);
}
const emitter = new ValidatorEventEmitter();
const chainHeaderTracker = new ChainHeaderTracker(logger, api, emitter);
this.blockProposingService = new BlockProposingService(config, loggerVc, api, clock, validatorStore, metrics, {
graffiti,
strictFeeRecipientCheck,
builder,
});
this.attestationService = new AttestationService(
loggerVc,
api,
clock,
validatorStore,
emitter,
chainHeaderTracker,
metrics,
{afterBlockDelaySlotFraction: opts.afterBlockDelaySlotFraction}
);
this.syncCommitteeService = new SyncCommitteeService(
config,
loggerVc,
api,
clock,
validatorStore,
chainHeaderTracker,
metrics
);
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);
}
}
get isRunning(): boolean {
return this.state === Status.running;
}
/** Waits for genesis and genesis time */
static async initializeFromBeaconNode(
opts: ValidatorOptions,
signal?: AbortSignal,
metrics?: Metrics | null
): Promise<Validator> {
const {config} = opts.dbOps;
const {logger} = opts;
const api =
typeof opts.api === "string"
? // 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
getClient({baseUrl: opts.api, getAbortSignal: () => signal}, {config, logger})
: opts.api;
const genesis = await waitForGenesis(api, opts.logger, signal);
logger.info("Genesis available");
const {data: externalSpecJson} = await api.config.getSpec();
assertEqualParams(config, externalSpecJson);
logger.info("Verified node and validator have same config");
await assertEqualGenesis(opts, genesis);
logger.info("Verified node and validator have 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 {data: stateValidators} = await this.api.beacon.getStateValidators("head", {id: [publicKey]});
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);
await this.api.beacon.submitPoolVoluntaryExit(signedVoluntaryExit);
this.logger.info(`Submitted voluntary exit for ${publicKey} to the network`);
}
}
/** 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);
}
}