Skip to content
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 12 commits into from
Oct 31, 2023
Prev Previous commit
Next Next commit
chore: rename loadCachedBeaconState to loadUnfinalizedCachedBeaconState
  • Loading branch information
twoeths committed Oct 27, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 56bff342d2728d7cb3772103fb8064b6095fb505
3 changes: 2 additions & 1 deletion packages/state-transition/src/cache/stateCache.ts
Original file line number Diff line number Diff line change
@@ -156,8 +156,9 @@ export function createCachedBeaconState<T extends BeaconStateAllForks>(
* Create a CachedBeaconState given a cached seed state and state bytes
* This guarantees that the returned state shares the same tree with the seed state
* Check loadState() api for more details
* TODO: after EIP-6110 need to provide a pivotValidatorIndex to decide which comes to finalized validators cache, which comes to unfinalized cache
*/
export function loadCachedBeaconState<T extends BeaconStateAllForks & BeaconStateCache>(
export function loadUnfinalizedCachedBeaconState<T extends BeaconStateAllForks & BeaconStateCache>(
cachedSeedState: T,
stateBytes: Uint8Array,
opts?: EpochCacheOpts
2 changes: 1 addition & 1 deletion packages/state-transition/src/index.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ export type {
// Main state caches
export {
createCachedBeaconState,
loadCachedBeaconState,
loadUnfinalizedCachedBeaconState,
type BeaconStateCache,
isCachedBeaconState,
isStateBalancesNodesPopulated,
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import {config} from "@lodestar/config/default";
import {createBeaconConfig} from "@lodestar/config";
import {createCachedBeaconStateTest} from "../utils/state.js";
import {PubkeyIndexMap} from "../../src/cache/pubkeyCache.js";
import {createCachedBeaconState, loadCachedBeaconState} from "../../src/cache/stateCache.js";
import {createCachedBeaconState, loadUnfinalizedCachedBeaconState} from "../../src/cache/stateCache.js";
import {interopPubkeysCached} from "../utils/interop.js";
import {modifyStateSameValidator, newStateWithValidators} from "../utils/capella.js";

@@ -136,15 +136,15 @@ describe("CachedBeaconState", () => {

// confirm loadState() result
const stateBytes = state.serialize();
const newCachedState = loadCachedBeaconState(seedState, stateBytes, {skipSyncCommitteeCache: true});
const newCachedState = loadUnfinalizedCachedBeaconState(seedState, stateBytes, {skipSyncCommitteeCache: true});
const newStateBytes = newCachedState.serialize();
expect(newStateBytes).to.be.deep.equal(stateBytes, "loadState: state bytes are not equal");
expect(newCachedState.hashTreeRoot()).to.be.deep.equal(
state.hashTreeRoot(),
"loadState: state root is not equal"
);

// confirm loadCachedBeaconState() result
// confirm loadUnfinalizedCachedBeaconState() result
for (let i = 0; i < newCachedState.validators.length; i++) {
expect(newCachedState.epochCtx.pubkey2index.get(newCachedState.validators.get(i).pubkey)).to.be.equal(i);
expect(newCachedState.epochCtx.index2pubkey[i].toBytes()).to.be.deep.equal(pubkeys[i]);