@lodestar/state-transition 1.49.0 → 1.50.0-dev.1f38fe79fb
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/rewards/attestationsRewards.d.ts.map +1 -1
- package/lib/rewards/attestationsRewards.js +7 -5
- package/lib/rewards/attestationsRewards.js.map +1 -1
- package/lib/stateView/beaconStateView.d.ts +4 -0
- package/lib/stateView/beaconStateView.d.ts.map +1 -1
- package/lib/stateView/beaconStateView.js +20 -0
- package/lib/stateView/beaconStateView.js.map +1 -1
- package/lib/stateView/interface.d.ts +2 -0
- package/lib/stateView/interface.d.ts.map +1 -1
- package/lib/stateView/interface.js.map +1 -1
- package/lib/stateView/nativeBeaconStateView.d.ts +4 -0
- package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
- package/lib/stateView/nativeBeaconStateView.js +14 -0
- package/lib/stateView/nativeBeaconStateView.js.map +1 -1
- package/lib/stateView/stateViewFactory.d.ts +5 -13
- package/lib/stateView/stateViewFactory.d.ts.map +1 -1
- package/lib/stateView/stateViewFactory.js +5 -5
- package/lib/stateView/stateViewFactory.js.map +1 -1
- package/lib/util/index.d.ts +1 -0
- package/lib/util/index.d.ts.map +1 -1
- package/lib/util/index.js +1 -0
- package/lib/util/index.js.map +1 -1
- package/lib/util/sszBytes.d.ts +14 -17
- package/lib/util/sszBytes.d.ts.map +1 -1
- package/lib/util/sszBytes.js +80 -20
- package/lib/util/sszBytes.js.map +1 -1
- package/lib/util/weakSubjectivity.d.ts +11 -6
- package/lib/util/weakSubjectivity.d.ts.map +1 -1
- package/lib/util/weakSubjectivity.js +22 -28
- package/lib/util/weakSubjectivity.js.map +1 -1
- package/package.json +7 -7
- package/src/rewards/attestationsRewards.ts +9 -4
- package/src/stateView/beaconStateView.ts +26 -0
- package/src/stateView/interface.ts +2 -0
- package/src/stateView/nativeBeaconStateView.ts +16 -0
- package/src/stateView/stateViewFactory.ts +9 -16
- package/src/util/index.ts +5 -0
- package/src/util/sszBytes.ts +111 -22
- package/src/util/weakSubjectivity.ts +43 -40
package/src/util/sszBytes.ts
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
import {ChainForkConfig} from "@lodestar/config";
|
|
2
|
-
import {ForkAll, ForkSeq} from "@lodestar/params";
|
|
3
|
-
import {SSZTypesFor, Slot} from "@lodestar/types";
|
|
2
|
+
import {EFFECTIVE_BALANCE_INCREMENT, ForkAll, ForkSeq} from "@lodestar/params";
|
|
3
|
+
import {Epoch, SSZTypesFor, Slot, ssz} from "@lodestar/types";
|
|
4
4
|
import {bytesToInt} from "@lodestar/utils";
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
const SLOT_BYTE_COUNT = 8;
|
|
6
|
+
const UINT64_SIZE = 8;
|
|
7
|
+
const ROOT_SIZE = 32;
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
|
-
* 48 + 32 + 8 + 1 + 8 + 8 + 8 + 8 = 121
|
|
13
10
|
* ```
|
|
14
11
|
* class Validator(Container):
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
```
|
|
12
|
+
* pubkey: BLSPubkey [fixed - 48 bytes]
|
|
13
|
+
* withdrawal_credentials: Bytes32 [fixed - 32 bytes]
|
|
14
|
+
* effective_balance: Gwei [fixed - 8 bytes]
|
|
15
|
+
* slashed: boolean [fixed - 1 byte]
|
|
16
|
+
* activation_eligibility_epoch: Epoch [fixed - 8 bytes]
|
|
17
|
+
* activation_epoch: Epoch [fixed - 8 bytes]
|
|
18
|
+
* exit_epoch: Epoch [fixed - 8 bytes]
|
|
19
|
+
* withdrawable_epoch: Epoch [fixed - 8 bytes]
|
|
20
|
+
* ```
|
|
25
21
|
*/
|
|
26
|
-
|
|
22
|
+
const VALIDATOR_EFFECTIVE_BALANCE_OFFSET = 48 + ROOT_SIZE;
|
|
23
|
+
const VALIDATOR_ACTIVATION_EPOCH_OFFSET = VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE + 1 + UINT64_SIZE;
|
|
24
|
+
const VALIDATOR_EXIT_EPOCH_OFFSET = VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE;
|
|
25
|
+
export const VALIDATOR_BYTES_SIZE = VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE + UINT64_SIZE;
|
|
27
26
|
|
|
28
27
|
/**
|
|
29
|
-
* 8 + 32 = 40
|
|
30
28
|
* ```
|
|
31
29
|
* class BeaconState(Container):
|
|
32
30
|
* genesis_time: uint64 [fixed - 8 bytes]
|
|
@@ -35,10 +33,12 @@ export const VALIDATOR_BYTES_SIZE = 121;
|
|
|
35
33
|
* ...
|
|
36
34
|
* ```
|
|
37
35
|
*/
|
|
38
|
-
const
|
|
36
|
+
const STATE_GENESIS_TIME_OFFSET = 0;
|
|
37
|
+
const STATE_GENESIS_VALIDATORS_ROOT_OFFSET = STATE_GENESIS_TIME_OFFSET + UINT64_SIZE;
|
|
38
|
+
const STATE_SLOT_OFFSET = STATE_GENESIS_VALIDATORS_ROOT_OFFSET + ROOT_SIZE;
|
|
39
39
|
|
|
40
40
|
export function getForkFromStateBytes(config: ChainForkConfig, bytes: Uint8Array): ForkSeq {
|
|
41
|
-
const slot = bytesToInt(bytes.subarray(
|
|
41
|
+
const slot = bytesToInt(bytes.subarray(STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE));
|
|
42
42
|
return config.getForkSeq(slot);
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -48,5 +48,94 @@ export function getStateTypeFromBytes(config: ChainForkConfig, bytes: Uint8Array
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function getStateSlotFromBytes(bytes: Uint8Array): Slot {
|
|
51
|
-
return bytesToInt(bytes.subarray(
|
|
51
|
+
return bytesToInt(bytes.subarray(STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type StateBytesMetadata = {slot: Slot; genesisTime: number; genesisValidatorsRoot: Uint8Array};
|
|
55
|
+
|
|
56
|
+
export function readBeaconStateBytesMetadata(bytes: Uint8Array): StateBytesMetadata | null {
|
|
57
|
+
if (bytes.length < STATE_SLOT_OFFSET + UINT64_SIZE) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const data = {uint8Array: bytes, dataView: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)};
|
|
61
|
+
return {
|
|
62
|
+
genesisTime: ssz.UintNum64.value_deserializeFromBytes(
|
|
63
|
+
data,
|
|
64
|
+
STATE_GENESIS_TIME_OFFSET,
|
|
65
|
+
STATE_GENESIS_TIME_OFFSET + UINT64_SIZE
|
|
66
|
+
),
|
|
67
|
+
genesisValidatorsRoot: Uint8Array.from(
|
|
68
|
+
bytes.subarray(STATE_GENESIS_VALIDATORS_ROOT_OFFSET, STATE_GENESIS_VALIDATORS_ROOT_OFFSET + ROOT_SIZE)
|
|
69
|
+
),
|
|
70
|
+
slot: ssz.Slot.value_deserializeFromBytes(data, STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Read only the validator fields needed for weak subjectivity, without allocating validator objects. */
|
|
75
|
+
export function scanActiveValidatorsFromStateBytes(
|
|
76
|
+
stateBytes: Uint8Array,
|
|
77
|
+
stateType: SSZTypesFor<ForkAll, "BeaconState">,
|
|
78
|
+
epoch: Epoch
|
|
79
|
+
): {activeValidatorCount: number; totalActiveBalanceIncrements: number} | null {
|
|
80
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, stateType);
|
|
81
|
+
if (range === null) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const data = {
|
|
86
|
+
uint8Array: stateBytes,
|
|
87
|
+
dataView: new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength),
|
|
88
|
+
};
|
|
89
|
+
let activeValidatorCount = 0;
|
|
90
|
+
let totalActiveBalanceIncrements = 0;
|
|
91
|
+
for (let offset = range.start; offset < range.end; offset += VALIDATOR_BYTES_SIZE) {
|
|
92
|
+
const activationEpoch = ssz.EpochInf.value_deserializeFromBytes(
|
|
93
|
+
data,
|
|
94
|
+
offset + VALIDATOR_ACTIVATION_EPOCH_OFFSET,
|
|
95
|
+
offset + VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE
|
|
96
|
+
);
|
|
97
|
+
const exitEpoch = ssz.EpochInf.value_deserializeFromBytes(
|
|
98
|
+
data,
|
|
99
|
+
offset + VALIDATOR_EXIT_EPOCH_OFFSET,
|
|
100
|
+
offset + VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE
|
|
101
|
+
);
|
|
102
|
+
if (activationEpoch <= epoch && epoch < exitEpoch) {
|
|
103
|
+
activeValidatorCount++;
|
|
104
|
+
const effectiveBalance = ssz.UintNum64.value_deserializeFromBytes(
|
|
105
|
+
data,
|
|
106
|
+
offset + VALIDATOR_EFFECTIVE_BALANCE_OFFSET,
|
|
107
|
+
offset + VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE
|
|
108
|
+
);
|
|
109
|
+
totalActiveBalanceIncrements += Math.floor(effectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return {activeValidatorCount, totalActiveBalanceIncrements: Math.max(1, totalActiveBalanceIncrements)};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function getValidatorCountFromStateBytes(config: ChainForkConfig, stateBytes: Uint8Array): number | null {
|
|
116
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, getStateTypeFromBytes(config, stateBytes));
|
|
117
|
+
// A non-null range always holds a whole number of validator records, so the division is exact
|
|
118
|
+
return range === null ? null : (range.end - range.start) / VALIDATOR_BYTES_SIZE;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function getValidatorsRangeFromStateBytes(
|
|
122
|
+
stateBytes: Uint8Array,
|
|
123
|
+
stateType: SSZTypesFor<ForkAll, "BeaconState">
|
|
124
|
+
): {start: number; end: number} | null {
|
|
125
|
+
let range: {start: number; end: number};
|
|
126
|
+
try {
|
|
127
|
+
const containerType = stateType as (typeof ssz)[ForkAll]["BeaconState"];
|
|
128
|
+
const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
|
|
129
|
+
const ranges = containerType.getFieldRanges(dataView, 0, stateBytes.length);
|
|
130
|
+
range = ranges[Object.keys(containerType.fields).indexOf("validators")];
|
|
131
|
+
} catch {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (!range || range.start < 0 || range.end > stateBytes.length || range.end < range.start) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
if ((range.end - range.start) % VALIDATOR_BYTES_SIZE !== 0) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
return range;
|
|
52
141
|
}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {ChainForkConfig} from "@lodestar/config";
|
|
2
2
|
import {
|
|
3
3
|
EFFECTIVE_BALANCE_INCREMENT,
|
|
4
4
|
ForkSeq,
|
|
5
|
+
GENESIS_SLOT,
|
|
5
6
|
MAX_DEPOSITS,
|
|
6
7
|
MAX_EFFECTIVE_BALANCE,
|
|
7
8
|
SLOTS_PER_EPOCH,
|
|
8
9
|
isForkPostElectra,
|
|
9
10
|
isForkPostGloas,
|
|
10
11
|
} from "@lodestar/params";
|
|
11
|
-
import {Epoch, Root, ssz} from "@lodestar/types";
|
|
12
|
-
import {Checkpoint} from "@lodestar/types/phase0";
|
|
13
|
-
import {toRootHex} from "@lodestar/utils";
|
|
12
|
+
import {Epoch, Root, Slot, TimeSeconds, ssz} from "@lodestar/types";
|
|
14
13
|
import {ZERO_HASH} from "../constants/constants.js";
|
|
15
14
|
import {BeaconStateAllForks, CachedBeaconStateAllForks} from "../types.js";
|
|
16
15
|
import {computeCheckpointEpochAtStateSlot, computeEpochAtSlot, getCurrentEpoch} from "./epoch.js";
|
|
17
|
-
import {
|
|
16
|
+
import {StateBytesMetadata, getStateTypeFromBytes, scanActiveValidatorsFromStateBytes} from "./sszBytes.js";
|
|
18
17
|
import {
|
|
19
18
|
getActivationChurnLimit,
|
|
20
19
|
getActiveValidatorIndices,
|
|
@@ -79,12 +78,10 @@ export function computeWeakSubjectivityPeriodCachedState(
|
|
|
79
78
|
|
|
80
79
|
/**
|
|
81
80
|
* Same to computeWeakSubjectivityPeriodCachedState but for normal state
|
|
82
|
-
* This is called only 1 time at app startup so it's ok to calculate totalActiveBalanceIncrements manually
|
|
83
81
|
*/
|
|
84
82
|
export function computeWeakSubjectivityPeriod(config: ChainForkConfig, state: BeaconStateAllForks): number {
|
|
85
83
|
const activeIndices = getActiveValidatorIndices(state, getCurrentEpoch(state));
|
|
86
84
|
const validators = state.validators.getAllReadonlyValues();
|
|
87
|
-
const fork = config.getForkName(state.slot);
|
|
88
85
|
|
|
89
86
|
let totalActiveBalanceIncrements = 0;
|
|
90
87
|
for (const index of activeIndices) {
|
|
@@ -94,6 +91,22 @@ export function computeWeakSubjectivityPeriod(config: ChainForkConfig, state: Be
|
|
|
94
91
|
totalActiveBalanceIncrements = 1;
|
|
95
92
|
}
|
|
96
93
|
|
|
94
|
+
return computeWeakSubjectivityPeriodFromActiveValidators(config, state.slot, {
|
|
95
|
+
activeValidatorCount: activeIndices.length,
|
|
96
|
+
totalActiveBalanceIncrements,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function computeWeakSubjectivityPeriodFromActiveValidators(
|
|
101
|
+
config: ChainForkConfig,
|
|
102
|
+
slot: Slot,
|
|
103
|
+
{
|
|
104
|
+
activeValidatorCount,
|
|
105
|
+
totalActiveBalanceIncrements,
|
|
106
|
+
}: {activeValidatorCount: number; totalActiveBalanceIncrements: number}
|
|
107
|
+
): number {
|
|
108
|
+
const fork = config.getForkName(slot);
|
|
109
|
+
|
|
97
110
|
const churnLimitsGloas = getGloasChurnLimits(config, totalActiveBalanceIncrements);
|
|
98
111
|
|
|
99
112
|
return isForkPostGloas(fork)
|
|
@@ -115,9 +128,9 @@ export function computeWeakSubjectivityPeriod(config: ChainForkConfig, state: Be
|
|
|
115
128
|
config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
|
116
129
|
)
|
|
117
130
|
: computeWeakSubjectivityPeriodFromConstituentsPhase0(
|
|
118
|
-
|
|
131
|
+
activeValidatorCount,
|
|
119
132
|
totalActiveBalanceIncrements,
|
|
120
|
-
getChurnLimit(config,
|
|
133
|
+
getChurnLimit(config, activeValidatorCount),
|
|
121
134
|
config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
|
122
135
|
);
|
|
123
136
|
}
|
|
@@ -189,37 +202,27 @@ export function getLatestBlockRoot(state: BeaconStateAllForks): Root {
|
|
|
189
202
|
return ssz.phase0.BeaconBlockHeader.hashTreeRoot(header);
|
|
190
203
|
}
|
|
191
204
|
|
|
192
|
-
export
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
205
|
+
export type WeakSubjectivitySummary = {checkpointEpoch: Epoch; genesisTime: number; period: number};
|
|
206
|
+
|
|
207
|
+
/** The metadata must describe the supplied state bytes. */
|
|
208
|
+
export function computeWeakSubjectivitySummaryFromStateBytes(
|
|
209
|
+
config: ChainForkConfig,
|
|
210
|
+
bytes: Uint8Array,
|
|
211
|
+
metadata: StateBytesMetadata
|
|
212
|
+
): WeakSubjectivitySummary | null {
|
|
213
|
+
const {slot, genesisTime} = metadata;
|
|
214
|
+
const stateType = getStateTypeFromBytes(config, bytes);
|
|
215
|
+
const activeValidators = scanActiveValidatorsFromStateBytes(bytes, stateType, computeEpochAtSlot(slot));
|
|
216
|
+
if (activeValidators === null) return null;
|
|
217
|
+
const period = computeWeakSubjectivityPeriodFromActiveValidators(config, slot, activeValidators);
|
|
218
|
+
return {checkpointEpoch: computeCheckpointEpochAtStateSlot(slot), genesisTime, period};
|
|
203
219
|
}
|
|
204
220
|
|
|
205
|
-
export function
|
|
206
|
-
config:
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
):
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
if (!ssz.Root.equals(blockRoot, wsCheckpoint.root)) {
|
|
213
|
-
throw new Error(`Roots do not match. expected=${toRootHex(wsCheckpoint.root)}, actual=${toRootHex(blockRoot)}`);
|
|
214
|
-
}
|
|
215
|
-
if (!ssz.Epoch.equals(wsStateEpoch, wsCheckpoint.epoch)) {
|
|
216
|
-
throw new Error(`Epochs do not match. expected=${wsCheckpoint.epoch}, actual=${wsStateEpoch}`);
|
|
217
|
-
}
|
|
218
|
-
const wsPeriod = computeWeakSubjectivityPeriod(config, wsState);
|
|
219
|
-
const clockEpoch = computeEpochAtSlot(getCurrentSlot(config, wsState.genesisTime));
|
|
220
|
-
if (clockEpoch > wsStateEpoch + wsPeriod) {
|
|
221
|
-
throw new Error(
|
|
222
|
-
`The downloaded state with epoch ${wsStateEpoch} is not within weak subjectivity period of ${wsPeriod} from the current epoch ${clockEpoch}. Please verify your checkpoint source`
|
|
223
|
-
);
|
|
224
|
-
}
|
|
221
|
+
export function isWithinWeakSubjectivityPeriodFromSummary(
|
|
222
|
+
config: ChainForkConfig,
|
|
223
|
+
summary: WeakSubjectivitySummary,
|
|
224
|
+
now: TimeSeconds = Date.now() / 1000
|
|
225
|
+
): boolean {
|
|
226
|
+
const clockSlot = GENESIS_SLOT + Math.floor((now - summary.genesisTime) / (config.SLOT_DURATION_MS / 1000));
|
|
227
|
+
return computeEpochAtSlot(clockSlot) <= summary.checkpointEpoch + summary.period;
|
|
225
228
|
}
|