@lodestar/validator 1.46.0-dev.ba5ee9aff0 → 1.46.0-dev.c65aaefd91
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/lib/buckets.js +1 -2
- package/lib/buckets.js.map +1 -1
- package/lib/defaults.d.ts.map +1 -1
- package/lib/genesis.js.map +1 -1
- package/lib/metrics.d.ts +2 -2
- package/lib/metrics.d.ts.map +1 -1
- package/lib/metrics.js +2 -4
- package/lib/metrics.js.map +1 -1
- package/lib/repositories/metaDataRepository.js.map +1 -1
- package/lib/services/attestation.d.ts +22 -1
- package/lib/services/attestation.d.ts.map +1 -1
- package/lib/services/attestation.js.map +1 -1
- package/lib/services/attestationDuties.d.ts +22 -1
- package/lib/services/attestationDuties.d.ts.map +1 -1
- package/lib/services/attestationDuties.js +1 -1
- package/lib/services/attestationDuties.js.map +1 -1
- package/lib/services/block.d.ts +18 -1
- package/lib/services/block.d.ts.map +1 -1
- package/lib/services/block.js.map +1 -1
- package/lib/services/blockDuties.d.ts +11 -1
- package/lib/services/blockDuties.d.ts.map +1 -1
- package/lib/services/blockDuties.js.map +1 -1
- package/lib/services/chainHeaderTracker.d.ts +6 -0
- package/lib/services/chainHeaderTracker.d.ts.map +1 -1
- package/lib/services/chainHeaderTracker.js +9 -2
- package/lib/services/chainHeaderTracker.js.map +1 -1
- package/lib/services/doppelgangerService.d.ts +1 -1
- package/lib/services/doppelgangerService.d.ts.map +1 -1
- package/lib/services/doppelgangerService.js +1 -2
- package/lib/services/doppelgangerService.js.map +1 -1
- package/lib/services/emitter.d.ts +1 -1
- package/lib/services/emitter.d.ts.map +1 -1
- package/lib/services/emitter.js +1 -2
- package/lib/services/emitter.js.map +1 -1
- package/lib/services/externalSignerSync.js.map +1 -1
- package/lib/services/indices.d.ts +2 -0
- package/lib/services/indices.d.ts.map +1 -1
- package/lib/services/indices.js.map +1 -1
- package/lib/services/prepareBeaconProposer.d.ts +2 -1
- package/lib/services/prepareBeaconProposer.d.ts.map +1 -1
- package/lib/services/prepareBeaconProposer.js.map +1 -1
- package/lib/services/proposerPreferences.d.ts +2 -1
- package/lib/services/proposerPreferences.d.ts.map +1 -1
- package/lib/services/proposerPreferences.js.map +1 -1
- package/lib/services/ptc.d.ts +2 -1
- package/lib/services/ptc.d.ts.map +1 -1
- package/lib/services/ptc.js.map +1 -1
- package/lib/services/ptcDuties.d.ts +2 -1
- package/lib/services/ptcDuties.d.ts.map +1 -1
- package/lib/services/ptcDuties.js.map +1 -1
- package/lib/services/syncCommittee.d.ts +22 -1
- package/lib/services/syncCommittee.d.ts.map +1 -1
- package/lib/services/syncCommittee.js +25 -6
- package/lib/services/syncCommittee.js.map +1 -1
- package/lib/services/syncCommitteeDuties.d.ts +22 -1
- package/lib/services/syncCommitteeDuties.d.ts.map +1 -1
- package/lib/services/syncCommitteeDuties.js.map +1 -1
- package/lib/services/syncingStatusTracker.d.ts +9 -1
- package/lib/services/syncingStatusTracker.d.ts.map +1 -1
- package/lib/services/syncingStatusTracker.js +13 -0
- package/lib/services/syncingStatusTracker.js.map +1 -1
- package/lib/services/utils.js.map +1 -1
- package/lib/services/validatorStore.js +2 -3
- package/lib/services/validatorStore.js.map +1 -1
- package/lib/slashingProtection/attestation/attestationByTargetRepository.js.map +1 -1
- package/lib/slashingProtection/attestation/attestationLowerBoundRepository.js.map +1 -1
- package/lib/slashingProtection/attestation/errors.js +1 -2
- package/lib/slashingProtection/attestation/errors.js.map +1 -1
- package/lib/slashingProtection/attestation/index.js.map +1 -1
- package/lib/slashingProtection/block/blockBySlotRepository.js.map +1 -1
- package/lib/slashingProtection/block/errors.js +1 -2
- package/lib/slashingProtection/block/errors.js.map +1 -1
- package/lib/slashingProtection/block/index.js.map +1 -1
- package/lib/slashingProtection/index.js.map +1 -1
- package/lib/slashingProtection/interchange/errors.js +1 -2
- package/lib/slashingProtection/interchange/errors.js.map +1 -1
- package/lib/slashingProtection/interchange/formats/completeV4.d.ts +1 -1
- package/lib/slashingProtection/interchange/formats/completeV4.d.ts.map +1 -1
- package/lib/slashingProtection/interchange/formats/completeV4.js.map +1 -1
- package/lib/slashingProtection/interchange/formats/v5.js.map +1 -1
- package/lib/slashingProtection/interchange/parseInterchange.js.map +1 -1
- package/lib/slashingProtection/interchange/serializeInterchange.js.map +1 -1
- package/lib/slashingProtection/minMaxSurround/distanceStoreRepository.js.map +1 -1
- package/lib/slashingProtection/minMaxSurround/errors.js +1 -2
- package/lib/slashingProtection/minMaxSurround/errors.js.map +1 -1
- package/lib/slashingProtection/minMaxSurround/minMaxSurround.js.map +1 -1
- package/lib/slashingProtection/utils.d.ts +1 -1
- package/lib/slashingProtection/utils.d.ts.map +1 -1
- package/lib/slashingProtection/utils.js.map +1 -1
- package/lib/util/batch.js.map +1 -1
- package/lib/util/difference.js.map +1 -1
- package/lib/util/externalSignerClient.js +1 -2
- package/lib/util/externalSignerClient.js.map +1 -1
- package/lib/util/format.js.map +1 -1
- package/lib/util/index.d.ts +0 -1
- package/lib/util/index.d.ts.map +1 -1
- package/lib/util/index.js +0 -1
- package/lib/util/index.js.map +1 -1
- package/lib/util/logger.d.ts +1 -1
- package/lib/util/logger.d.ts.map +1 -1
- package/lib/util/logger.js.map +1 -1
- package/lib/util/url.js.map +1 -1
- package/lib/validator.d.ts +2 -2
- package/lib/validator.d.ts.map +1 -1
- package/lib/validator.js +2 -3
- package/lib/validator.js.map +1 -1
- package/package.json +15 -15
- package/src/services/attestation.ts +2 -2
- package/src/services/attestationDuties.ts +7 -2
- package/src/services/block.ts +2 -1
- package/src/services/blockDuties.ts +2 -2
- package/src/services/chainHeaderTracker.ts +15 -2
- package/src/services/doppelgangerService.ts +1 -2
- package/src/services/prepareBeaconProposer.ts +2 -1
- package/src/services/proposerPreferences.ts +2 -2
- package/src/services/ptc.ts +2 -1
- package/src/services/ptcDuties.ts +2 -2
- package/src/services/syncCommittee.ts +31 -8
- package/src/services/syncCommitteeDuties.ts +2 -1
- package/src/services/syncingStatusTracker.ts +15 -1
- package/src/services/validatorStore.ts +1 -1
- package/src/util/index.ts +0 -1
- package/src/util/logger.ts +1 -1
- package/src/validator.ts +2 -3
- package/lib/util/clock.d.ts +0 -55
- package/lib/util/clock.d.ts.map +0 -1
- package/lib/util/clock.js +0 -141
- package/lib/util/clock.js.map +0 -1
- package/src/util/clock.ts +0 -182
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {ApiClient} from "@lodestar/api";
|
|
2
2
|
import {ChainForkConfig} from "@lodestar/config";
|
|
3
3
|
import {ForkName, isForkPostAltair} from "@lodestar/params";
|
|
4
|
+
import {IClock} from "@lodestar/state-transition";
|
|
4
5
|
import {CommitteeIndex, Root, Slot, altair} from "@lodestar/types";
|
|
5
6
|
import {sleep} from "@lodestar/utils";
|
|
6
7
|
import {Metrics} from "../metrics.js";
|
|
7
8
|
import {PubkeyHex} from "../types.js";
|
|
8
|
-
import {
|
|
9
|
+
import {LoggerVc} from "../util/index.js";
|
|
9
10
|
import {ChainHeaderTracker} from "./chainHeaderTracker.js";
|
|
10
11
|
import {ValidatorEventEmitter} from "./emitter.js";
|
|
11
12
|
import {SyncCommitteeDutiesService, SyncDutyAndProofs} from "./syncCommitteeDuties.js";
|
|
@@ -66,7 +67,16 @@ export class SyncCommitteeService {
|
|
|
66
67
|
return;
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
//
|
|
70
|
+
// An optimistic validator MUST NOT participate in sync committees, since it has not
|
|
71
|
+
// fully verified the execution payload of the head block it would sign over.
|
|
72
|
+
// https://github.com/ethereum/consensus-specs/blob/v1.6.1/sync/optimistic.md#participating-in-sync-committees
|
|
73
|
+
if (this.syncingStatusTracker.isNodeOptimistic() === true) {
|
|
74
|
+
this.logger.debug("Skipping sync committee duties while node is optimistic", {slot});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Fetch info first so a potential delay is absorbed by the sleep() below.
|
|
79
|
+
// This signs selection proofs, so keep it after the optimistic-node gate above.
|
|
70
80
|
const dutiesAtSlot = await this.dutiesService.getDutiesAtSlot(slot);
|
|
71
81
|
if (dutiesAtSlot.length === 0) {
|
|
72
82
|
return;
|
|
@@ -82,9 +92,15 @@ export class SyncCommitteeService {
|
|
|
82
92
|
]);
|
|
83
93
|
this.metrics?.syncCommitteeStepCallProduceMessage.observe(this.clock.secFromSlot(slot) - syncMessageDueMs / 1000);
|
|
84
94
|
|
|
85
|
-
|
|
95
|
+
const head = await this.getHeadForSyncCommitteeMessage(slot);
|
|
96
|
+
if (head.executionOptimistic) {
|
|
97
|
+
this.logger.debug("Skipping sync committee duties while head is optimistic", {slot});
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Step 1. Sign and publish a `SyncCommitteeMessage` for each validator.
|
|
86
102
|
// Differs from AttestationService, `SyncCommitteeMessage` are equal for all
|
|
87
|
-
const beaconBlockRoot = await this.produceAndPublishSyncCommittees(fork, slot, dutiesAtSlot);
|
|
103
|
+
const beaconBlockRoot = await this.produceAndPublishSyncCommittees(fork, slot, head.root, dutiesAtSlot);
|
|
88
104
|
|
|
89
105
|
// Step 2. If an attestation was produced, make an aggregate.
|
|
90
106
|
// First, wait until the `CONTRIBUTION_DUE_BPS` of the slot
|
|
@@ -126,6 +142,7 @@ export class SyncCommitteeService {
|
|
|
126
142
|
private async produceAndPublishSyncCommittees(
|
|
127
143
|
fork: ForkName,
|
|
128
144
|
slot: Slot,
|
|
145
|
+
blockRoot: Root,
|
|
129
146
|
duties: SyncDutyAndProofs[]
|
|
130
147
|
): Promise<Root> {
|
|
131
148
|
const logCtx = {slot};
|
|
@@ -136,10 +153,6 @@ export class SyncCommitteeService {
|
|
|
136
153
|
// Spec: the validator should prepare a SyncCommitteeMessage for the previous slot (slot - 1)
|
|
137
154
|
// as soon as they have determined the head block of slot - 1
|
|
138
155
|
|
|
139
|
-
const blockRoot: Uint8Array =
|
|
140
|
-
this.chainHeaderTracker.getCurrentChainHead(slot) ??
|
|
141
|
-
(await this.api.beacon.getBlockRoot({blockId: "head"})).value().root;
|
|
142
|
-
|
|
143
156
|
const signatures: altair.SyncCommitteeMessage[] = [];
|
|
144
157
|
|
|
145
158
|
await Promise.all(
|
|
@@ -182,6 +195,16 @@ export class SyncCommitteeService {
|
|
|
182
195
|
return blockRoot;
|
|
183
196
|
}
|
|
184
197
|
|
|
198
|
+
private async getHeadForSyncCommitteeMessage(slot: Slot): Promise<{root: Root; executionOptimistic: boolean}> {
|
|
199
|
+
const cachedHead = this.chainHeaderTracker.getCurrentChainHeadData(slot);
|
|
200
|
+
if (cachedHead) {
|
|
201
|
+
return cachedHead;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const res = await this.api.beacon.getBlockRoot({blockId: "head"});
|
|
205
|
+
return {root: res.value().root, executionOptimistic: res.meta().executionOptimistic};
|
|
206
|
+
}
|
|
207
|
+
|
|
185
208
|
/**
|
|
186
209
|
* Performs the second step of the attesting process: downloading an aggregated `SyncCommittee`,
|
|
187
210
|
* converting it into a `SignedAggregateAndProof` and returning it to the BN.
|
|
@@ -2,6 +2,7 @@ import {ApiClient, routes} from "@lodestar/api";
|
|
|
2
2
|
import {ChainForkConfig} from "@lodestar/config";
|
|
3
3
|
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SYNC_COMMITTEE_SUBNET_SIZE} from "@lodestar/params";
|
|
4
4
|
import {
|
|
5
|
+
IClock,
|
|
5
6
|
computeEpochAtSlot,
|
|
6
7
|
computeSyncPeriodAtEpoch,
|
|
7
8
|
computeSyncPeriodAtSlot,
|
|
@@ -12,7 +13,7 @@ import {BLSSignature, Epoch, Slot, SyncPeriod, ValidatorIndex} from "@lodestar/t
|
|
|
12
13
|
import {toPubkeyHex} from "@lodestar/utils";
|
|
13
14
|
import {Metrics} from "../metrics.js";
|
|
14
15
|
import {PubkeyHex} from "../types.js";
|
|
15
|
-
import {
|
|
16
|
+
import {LoggerVc} from "../util/index.js";
|
|
16
17
|
import {SyncingStatusTracker} from "./syncingStatusTracker.js";
|
|
17
18
|
import {syncCommitteeIndicesToSubnets} from "./utils.js";
|
|
18
19
|
import {ValidatorStore} from "./validatorStore.js";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {ApiClient, routes} from "@lodestar/api";
|
|
2
|
+
import {IClock} from "@lodestar/state-transition";
|
|
2
3
|
import {Slot} from "@lodestar/types";
|
|
3
4
|
import {Logger} from "@lodestar/utils";
|
|
4
5
|
import {BeaconHealth, Metrics} from "../metrics.js";
|
|
5
|
-
import {IClock} from "../util/clock.js";
|
|
6
6
|
|
|
7
7
|
export type SyncingStatus = routes.node.SyncingStatus;
|
|
8
8
|
|
|
@@ -35,6 +35,20 @@ export class SyncingStatusTracker {
|
|
|
35
35
|
this.fns.push(fn);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Whether the node is currently optimistic (i.e. the head block's execution payload
|
|
40
|
+
* has not yet been validated) based on the last successful syncing status check.
|
|
41
|
+
*
|
|
42
|
+
* Returns `undefined` if the status is not yet known (never fetched or last check
|
|
43
|
+
* errored); callers should not assume the node is optimistic in that case.
|
|
44
|
+
*/
|
|
45
|
+
isNodeOptimistic(): boolean | undefined {
|
|
46
|
+
if (this.prevSyncingStatus === undefined || this.prevSyncingStatus instanceof Error) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
return this.prevSyncingStatus.isOptimistic;
|
|
50
|
+
}
|
|
51
|
+
|
|
38
52
|
private checkSyncingStatus = async (slot: Slot, signal: AbortSignal): Promise<void> => {
|
|
39
53
|
try {
|
|
40
54
|
const syncingStatus = (await this.api.node.getSyncingStatus()).value();
|
|
@@ -738,7 +738,7 @@ export class ValidatorStore {
|
|
|
738
738
|
proposalSlot: duty.slot,
|
|
739
739
|
validatorIndex: duty.validatorIndex,
|
|
740
740
|
feeRecipient: fromHex(feeRecipient),
|
|
741
|
-
targetGasLimit: gasLimit,
|
|
741
|
+
targetGasLimit: BigInt(gasLimit),
|
|
742
742
|
};
|
|
743
743
|
|
|
744
744
|
const signingSlot = duty.slot;
|
package/src/util/index.ts
CHANGED
package/src/util/logger.ts
CHANGED
package/src/validator.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
assertEqualParams,
|
|
7
7
|
createBeaconConfig,
|
|
8
8
|
} from "@lodestar/config";
|
|
9
|
-
import {computeEpochAtSlot, getCurrentSlot} from "@lodestar/state-transition";
|
|
9
|
+
import {Clock, ClockOptions, IClock, computeEpochAtSlot, getCurrentSlot} from "@lodestar/state-transition";
|
|
10
10
|
import {BLSPubkey, phase0, ssz} from "@lodestar/types";
|
|
11
11
|
import {Genesis} from "@lodestar/types/phase0";
|
|
12
12
|
import {Logger, toPrintableUrl, toRootHex} from "@lodestar/utils";
|
|
@@ -29,7 +29,6 @@ import {SyncingStatusTracker} from "./services/syncingStatusTracker.js";
|
|
|
29
29
|
import {Signer, ValidatorProposerConfig, ValidatorStore, defaultOptions} from "./services/validatorStore.js";
|
|
30
30
|
import {ISlashingProtection, Interchange, InterchangeFormatVersion} from "./slashingProtection/index.js";
|
|
31
31
|
import {LodestarValidatorDatabaseController, ProcessShutdownCallback, PubkeyHex} from "./types.js";
|
|
32
|
-
import {Clock, ClockOptions, IClock} from "./util/clock.js";
|
|
33
32
|
import {getLoggerVc} from "./util/index.js";
|
|
34
33
|
|
|
35
34
|
export type ValidatorModules = {
|
|
@@ -362,7 +361,7 @@ export class Validator {
|
|
|
362
361
|
|
|
363
362
|
const res = await api.config.getSpec();
|
|
364
363
|
assertEqualParams(config, res.value());
|
|
365
|
-
logger.info("Verified connected beacon node and validator have same
|
|
364
|
+
logger.info("Verified connected beacon node and validator have the same config");
|
|
366
365
|
|
|
367
366
|
await assertEqualGenesis(opts, genesis);
|
|
368
367
|
logger.info("Verified connected beacon node and validator have the same genesisValidatorRoot");
|
package/lib/util/clock.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { ChainForkConfig } from "@lodestar/config";
|
|
2
|
-
import { Epoch, Slot, TimeSeconds } from "@lodestar/types";
|
|
3
|
-
import { Logger } from "@lodestar/utils";
|
|
4
|
-
type RunEveryFn = (slot: Slot, signal: AbortSignal) => Promise<void>;
|
|
5
|
-
export type ClockOptions = {
|
|
6
|
-
skipSlots?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export interface IClock {
|
|
9
|
-
readonly genesisTime: number;
|
|
10
|
-
readonly secondsPerSlot: number;
|
|
11
|
-
readonly currentEpoch: number;
|
|
12
|
-
start(signal: AbortSignal): void;
|
|
13
|
-
runEverySlot(fn: (slot: Slot, signal: AbortSignal) => Promise<void>): void;
|
|
14
|
-
runEveryEpoch(fn: (epoch: Epoch, signal: AbortSignal) => Promise<void>): void;
|
|
15
|
-
msToSlot(slot: Slot): number;
|
|
16
|
-
msFromSlot(slot: Slot): number;
|
|
17
|
-
secFromSlot(slot: Slot): number;
|
|
18
|
-
getCurrentSlot(): Slot;
|
|
19
|
-
getCurrentEpoch(): Epoch;
|
|
20
|
-
}
|
|
21
|
-
export declare enum TimeItem {
|
|
22
|
-
Slot = 0,
|
|
23
|
-
Epoch = 1
|
|
24
|
-
}
|
|
25
|
-
export declare class Clock implements IClock {
|
|
26
|
-
private readonly opts;
|
|
27
|
-
readonly genesisTime: number;
|
|
28
|
-
readonly secondsPerSlot: number;
|
|
29
|
-
private readonly config;
|
|
30
|
-
private readonly logger;
|
|
31
|
-
private readonly fns;
|
|
32
|
-
constructor(config: ChainForkConfig, logger: Logger, opts: {
|
|
33
|
-
genesisTime: number;
|
|
34
|
-
} & ClockOptions);
|
|
35
|
-
get currentEpoch(): Epoch;
|
|
36
|
-
start(signal: AbortSignal): void;
|
|
37
|
-
getCurrentSlot(): Slot;
|
|
38
|
-
getCurrentEpoch(): Epoch;
|
|
39
|
-
runEverySlot(fn: RunEveryFn): void;
|
|
40
|
-
runEveryEpoch(fn: RunEveryFn): void;
|
|
41
|
-
/** Milliseconds from now to a specific slot */
|
|
42
|
-
msToSlot(slot: Slot): number;
|
|
43
|
-
/** Milliseconds elapsed from a specific slot to now */
|
|
44
|
-
msFromSlot(slot: Slot): number;
|
|
45
|
-
/** Seconds elapsed from a specific slot to now */
|
|
46
|
-
secFromSlot(slot: Slot): number;
|
|
47
|
-
private runAtMostEvery;
|
|
48
|
-
private timeUntilNext;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Same to the spec but we use Math.round instead of Math.floor.
|
|
52
|
-
*/
|
|
53
|
-
export declare function getCurrentSlotAround(config: ChainForkConfig, genesisTime: TimeSeconds): Slot;
|
|
54
|
-
export {};
|
|
55
|
-
//# sourceMappingURL=clock.d.ts.map
|
package/lib/util/clock.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clock.d.ts","sourceRoot":"","sources":["../../src/util/clock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAC,MAAM,kBAAkB,CAAC;AAGjD,OAAO,EAAC,KAAK,EAAE,IAAI,EAAE,WAAW,EAAC,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAe,MAAM,EAAwB,MAAM,iBAAiB,CAAC;AAE5E,KAAK,UAAU,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAErE,MAAM,MAAM,YAAY,GAAG;IACzB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAEhC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAE9B,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAAC;IACjC,YAAY,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,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AE7B;IAED,kDAAkD;IAClD,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAE9B;YAQa,cAAc;IA2C5B,OAAO,CAAC,aAAa;CAgBtB;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,eAAe,EAAE,WAAW,EAAE,WAAW,GAAG,IAAI,CAI5F"}
|
package/lib/util/clock.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { GENESIS_SLOT, SLOTS_PER_EPOCH } from "@lodestar/params";
|
|
2
|
-
import { computeEpochAtSlot, computeTimeAtSlot, getCurrentSlot } from "@lodestar/state-transition";
|
|
3
|
-
import { ErrorAborted, isErrorAborted, sleep } from "@lodestar/utils";
|
|
4
|
-
export { TimeItem };
|
|
5
|
-
var TimeItem;
|
|
6
|
-
(function (TimeItem) {
|
|
7
|
-
TimeItem[TimeItem["Slot"] = 0] = "Slot";
|
|
8
|
-
TimeItem[TimeItem["Epoch"] = 1] = "Epoch";
|
|
9
|
-
})(TimeItem || (TimeItem = {}));
|
|
10
|
-
export class Clock {
|
|
11
|
-
opts;
|
|
12
|
-
genesisTime;
|
|
13
|
-
secondsPerSlot;
|
|
14
|
-
config;
|
|
15
|
-
logger;
|
|
16
|
-
fns = [];
|
|
17
|
-
constructor(config, logger, opts) {
|
|
18
|
-
this.opts = opts;
|
|
19
|
-
this.genesisTime = opts.genesisTime;
|
|
20
|
-
this.secondsPerSlot = config.SLOT_DURATION_MS / 1000;
|
|
21
|
-
this.config = config;
|
|
22
|
-
this.logger = logger;
|
|
23
|
-
}
|
|
24
|
-
get currentEpoch() {
|
|
25
|
-
return computeEpochAtSlot(getCurrentSlot(this.config, this.genesisTime));
|
|
26
|
-
}
|
|
27
|
-
start(signal) {
|
|
28
|
-
for (const { timeItem, fn } of this.fns) {
|
|
29
|
-
this.runAtMostEvery(timeItem, signal, fn).catch((e) => {
|
|
30
|
-
if (!isErrorAborted(e)) {
|
|
31
|
-
this.logger.error("runAtMostEvery", {}, e);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
getCurrentSlot() {
|
|
37
|
-
return getCurrentSlot(this.config, this.genesisTime);
|
|
38
|
-
}
|
|
39
|
-
getCurrentEpoch() {
|
|
40
|
-
return computeEpochAtSlot(getCurrentSlot(this.config, this.genesisTime));
|
|
41
|
-
}
|
|
42
|
-
runEverySlot(fn) {
|
|
43
|
-
this.fns.push({ timeItem: TimeItem.Slot, fn });
|
|
44
|
-
}
|
|
45
|
-
runEveryEpoch(fn) {
|
|
46
|
-
this.fns.push({ timeItem: TimeItem.Epoch, fn });
|
|
47
|
-
}
|
|
48
|
-
/** Milliseconds from now to a specific slot */
|
|
49
|
-
msToSlot(slot) {
|
|
50
|
-
return computeTimeAtSlot(this.config, slot, this.genesisTime) * 1000 - Date.now();
|
|
51
|
-
}
|
|
52
|
-
/** Milliseconds elapsed from a specific slot to now */
|
|
53
|
-
msFromSlot(slot) {
|
|
54
|
-
return Date.now() - computeTimeAtSlot(this.config, slot, this.genesisTime) * 1000;
|
|
55
|
-
}
|
|
56
|
-
/** Seconds elapsed from a specific slot to now */
|
|
57
|
-
secFromSlot(slot) {
|
|
58
|
-
return Date.now() / 1000 - computeTimeAtSlot(this.config, slot, this.genesisTime);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* If a task happens to take more than one slot to run, we might skip a slot. This is unfortunate,
|
|
62
|
-
* however the alternative is to *always* process every slot, which has the chance of creating a
|
|
63
|
-
* theoretically unlimited backlog of tasks. It was a conscious decision to choose to drop tasks
|
|
64
|
-
* on an overloaded/latent system rather than overload it even more.
|
|
65
|
-
*/
|
|
66
|
-
async runAtMostEvery(timeItem, signal, fn) {
|
|
67
|
-
// Run immediately first
|
|
68
|
-
let slot = getCurrentSlot(this.config, this.genesisTime);
|
|
69
|
-
let slotOrEpoch = timeItem === TimeItem.Slot ? slot : computeEpochAtSlot(slot);
|
|
70
|
-
while (!signal.aborted) {
|
|
71
|
-
// Must catch fn() to ensure `sleep()` is awaited both for resolve and reject
|
|
72
|
-
const task = fn(slotOrEpoch, signal).catch((e) => {
|
|
73
|
-
if (!isErrorAborted(e))
|
|
74
|
-
this.logger.error("Error on runEvery fn", {}, e);
|
|
75
|
-
});
|
|
76
|
-
if (timeItem !== TimeItem.Slot || this.opts.skipSlots !== false) {
|
|
77
|
-
// await response to only continue with next task if current task finished within slot
|
|
78
|
-
await task;
|
|
79
|
-
}
|
|
80
|
-
try {
|
|
81
|
-
await sleep(this.timeUntilNext(timeItem), signal);
|
|
82
|
-
// calling getCurrentSlot here may not be correct when we're close to the next slot
|
|
83
|
-
// it's safe to call getCurrentSlotAround after we sleep
|
|
84
|
-
const nextSlot = getCurrentSlotAround(this.config, this.genesisTime);
|
|
85
|
-
if (timeItem === TimeItem.Slot) {
|
|
86
|
-
if (nextSlot > slot + 1) {
|
|
87
|
-
// It's not very likely that we skip more than one slot as HTTP timeout is set
|
|
88
|
-
// to SLOT_DURATION_MS so we will fail task before skipping another slot.
|
|
89
|
-
this.logger.warn("Skipped slot due to task taking more than one slot to run", {
|
|
90
|
-
skippedSlot: slot + 1,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
slotOrEpoch = nextSlot;
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
slotOrEpoch = computeEpochAtSlot(nextSlot);
|
|
97
|
-
}
|
|
98
|
-
slot = nextSlot;
|
|
99
|
-
}
|
|
100
|
-
catch (e) {
|
|
101
|
-
if (e instanceof ErrorAborted) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
throw e;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
timeUntilNext(timeItem) {
|
|
109
|
-
const milliSecondsPerSlot = this.config.SLOT_DURATION_MS;
|
|
110
|
-
const msFromGenesis = Date.now() - this.genesisTime * 1000;
|
|
111
|
-
if (timeItem === TimeItem.Slot) {
|
|
112
|
-
if (msFromGenesis >= 0) {
|
|
113
|
-
return milliSecondsPerSlot - (msFromGenesis % milliSecondsPerSlot);
|
|
114
|
-
}
|
|
115
|
-
return Math.abs(msFromGenesis) % milliSecondsPerSlot;
|
|
116
|
-
}
|
|
117
|
-
const milliSecondsPerEpoch = SLOTS_PER_EPOCH * milliSecondsPerSlot;
|
|
118
|
-
if (msFromGenesis >= 0) {
|
|
119
|
-
return milliSecondsPerEpoch - (msFromGenesis % milliSecondsPerEpoch);
|
|
120
|
-
}
|
|
121
|
-
return Math.abs(msFromGenesis) % milliSecondsPerEpoch;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Same to the spec but we use Math.round instead of Math.floor.
|
|
126
|
-
*/
|
|
127
|
-
export function getCurrentSlotAround(config, genesisTime) {
|
|
128
|
-
const diffInSeconds = Date.now() / 1000 - genesisTime;
|
|
129
|
-
const slotsSinceGenesis = Math.round((diffInSeconds * 1000) / config.SLOT_DURATION_MS);
|
|
130
|
-
return GENESIS_SLOT + slotsSinceGenesis;
|
|
131
|
-
}
|
|
132
|
-
// function useEventStream() {
|
|
133
|
-
// this.stream = this.events.getEventStream([BeaconEventType.BLOCK, BeaconEventType.HEAD, BeaconEventType.CHAIN_REORG]);
|
|
134
|
-
// pipeToEmitter(this.stream, this).catch((e: Error) => {
|
|
135
|
-
// this.logger.error("Error on stream pipe", {}, e);
|
|
136
|
-
// });
|
|
137
|
-
// // On stop
|
|
138
|
-
// this.stream.stop();
|
|
139
|
-
// this.stream = null;
|
|
140
|
-
// }
|
|
141
|
-
//# sourceMappingURL=clock.js.map
|
package/lib/util/clock.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../../src/util/clock.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,YAAY,EAAE,eAAe,EAAC,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAC,kBAAkB,EAAE,iBAAiB,EAAE,cAAc,E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C,gBAAgB,CAAC,CAAC;IACvF,OAAO,YAAY,GAAG,iBAAiB,CAAC;AAAA,CACzC;AAED,8BAA8B;AAC9B,0HAA0H;AAC1H,2DAA2D;AAC3D,wDAAwD;AACxD,QAAQ;AAER,eAAe;AACf,wBAAwB;AACxB,wBAAwB;AACxB,IAAI"}
|
package/src/util/clock.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import {ChainForkConfig} from "@lodestar/config";
|
|
2
|
-
import {GENESIS_SLOT, SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
3
|
-
import {computeEpochAtSlot, computeTimeAtSlot, getCurrentSlot} from "@lodestar/state-transition";
|
|
4
|
-
import {Epoch, Slot, TimeSeconds} from "@lodestar/types";
|
|
5
|
-
import {ErrorAborted, Logger, isErrorAborted, sleep} from "@lodestar/utils";
|
|
6
|
-
|
|
7
|
-
type RunEveryFn = (slot: Slot, signal: AbortSignal) => Promise<void>;
|
|
8
|
-
|
|
9
|
-
export type ClockOptions = {
|
|
10
|
-
skipSlots?: boolean;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export interface IClock {
|
|
14
|
-
readonly genesisTime: number;
|
|
15
|
-
readonly secondsPerSlot: number;
|
|
16
|
-
|
|
17
|
-
readonly currentEpoch: number;
|
|
18
|
-
|
|
19
|
-
start(signal: AbortSignal): void;
|
|
20
|
-
runEverySlot(fn: (slot: Slot, signal: AbortSignal) => Promise<void>): void;
|
|
21
|
-
runEveryEpoch(fn: (epoch: Epoch, signal: AbortSignal) => Promise<void>): void;
|
|
22
|
-
msToSlot(slot: Slot): number;
|
|
23
|
-
msFromSlot(slot: Slot): number;
|
|
24
|
-
secFromSlot(slot: Slot): number;
|
|
25
|
-
getCurrentSlot(): Slot;
|
|
26
|
-
getCurrentEpoch(): Epoch;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export enum TimeItem {
|
|
30
|
-
Slot,
|
|
31
|
-
Epoch,
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class Clock implements IClock {
|
|
35
|
-
readonly genesisTime: number;
|
|
36
|
-
readonly secondsPerSlot: number;
|
|
37
|
-
private readonly config: ChainForkConfig;
|
|
38
|
-
private readonly logger: Logger;
|
|
39
|
-
private readonly fns: {timeItem: TimeItem; fn: RunEveryFn}[] = [];
|
|
40
|
-
|
|
41
|
-
constructor(
|
|
42
|
-
config: ChainForkConfig,
|
|
43
|
-
logger: Logger,
|
|
44
|
-
private readonly opts: {genesisTime: number} & ClockOptions
|
|
45
|
-
) {
|
|
46
|
-
this.genesisTime = opts.genesisTime;
|
|
47
|
-
this.secondsPerSlot = config.SLOT_DURATION_MS / 1000;
|
|
48
|
-
this.config = config;
|
|
49
|
-
this.logger = logger;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get currentEpoch(): Epoch {
|
|
53
|
-
return computeEpochAtSlot(getCurrentSlot(this.config, this.genesisTime));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
start(signal: AbortSignal): void {
|
|
57
|
-
for (const {timeItem, fn} of this.fns) {
|
|
58
|
-
this.runAtMostEvery(timeItem, signal, fn).catch((e: Error) => {
|
|
59
|
-
if (!isErrorAborted(e)) {
|
|
60
|
-
this.logger.error("runAtMostEvery", {}, e);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getCurrentSlot(): Slot {
|
|
67
|
-
return getCurrentSlot(this.config, this.genesisTime);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
getCurrentEpoch(): Epoch {
|
|
71
|
-
return computeEpochAtSlot(getCurrentSlot(this.config, this.genesisTime));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
runEverySlot(fn: RunEveryFn): void {
|
|
75
|
-
this.fns.push({timeItem: TimeItem.Slot, fn});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
runEveryEpoch(fn: RunEveryFn): void {
|
|
79
|
-
this.fns.push({timeItem: TimeItem.Epoch, fn});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Milliseconds from now to a specific slot */
|
|
83
|
-
msToSlot(slot: Slot): number {
|
|
84
|
-
return computeTimeAtSlot(this.config, slot, this.genesisTime) * 1000 - Date.now();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** Milliseconds elapsed from a specific slot to now */
|
|
88
|
-
msFromSlot(slot: Slot): number {
|
|
89
|
-
return Date.now() - computeTimeAtSlot(this.config, slot, this.genesisTime) * 1000;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Seconds elapsed from a specific slot to now */
|
|
93
|
-
secFromSlot(slot: Slot): number {
|
|
94
|
-
return Date.now() / 1000 - computeTimeAtSlot(this.config, slot, this.genesisTime);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* If a task happens to take more than one slot to run, we might skip a slot. This is unfortunate,
|
|
99
|
-
* however the alternative is to *always* process every slot, which has the chance of creating a
|
|
100
|
-
* theoretically unlimited backlog of tasks. It was a conscious decision to choose to drop tasks
|
|
101
|
-
* on an overloaded/latent system rather than overload it even more.
|
|
102
|
-
*/
|
|
103
|
-
private async runAtMostEvery(timeItem: TimeItem, signal: AbortSignal, fn: RunEveryFn): Promise<void> {
|
|
104
|
-
// Run immediately first
|
|
105
|
-
let slot = getCurrentSlot(this.config, this.genesisTime);
|
|
106
|
-
let slotOrEpoch = timeItem === TimeItem.Slot ? slot : computeEpochAtSlot(slot);
|
|
107
|
-
while (!signal.aborted) {
|
|
108
|
-
// Must catch fn() to ensure `sleep()` is awaited both for resolve and reject
|
|
109
|
-
const task = fn(slotOrEpoch, signal).catch((e: Error) => {
|
|
110
|
-
if (!isErrorAborted(e)) this.logger.error("Error on runEvery fn", {}, e);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
if (timeItem !== TimeItem.Slot || this.opts.skipSlots !== false) {
|
|
114
|
-
// await response to only continue with next task if current task finished within slot
|
|
115
|
-
await task;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
await sleep(this.timeUntilNext(timeItem), signal);
|
|
120
|
-
// calling getCurrentSlot here may not be correct when we're close to the next slot
|
|
121
|
-
// it's safe to call getCurrentSlotAround after we sleep
|
|
122
|
-
const nextSlot = getCurrentSlotAround(this.config, this.genesisTime);
|
|
123
|
-
|
|
124
|
-
if (timeItem === TimeItem.Slot) {
|
|
125
|
-
if (nextSlot > slot + 1) {
|
|
126
|
-
// It's not very likely that we skip more than one slot as HTTP timeout is set
|
|
127
|
-
// to SLOT_DURATION_MS so we will fail task before skipping another slot.
|
|
128
|
-
this.logger.warn("Skipped slot due to task taking more than one slot to run", {
|
|
129
|
-
skippedSlot: slot + 1,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
slotOrEpoch = nextSlot;
|
|
133
|
-
} else {
|
|
134
|
-
slotOrEpoch = computeEpochAtSlot(nextSlot);
|
|
135
|
-
}
|
|
136
|
-
slot = nextSlot;
|
|
137
|
-
} catch (e) {
|
|
138
|
-
if (e instanceof ErrorAborted) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
throw e;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
private timeUntilNext(timeItem: TimeItem): number {
|
|
147
|
-
const milliSecondsPerSlot = this.config.SLOT_DURATION_MS;
|
|
148
|
-
const msFromGenesis = Date.now() - this.genesisTime * 1000;
|
|
149
|
-
|
|
150
|
-
if (timeItem === TimeItem.Slot) {
|
|
151
|
-
if (msFromGenesis >= 0) {
|
|
152
|
-
return milliSecondsPerSlot - (msFromGenesis % milliSecondsPerSlot);
|
|
153
|
-
}
|
|
154
|
-
return Math.abs(msFromGenesis) % milliSecondsPerSlot;
|
|
155
|
-
}
|
|
156
|
-
const milliSecondsPerEpoch = SLOTS_PER_EPOCH * milliSecondsPerSlot;
|
|
157
|
-
if (msFromGenesis >= 0) {
|
|
158
|
-
return milliSecondsPerEpoch - (msFromGenesis % milliSecondsPerEpoch);
|
|
159
|
-
}
|
|
160
|
-
return Math.abs(msFromGenesis) % milliSecondsPerEpoch;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Same to the spec but we use Math.round instead of Math.floor.
|
|
166
|
-
*/
|
|
167
|
-
export function getCurrentSlotAround(config: ChainForkConfig, genesisTime: TimeSeconds): Slot {
|
|
168
|
-
const diffInSeconds = Date.now() / 1000 - genesisTime;
|
|
169
|
-
const slotsSinceGenesis = Math.round((diffInSeconds * 1000) / config.SLOT_DURATION_MS);
|
|
170
|
-
return GENESIS_SLOT + slotsSinceGenesis;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// function useEventStream() {
|
|
174
|
-
// this.stream = this.events.getEventStream([BeaconEventType.BLOCK, BeaconEventType.HEAD, BeaconEventType.CHAIN_REORG]);
|
|
175
|
-
// pipeToEmitter(this.stream, this).catch((e: Error) => {
|
|
176
|
-
// this.logger.error("Error on stream pipe", {}, e);
|
|
177
|
-
// });
|
|
178
|
-
|
|
179
|
-
// // On stop
|
|
180
|
-
// this.stream.stop();
|
|
181
|
-
// this.stream = null;
|
|
182
|
-
// }
|