-
-
Notifications
You must be signed in to change notification settings - Fork 477
chore: follow up on #9390 on EIP-7688 changes #9689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
fd2185f
e294e46
4ddc5c9
4f63eff
9afbc1e
c8619cc
e972835
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,29 @@ export function loadState( | |
| ): MigrateStateOutput { | ||
| // casting only to make typescript happy | ||
| const stateType = getStateTypeFromBytes(config, stateBytes) as typeof ssz.capella.BeaconState; | ||
| const fork = getForkFromStateBytes(config, stateBytes); | ||
| const seedFork = config.getForkSeq(seedState.slot); | ||
|
|
||
| const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength); | ||
| const fieldRanges = stateType.getFieldRanges(dataView, 0, stateBytes.length); | ||
| const allFields = Object.keys(stateType.fields); | ||
| const validatorsFieldIndex = allFields.indexOf("validators"); | ||
| const validatorsRange = fieldRanges[validatorsFieldIndex]; | ||
| const newValidatorsBytes = stateBytes.subarray(validatorsRange.start, validatorsRange.end); | ||
|
|
||
| // EIP-7688 replaces List with ProgressiveList for validators and inactivityScores at gloas, | ||
| // changing the merkle tree shape. Seed nodes cannot be reused when one state is pre-gloas | ||
| // and the other post-gloas. | ||
| const crossesGloasFork = | ||
| (fork >= ForkSeq.gloas && seedFork < ForkSeq.gloas) || (fork < ForkSeq.gloas && seedFork >= ForkSeq.gloas); | ||
| if (crossesGloasFork) { | ||
| const migratedState = stateType.deserializeToViewDU(stateBytes) as BeaconStateAllForks; | ||
| // modified validators must still be reported so that the pubkey cache is refreshed for | ||
| // any index that differs from the seed state, which may not be an ancestor of this state | ||
| const modifiedValidators = findModifiedAndAppendedValidators(seedState, newValidatorsBytes, seedValidatorsBytes); | ||
| return {state: migratedState, modifiedValidators}; | ||
| } | ||
|
|
||
| // start with default view has the same performance to start with seed state | ||
| // and it is not fork dependent | ||
| const migratedState = deserializeContainerIgnoreFields( | ||
|
|
@@ -39,19 +58,10 @@ export function loadState( | |
| ) as BeaconStateAllForks; | ||
|
|
||
| // validators are rarely changed | ||
| const validatorsRange = fieldRanges[validatorsFieldIndex]; | ||
| const modifiedValidators = loadValidators( | ||
| migratedState, | ||
| seedState, | ||
| stateBytes.subarray(validatorsRange.start, validatorsRange.end), | ||
| seedValidatorsBytes | ||
| ); | ||
| const modifiedValidators = loadValidators(migratedState, seedState, newValidatorsBytes, seedValidatorsBytes); | ||
|
|
||
| // inactivityScores are rarely changed | ||
| // this saves ~500ms of hashTreeRoot() time of state | ||
| const fork = getForkFromStateBytes(config, stateBytes); | ||
| const seedFork = config.getForkSeq(seedState.slot); | ||
|
|
||
| if (fork >= ForkSeq.altair && seedFork >= ForkSeq.altair) { | ||
| const inactivityScoresIndex = allFields.indexOf("inactivityScores"); | ||
| const inactivityScoresRange = fieldRanges[inactivityScoresIndex]; | ||
|
|
@@ -141,7 +151,9 @@ function loadInactivityScores( | |
| } | ||
| } else { | ||
| if (newValidator - 1 < 0) { | ||
| migratedState.inactivityScores = ssz.altair.InactivityScores.defaultViewDU(); | ||
| // use the state's own field type, the list shape differs between altair (List) and gloas (ProgressiveList) | ||
| const inactivityScoresType = (migratedState.type as typeof ssz.altair.BeaconState).fields.inactivityScores; | ||
| migratedState.inactivityScores = inactivityScoresType.defaultViewDU(); | ||
| } else { | ||
| migratedState.inactivityScores = migratedState.inactivityScores.sliceTo(newValidator - 1); | ||
| } | ||
|
|
@@ -177,6 +189,33 @@ function loadInactivityScores( | |
| * @param migratedState state to be migrated, the validators are loaded to this state | ||
| * @returns modified validator indices | ||
| */ | ||
| /** | ||
| * Find indices of validators whose serialized bytes differ from the seed state, plus indices | ||
| * appended past the seed state's validator count. Unlike loadValidators() this only diffs | ||
| * bytes and does not share the seed state's tree. | ||
| */ | ||
| function findModifiedAndAppendedValidators( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice the modified validators are only consumed by loadCachedBeaconState() and noone uses it anymore, this is a dead code
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like this comment was not addressed @ensi321 can you follow up on it please |
||
| seedState: BeaconStateAllForks, | ||
| newValidatorsBytes: Uint8Array, | ||
| seedStateValidatorsBytes?: Uint8Array | ||
| ): number[] { | ||
| const seedValidatorCount = seedState.validators.length; | ||
| const newValidatorCount = Math.floor(newValidatorsBytes.length / VALIDATOR_BYTES_SIZE); | ||
| const minValidatorCount = Math.min(seedValidatorCount, newValidatorCount); | ||
| const seedValidatorsBytes = seedStateValidatorsBytes ?? seedState.validators.serialize(); | ||
| const modifiedValidators: number[] = []; | ||
| findModifiedValidators( | ||
| seedValidatorsBytes.subarray(0, minValidatorCount * VALIDATOR_BYTES_SIZE), | ||
| newValidatorsBytes.subarray(0, minValidatorCount * VALIDATOR_BYTES_SIZE), | ||
| modifiedValidators | ||
| ); | ||
|
|
||
| for (let validatorIndex = seedValidatorCount; validatorIndex < newValidatorCount; validatorIndex++) { | ||
| modifiedValidators.push(validatorIndex); | ||
| } | ||
| return modifiedValidators; | ||
| } | ||
|
|
||
| function loadValidators( | ||
| migratedState: BeaconStateAllForks, | ||
| seedState: BeaconStateAllForks, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.