@lodestar/state-transition 1.49.0 → 1.50.0-dev.023ad6c012
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/block/indexedBuilderState.d.ts +19 -0
- package/lib/block/indexedBuilderState.d.ts.map +1 -0
- package/lib/block/indexedBuilderState.js +60 -0
- package/lib/block/indexedBuilderState.js.map +1 -0
- package/lib/block/processBuilderDepositRequest.d.ts +2 -2
- package/lib/block/processBuilderDepositRequest.d.ts.map +1 -1
- package/lib/block/processBuilderDepositRequest.js +5 -4
- package/lib/block/processBuilderDepositRequest.js.map +1 -1
- package/lib/block/processBuilderExitRequest.d.ts +2 -2
- package/lib/block/processBuilderExitRequest.d.ts.map +1 -1
- package/lib/block/processBuilderExitRequest.js +4 -3
- package/lib/block/processBuilderExitRequest.js.map +1 -1
- package/lib/block/processParentExecutionPayload.d.ts.map +1 -1
- package/lib/block/processParentExecutionPayload.js +9 -5
- package/lib/block/processParentExecutionPayload.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/rewards/attestationsRewards.d.ts.map +1 -1
- package/lib/rewards/attestationsRewards.js +15 -6
- 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/gloas.d.ts +11 -14
- package/lib/util/gloas.d.ts.map +1 -1
- package/lib/util/gloas.js +2 -42
- package/lib/util/gloas.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 +16 -17
- package/lib/util/sszBytes.d.ts.map +1 -1
- package/lib/util/sszBytes.js +93 -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/block/indexedBuilderState.ts +65 -0
- package/src/block/processBuilderDepositRequest.ts +6 -17
- package/src/block/processBuilderExitRequest.ts +5 -9
- package/src/block/processParentExecutionPayload.ts +9 -6
- package/src/index.ts +1 -0
- package/src/rewards/attestationsRewards.ts +17 -5
- 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/gloas.ts +2 -53
- package/src/util/index.ts +6 -0
- package/src/util/sszBytes.ts +129 -22
- package/src/util/weakSubjectivity.ts +43 -40
|
@@ -277,6 +277,8 @@ export interface IBeaconStateViewGloas extends IBeaconStateViewFulu {
|
|
|
277
277
|
executionPayloadAvailability: BitArray;
|
|
278
278
|
latestExecutionPayloadBid: ExecutionPayloadBid;
|
|
279
279
|
payloadExpectedWithdrawals: capella.Withdrawal[];
|
|
280
|
+
builderPendingPayments: gloas.BuilderPendingPayments;
|
|
281
|
+
builderPendingWithdrawals: gloas.BuilderPendingWithdrawals;
|
|
280
282
|
getBuilder(index: BuilderIndex): gloas.Builder;
|
|
281
283
|
getBuildersLength(): number;
|
|
282
284
|
canBuilderCoverBid(builderIndex: BuilderIndex, bidAmount: number): boolean;
|
|
@@ -118,6 +118,8 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
|
|
|
118
118
|
private _latestBlockHash: Bytes32 | null = null;
|
|
119
119
|
private _latestExecutionPayloadBid: ExecutionPayloadBid | null = null;
|
|
120
120
|
private _payloadExpectedWithdrawals: capella.Withdrawal[] | null = null;
|
|
121
|
+
private _builderPendingPayments: gloas.BuilderPendingPayments | null = null;
|
|
122
|
+
private _builderPendingWithdrawals: gloas.BuilderPendingWithdrawals | null = null;
|
|
121
123
|
|
|
122
124
|
// Per-argument caches for argument-taking methods. The binding is treated as
|
|
123
125
|
// immutable for the view's lifetime, so a given argument always yields the
|
|
@@ -892,6 +894,20 @@ export class NativeBeaconStateView implements IBeaconStateViewLatestFork {
|
|
|
892
894
|
return this._payloadExpectedWithdrawals;
|
|
893
895
|
}
|
|
894
896
|
|
|
897
|
+
get builderPendingPayments(): gloas.BuilderPendingPayments {
|
|
898
|
+
if (this._builderPendingPayments === null) {
|
|
899
|
+
this._builderPendingPayments = this.binding.builderPendingPayments;
|
|
900
|
+
}
|
|
901
|
+
return this._builderPendingPayments;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
get builderPendingWithdrawals(): gloas.BuilderPendingWithdrawals {
|
|
905
|
+
if (this._builderPendingWithdrawals === null) {
|
|
906
|
+
this._builderPendingWithdrawals = this.binding.builderPendingWithdrawals;
|
|
907
|
+
}
|
|
908
|
+
return this._builderPendingWithdrawals;
|
|
909
|
+
}
|
|
910
|
+
|
|
895
911
|
getBuilder(index: BuilderIndex): gloas.Builder {
|
|
896
912
|
let cached = this._getBuilder.get(index);
|
|
897
913
|
if (cached === undefined) {
|
|
@@ -1,40 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
|
|
2
2
|
import {BeaconConfig} from "@lodestar/config";
|
|
3
3
|
import {createCachedBeaconState} from "../cache/stateCache.js";
|
|
4
|
-
import {BeaconStateAllForks} from "../cache/types.js";
|
|
5
4
|
import {getStateTypeFromBytes} from "../util/sszBytes.js";
|
|
6
5
|
import {BeaconStateView} from "./beaconStateView.js";
|
|
7
6
|
import {IBeaconStateView} from "./interface.js";
|
|
8
7
|
|
|
9
8
|
// ---- createBeaconStateView (startup path) ----
|
|
10
9
|
|
|
11
|
-
type
|
|
12
|
-
useNative:
|
|
13
|
-
anchorState: BeaconStateAllForks;
|
|
10
|
+
type CreateBeaconStateViewOpts = {
|
|
11
|
+
useNative: boolean;
|
|
14
12
|
config: BeaconConfig;
|
|
15
|
-
pubkeyCache: PubkeyCache;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type NativeOpts = {
|
|
19
|
-
useNative: true;
|
|
20
13
|
stateBytes: Uint8Array;
|
|
21
14
|
};
|
|
22
15
|
|
|
23
16
|
/**
|
|
24
|
-
* Create a BeaconStateView from
|
|
17
|
+
* Create a BeaconStateView from raw SSZ bytes. Used at node startup.
|
|
25
18
|
*
|
|
26
|
-
*
|
|
27
|
-
* passed separately to BeaconNode.init, so it must live outside this factory).
|
|
19
|
+
* Caller must reserve pubkey capacity before calling this function.
|
|
28
20
|
*
|
|
29
21
|
* Set `useNative: true` to use the native (Zig) implementation once available.
|
|
30
22
|
*/
|
|
31
|
-
export function createBeaconStateView(opts:
|
|
23
|
+
export function createBeaconStateView(opts: CreateBeaconStateViewOpts): IBeaconStateView {
|
|
32
24
|
if (opts.useNative) {
|
|
33
25
|
throw new Error("Native (Zig) BeaconStateView not yet implemented");
|
|
34
26
|
// TODO: return a new instance of NativeBeaconStateView
|
|
35
27
|
}
|
|
36
|
-
const {
|
|
37
|
-
const
|
|
28
|
+
const {config, stateBytes} = opts;
|
|
29
|
+
const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
|
|
30
|
+
const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: false});
|
|
38
31
|
return new BeaconStateView(cachedState);
|
|
39
32
|
}
|
|
40
33
|
|
package/src/util/gloas.ts
CHANGED
|
@@ -178,21 +178,6 @@ export function initiateBuilderExit(state: CachedBeaconStateGloas, builderIndex:
|
|
|
178
178
|
builder.withdrawableEpoch = currentEpoch + state.config.MIN_BUILDER_WITHDRAWABILITY_DELAY;
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
/**
|
|
182
|
-
* Find the index of a builder by their public key.
|
|
183
|
-
* Returns null if not found.
|
|
184
|
-
*
|
|
185
|
-
* May consider builder pubkey cache if performance becomes an issue.
|
|
186
|
-
*/
|
|
187
|
-
export function findBuilderIndexByPubkey(state: CachedBeaconStateGloas, pubkey: Uint8Array): BuilderIndex | null {
|
|
188
|
-
for (let i = 0; i < state.builders.length; i++) {
|
|
189
|
-
if (byteArrayEquals(state.builders.getReadonly(i).pubkey, pubkey)) {
|
|
190
|
-
return i;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
181
|
/**
|
|
197
182
|
* Use cached block roots to avoid repeated state root lookups while matching the spec's is_attestation_same_slot behavior.
|
|
198
183
|
*/
|
|
@@ -248,41 +233,6 @@ export function getPtcWindowEpochCacheData(state: CachedBeaconStateGloas): {
|
|
|
248
233
|
};
|
|
249
234
|
}
|
|
250
235
|
|
|
251
|
-
/**
|
|
252
|
-
* Add a new builder to the builders registry. Reuses slots from exited and fully withdrawn
|
|
253
|
-
* builders when available, otherwise appends.
|
|
254
|
-
*
|
|
255
|
-
* Spec: https://github.com/ethereum/consensus-specs/blob/v1.7.0-alpha.11/specs/gloas/beacon-chain.md#new-add_builder_to_registry
|
|
256
|
-
*/
|
|
257
|
-
export function addBuilderToRegistry(
|
|
258
|
-
state: CachedBeaconStateGloas,
|
|
259
|
-
pubkey: Uint8Array,
|
|
260
|
-
version: number,
|
|
261
|
-
executionAddress: Uint8Array,
|
|
262
|
-
amount: number,
|
|
263
|
-
slot: number
|
|
264
|
-
): void {
|
|
265
|
-
const currentEpoch = computeEpochAtSlot(state.slot);
|
|
266
|
-
const depositEpoch = computeEpochAtSlot(slot);
|
|
267
|
-
|
|
268
|
-
let builderIndex = state.builders.length;
|
|
269
|
-
for (let i = 0; i < state.builders.length; i++) {
|
|
270
|
-
const builder = state.builders.getReadonly(i);
|
|
271
|
-
if (builder.withdrawableEpoch <= currentEpoch && builder.balance === 0) {
|
|
272
|
-
builderIndex = i;
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const newBuilder = createBuilderView(pubkey, version, executionAddress, amount, depositEpoch);
|
|
278
|
-
|
|
279
|
-
if (builderIndex < state.builders.length) {
|
|
280
|
-
state.builders.set(builderIndex, newBuilder);
|
|
281
|
-
} else {
|
|
282
|
-
state.builders.push(newBuilder);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
236
|
/**
|
|
287
237
|
* Append a new builder to the registry without scanning for a reusable slot.
|
|
288
238
|
*
|
|
@@ -301,10 +251,9 @@ export function appendBuilderToRegistry(
|
|
|
301
251
|
}
|
|
302
252
|
|
|
303
253
|
/**
|
|
304
|
-
* Build a Builder view
|
|
305
|
-
* and the append-only {@link appendBuilderToRegistry} so both paths produce an identical view.
|
|
254
|
+
* Build a Builder view from builder fields.
|
|
306
255
|
*/
|
|
307
|
-
function createBuilderView(
|
|
256
|
+
export function createBuilderView(
|
|
308
257
|
pubkey: Uint8Array,
|
|
309
258
|
version: number,
|
|
310
259
|
executionAddress: Uint8Array,
|
package/src/util/index.ts
CHANGED
|
@@ -28,6 +28,12 @@ export * from "./signatureSets.js";
|
|
|
28
28
|
export * from "./signingRoot.js";
|
|
29
29
|
export * from "./slot.js";
|
|
30
30
|
export * from "./ssz.js";
|
|
31
|
+
export {
|
|
32
|
+
type StateBytesMetadata,
|
|
33
|
+
getValidatorCountFromStateBytes,
|
|
34
|
+
getValidatorPubkeyFromStateBytes,
|
|
35
|
+
readBeaconStateBytesMetadata,
|
|
36
|
+
} from "./sszBytes.js";
|
|
31
37
|
export * from "./syncCommittee.js";
|
|
32
38
|
export * from "./validator.js";
|
|
33
39
|
export * from "./weakSubjectivity.js";
|
package/src/util/sszBytes.ts
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
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 PUBKEY_SIZE = 48;
|
|
23
|
+
const VALIDATOR_EFFECTIVE_BALANCE_OFFSET = PUBKEY_SIZE + ROOT_SIZE;
|
|
24
|
+
const VALIDATOR_ACTIVATION_EPOCH_OFFSET = VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE + 1 + UINT64_SIZE;
|
|
25
|
+
const VALIDATOR_EXIT_EPOCH_OFFSET = VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE;
|
|
26
|
+
export const VALIDATOR_BYTES_SIZE = VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE + UINT64_SIZE;
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
|
-
* 8 + 32 = 40
|
|
30
29
|
* ```
|
|
31
30
|
* class BeaconState(Container):
|
|
32
31
|
* genesis_time: uint64 [fixed - 8 bytes]
|
|
@@ -35,10 +34,12 @@ export const VALIDATOR_BYTES_SIZE = 121;
|
|
|
35
34
|
* ...
|
|
36
35
|
* ```
|
|
37
36
|
*/
|
|
38
|
-
const
|
|
37
|
+
const STATE_GENESIS_TIME_OFFSET = 0;
|
|
38
|
+
const STATE_GENESIS_VALIDATORS_ROOT_OFFSET = STATE_GENESIS_TIME_OFFSET + UINT64_SIZE;
|
|
39
|
+
const STATE_SLOT_OFFSET = STATE_GENESIS_VALIDATORS_ROOT_OFFSET + ROOT_SIZE;
|
|
39
40
|
|
|
40
41
|
export function getForkFromStateBytes(config: ChainForkConfig, bytes: Uint8Array): ForkSeq {
|
|
41
|
-
const slot = bytesToInt(bytes.subarray(
|
|
42
|
+
const slot = bytesToInt(bytes.subarray(STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE));
|
|
42
43
|
return config.getForkSeq(slot);
|
|
43
44
|
}
|
|
44
45
|
|
|
@@ -48,5 +49,111 @@ export function getStateTypeFromBytes(config: ChainForkConfig, bytes: Uint8Array
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
export function getStateSlotFromBytes(bytes: Uint8Array): Slot {
|
|
51
|
-
return bytesToInt(bytes.subarray(
|
|
52
|
+
return bytesToInt(bytes.subarray(STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type StateBytesMetadata = {slot: Slot; genesisTime: number; genesisValidatorsRoot: Uint8Array};
|
|
56
|
+
|
|
57
|
+
export function readBeaconStateBytesMetadata(bytes: Uint8Array): StateBytesMetadata | null {
|
|
58
|
+
if (bytes.length < STATE_SLOT_OFFSET + UINT64_SIZE) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const data = {uint8Array: bytes, dataView: new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)};
|
|
62
|
+
return {
|
|
63
|
+
genesisTime: ssz.UintNum64.value_deserializeFromBytes(
|
|
64
|
+
data,
|
|
65
|
+
STATE_GENESIS_TIME_OFFSET,
|
|
66
|
+
STATE_GENESIS_TIME_OFFSET + UINT64_SIZE
|
|
67
|
+
),
|
|
68
|
+
genesisValidatorsRoot: Uint8Array.from(
|
|
69
|
+
bytes.subarray(STATE_GENESIS_VALIDATORS_ROOT_OFFSET, STATE_GENESIS_VALIDATORS_ROOT_OFFSET + ROOT_SIZE)
|
|
70
|
+
),
|
|
71
|
+
slot: ssz.Slot.value_deserializeFromBytes(data, STATE_SLOT_OFFSET, STATE_SLOT_OFFSET + UINT64_SIZE),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Read only the validator fields needed for weak subjectivity, without allocating validator objects. */
|
|
76
|
+
export function scanActiveValidatorsFromStateBytes(
|
|
77
|
+
stateBytes: Uint8Array,
|
|
78
|
+
stateType: SSZTypesFor<ForkAll, "BeaconState">,
|
|
79
|
+
epoch: Epoch
|
|
80
|
+
): {activeValidatorCount: number; totalActiveBalanceIncrements: number} | null {
|
|
81
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, stateType);
|
|
82
|
+
if (range === null) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const data = {
|
|
87
|
+
uint8Array: stateBytes,
|
|
88
|
+
dataView: new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength),
|
|
89
|
+
};
|
|
90
|
+
let activeValidatorCount = 0;
|
|
91
|
+
let totalActiveBalanceIncrements = 0;
|
|
92
|
+
for (let offset = range.start; offset < range.end; offset += VALIDATOR_BYTES_SIZE) {
|
|
93
|
+
const activationEpoch = ssz.EpochInf.value_deserializeFromBytes(
|
|
94
|
+
data,
|
|
95
|
+
offset + VALIDATOR_ACTIVATION_EPOCH_OFFSET,
|
|
96
|
+
offset + VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE
|
|
97
|
+
);
|
|
98
|
+
const exitEpoch = ssz.EpochInf.value_deserializeFromBytes(
|
|
99
|
+
data,
|
|
100
|
+
offset + VALIDATOR_EXIT_EPOCH_OFFSET,
|
|
101
|
+
offset + VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE
|
|
102
|
+
);
|
|
103
|
+
if (activationEpoch <= epoch && epoch < exitEpoch) {
|
|
104
|
+
activeValidatorCount++;
|
|
105
|
+
const effectiveBalance = ssz.UintNum64.value_deserializeFromBytes(
|
|
106
|
+
data,
|
|
107
|
+
offset + VALIDATOR_EFFECTIVE_BALANCE_OFFSET,
|
|
108
|
+
offset + VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE
|
|
109
|
+
);
|
|
110
|
+
totalActiveBalanceIncrements += Math.floor(effectiveBalance / EFFECTIVE_BALANCE_INCREMENT);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {activeValidatorCount, totalActiveBalanceIncrements: Math.max(1, totalActiveBalanceIncrements)};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function getValidatorCountFromStateBytes(config: ChainForkConfig, stateBytes: Uint8Array): number | null {
|
|
117
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, getStateTypeFromBytes(config, stateBytes));
|
|
118
|
+
// A non-null range always holds a whole number of validator records, so the division is exact
|
|
119
|
+
return range === null ? null : (range.end - range.start) / VALIDATOR_BYTES_SIZE;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Returns a view into `stateBytes`, or null if the index is not in the registry. */
|
|
123
|
+
export function getValidatorPubkeyFromStateBytes(
|
|
124
|
+
config: ChainForkConfig,
|
|
125
|
+
stateBytes: Uint8Array,
|
|
126
|
+
index: number
|
|
127
|
+
): Uint8Array | null {
|
|
128
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, getStateTypeFromBytes(config, stateBytes));
|
|
129
|
+
if (range === null) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const start = range.start + index * VALIDATOR_BYTES_SIZE;
|
|
133
|
+
if (index < 0 || start >= range.end) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
return stateBytes.subarray(start, start + PUBKEY_SIZE);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function getValidatorsRangeFromStateBytes(
|
|
140
|
+
stateBytes: Uint8Array,
|
|
141
|
+
stateType: SSZTypesFor<ForkAll, "BeaconState">
|
|
142
|
+
): {start: number; end: number} | null {
|
|
143
|
+
let range: {start: number; end: number};
|
|
144
|
+
try {
|
|
145
|
+
const containerType = stateType as (typeof ssz)[ForkAll]["BeaconState"];
|
|
146
|
+
const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
|
|
147
|
+
const ranges = containerType.getFieldRanges(dataView, 0, stateBytes.length);
|
|
148
|
+
range = ranges[Object.keys(containerType.fields).indexOf("validators")];
|
|
149
|
+
} catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (!range || range.start < 0 || range.end > stateBytes.length || range.end < range.start) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
if ((range.end - range.start) % VALIDATOR_BYTES_SIZE !== 0) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return range;
|
|
52
159
|
}
|
|
@@ -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
|
}
|