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: add historical state regen #6033

Merged
merged 29 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
583bc18
feat: add historical state regen
wemeetagain Oct 11, 2023
8c1790b
chore: wire up metrics
wemeetagain Oct 11, 2023
93c1981
chore: make historical state regen module optional
wemeetagain Oct 13, 2023
06e58af
chore: persist pubkey cache across historical state regen runs
wemeetagain Oct 13, 2023
623b58c
chore: cleanup worker termination
wemeetagain Oct 13, 2023
deba946
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Oct 13, 2023
b8d6113
chore: fix worker usage
wemeetagain Oct 23, 2023
0315e1a
fix: swap Level for ClassicLevel for multithreading
matthewkeil Nov 6, 2023
cdf3732
fix: getStateV2 state handling hack
matthewkeil Nov 6, 2023
a8f719d
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Jan 22, 2024
6d9d256
chore: update classic-level
wemeetagain Jan 22, 2024
5a812e1
chore: fix build errors
wemeetagain Jan 22, 2024
dabd767
chore: add comments
wemeetagain Jan 22, 2024
51fc29a
chore: fix test worker path
wemeetagain Jan 22, 2024
21bb96b
chore: simplify function naming
wemeetagain Jan 22, 2024
dc45097
chore: optimize getSlotFromOffset
wemeetagain Jan 22, 2024
d32f103
chore: refactor to avoid needless deserialization
wemeetagain Jan 22, 2024
25b1c9f
fix: update metrics names
wemeetagain Jan 22, 2024
b6fd577
feat: add historical state regen dashboard
wemeetagain Jan 22, 2024
e8c9d27
fix: update vm dashboards with historical state worker
wemeetagain Jan 22, 2024
03a241c
chore: fix test data
wemeetagain Jan 22, 2024
6001521
feat: transfer state across worker boundary
wemeetagain Jan 22, 2024
41c8b5d
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Jul 9, 2024
7ea9c1a
chore: address some pr comments
wemeetagain Jul 9, 2024
4a297dd
chore: clean module close
wemeetagain Jul 9, 2024
73c1b7c
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Jul 16, 2024
cc4197f
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Jul 18, 2024
80debf1
Merge branch 'unstable' into cayman/historical-state-regen
wemeetagain Jul 23, 2024
e8f4d97
feat: add metrics
twoeths Jul 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {BlockAttributes, produceBlockBody} from "./produceBlock/produceBlockBody
import {computeNewStateRoot} from "./produceBlock/computeNewStateRoot.js";
import {BlockInput} from "./blocks/types.js";
import {SeenAttestationDatas} from "./seenCache/seenAttestationData.js";
import {HistoricalStateRegen} from "./historicalState/index.js";

/**
* Arbitrary constants, blobs should be consumed immediately in the same slot they are produced.
Expand Down Expand Up @@ -105,6 +106,7 @@ export class BeaconChain implements IBeaconChain {
readonly regen: QueuedStateRegenerator;
readonly lightClientServer: LightClientServer;
readonly reprocessController: ReprocessController;
readonly historicalStateRegen?: HistoricalStateRegen;

// Ops pool
readonly attestationPool: AttestationPool;
Expand Down Expand Up @@ -162,6 +164,7 @@ export class BeaconChain implements IBeaconChain {
eth1,
executionEngine,
executionBuilder,
historicalStateRegen,
}: {
config: BeaconConfig;
db: IBeaconDb;
Expand All @@ -174,6 +177,7 @@ export class BeaconChain implements IBeaconChain {
eth1: IEth1ForBlockProduction;
executionEngine: IExecutionEngine;
executionBuilder?: IExecutionBuilder;
historicalStateRegen?: HistoricalStateRegen;
}
) {
this.opts = opts;
Expand All @@ -188,6 +192,7 @@ export class BeaconChain implements IBeaconChain {
this.eth1 = eth1;
this.executionEngine = executionEngine;
this.executionBuilder = executionBuilder;
this.historicalStateRegen = historicalStateRegen;
const signal = this.abortController.signal;
const emitter = new ChainEventEmitter();
// by default, verify signatures on both main threads and worker threads
Expand Down Expand Up @@ -390,11 +395,16 @@ export class BeaconChain implements IBeaconChain {
return state && {state, executionOptimistic: isOptimisticBlock(block)};
}
} else {
// request for finalized state
// request for finalized state using historical state regen
const stateSerialized = await this.historicalStateRegen?.getHistoricalState(slot);
wemeetagain marked this conversation as resolved.
Show resolved Hide resolved
if (!stateSerialized) {
return null;
}
const state = this.config
.getForkTypes(slot)
.BeaconState.deserialize(stateSerialized) as unknown as BeaconStateAllForks;

// do not attempt regen, just check if state is already in DB
const state = await this.db.stateArchive.get(slot);
return state && {state, executionOptimistic: false};
return {state, executionOptimistic: false};
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
BeaconStateAllForks,
BeaconStateTransitionMetrics,
CachedBeaconStateAllForks,
DataAvailableStatus,
ExecutionPayloadStatus,
PubkeyIndexMap,
createCachedBeaconState,
stateTransition,
} from "@lodestar/state-transition";
import {SignedBeaconBlock} from "@lodestar/types/allForks";
import {BeaconConfig} from "@lodestar/config";
import {IBeaconDb} from "../../db/index.js";

export function syncPubkeyCache(state: BeaconStateAllForks, pubkey2index: PubkeyIndexMap): void {
// Get the validators sub tree once for all the loop
const validators = state.validators;

const newCount = state.validators.length;
for (let i = pubkey2index.size; i < newCount; i++) {
const pubkey = validators.getReadonly(i).pubkey;
pubkey2index.set(pubkey, i);
}
}

export async function getNearestState(
slot: number,
config: BeaconConfig,
db: IBeaconDb,
pubkey2index: PubkeyIndexMap
): Promise<CachedBeaconStateAllForks> {
const states = await db.stateArchive.values({limit: 1, lte: slot, reverse: true});
if (!states.length) {
throw new Error("No near state found in the database");
}

const state = states[0];
syncPubkeyCache(state, pubkey2index);

return createCachedBeaconState(
state,
{
config,
pubkey2index,
index2pubkey: [],
},
{
skipSyncPubkeys: true,
}
);
}

export function getBlocksBetween(from: number, to: number, db: IBeaconDb): AsyncIterable<SignedBeaconBlock> {
return db.blockArchive.valuesStream({gt: from, lte: to});
}
wemeetagain marked this conversation as resolved.
Show resolved Hide resolved

export async function getHistoricalState(
slot: number,
config: BeaconConfig,
db: IBeaconDb,
pubkey2index: PubkeyIndexMap,
metrics?: BeaconStateTransitionMetrics
): Promise<Uint8Array> {
let state = await getNearestState(slot, config, db, pubkey2index);
for await (const block of getBlocksBetween(state.slot, slot, db)) {
state = stateTransition(
twoeths marked this conversation as resolved.
Show resolved Hide resolved
state,
block,
{
verifyProposer: false,
verifySignatures: false,
verifyStateRoot: false,
executionPayloadStatus: ExecutionPayloadStatus.valid,
dataAvailableStatus: DataAvailableStatus.available,
},
metrics
);
}
return state.serialize();
twoeths marked this conversation as resolved.
Show resolved Hide resolved
}
63 changes: 63 additions & 0 deletions packages/beacon-node/src/chain/historicalState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {ModuleThread, Thread, spawn} from "@chainsafe/threads";
import {chainConfigToJson} from "@lodestar/config";
import {LoggerNode} from "@lodestar/logger/node";
import {
HistoricalStateRegenInitModules,
HistoricalStateRegenModules,
HistoricalStateWorkerApi,
HistoricalStateWorkerData,
} from "./types.js";

/**
* HistoricalStateRegen limits the damage from recreating historical states
* by running regen in a separate worker thread.
*/
export class HistoricalStateRegen implements HistoricalStateWorkerApi {
private readonly api: ModuleThread<HistoricalStateWorkerApi>;
private readonly logger: LoggerNode;

constructor(modules: HistoricalStateRegenModules) {
this.api = modules.api;
this.logger = modules.logger;
}
static async init(modules: HistoricalStateRegenInitModules): Promise<HistoricalStateRegen> {
const workerData: HistoricalStateWorkerData = {
chainConfigJson: chainConfigToJson(modules.config),
genesisValidatorsRoot: modules.config.genesisValidatorsRoot,
genesisTime: modules.opts.genesisTime,
maxConcurrency: 1,
maxLength: 50,
dbLocation: modules.opts.dbLocation,
metricsEnabled: Boolean(modules.metrics),
loggerOpts: modules.logger.toOpts(),
};

const worker = new Worker("./worker.js", {
workerData,
} as ConstructorParameters<typeof Worker>[1]);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
wemeetagain marked this conversation as resolved.
Show resolved Hide resolved
const api = await spawn<HistoricalStateWorkerApi>(worker, {
// A Lodestar Node may do very expensive task at start blocking the event loop and causing
// the initialization to timeout. The number below is big enough to almost disable the timeout
timeout: 5 * 60 * 1000,
twoeths marked this conversation as resolved.
Show resolved Hide resolved
});

return new HistoricalStateRegen({...modules, api});
}

async scrapeMetrics(): Promise<string> {
return this.api.scrapeMetrics();
}

async close(): Promise<void> {
await this.api.close();
this.logger.debug("Terminating historical state worker");
await Thread.terminate(this.api);
this.logger.debug("Terminated historical state worker");
}

async getHistoricalState(slot: number): Promise<Uint8Array> {
return this.api.getHistoricalState(slot);
twoeths marked this conversation as resolved.
Show resolved Hide resolved
}
}
34 changes: 34 additions & 0 deletions packages/beacon-node/src/chain/historicalState/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {ModuleThread} from "@chainsafe/threads";
import {BeaconConfig} from "@lodestar/config";
import {LoggerNode, LoggerNodeOpts} from "@lodestar/logger/node";
import {Metrics} from "../../metrics/index.js";

export type HistoricalStateRegenInitModules = {
opts: {
genesisTime: number;
dbLocation: string;
};
config: BeaconConfig;
logger: LoggerNode;
metrics: Metrics | null;
};
export type HistoricalStateRegenModules = HistoricalStateRegenInitModules & {
api: ModuleThread<HistoricalStateWorkerApi>;
};

export type HistoricalStateWorkerData = {
chainConfigJson: Record<string, string>;
genesisValidatorsRoot: Uint8Array;
genesisTime: number;
maxConcurrency: number;
maxLength: number;
dbLocation: string;
metricsEnabled: boolean;
loggerOpts: LoggerNodeOpts;
};

export type HistoricalStateWorkerApi = {
close(): Promise<void>;
scrapeMetrics(): Promise<string>;
getHistoricalState(slot: number): Promise<Uint8Array>;
};
Loading
Loading