@lodestar/state-transition 1.50.0-dev.9c3f7bebe9 → 1.50.0-dev.9c7f3c203f
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 +23 -18
- package/lib/block/processParentExecutionPayload.js.map +1 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/rewards/attestationsRewards.d.ts.map +1 -1
- package/lib/rewards/attestationsRewards.js +8 -1
- package/lib/rewards/attestationsRewards.js.map +1 -1
- package/lib/signatureSets/index.d.ts.map +1 -1
- package/lib/signatureSets/index.js +2 -2
- package/lib/signatureSets/index.js.map +1 -1
- package/lib/signatureSets/voluntaryExits.d.ts +2 -6
- package/lib/signatureSets/voluntaryExits.d.ts.map +1 -1
- package/lib/signatureSets/voluntaryExits.js +2 -32
- package/lib/signatureSets/voluntaryExits.js.map +1 -1
- package/lib/stateView/beaconStateView.d.ts +22 -21
- package/lib/stateView/beaconStateView.d.ts.map +1 -1
- package/lib/stateView/beaconStateView.js +71 -71
- package/lib/stateView/beaconStateView.js.map +1 -1
- package/lib/stateView/interface.d.ts +34 -21
- package/lib/stateView/interface.d.ts.map +1 -1
- package/lib/stateView/interface.js.map +1 -1
- package/lib/stateView/nativeBeaconStateView.d.ts +92 -100
- package/lib/stateView/nativeBeaconStateView.d.ts.map +1 -1
- package/lib/stateView/nativeBeaconStateView.js +339 -368
- package/lib/stateView/nativeBeaconStateView.js.map +1 -1
- package/lib/stateView/stateViewFactory.d.ts +10 -17
- package/lib/stateView/stateViewFactory.d.ts.map +1 -1
- package/lib/stateView/stateViewFactory.js +17 -13
- 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 -1
- package/lib/util/index.d.ts.map +1 -1
- package/lib/util/index.js +1 -1
- package/lib/util/index.js.map +1 -1
- package/lib/util/sszBytes.d.ts +3 -0
- package/lib/util/sszBytes.d.ts.map +1 -1
- package/lib/util/sszBytes.js +40 -14
- package/lib/util/sszBytes.js.map +1 -1
- package/package.json +8 -8
- 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 +27 -23
- package/src/index.ts +4 -1
- package/src/rewards/attestationsRewards.ts +8 -1
- package/src/signatureSets/index.ts +4 -2
- package/src/signatureSets/voluntaryExits.ts +3 -55
- package/src/stateView/beaconStateView.ts +75 -76
- package/src/stateView/interface.ts +61 -21
- package/src/stateView/nativeBeaconStateView.ts +354 -375
- package/src/stateView/stateViewFactory.ts +29 -27
- package/src/util/gloas.ts +2 -53
- package/src/util/index.ts +6 -1
- package/src/util/sszBytes.ts +52 -16
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
import
|
|
1
|
+
import bindings from "@chainsafe/lodestar-z";
|
|
2
|
+
import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
|
|
2
3
|
import {BeaconConfig} from "@lodestar/config";
|
|
3
4
|
import {createCachedBeaconState} from "../cache/stateCache.js";
|
|
4
|
-
import {BeaconStateAllForks} from "../cache/types.js";
|
|
5
5
|
import {getStateTypeFromBytes} from "../util/sszBytes.js";
|
|
6
6
|
import {BeaconStateView} from "./beaconStateView.js";
|
|
7
|
-
import {IBeaconStateView} from "./interface.js";
|
|
7
|
+
import {IBeaconStateView, IBeaconStateViewNative} from "./interface.js";
|
|
8
|
+
import {NativeBeaconStateView} from "./nativeBeaconStateView.js";
|
|
8
9
|
|
|
9
10
|
// ---- createBeaconStateView (startup path) ----
|
|
10
11
|
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
anchorState: BeaconStateAllForks;
|
|
12
|
+
type CreateBeaconStateViewOpts = {
|
|
13
|
+
nativeStateTransition: boolean;
|
|
14
14
|
config: BeaconConfig;
|
|
15
|
-
pubkeyCache: PubkeyCache;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type NativeOpts = {
|
|
19
|
-
useNative: true;
|
|
20
15
|
stateBytes: Uint8Array;
|
|
21
16
|
};
|
|
22
17
|
|
|
23
18
|
/**
|
|
24
|
-
* Create a BeaconStateView from
|
|
19
|
+
* Create a BeaconStateView from raw SSZ bytes. Used at node startup.
|
|
25
20
|
*
|
|
26
|
-
*
|
|
27
|
-
* passed separately to BeaconNode.init, so it must live outside this factory).
|
|
21
|
+
* Caller must reserve pubkey capacity before calling this function.
|
|
28
22
|
*
|
|
29
|
-
* Set `
|
|
23
|
+
* Set `nativeStateTransition: true` to use the native (Zig) implementation.
|
|
30
24
|
*/
|
|
31
|
-
export function createBeaconStateView(opts:
|
|
32
|
-
if (opts.
|
|
33
|
-
|
|
34
|
-
// TODO: return a new instance of NativeBeaconStateView
|
|
25
|
+
export function createBeaconStateView(opts: CreateBeaconStateViewOpts): IBeaconStateView {
|
|
26
|
+
if (opts.nativeStateTransition) {
|
|
27
|
+
return createNativeBeaconStateView(opts.config, opts.stateBytes);
|
|
35
28
|
}
|
|
36
|
-
const {
|
|
37
|
-
const
|
|
29
|
+
const {config, stateBytes} = opts;
|
|
30
|
+
const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
|
|
31
|
+
const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: false});
|
|
38
32
|
return new BeaconStateView(cachedState);
|
|
39
33
|
}
|
|
40
34
|
|
|
41
35
|
// ---- createBeaconStateViewForHistoricalRegen (regen path) ----
|
|
42
36
|
|
|
43
37
|
type RegenNodeJSOpts = {
|
|
44
|
-
|
|
38
|
+
nativeStateTransition: false;
|
|
45
39
|
config: BeaconConfig;
|
|
46
40
|
stateBytes: Uint8Array;
|
|
47
41
|
};
|
|
48
42
|
|
|
49
43
|
type RegenNativeOpts = {
|
|
50
|
-
|
|
44
|
+
nativeStateTransition: true;
|
|
45
|
+
config: BeaconConfig;
|
|
51
46
|
stateBytes: Uint8Array;
|
|
52
47
|
};
|
|
53
48
|
|
|
54
49
|
/**
|
|
55
50
|
* Create a BeaconStateView from raw SSZ bytes. Used in the historical state regen worker thread.
|
|
56
51
|
*
|
|
57
|
-
* Set `
|
|
52
|
+
* Set `nativeStateTransition: true` to use the native (Zig) implementation.
|
|
58
53
|
*/
|
|
59
54
|
export function createBeaconStateViewForHistoricalRegen(opts: RegenNodeJSOpts | RegenNativeOpts): IBeaconStateView {
|
|
60
|
-
if (opts.
|
|
61
|
-
|
|
62
|
-
// TODO: return a new instance of NativeBeaconStateView
|
|
55
|
+
if (opts.nativeStateTransition) {
|
|
56
|
+
return createNativeBeaconStateView(opts.config, opts.stateBytes);
|
|
63
57
|
}
|
|
64
58
|
const {config, stateBytes} = opts;
|
|
65
59
|
const state = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);
|
|
66
60
|
const cachedState = createCachedBeaconState(state, {config, pubkeyCache}, {skipSyncPubkeys: true});
|
|
67
61
|
return new BeaconStateView(cachedState);
|
|
68
62
|
}
|
|
63
|
+
|
|
64
|
+
function createNativeBeaconStateView(config: BeaconConfig, stateBytes: Uint8Array): IBeaconStateView {
|
|
65
|
+
const nativeConfig = new bindings.BeaconConfig(config, config.genesisValidatorsRoot);
|
|
66
|
+
return new NativeBeaconStateView(
|
|
67
|
+
config,
|
|
68
|
+
bindings.BeaconStateView.createFromBytes(stateBytes, nativeConfig) as IBeaconStateViewNative
|
|
69
|
+
);
|
|
70
|
+
}
|
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,7 +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 {
|
|
31
|
+
export {
|
|
32
|
+
type StateBytesMetadata,
|
|
33
|
+
getValidatorCountFromStateBytes,
|
|
34
|
+
getValidatorPubkeyFromStateBytes,
|
|
35
|
+
readBeaconStateBytesMetadata,
|
|
36
|
+
} from "./sszBytes.js";
|
|
32
37
|
export * from "./syncCommittee.js";
|
|
33
38
|
export * from "./validator.js";
|
|
34
39
|
export * from "./weakSubjectivity.js";
|
package/src/util/sszBytes.ts
CHANGED
|
@@ -19,7 +19,8 @@ const ROOT_SIZE = 32;
|
|
|
19
19
|
* withdrawable_epoch: Epoch [fixed - 8 bytes]
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
|
-
const
|
|
22
|
+
const PUBKEY_SIZE = 48;
|
|
23
|
+
const VALIDATOR_EFFECTIVE_BALANCE_OFFSET = PUBKEY_SIZE + ROOT_SIZE;
|
|
23
24
|
const VALIDATOR_ACTIVATION_EPOCH_OFFSET = VALIDATOR_EFFECTIVE_BALANCE_OFFSET + UINT64_SIZE + 1 + UINT64_SIZE;
|
|
24
25
|
const VALIDATOR_EXIT_EPOCH_OFFSET = VALIDATOR_ACTIVATION_EPOCH_OFFSET + UINT64_SIZE;
|
|
25
26
|
export const VALIDATOR_BYTES_SIZE = VALIDATOR_EXIT_EPOCH_OFFSET + UINT64_SIZE + UINT64_SIZE;
|
|
@@ -77,25 +78,15 @@ export function scanActiveValidatorsFromStateBytes(
|
|
|
77
78
|
stateType: SSZTypesFor<ForkAll, "BeaconState">,
|
|
78
79
|
epoch: Epoch
|
|
79
80
|
): {activeValidatorCount: number; totalActiveBalanceIncrements: number} | null {
|
|
81
|
+
const range = getValidatorsRangeFromStateBytes(stateBytes, stateType);
|
|
82
|
+
if (range === null) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
80
86
|
const data = {
|
|
81
87
|
uint8Array: stateBytes,
|
|
82
88
|
dataView: new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength),
|
|
83
89
|
};
|
|
84
|
-
let range: {start: number; end: number};
|
|
85
|
-
try {
|
|
86
|
-
const containerType = stateType as (typeof ssz)[ForkAll]["BeaconState"];
|
|
87
|
-
const ranges = containerType.getFieldRanges(data.dataView, 0, stateBytes.length);
|
|
88
|
-
range = ranges[Object.keys(containerType.fields).indexOf("validators")];
|
|
89
|
-
} catch {
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
if (!range || range.start < 0 || range.end > stateBytes.length || range.end < range.start) {
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
if ((range.end - range.start) % VALIDATOR_BYTES_SIZE !== 0) {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
90
|
let activeValidatorCount = 0;
|
|
100
91
|
let totalActiveBalanceIncrements = 0;
|
|
101
92
|
for (let offset = range.start; offset < range.end; offset += VALIDATOR_BYTES_SIZE) {
|
|
@@ -121,3 +112,48 @@ export function scanActiveValidatorsFromStateBytes(
|
|
|
121
112
|
}
|
|
122
113
|
return {activeValidatorCount, totalActiveBalanceIncrements: Math.max(1, totalActiveBalanceIncrements)};
|
|
123
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;
|
|
159
|
+
}
|