@lodestar/state-transition 1.35.0-dev.98d359db41 → 1.35.0-dev.a70bac5bd3
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/README.md +1 -1
- package/lib/block/index.d.ts +2 -2
- package/lib/block/index.js +2 -2
- package/lib/block/index.js.map +1 -1
- package/lib/block/processAttestationPhase0.js +2 -1
- package/lib/block/processAttestationPhase0.js.map +1 -1
- package/lib/block/processAttestationsAltair.d.ts +1 -1
- package/lib/block/processAttestationsAltair.js +1 -1
- package/lib/block/processAttestationsAltair.js.map +1 -1
- package/lib/block/processAttesterSlashing.js.map +1 -1
- package/lib/block/processBlsToExecutionChange.js.map +1 -1
- package/lib/block/processConsolidationRequest.js.map +1 -1
- package/lib/block/processDeposit.d.ts +2 -2
- package/lib/block/processDeposit.js +1 -1
- package/lib/block/processDeposit.js.map +1 -1
- package/lib/block/processDepositRequest.js.map +1 -1
- package/lib/block/processOperations.js.map +1 -1
- package/lib/block/processSyncCommittee.js +2 -1
- package/lib/block/processSyncCommittee.js.map +1 -1
- package/lib/block/processWithdrawalRequest.js.map +1 -1
- package/lib/block/processWithdrawals.js.map +1 -1
- package/lib/block/slashValidator.js.map +1 -1
- package/lib/cache/epochCache.js +0 -130
- package/lib/cache/epochCache.js.map +1 -1
- package/lib/cache/epochTransitionCache.js.map +1 -1
- package/lib/epoch/index.js.map +1 -1
- package/lib/epoch/processSlashings.js.map +1 -1
- package/lib/index.d.ts +17 -17
- package/lib/index.js +16 -16
- package/lib/index.js.map +1 -1
- package/lib/signatureSets/blsToExecutionChange.js.map +1 -1
- package/lib/signatureSets/index.d.ts +1 -1
- package/lib/signatureSets/index.js +1 -1
- package/lib/signatureSets/index.js.map +1 -1
- package/lib/slot/upgradeStateToDeneb.d.ts +2 -1
- package/lib/slot/upgradeStateToDeneb.js.map +1 -1
- package/lib/slot/upgradeStateToGloas.js +2 -2
- package/lib/slot/upgradeStateToGloas.js.map +1 -1
- package/lib/types.d.ts +2 -2
- package/lib/util/blindedBlock.js.map +1 -1
- package/lib/util/execution.js.map +1 -1
- package/lib/util/genesis.js +4 -4
- package/lib/util/genesis.js.map +1 -1
- package/lib/util/index.d.ts +5 -5
- package/lib/util/index.js +5 -5
- package/lib/util/index.js.map +1 -1
- package/lib/util/interop.js +1 -1
- package/lib/util/interop.js.map +1 -1
- package/lib/util/rootCache.js +2 -5
- package/lib/util/rootCache.js.map +1 -1
- package/lib/util/seed.js +2 -1
- package/lib/util/seed.js.map +1 -1
- package/lib/util/weakSubjectivity.js.map +1 -1
- package/package.json +11 -13
- package/lib/block/externalData.d.ts.map +0 -1
- package/lib/block/index.d.ts.map +0 -1
- package/lib/block/initiateValidatorExit.d.ts.map +0 -1
- package/lib/block/isValidIndexedAttestation.d.ts.map +0 -1
- package/lib/block/processAttestationPhase0.d.ts.map +0 -1
- package/lib/block/processAttestations.d.ts.map +0 -1
- package/lib/block/processAttestationsAltair.d.ts.map +0 -1
- package/lib/block/processAttesterSlashing.d.ts.map +0 -1
- package/lib/block/processBlobKzgCommitments.d.ts.map +0 -1
- package/lib/block/processBlockHeader.d.ts.map +0 -1
- package/lib/block/processBlsToExecutionChange.d.ts.map +0 -1
- package/lib/block/processConsolidationRequest.d.ts.map +0 -1
- package/lib/block/processDeposit.d.ts.map +0 -1
- package/lib/block/processDepositRequest.d.ts.map +0 -1
- package/lib/block/processEth1Data.d.ts.map +0 -1
- package/lib/block/processExecutionPayload.d.ts.map +0 -1
- package/lib/block/processOperations.d.ts.map +0 -1
- package/lib/block/processProposerSlashing.d.ts.map +0 -1
- package/lib/block/processRandao.d.ts.map +0 -1
- package/lib/block/processSyncCommittee.d.ts.map +0 -1
- package/lib/block/processVoluntaryExit.d.ts.map +0 -1
- package/lib/block/processWithdrawalRequest.d.ts.map +0 -1
- package/lib/block/processWithdrawals.d.ts.map +0 -1
- package/lib/block/slashValidator.d.ts.map +0 -1
- package/lib/block/types.d.ts.map +0 -1
- package/lib/cache/effectiveBalanceIncrements.d.ts.map +0 -1
- package/lib/cache/epochCache.d.ts.map +0 -1
- package/lib/cache/epochTransitionCache.d.ts.map +0 -1
- package/lib/cache/pubkeyCache.d.ts.map +0 -1
- package/lib/cache/rewardCache.d.ts.map +0 -1
- package/lib/cache/stateCache.d.ts.map +0 -1
- package/lib/cache/syncCommitteeCache.d.ts.map +0 -1
- package/lib/cache/types.d.ts.map +0 -1
- package/lib/constants/constants.d.ts.map +0 -1
- package/lib/constants/index.d.ts.map +0 -1
- package/lib/epoch/computeUnrealizedCheckpoints.d.ts.map +0 -1
- package/lib/epoch/getAttestationDeltas.d.ts.map +0 -1
- package/lib/epoch/getRewardsAndPenalties.d.ts.map +0 -1
- package/lib/epoch/index.d.ts.map +0 -1
- package/lib/epoch/processEffectiveBalanceUpdates.d.ts.map +0 -1
- package/lib/epoch/processEth1DataReset.d.ts.map +0 -1
- package/lib/epoch/processHistoricalRootsUpdate.d.ts.map +0 -1
- package/lib/epoch/processHistoricalSummariesUpdate.d.ts.map +0 -1
- package/lib/epoch/processInactivityUpdates.d.ts.map +0 -1
- package/lib/epoch/processJustificationAndFinalization.d.ts.map +0 -1
- package/lib/epoch/processParticipationFlagUpdates.d.ts.map +0 -1
- package/lib/epoch/processParticipationRecordUpdates.d.ts.map +0 -1
- package/lib/epoch/processPendingAttestations.d.ts.map +0 -1
- package/lib/epoch/processPendingConsolidations.d.ts.map +0 -1
- package/lib/epoch/processPendingDeposits.d.ts.map +0 -1
- package/lib/epoch/processProposerLookahead.d.ts.map +0 -1
- package/lib/epoch/processRandaoMixesReset.d.ts.map +0 -1
- package/lib/epoch/processRegistryUpdates.d.ts.map +0 -1
- package/lib/epoch/processRewardsAndPenalties.d.ts.map +0 -1
- package/lib/epoch/processSlashings.d.ts.map +0 -1
- package/lib/epoch/processSlashingsReset.d.ts.map +0 -1
- package/lib/epoch/processSyncCommitteeUpdates.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/metrics.d.ts.map +0 -1
- package/lib/signatureSets/attesterSlashings.d.ts.map +0 -1
- package/lib/signatureSets/blsToExecutionChange.d.ts.map +0 -1
- package/lib/signatureSets/index.d.ts.map +0 -1
- package/lib/signatureSets/indexedAttestation.d.ts.map +0 -1
- package/lib/signatureSets/proposer.d.ts.map +0 -1
- package/lib/signatureSets/proposerSlashings.d.ts.map +0 -1
- package/lib/signatureSets/randao.d.ts.map +0 -1
- package/lib/signatureSets/voluntaryExits.d.ts.map +0 -1
- package/lib/slot/index.d.ts.map +0 -1
- package/lib/slot/upgradeStateToAltair.d.ts.map +0 -1
- package/lib/slot/upgradeStateToBellatrix.d.ts.map +0 -1
- package/lib/slot/upgradeStateToCapella.d.ts.map +0 -1
- package/lib/slot/upgradeStateToDeneb.d.ts.map +0 -1
- package/lib/slot/upgradeStateToElectra.d.ts.map +0 -1
- package/lib/slot/upgradeStateToFulu.d.ts.map +0 -1
- package/lib/slot/upgradeStateToGloas.d.ts.map +0 -1
- package/lib/stateTransition.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/util/aggregator.d.ts.map +0 -1
- package/lib/util/altair.d.ts.map +0 -1
- package/lib/util/array.d.ts.map +0 -1
- package/lib/util/attestation.d.ts.map +0 -1
- package/lib/util/attesterStatus.d.ts.map +0 -1
- package/lib/util/balance.d.ts.map +0 -1
- package/lib/util/blindedBlock.d.ts.map +0 -1
- package/lib/util/blockRoot.d.ts.map +0 -1
- package/lib/util/calculateCommitteeAssignments.d.ts.map +0 -1
- package/lib/util/capella.d.ts.map +0 -1
- package/lib/util/computeAnchorCheckpoint.d.ts.map +0 -1
- package/lib/util/deposit.d.ts.map +0 -1
- package/lib/util/domain.d.ts.map +0 -1
- package/lib/util/electra.d.ts.map +0 -1
- package/lib/util/epoch.d.ts.map +0 -1
- package/lib/util/epochShuffling.d.ts.map +0 -1
- package/lib/util/execution.d.ts.map +0 -1
- package/lib/util/finality.d.ts.map +0 -1
- package/lib/util/fulu.d.ts.map +0 -1
- package/lib/util/genesis.d.ts.map +0 -1
- package/lib/util/index.d.ts.map +0 -1
- package/lib/util/interop.d.ts.map +0 -1
- package/lib/util/loadState/findModifiedInactivityScores.d.ts.map +0 -1
- package/lib/util/loadState/findModifiedValidators.d.ts.map +0 -1
- package/lib/util/loadState/index.d.ts.map +0 -1
- package/lib/util/loadState/loadState.d.ts.map +0 -1
- package/lib/util/loadState/loadValidator.d.ts.map +0 -1
- package/lib/util/rootCache.d.ts.map +0 -1
- package/lib/util/seed.d.ts.map +0 -1
- package/lib/util/shufflingDecisionRoot.d.ts.map +0 -1
- package/lib/util/signatureSets.d.ts.map +0 -1
- package/lib/util/signingRoot.d.ts.map +0 -1
- package/lib/util/slot.d.ts.map +0 -1
- package/lib/util/sszBytes.d.ts.map +0 -1
- package/lib/util/syncCommittee.d.ts.map +0 -1
- package/lib/util/targetUnslashedBalance.d.ts.map +0 -1
- package/lib/util/validator.d.ts.map +0 -1
- package/lib/util/weakSubjectivity.d.ts.map +0 -1
- package/src/block/externalData.ts +0 -26
- package/src/block/index.ts +0 -81
- package/src/block/initiateValidatorExit.ts +0 -62
- package/src/block/isValidIndexedAttestation.ts +0 -70
- package/src/block/processAttestationPhase0.ts +0 -158
- package/src/block/processAttestations.ts +0 -25
- package/src/block/processAttestationsAltair.ts +0 -184
- package/src/block/processAttesterSlashing.ts +0 -59
- package/src/block/processBlobKzgCommitments.ts +0 -21
- package/src/block/processBlockHeader.ts +0 -54
- package/src/block/processBlsToExecutionChange.ts +0 -78
- package/src/block/processConsolidationRequest.ts +0 -147
- package/src/block/processDeposit.ts +0 -166
- package/src/block/processDepositRequest.ts +0 -19
- package/src/block/processEth1Data.ts +0 -86
- package/src/block/processExecutionPayload.ts +0 -84
- package/src/block/processOperations.ts +0 -83
- package/src/block/processProposerSlashing.ts +0 -66
- package/src/block/processRandao.ts +0 -27
- package/src/block/processSyncCommittee.ts +0 -117
- package/src/block/processVoluntaryExit.ts +0 -55
- package/src/block/processWithdrawalRequest.ts +0 -98
- package/src/block/processWithdrawals.ts +0 -207
- package/src/block/slashValidator.ts +0 -98
- package/src/block/types.ts +0 -9
- package/src/cache/effectiveBalanceIncrements.ts +0 -39
- package/src/cache/epochCache.ts +0 -1213
- package/src/cache/epochTransitionCache.ts +0 -542
- package/src/cache/pubkeyCache.ts +0 -33
- package/src/cache/rewardCache.ts +0 -19
- package/src/cache/stateCache.ts +0 -268
- package/src/cache/syncCommitteeCache.ts +0 -96
- package/src/cache/types.ts +0 -18
- package/src/constants/constants.ts +0 -12
- package/src/constants/index.ts +0 -1
- package/src/epoch/computeUnrealizedCheckpoints.ts +0 -55
- package/src/epoch/getAttestationDeltas.ts +0 -169
- package/src/epoch/getRewardsAndPenalties.ts +0 -137
- package/src/epoch/index.ts +0 -202
- package/src/epoch/processEffectiveBalanceUpdates.ts +0 -111
- package/src/epoch/processEth1DataReset.ts +0 -17
- package/src/epoch/processHistoricalRootsUpdate.ts +0 -25
- package/src/epoch/processHistoricalSummariesUpdate.ts +0 -23
- package/src/epoch/processInactivityUpdates.ts +0 -60
- package/src/epoch/processJustificationAndFinalization.ts +0 -90
- package/src/epoch/processParticipationFlagUpdates.ts +0 -27
- package/src/epoch/processParticipationRecordUpdates.ts +0 -14
- package/src/epoch/processPendingAttestations.ts +0 -75
- package/src/epoch/processPendingConsolidations.ts +0 -59
- package/src/epoch/processPendingDeposits.ts +0 -136
- package/src/epoch/processProposerLookahead.ts +0 -39
- package/src/epoch/processRandaoMixesReset.ts +0 -18
- package/src/epoch/processRegistryUpdates.ts +0 -65
- package/src/epoch/processRewardsAndPenalties.ts +0 -58
- package/src/epoch/processSlashings.ts +0 -97
- package/src/epoch/processSlashingsReset.ts +0 -20
- package/src/epoch/processSyncCommitteeUpdates.ts +0 -44
- package/src/index.ts +0 -67
- package/src/metrics.ts +0 -169
- package/src/signatureSets/attesterSlashings.ts +0 -39
- package/src/signatureSets/blsToExecutionChange.ts +0 -43
- package/src/signatureSets/index.ts +0 -73
- package/src/signatureSets/indexedAttestation.ts +0 -51
- package/src/signatureSets/proposer.ts +0 -47
- package/src/signatureSets/proposerSlashings.ts +0 -41
- package/src/signatureSets/randao.ts +0 -31
- package/src/signatureSets/voluntaryExits.ts +0 -44
- package/src/slot/index.ts +0 -32
- package/src/slot/upgradeStateToAltair.ts +0 -149
- package/src/slot/upgradeStateToBellatrix.ts +0 -63
- package/src/slot/upgradeStateToCapella.ts +0 -71
- package/src/slot/upgradeStateToDeneb.ts +0 -40
- package/src/slot/upgradeStateToElectra.ts +0 -126
- package/src/slot/upgradeStateToFulu.ts +0 -31
- package/src/slot/upgradeStateToGloas.ts +0 -29
- package/src/stateTransition.ts +0 -305
- package/src/types.ts +0 -26
- package/src/util/aggregator.ts +0 -33
- package/src/util/altair.ts +0 -13
- package/src/util/array.ts +0 -53
- package/src/util/attestation.ts +0 -36
- package/src/util/attesterStatus.ts +0 -83
- package/src/util/balance.ts +0 -83
- package/src/util/blindedBlock.ts +0 -144
- package/src/util/blockRoot.ts +0 -72
- package/src/util/calculateCommitteeAssignments.ts +0 -43
- package/src/util/capella.ts +0 -8
- package/src/util/computeAnchorCheckpoint.ts +0 -38
- package/src/util/deposit.ts +0 -22
- package/src/util/domain.ts +0 -31
- package/src/util/electra.ts +0 -68
- package/src/util/epoch.ts +0 -135
- package/src/util/epochShuffling.ts +0 -185
- package/src/util/execution.ts +0 -177
- package/src/util/finality.ts +0 -17
- package/src/util/fulu.ts +0 -43
- package/src/util/genesis.ts +0 -343
- package/src/util/index.ts +0 -29
- package/src/util/interop.ts +0 -22
- package/src/util/loadState/findModifiedInactivityScores.ts +0 -47
- package/src/util/loadState/findModifiedValidators.ts +0 -46
- package/src/util/loadState/index.ts +0 -2
- package/src/util/loadState/loadState.ts +0 -225
- package/src/util/loadState/loadValidator.ts +0 -77
- package/src/util/rootCache.ts +0 -37
- package/src/util/seed.ts +0 -381
- package/src/util/shufflingDecisionRoot.ts +0 -78
- package/src/util/signatureSets.ts +0 -65
- package/src/util/signingRoot.ts +0 -13
- package/src/util/slot.ts +0 -27
- package/src/util/sszBytes.ts +0 -52
- package/src/util/syncCommittee.ts +0 -69
- package/src/util/targetUnslashedBalance.ts +0 -30
- package/src/util/validator.ts +0 -105
- package/src/util/weakSubjectivity.ts +0 -186
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import {ChainForkConfig} from "@lodestar/config";
|
|
2
|
-
import {ForkSeq} from "@lodestar/params";
|
|
3
|
-
import {deserializeContainerIgnoreFields, ssz} from "@lodestar/types";
|
|
4
|
-
import {BeaconStateAllForks, BeaconStateAltair} from "../../types.js";
|
|
5
|
-
import {VALIDATOR_BYTES_SIZE, getForkFromStateBytes, getStateTypeFromBytes} from "../sszBytes.js";
|
|
6
|
-
import {findModifiedInactivityScores} from "./findModifiedInactivityScores.js";
|
|
7
|
-
import {findModifiedValidators} from "./findModifiedValidators.js";
|
|
8
|
-
import {loadValidator} from "./loadValidator.js";
|
|
9
|
-
|
|
10
|
-
type MigrateStateOutput = {state: BeaconStateAllForks; modifiedValidators: number[]};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Load state from bytes given a seed state so that we share the same base tree. This gives some benefits:
|
|
14
|
-
* - Have single base tree across the application
|
|
15
|
-
* - Faster to load state
|
|
16
|
-
* - Less memory usage
|
|
17
|
-
* - Utilize the cached HashObjects in seed state due to a lot of validators are not changed, also the inactivity scores.
|
|
18
|
-
* @returns the new state and modified validators
|
|
19
|
-
*/
|
|
20
|
-
export function loadState(
|
|
21
|
-
config: ChainForkConfig,
|
|
22
|
-
seedState: BeaconStateAllForks,
|
|
23
|
-
stateBytes: Uint8Array,
|
|
24
|
-
seedValidatorsBytes?: Uint8Array
|
|
25
|
-
): MigrateStateOutput {
|
|
26
|
-
// casting only to make typescript happy
|
|
27
|
-
const stateType = getStateTypeFromBytes(config, stateBytes) as typeof ssz.capella.BeaconState;
|
|
28
|
-
const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
|
|
29
|
-
const fieldRanges = stateType.getFieldRanges(dataView, 0, stateBytes.length);
|
|
30
|
-
const allFields = Object.keys(stateType.fields);
|
|
31
|
-
const validatorsFieldIndex = allFields.indexOf("validators");
|
|
32
|
-
// start with default view has the same performance to start with seed state
|
|
33
|
-
// and it is not fork dependent
|
|
34
|
-
const migratedState = deserializeContainerIgnoreFields(
|
|
35
|
-
stateType,
|
|
36
|
-
stateBytes,
|
|
37
|
-
["validators", "inactivityScores"],
|
|
38
|
-
fieldRanges
|
|
39
|
-
) as BeaconStateAllForks;
|
|
40
|
-
|
|
41
|
-
// validators are rarely changed
|
|
42
|
-
const validatorsRange = fieldRanges[validatorsFieldIndex];
|
|
43
|
-
const modifiedValidators = loadValidators(
|
|
44
|
-
migratedState,
|
|
45
|
-
seedState,
|
|
46
|
-
stateBytes.subarray(validatorsRange.start, validatorsRange.end),
|
|
47
|
-
seedValidatorsBytes
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
// inactivityScores are rarely changed
|
|
51
|
-
// this saves ~500ms of hashTreeRoot() time of state
|
|
52
|
-
const fork = getForkFromStateBytes(config, stateBytes);
|
|
53
|
-
const seedFork = config.getForkSeq(seedState.slot);
|
|
54
|
-
|
|
55
|
-
if (fork >= ForkSeq.altair && seedFork >= ForkSeq.altair) {
|
|
56
|
-
const inactivityScoresIndex = allFields.indexOf("inactivityScores");
|
|
57
|
-
const inactivityScoresRange = fieldRanges[inactivityScoresIndex];
|
|
58
|
-
loadInactivityScores(
|
|
59
|
-
migratedState as BeaconStateAltair,
|
|
60
|
-
seedState as BeaconStateAltair,
|
|
61
|
-
stateBytes.subarray(inactivityScoresRange.start, inactivityScoresRange.end)
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
migratedState.commit();
|
|
65
|
-
|
|
66
|
-
return {state: migratedState, modifiedValidators};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Load state and validators Uint8Array from state bytes.
|
|
71
|
-
*/
|
|
72
|
-
export function loadStateAndValidators(
|
|
73
|
-
chainForkConfig: ChainForkConfig,
|
|
74
|
-
stateBytes: Uint8Array
|
|
75
|
-
): {state: BeaconStateAllForks; validatorsBytes: Uint8Array} {
|
|
76
|
-
// stateType could be any types, casting just to make typescript happy
|
|
77
|
-
const stateType = getStateTypeFromBytes(chainForkConfig, stateBytes) as typeof ssz.phase0.BeaconState;
|
|
78
|
-
const state = stateType.deserializeToViewDU(stateBytes);
|
|
79
|
-
const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
|
|
80
|
-
const fieldRanges = stateType.getFieldRanges(dataView, 0, stateBytes.length);
|
|
81
|
-
const allFields = Object.keys(stateType.fields);
|
|
82
|
-
const validatorFieldIndex = allFields.indexOf("validators");
|
|
83
|
-
const validatorRange = fieldRanges[validatorFieldIndex];
|
|
84
|
-
const validatorsBytes = stateBytes.subarray(validatorRange.start, validatorRange.end);
|
|
85
|
-
return {state, validatorsBytes};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* This value is rarely changed as monitored 3 month state diffs on mainnet as of Sep 2023.
|
|
90
|
-
* Reusing this data helps save hashTreeRoot time of state ~500ms
|
|
91
|
-
*
|
|
92
|
-
* Given the below tree:
|
|
93
|
-
*
|
|
94
|
-
* seedState.inactivityScores ====> ROOT
|
|
95
|
-
* / \
|
|
96
|
-
* Hash01 Hash23
|
|
97
|
-
* / \ / \
|
|
98
|
-
* Sco0 Sco1 Sco2 Sco3
|
|
99
|
-
*
|
|
100
|
-
* if score 3 is modified, the new tree looks like this:
|
|
101
|
-
*
|
|
102
|
-
* migratedState.inactivityScores ====> ROOTa
|
|
103
|
-
* / \
|
|
104
|
-
* Hash01 Hash23a
|
|
105
|
-
* / \ / \
|
|
106
|
-
* Sco0 Sco1 Sco2 Sco3a
|
|
107
|
-
*/
|
|
108
|
-
function loadInactivityScores(
|
|
109
|
-
migratedState: BeaconStateAltair,
|
|
110
|
-
seedState: BeaconStateAltair,
|
|
111
|
-
inactivityScoresBytes: Uint8Array
|
|
112
|
-
): void {
|
|
113
|
-
// migratedState starts with the same inactivityScores to seed state
|
|
114
|
-
migratedState.inactivityScores = seedState.inactivityScores.clone();
|
|
115
|
-
const oldValidator = migratedState.inactivityScores.length;
|
|
116
|
-
// UintNum64 = 8 bytes
|
|
117
|
-
const newValidator = inactivityScoresBytes.length / 8;
|
|
118
|
-
const minValidator = Math.min(oldValidator, newValidator);
|
|
119
|
-
const oldInactivityScores = migratedState.inactivityScores.serialize();
|
|
120
|
-
const isMoreValidator = newValidator >= oldValidator;
|
|
121
|
-
const modifiedValidators: number[] = [];
|
|
122
|
-
findModifiedInactivityScores(
|
|
123
|
-
isMoreValidator ? oldInactivityScores : oldInactivityScores.subarray(0, minValidator * 8),
|
|
124
|
-
isMoreValidator ? inactivityScoresBytes.subarray(0, minValidator * 8) : inactivityScoresBytes,
|
|
125
|
-
modifiedValidators
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
for (const validatorIndex of modifiedValidators) {
|
|
129
|
-
migratedState.inactivityScores.set(
|
|
130
|
-
validatorIndex,
|
|
131
|
-
ssz.UintNum64.deserialize(inactivityScoresBytes.subarray(validatorIndex * 8, (validatorIndex + 1) * 8))
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (isMoreValidator) {
|
|
136
|
-
// add new inactivityScores
|
|
137
|
-
for (let validatorIndex = oldValidator; validatorIndex < newValidator; validatorIndex++) {
|
|
138
|
-
migratedState.inactivityScores.push(
|
|
139
|
-
ssz.UintNum64.deserialize(inactivityScoresBytes.subarray(validatorIndex * 8, (validatorIndex + 1) * 8))
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
} else {
|
|
143
|
-
if (newValidator - 1 < 0) {
|
|
144
|
-
migratedState.inactivityScores = ssz.altair.InactivityScores.defaultViewDU();
|
|
145
|
-
} else {
|
|
146
|
-
migratedState.inactivityScores = migratedState.inactivityScores.sliceTo(newValidator - 1);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* As of Sep 2023, common validators of 2 mainnet states are rarely changed. However, the benchmark shows that
|
|
153
|
-
* 10k modified validators is not an issue. (see packages/state-transition/test/perf/util/loadState/findModifiedValidators.test.ts)
|
|
154
|
-
*
|
|
155
|
-
* This method loads validators from bytes given a seed state so that they share the same base tree. This gives some benefits:
|
|
156
|
-
* - Have single base tree across the application
|
|
157
|
-
* - Faster to load state
|
|
158
|
-
* - Less memory usage
|
|
159
|
-
* - Utilize the cached HashObjects in seed state due to a lot of validators are not changed
|
|
160
|
-
*
|
|
161
|
-
* Given the below tree:
|
|
162
|
-
*
|
|
163
|
-
* seedState.validators ====> ROOT
|
|
164
|
-
* / \
|
|
165
|
-
* Hash01 Hash23
|
|
166
|
-
* / \ / \
|
|
167
|
-
* Val0 Val1 Val2 Val3
|
|
168
|
-
*
|
|
169
|
-
* if validator 3 is modified, the new tree looks like this:
|
|
170
|
-
*
|
|
171
|
-
* migratedState.validators ====> ROOTa
|
|
172
|
-
* / \
|
|
173
|
-
* Hash01 Hash23a
|
|
174
|
-
* / \ / \
|
|
175
|
-
* Val0 Val1 Val2 Val3a
|
|
176
|
-
*
|
|
177
|
-
* @param migratedState state to be migrated, the validators are loaded to this state
|
|
178
|
-
* @returns modified validator indices
|
|
179
|
-
*/
|
|
180
|
-
function loadValidators(
|
|
181
|
-
migratedState: BeaconStateAllForks,
|
|
182
|
-
seedState: BeaconStateAllForks,
|
|
183
|
-
newValidatorsBytes: Uint8Array,
|
|
184
|
-
seedStateValidatorsBytes?: Uint8Array
|
|
185
|
-
): number[] {
|
|
186
|
-
const seedValidatorCount = seedState.validators.length;
|
|
187
|
-
const newValidatorCount = Math.floor(newValidatorsBytes.length / VALIDATOR_BYTES_SIZE);
|
|
188
|
-
const isMoreValidator = newValidatorCount >= seedValidatorCount;
|
|
189
|
-
const minValidatorCount = Math.min(seedValidatorCount, newValidatorCount);
|
|
190
|
-
// migrated state starts with the same validators to seed state
|
|
191
|
-
migratedState.validators = seedState.validators.clone();
|
|
192
|
-
// 80% of validators serialization time comes from memory allocation
|
|
193
|
-
// seedStateValidatorsBytes is an optimization at beacon-node side to avoid memory allocation here
|
|
194
|
-
const seedValidatorsBytes = seedStateValidatorsBytes ?? seedState.validators.serialize();
|
|
195
|
-
const modifiedValidators: number[] = [];
|
|
196
|
-
findModifiedValidators(
|
|
197
|
-
isMoreValidator ? seedValidatorsBytes : seedValidatorsBytes.subarray(0, minValidatorCount * VALIDATOR_BYTES_SIZE),
|
|
198
|
-
isMoreValidator ? newValidatorsBytes.subarray(0, minValidatorCount * VALIDATOR_BYTES_SIZE) : newValidatorsBytes,
|
|
199
|
-
modifiedValidators
|
|
200
|
-
);
|
|
201
|
-
|
|
202
|
-
for (const i of modifiedValidators) {
|
|
203
|
-
const seedValidator = seedState.validators.get(i);
|
|
204
|
-
const newValidatorBytes = newValidatorsBytes.subarray(i * VALIDATOR_BYTES_SIZE, (i + 1) * VALIDATOR_BYTES_SIZE);
|
|
205
|
-
migratedState.validators.set(i, loadValidator(seedValidator, newValidatorBytes));
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (newValidatorCount >= seedValidatorCount) {
|
|
209
|
-
// add new validators
|
|
210
|
-
for (let validatorIndex = seedValidatorCount; validatorIndex < newValidatorCount; validatorIndex++) {
|
|
211
|
-
migratedState.validators.push(
|
|
212
|
-
ssz.phase0.Validator.deserializeToViewDU(
|
|
213
|
-
newValidatorsBytes.subarray(
|
|
214
|
-
validatorIndex * VALIDATOR_BYTES_SIZE,
|
|
215
|
-
(validatorIndex + 1) * VALIDATOR_BYTES_SIZE
|
|
216
|
-
)
|
|
217
|
-
)
|
|
218
|
-
);
|
|
219
|
-
modifiedValidators.push(validatorIndex);
|
|
220
|
-
}
|
|
221
|
-
} else {
|
|
222
|
-
migratedState.validators = migratedState.validators.sliceTo(newValidatorCount - 1);
|
|
223
|
-
}
|
|
224
|
-
return modifiedValidators;
|
|
225
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import {CompositeViewDU} from "@chainsafe/ssz";
|
|
2
|
-
import {ChainForkConfig} from "@lodestar/config";
|
|
3
|
-
import {ValidatorIndex, deserializeContainerIgnoreFields, ssz} from "@lodestar/types";
|
|
4
|
-
import {getStateTypeFromBytes} from "../sszBytes.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Load validator from bytes given a seed validator.
|
|
8
|
-
* - Reuse pubkey and withdrawal credentials if possible to save memory
|
|
9
|
-
* - If it's a new validator, deserialize it
|
|
10
|
-
*/
|
|
11
|
-
export function loadValidator(
|
|
12
|
-
seedValidator: CompositeViewDU<typeof ssz.phase0.Validator>,
|
|
13
|
-
newValidatorBytes: Uint8Array
|
|
14
|
-
): CompositeViewDU<typeof ssz.phase0.Validator> {
|
|
15
|
-
const ignoredFields = getSameFields(seedValidator, newValidatorBytes);
|
|
16
|
-
if (ignoredFields.length > 0) {
|
|
17
|
-
const newValidatorValue = deserializeContainerIgnoreFields(ssz.phase0.Validator, newValidatorBytes, ignoredFields);
|
|
18
|
-
for (const field of ignoredFields) {
|
|
19
|
-
newValidatorValue[field] = seedValidator[field];
|
|
20
|
-
}
|
|
21
|
-
return ssz.phase0.Validator.toViewDU(newValidatorValue);
|
|
22
|
-
}
|
|
23
|
-
return ssz.phase0.Validator.deserializeToViewDU(newValidatorBytes);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Return pubkey or withdrawalCredentials or both if they are the same.
|
|
28
|
-
*/
|
|
29
|
-
function getSameFields(
|
|
30
|
-
validator: CompositeViewDU<typeof ssz.phase0.Validator>,
|
|
31
|
-
validatorBytes: Uint8Array
|
|
32
|
-
): ("pubkey" | "withdrawalCredentials")[] {
|
|
33
|
-
const ignoredFields: ("pubkey" | "withdrawalCredentials")[] = [];
|
|
34
|
-
const pubkey = validatorBytes.subarray(0, 48);
|
|
35
|
-
if (Buffer.compare(pubkey, validator.pubkey) === 0) {
|
|
36
|
-
ignoredFields.push("pubkey");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const withdrawalCredentials = validatorBytes.subarray(48, 80);
|
|
40
|
-
if (Buffer.compare(withdrawalCredentials, validator.withdrawalCredentials) === 0) {
|
|
41
|
-
ignoredFields.push("withdrawalCredentials");
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return ignoredFields;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Extract and deserialize validator effective balances from state bytes
|
|
49
|
-
*/
|
|
50
|
-
export function getEffectiveBalancesFromStateBytes(
|
|
51
|
-
config: ChainForkConfig,
|
|
52
|
-
stateBytes: Uint8Array,
|
|
53
|
-
validatorIndices: ValidatorIndex[]
|
|
54
|
-
): number[] {
|
|
55
|
-
// stateType could be any types, casting just to make typescript happy
|
|
56
|
-
const stateType = getStateTypeFromBytes(config, stateBytes) as typeof ssz.phase0.BeaconState;
|
|
57
|
-
const stateView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
|
|
58
|
-
const stateFieldRanges = stateType.getFieldRanges(stateView, 0, stateBytes.length);
|
|
59
|
-
const stateFields = Object.keys(stateType.fields);
|
|
60
|
-
const validatorsFieldIndex = stateFields.indexOf("validators");
|
|
61
|
-
const validatorsRange = stateFieldRanges[validatorsFieldIndex];
|
|
62
|
-
const validatorsBytes = stateBytes.subarray(validatorsRange.start, validatorsRange.end);
|
|
63
|
-
const validatorSize = ssz.phase0.Validator.fixedSize as number;
|
|
64
|
-
|
|
65
|
-
const effectiveBalances: number[] = [];
|
|
66
|
-
|
|
67
|
-
for (const index of validatorIndices) {
|
|
68
|
-
const validatorBytes = validatorsBytes.subarray(index * validatorSize, (index + 1) * validatorSize);
|
|
69
|
-
if (validatorBytes.byteLength === 0) {
|
|
70
|
-
throw Error(`Validator index ${index} out of range`);
|
|
71
|
-
}
|
|
72
|
-
const validator = ssz.phase0.Validator.deserialize(validatorBytes);
|
|
73
|
-
effectiveBalances.push(validator.effectiveBalance);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return effectiveBalances;
|
|
77
|
-
}
|
package/src/util/rootCache.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import {Epoch, Root, Slot, phase0} from "@lodestar/types";
|
|
2
|
-
import {CachedBeaconStateAllForks} from "../types.js";
|
|
3
|
-
import {getBlockRoot, getBlockRootAtSlot} from "./blockRoot.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Cache to prevent accessing the state tree to fetch block roots repeteadly.
|
|
7
|
-
* In normal network conditions the same root is read multiple times, specially the target.
|
|
8
|
-
*/
|
|
9
|
-
export class RootCache {
|
|
10
|
-
readonly currentJustifiedCheckpoint: phase0.Checkpoint;
|
|
11
|
-
readonly previousJustifiedCheckpoint: phase0.Checkpoint;
|
|
12
|
-
private readonly blockRootEpochCache = new Map<Epoch, Root>();
|
|
13
|
-
private readonly blockRootSlotCache = new Map<Slot, Root>();
|
|
14
|
-
|
|
15
|
-
constructor(private readonly state: CachedBeaconStateAllForks) {
|
|
16
|
-
this.currentJustifiedCheckpoint = state.currentJustifiedCheckpoint;
|
|
17
|
-
this.previousJustifiedCheckpoint = state.previousJustifiedCheckpoint;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
getBlockRoot(epoch: Epoch): Root {
|
|
21
|
-
let root = this.blockRootEpochCache.get(epoch);
|
|
22
|
-
if (!root) {
|
|
23
|
-
root = getBlockRoot(this.state, epoch);
|
|
24
|
-
this.blockRootEpochCache.set(epoch, root);
|
|
25
|
-
}
|
|
26
|
-
return root;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
getBlockRootAtSlot(slot: Slot): Root {
|
|
30
|
-
let root = this.blockRootSlotCache.get(slot);
|
|
31
|
-
if (!root) {
|
|
32
|
-
root = getBlockRootAtSlot(this.state, slot);
|
|
33
|
-
this.blockRootSlotCache.set(slot, root);
|
|
34
|
-
}
|
|
35
|
-
return root;
|
|
36
|
-
}
|
|
37
|
-
}
|