-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
epoch.ts
72 lines (63 loc) 路 2.05 KB
/
epoch.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
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, GENESIS_EPOCH, MAX_SEED_LOOKAHEAD, SLOTS_PER_EPOCH} from "@lodestar/params";
import {allForks, Epoch, Slot, SyncPeriod} from "@lodestar/types";
/**
* Return the epoch number at the given slot.
*/
export function computeEpochAtSlot(slot: Slot): Epoch {
return Math.floor(slot / SLOTS_PER_EPOCH);
}
/**
* Return the epoch at the state slot for purposes of checkpoint.
* Ideally this slot % SLOTS_PER_EPOCH === 0, but just to handle the improbable case of
* non boundary slot, using ceil so that the state's latestBlockHeader would always
* lie before this epooch
*/
export function computeCheckpointEpochAtStateSlot(slot: Slot): Epoch {
return Math.ceil(slot / SLOTS_PER_EPOCH);
}
/**
* Return the starting slot of the given epoch.
*/
export function computeStartSlotAtEpoch(epoch: Epoch): Slot {
return epoch * SLOTS_PER_EPOCH;
}
/**
* Return the end slot of the given epoch.
*/
export function computeEndSlotAtEpoch(epoch: Epoch): Slot {
return computeStartSlotAtEpoch(epoch + 1) - 1;
}
/**
* Return the epoch at which an activation or exit triggered in ``epoch`` takes effect.
*/
export function computeActivationExitEpoch(epoch: Epoch): Epoch {
return epoch + 1 + MAX_SEED_LOOKAHEAD;
}
/**
* Return the current epoch of the given state.
*/
export function getCurrentEpoch(state: Pick<allForks.BeaconState, "slot">): Epoch {
return computeEpochAtSlot(state.slot);
}
/**
* Return the previous epoch of the given state.
*/
export function getPreviousEpoch(state: Pick<allForks.BeaconState, "slot">): Epoch {
const currentEpoch = getCurrentEpoch(state);
if (currentEpoch === GENESIS_EPOCH) {
return GENESIS_EPOCH;
}
return currentEpoch - 1;
}
/**
* Return the sync committee period at slot
*/
export function computeSyncPeriodAtSlot(slot: Slot): SyncPeriod {
return computeSyncPeriodAtEpoch(computeEpochAtSlot(slot));
}
/**
* Return the sync committee period at epoch
*/
export function computeSyncPeriodAtEpoch(epoch: Epoch): SyncPeriod {
return Math.floor(epoch / EPOCHS_PER_SYNC_COMMITTEE_PERIOD);
}