@lodestar/state-transition 1.39.0-dev.ad23ef56aa → 1.39.0-dev.b2437a6348
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/cache/epochCache.d.ts +6 -12
- package/lib/cache/epochCache.d.ts.map +1 -1
- package/lib/cache/epochCache.js +33 -105
- package/lib/cache/epochCache.js.map +1 -1
- package/lib/cache/epochTransitionCache.d.ts +5 -12
- package/lib/cache/epochTransitionCache.d.ts.map +1 -1
- package/lib/cache/epochTransitionCache.js +4 -14
- package/lib/cache/epochTransitionCache.js.map +1 -1
- package/lib/cache/stateCache.d.ts.map +1 -1
- package/lib/cache/stateCache.js +1 -2
- package/lib/cache/stateCache.js.map +1 -1
- package/lib/epoch/processProposerLookahead.d.ts.map +1 -1
- package/lib/epoch/processProposerLookahead.js +3 -6
- package/lib/epoch/processProposerLookahead.js.map +1 -1
- package/lib/rewards/blockRewards.d.ts +2 -1
- package/lib/rewards/blockRewards.d.ts.map +1 -1
- package/lib/rewards/blockRewards.js +3 -2
- package/lib/rewards/blockRewards.js.map +1 -1
- package/lib/rewards/syncCommitteeRewards.d.ts.map +1 -1
- package/lib/rewards/syncCommitteeRewards.js +10 -11
- package/lib/rewards/syncCommitteeRewards.js.map +1 -1
- package/lib/types.d.ts +1 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/util/epochShuffling.d.ts +1 -34
- package/lib/util/epochShuffling.d.ts.map +1 -1
- package/lib/util/epochShuffling.js +1 -1
- package/lib/util/epochShuffling.js.map +1 -1
- package/package.json +7 -7
- package/src/cache/epochCache.ts +40 -118
- package/src/cache/epochTransitionCache.ts +9 -34
- package/src/cache/stateCache.ts +1 -2
- package/src/epoch/processProposerLookahead.ts +3 -7
- package/src/rewards/blockRewards.ts +6 -3
- package/src/rewards/syncCommitteeRewards.ts +10 -13
- package/src/types.ts +1 -0
- package/src/util/epochShuffling.ts +2 -43
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
ForkSeq,
|
|
5
|
-
MIN_ACTIVATION_BALANCE,
|
|
6
|
-
SLOTS_PER_HISTORICAL_ROOT,
|
|
7
|
-
} from "@lodestar/params";
|
|
8
|
-
import {Epoch, RootHex, ValidatorIndex} from "@lodestar/types";
|
|
9
|
-
import {intDiv, toRootHex} from "@lodestar/utils";
|
|
1
|
+
import {EPOCHS_PER_SLASHINGS_VECTOR, FAR_FUTURE_EPOCH, ForkSeq, MIN_ACTIVATION_BALANCE} from "@lodestar/params";
|
|
2
|
+
import {Epoch, ValidatorIndex} from "@lodestar/types";
|
|
3
|
+
import {intDiv} from "@lodestar/utils";
|
|
10
4
|
import {processPendingAttestations} from "../epoch/processPendingAttestations.js";
|
|
11
5
|
import {
|
|
12
6
|
CachedBeaconStateAllForks,
|
|
@@ -26,16 +20,13 @@ import {
|
|
|
26
20
|
FLAG_UNSLASHED,
|
|
27
21
|
hasMarkers,
|
|
28
22
|
} from "../util/attesterStatus.js";
|
|
23
|
+
import {EpochShuffling} from "../util/epochShuffling.js";
|
|
29
24
|
|
|
30
25
|
export type EpochTransitionCacheOpts = {
|
|
31
26
|
/**
|
|
32
27
|
* Assert progressive balances the same to EpochTransitionCache
|
|
33
28
|
*/
|
|
34
29
|
assertCorrectProgressiveBalances?: boolean;
|
|
35
|
-
/**
|
|
36
|
-
* Do not queue shuffling calculation async. Forces sync JIT calculation in afterProcessEpoch
|
|
37
|
-
*/
|
|
38
|
-
asyncShufflingCalculation?: boolean;
|
|
39
30
|
};
|
|
40
31
|
|
|
41
32
|
/**
|
|
@@ -162,9 +153,10 @@ export interface EpochTransitionCache {
|
|
|
162
153
|
nextShufflingActiveIndices: Uint32Array;
|
|
163
154
|
|
|
164
155
|
/**
|
|
165
|
-
*
|
|
156
|
+
* Pre-computed shuffling for epoch N+2, populated by processProposerLookahead (Fulu+).
|
|
157
|
+
* Used by afterProcessEpoch to avoid recomputing the same shuffling.
|
|
166
158
|
*/
|
|
167
|
-
|
|
159
|
+
nextShuffling: EpochShuffling | null;
|
|
168
160
|
|
|
169
161
|
/**
|
|
170
162
|
* Altair specific, this is total active balances for the next epoch.
|
|
@@ -179,12 +171,6 @@ export interface EpochTransitionCache {
|
|
|
179
171
|
*/
|
|
180
172
|
nextEpochTotalActiveBalanceByIncrement: number;
|
|
181
173
|
|
|
182
|
-
/**
|
|
183
|
-
* Compute the shuffling sync or async. Defaults to synchronous. Need to pass `true` with the
|
|
184
|
-
* `EpochTransitionCacheOpts`
|
|
185
|
-
*/
|
|
186
|
-
asyncShufflingCalculation: boolean;
|
|
187
|
-
|
|
188
174
|
/**
|
|
189
175
|
* Track by validator index if it's active in the prev epoch.
|
|
190
176
|
* Used in metrics
|
|
@@ -379,12 +365,7 @@ export function beforeProcessEpoch(
|
|
|
379
365
|
}
|
|
380
366
|
});
|
|
381
367
|
|
|
382
|
-
//
|
|
383
|
-
const epochAfterNext = state.epochCtx.nextEpoch + 1;
|
|
384
|
-
// cannot call calculateShufflingDecisionRoot here because spec prevent getting current slot
|
|
385
|
-
// as a decision block. we are part way through the transition though and this was added in
|
|
386
|
-
// process slot beforeProcessEpoch happens so it available and valid
|
|
387
|
-
const nextShufflingDecisionRoot = toRootHex(state.blockRoots.get(state.slot % SLOTS_PER_HISTORICAL_ROOT));
|
|
368
|
+
// Prepare shuffling data for epoch after next (nextShuffling post epoch transition)
|
|
388
369
|
const nextShufflingActiveIndices = new Uint32Array(nextEpochShufflingActiveIndicesLength);
|
|
389
370
|
if (nextEpochShufflingActiveIndicesLength > nextEpochShufflingActiveValidatorIndices.length) {
|
|
390
371
|
throw new Error(
|
|
@@ -396,11 +377,6 @@ export function beforeProcessEpoch(
|
|
|
396
377
|
nextShufflingActiveIndices[i] = nextEpochShufflingActiveValidatorIndices[i];
|
|
397
378
|
}
|
|
398
379
|
|
|
399
|
-
const asyncShufflingCalculation = opts?.asyncShufflingCalculation ?? false;
|
|
400
|
-
if (asyncShufflingCalculation) {
|
|
401
|
-
state.epochCtx.shufflingCache?.build(epochAfterNext, nextShufflingDecisionRoot, state, nextShufflingActiveIndices);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
380
|
if (totalActiveStakeByIncrement < 1) {
|
|
405
381
|
totalActiveStakeByIncrement = 1;
|
|
406
382
|
} else if (totalActiveStakeByIncrement >= Number.MAX_SAFE_INTEGER) {
|
|
@@ -524,9 +500,8 @@ export function beforeProcessEpoch(
|
|
|
524
500
|
indicesEligibleForActivationQueue,
|
|
525
501
|
indicesEligibleForActivation: indicesEligibleForActivation.map(({validatorIndex}) => validatorIndex),
|
|
526
502
|
indicesToEject,
|
|
527
|
-
nextShufflingDecisionRoot,
|
|
528
503
|
nextShufflingActiveIndices,
|
|
529
|
-
|
|
504
|
+
nextShuffling: null,
|
|
530
505
|
// to be updated in processEffectiveBalanceUpdates
|
|
531
506
|
nextEpochTotalActiveBalanceByIncrement: 0,
|
|
532
507
|
isActivePrevEpoch,
|
package/src/cache/stateCache.ts
CHANGED
|
@@ -180,7 +180,7 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
|
|
|
180
180
|
stateBytes,
|
|
181
181
|
seedValidatorsBytes
|
|
182
182
|
);
|
|
183
|
-
const {pubkey2index, index2pubkey
|
|
183
|
+
const {pubkey2index, index2pubkey} = cachedSeedState.epochCtx;
|
|
184
184
|
// Get the validators sub tree once for all the loop
|
|
185
185
|
const validators = migratedState.validators;
|
|
186
186
|
for (const validatorIndex of modifiedValidators) {
|
|
@@ -196,7 +196,6 @@ export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStat
|
|
|
196
196
|
config: cachedSeedState.config,
|
|
197
197
|
pubkey2index,
|
|
198
198
|
index2pubkey,
|
|
199
|
-
shufflingCache,
|
|
200
199
|
},
|
|
201
200
|
{...(opts ?? {}), ...{skipSyncPubkeys: true}}
|
|
202
201
|
) as T;
|
|
@@ -22,13 +22,9 @@ export function processProposerLookahead(
|
|
|
22
22
|
// Fill in the last epoch with new proposer indices
|
|
23
23
|
const epoch = state.epochCtx.epoch + MIN_SEED_LOOKAHEAD + 1;
|
|
24
24
|
|
|
25
|
-
const shuffling =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
activeIndices: cache.nextShufflingActiveIndices,
|
|
29
|
-
}) ??
|
|
30
|
-
// Only for testing. shufflingCache should always be available in prod
|
|
31
|
-
computeEpochShuffling(state, cache.nextShufflingActiveIndices, epoch);
|
|
25
|
+
const shuffling = computeEpochShuffling(state, cache.nextShufflingActiveIndices, epoch);
|
|
26
|
+
// Save shuffling to cache so afterProcessEpoch can reuse it instead of recomputing
|
|
27
|
+
cache.nextShuffling = shuffling;
|
|
32
28
|
|
|
33
29
|
const lastEpochProposerLookahead = computeProposerIndices(fork, state, shuffling, epoch);
|
|
34
30
|
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
} from "@lodestar/params";
|
|
8
8
|
import {BeaconBlock, altair, phase0, rewards} from "@lodestar/types";
|
|
9
9
|
import {processAttestationsAltair} from "../block/processAttestationsAltair.js";
|
|
10
|
+
import {RewardCache} from "../cache/rewardCache.js";
|
|
10
11
|
import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../cache/stateCache.js";
|
|
11
12
|
import {getAttesterSlashableIndices} from "../util/attestation.js";
|
|
12
13
|
|
|
@@ -23,12 +24,14 @@ type SubRewardValue = number; // All reward values should be integer
|
|
|
23
24
|
export async function computeBlockRewards(
|
|
24
25
|
config: BeaconConfig,
|
|
25
26
|
block: BeaconBlock,
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
preStateIn: CachedBeaconStateAllForks,
|
|
28
|
+
proposerRewards?: RewardCache
|
|
28
29
|
): Promise<rewards.BlockRewards> {
|
|
30
|
+
const preState = preStateIn.clone();
|
|
31
|
+
|
|
29
32
|
const fork = config.getForkName(block.slot);
|
|
30
33
|
const {attestations: cachedAttestationsReward = 0, syncAggregate: cachedSyncAggregateReward = 0} =
|
|
31
|
-
|
|
34
|
+
proposerRewards ?? {};
|
|
32
35
|
let blockAttestationReward = cachedAttestationsReward;
|
|
33
36
|
let syncAggregateReward = cachedSyncAggregateReward;
|
|
34
37
|
|
|
@@ -4,8 +4,6 @@ import {BeaconBlock, ValidatorIndex, altair, rewards} from "@lodestar/types";
|
|
|
4
4
|
import {Index2PubkeyCache} from "../cache/pubkeyCache.js";
|
|
5
5
|
import {CachedBeaconStateAllForks, CachedBeaconStateAltair} from "../cache/stateCache.js";
|
|
6
6
|
|
|
7
|
-
type BalanceRecord = {val: number}; // Use val for convenient way to increment/decrement balance
|
|
8
|
-
|
|
9
7
|
export async function computeSyncCommitteeRewards(
|
|
10
8
|
config: BeaconConfig,
|
|
11
9
|
index2pubkey: Index2PubkeyCache,
|
|
@@ -19,7 +17,7 @@ export async function computeSyncCommitteeRewards(
|
|
|
19
17
|
}
|
|
20
18
|
|
|
21
19
|
const altairBlock = block as altair.BeaconBlock;
|
|
22
|
-
const preStateAltair = preState as CachedBeaconStateAltair;
|
|
20
|
+
const preStateAltair = preState.clone() as CachedBeaconStateAltair;
|
|
23
21
|
|
|
24
22
|
// Bound syncCommitteeValidatorIndices in case it goes beyond SYNC_COMMITTEE_SIZE just to be safe
|
|
25
23
|
const syncCommitteeValidatorIndices = preStateAltair.epochCtx.currentSyncCommitteeIndexed.validatorIndices.slice(
|
|
@@ -29,24 +27,23 @@ export async function computeSyncCommitteeRewards(
|
|
|
29
27
|
const {syncParticipantReward} = preStateAltair.epochCtx;
|
|
30
28
|
const {syncCommitteeBits} = altairBlock.body.syncAggregate;
|
|
31
29
|
|
|
32
|
-
//
|
|
33
|
-
const
|
|
34
|
-
for (const i of syncCommitteeValidatorIndices) {
|
|
35
|
-
balances.set(i, {val: preStateAltair.balances.get(i)});
|
|
36
|
-
}
|
|
30
|
+
// Track reward deltas per validator (can appear multiple times in sync committee)
|
|
31
|
+
const rewardDeltas: Map<ValidatorIndex, number> = new Map();
|
|
37
32
|
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
// Iterate by position index to correctly access syncCommitteeBits
|
|
34
|
+
for (let i = 0; i < syncCommitteeValidatorIndices.length; i++) {
|
|
35
|
+
const validatorIndex = syncCommitteeValidatorIndices[i];
|
|
36
|
+
const currentDelta = rewardDeltas.get(validatorIndex) ?? 0;
|
|
40
37
|
if (syncCommitteeBits.get(i)) {
|
|
41
38
|
// Positive rewards for participants
|
|
42
|
-
|
|
39
|
+
rewardDeltas.set(validatorIndex, currentDelta + syncParticipantReward);
|
|
43
40
|
} else {
|
|
44
41
|
// Negative rewards for non participants
|
|
45
|
-
|
|
42
|
+
rewardDeltas.set(validatorIndex, currentDelta - syncParticipantReward);
|
|
46
43
|
}
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
const rewards = Array.from(
|
|
46
|
+
const rewards = Array.from(rewardDeltas, ([validatorIndex, reward]) => ({validatorIndex, reward}));
|
|
50
47
|
|
|
51
48
|
if (validatorIds.length) {
|
|
52
49
|
const filtersSet = new Set(validatorIds);
|
package/src/types.ts
CHANGED
|
@@ -9,54 +9,13 @@ import {
|
|
|
9
9
|
TARGET_COMMITTEE_SIZE,
|
|
10
10
|
} from "@lodestar/params";
|
|
11
11
|
import {Epoch, RootHex, ValidatorIndex, ssz} from "@lodestar/types";
|
|
12
|
-
import {
|
|
12
|
+
import {intDiv, toRootHex} from "@lodestar/utils";
|
|
13
13
|
import {BeaconStateAllForks} from "../types.js";
|
|
14
14
|
import {getBlockRootAtSlot} from "./blockRoot.js";
|
|
15
15
|
import {computeAnchorCheckpoint} from "./computeAnchorCheckpoint.js";
|
|
16
16
|
import {computeStartSlotAtEpoch} from "./epoch.js";
|
|
17
17
|
import {getSeed} from "./seed.js";
|
|
18
18
|
|
|
19
|
-
export interface ShufflingBuildProps {
|
|
20
|
-
state: BeaconStateAllForks;
|
|
21
|
-
activeIndices: Uint32Array;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface PublicShufflingCacheMetrics {
|
|
25
|
-
shufflingCache: {
|
|
26
|
-
nextShufflingNotOnEpochCache: GaugeExtra<NoLabels>;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
export interface IShufflingCache {
|
|
30
|
-
metrics: PublicShufflingCacheMetrics | null;
|
|
31
|
-
logger: Logger | null;
|
|
32
|
-
/**
|
|
33
|
-
* Gets a cached shuffling via the epoch and decision root. If the state and
|
|
34
|
-
* activeIndices are passed and a shuffling is not available it will be built
|
|
35
|
-
* synchronously. If the state is not passed and the shuffling is not available
|
|
36
|
-
* nothing will be returned.
|
|
37
|
-
*
|
|
38
|
-
* NOTE: If a shuffling is already queued and not calculated it will build and resolve
|
|
39
|
-
* the promise but the already queued build will happen at some later time
|
|
40
|
-
*/
|
|
41
|
-
getSync<T extends ShufflingBuildProps | undefined>(
|
|
42
|
-
epoch: Epoch,
|
|
43
|
-
decisionRoot: RootHex,
|
|
44
|
-
buildProps?: T
|
|
45
|
-
): T extends ShufflingBuildProps ? EpochShuffling : EpochShuffling | null;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Gets a cached shuffling via the epoch and decision root. Returns a promise
|
|
49
|
-
* for the shuffling if it hs not calculated yet. Returns null if a build has
|
|
50
|
-
* not been queued nor a shuffling was calculated.
|
|
51
|
-
*/
|
|
52
|
-
get(epoch: Epoch, decisionRoot: RootHex): Promise<EpochShuffling | null>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Queue asynchronous build for an EpochShuffling
|
|
56
|
-
*/
|
|
57
|
-
build(epoch: Epoch, decisionRoot: RootHex, state: BeaconStateAllForks, activeIndices: Uint32Array): void;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
19
|
/**
|
|
61
20
|
* Readonly interface for EpochShuffling.
|
|
62
21
|
*/
|
|
@@ -164,7 +123,7 @@ export async function computeEpochShufflingAsync(
|
|
|
164
123
|
};
|
|
165
124
|
}
|
|
166
125
|
|
|
167
|
-
function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
|
|
126
|
+
export function calculateDecisionRoot(state: BeaconStateAllForks, epoch: Epoch): RootHex {
|
|
168
127
|
const pivotSlot = computeStartSlotAtEpoch(epoch - 1) - 1;
|
|
169
128
|
return toRootHex(getBlockRootAtSlot(state, pivotSlot));
|
|
170
129
|
}
|