-
-
Notifications
You must be signed in to change notification settings - Fork 300
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
feat: load state from Uint8Array #6057
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e8bc051
fix: implement loadState api
twoeths 1eb9b2a
chore: benchmark findModifiedValidators()
twoeths cd7b1c1
feat: implement deserializeContainerIgnoreFields()
twoeths 930e5bb
feat: implement loadValidator()
twoeths 50876c6
fix: type the ignoreFields
twoeths a669b4b
chore: benchmark loadState()
twoeths a420c54
fix: add '--max-old-space-size=4096' to github workflow
twoeths 1443a84
fix: revert default vc count in perf test
twoeths 73bdbee
Revert "fix: add '--max-old-space-size=4096' to github workflow"
twoeths 56bff34
chore: rename loadCachedBeaconState to loadUnfinalizedCachedBeaconState
twoeths a26c830
chore: loadValidator - reuse fields as much as possible
twoeths c7df27b
chore: more benchmarks to compare Uint8Array
twoeths File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,12 @@ import { | |
computeProposers, | ||
getActivationChurnLimit, | ||
} from "../util/index.js"; | ||
import {computeEpochShuffling, EpochShuffling} from "../util/epochShuffling.js"; | ||
import {computeEpochShuffling, EpochShuffling, getShufflingDecisionBlock} from "../util/epochShuffling.js"; | ||
import {computeBaseRewardPerIncrement, computeSyncParticipantReward} from "../util/syncCommittee.js"; | ||
import {sumTargetUnslashedBalanceIncrements} from "../util/targetUnslashedBalance.js"; | ||
import {EffectiveBalanceIncrements, getEffectiveBalanceIncrementsWithLen} from "./effectiveBalanceIncrements.js"; | ||
import {Index2PubkeyCache, PubkeyIndexMap, syncPubkeys} from "./pubkeyCache.js"; | ||
import {BeaconStateAllForks, BeaconStateAltair} from "./types.js"; | ||
import {BeaconStateAllForks, BeaconStateAltair, ShufflingGetter} from "./types.js"; | ||
import { | ||
computeSyncCommitteeCache, | ||
getSyncCommitteeCache, | ||
|
@@ -51,6 +51,7 @@ export type EpochCacheImmutableData = { | |
export type EpochCacheOpts = { | ||
skipSyncCommitteeCache?: boolean; | ||
skipSyncPubkeys?: boolean; | ||
shufflingGetter?: ShufflingGetter; | ||
}; | ||
|
||
/** Defers computing proposers by persisting only the seed, and dropping it once indexes are computed */ | ||
|
@@ -280,21 +281,28 @@ export class EpochCache { | |
const currentActiveIndices: ValidatorIndex[] = []; | ||
const nextActiveIndices: ValidatorIndex[] = []; | ||
|
||
const previousShufflingDecisionBlock = getShufflingDecisionBlock(state, previousEpoch); | ||
const previousShufflingIn = opts?.shufflingGetter?.(previousEpoch, previousShufflingDecisionBlock); | ||
const currentShufflingDecisionBlock = getShufflingDecisionBlock(state, currentEpoch); | ||
const currentShufflingIn = opts?.shufflingGetter?.(currentEpoch, currentShufflingDecisionBlock); | ||
const nextShufflingDecisionBlock = getShufflingDecisionBlock(state, nextEpoch); | ||
const nextShufflingIn = opts?.shufflingGetter?.(nextEpoch, nextShufflingDecisionBlock); | ||
|
||
for (let i = 0; i < validatorCount; i++) { | ||
const validator = validators[i]; | ||
|
||
// Note: Not usable for fork-choice balances since in-active validators are not zero'ed | ||
effectiveBalanceIncrements[i] = Math.floor(validator.effectiveBalance / EFFECTIVE_BALANCE_INCREMENT); | ||
|
||
if (isActiveValidator(validator, previousEpoch)) { | ||
if (previousShufflingIn == null && isActiveValidator(validator, previousEpoch)) { | ||
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. Can you detail more the motivation of this change? |
||
previousActiveIndices.push(i); | ||
} | ||
if (isActiveValidator(validator, currentEpoch)) { | ||
if (currentShufflingIn == null && isActiveValidator(validator, currentEpoch)) { | ||
currentActiveIndices.push(i); | ||
// We track totalActiveBalanceIncrements as ETH to fit total network balance in a JS number (53 bits) | ||
totalActiveBalanceIncrements += effectiveBalanceIncrements[i]; | ||
} | ||
if (isActiveValidator(validator, nextEpoch)) { | ||
if (nextShufflingIn == null && isActiveValidator(validator, nextEpoch)) { | ||
nextActiveIndices.push(i); | ||
} | ||
|
||
|
@@ -317,11 +325,11 @@ export class EpochCache { | |
throw Error("totalActiveBalanceIncrements >= Number.MAX_SAFE_INTEGER. MAX_EFFECTIVE_BALANCE is too low."); | ||
} | ||
|
||
const currentShuffling = computeEpochShuffling(state, currentActiveIndices, currentEpoch); | ||
const previousShuffling = isGenesis | ||
? currentShuffling | ||
: computeEpochShuffling(state, previousActiveIndices, previousEpoch); | ||
const nextShuffling = computeEpochShuffling(state, nextActiveIndices, nextEpoch); | ||
const currentShuffling = currentShufflingIn ?? computeEpochShuffling(state, currentActiveIndices, currentEpoch); | ||
const previousShuffling = | ||
previousShufflingIn ?? | ||
(isGenesis ? currentShuffling : computeEpochShuffling(state, previousActiveIndices, previousEpoch)); | ||
const nextShuffling = nextShufflingIn ?? computeEpochShuffling(state, nextActiveIndices, nextEpoch); | ||
|
||
const currentProposerSeed = getSeed(state, currentEpoch, DOMAIN_BEACON_PROPOSER); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/state-transition/src/util/loadState/findModifiedInactivityScores.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// UintNum64 = 8 bytes | ||
export const INACTIVITY_SCORE_SIZE = 8; | ||
|
||
/** | ||
* As monitored on mainnet, inactivityScores are not changed much and they are mostly 0 | ||
* Using Buffer.compare is the fastest way as noted in `./findModifiedValidators.ts` | ||
* @returns output parameter modifiedValidators: validator indices that are modified | ||
*/ | ||
export function findModifiedInactivityScores( | ||
inactivityScoresBytes: Uint8Array, | ||
inactivityScoresBytes2: Uint8Array, | ||
modifiedValidators: number[], | ||
validatorOffset = 0 | ||
): void { | ||
if (inactivityScoresBytes.length !== inactivityScoresBytes2.length) { | ||
throw new Error( | ||
"inactivityScoresBytes.length !== inactivityScoresBytes2.length " + | ||
inactivityScoresBytes.length + | ||
" vs " + | ||
inactivityScoresBytes2.length | ||
); | ||
} | ||
|
||
if (Buffer.compare(inactivityScoresBytes, inactivityScoresBytes2) === 0) { | ||
return; | ||
} | ||
|
||
if (inactivityScoresBytes.length === INACTIVITY_SCORE_SIZE) { | ||
modifiedValidators.push(validatorOffset); | ||
return; | ||
} | ||
|
||
const numValidator = Math.floor(inactivityScoresBytes.length / INACTIVITY_SCORE_SIZE); | ||
const halfValidator = Math.floor(numValidator / 2); | ||
findModifiedInactivityScores( | ||
inactivityScoresBytes.subarray(0, halfValidator * INACTIVITY_SCORE_SIZE), | ||
inactivityScoresBytes2.subarray(0, halfValidator * INACTIVITY_SCORE_SIZE), | ||
modifiedValidators, | ||
validatorOffset | ||
); | ||
findModifiedInactivityScores( | ||
inactivityScoresBytes.subarray(halfValidator * INACTIVITY_SCORE_SIZE), | ||
inactivityScoresBytes2.subarray(halfValidator * INACTIVITY_SCORE_SIZE), | ||
modifiedValidators, | ||
validatorOffset + halfValidator | ||
); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/state-transition/src/util/loadState/findModifiedValidators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {VALIDATOR_BYTES_SIZE} from "../sszBytes.js"; | ||
|
||
/** | ||
* Find modified validators by comparing two validators bytes using Buffer.compare() recursively | ||
* - As noted in packages/state-transition/test/perf/util/loadState/findModifiedValidators.test.ts, serializing validators and compare Uint8Array is the fastest way | ||
* - The performance is quite stable and can afford a lot of difference in validators (the benchmark tested up to 10k but it's not likely we have that difference in mainnet) | ||
* - Also packages/state-transition/test/perf/misc/byteArrayEquals.test.ts shows that Buffer.compare() is very efficient for large Uint8Array | ||
* | ||
* @returns output parameter modifiedValidators: validator indices that are modified | ||
*/ | ||
export function findModifiedValidators( | ||
validatorsBytes: Uint8Array, | ||
validatorsBytes2: Uint8Array, | ||
modifiedValidators: number[], | ||
validatorOffset = 0 | ||
): void { | ||
if (validatorsBytes.length !== validatorsBytes2.length) { | ||
throw new Error( | ||
"validatorsBytes.length !== validatorsBytes2.length " + validatorsBytes.length + " vs " + validatorsBytes2.length | ||
); | ||
} | ||
|
||
if (Buffer.compare(validatorsBytes, validatorsBytes2) === 0) { | ||
return; | ||
} | ||
|
||
if (validatorsBytes.length === VALIDATOR_BYTES_SIZE) { | ||
modifiedValidators.push(validatorOffset); | ||
return; | ||
} | ||
|
||
const numValidator = Math.floor(validatorsBytes.length / VALIDATOR_BYTES_SIZE); | ||
const halfValidator = Math.floor(numValidator / 2); | ||
findModifiedValidators( | ||
validatorsBytes.subarray(0, halfValidator * VALIDATOR_BYTES_SIZE), | ||
validatorsBytes2.subarray(0, halfValidator * VALIDATOR_BYTES_SIZE), | ||
modifiedValidators, | ||
validatorOffset | ||
); | ||
findModifiedValidators( | ||
validatorsBytes.subarray(halfValidator * VALIDATOR_BYTES_SIZE), | ||
validatorsBytes2.subarray(halfValidator * VALIDATOR_BYTES_SIZE), | ||
modifiedValidators, | ||
validatorOffset + halfValidator | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the
In
suffix mean? Can you comment above this block detailing the rationale and purpose of these lines?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those are shufflings comeing from
ShufflingCache
provided by BeaconChain. Will refactor tocachedNextShuffling
and add more comments 👍