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 debug_getHistoricalSummaries endpoint #7245

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
30 changes: 29 additions & 1 deletion packages/api/src/beacon/routes/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ const DebugChainHeadListType = ArrayOf(DebugChainHeadType);
type ProtoNodeList = ValueOf<typeof ProtoNodeListType>;
type DebugChainHeadList = ValueOf<typeof DebugChainHeadListType>;
type ForkChoiceResponse = ValueOf<typeof ForkChoiceResponseType>;

const HistoricalSummariesResponseType = new ContainerType(
{
HistoricalSummaries: ssz.capella.HistoricalSummaries,
proof: ArrayOf(ssz.Bytes8),
},
{jsonCase: "eth2"}
);
export type Endpoints = {
/**
* Retrieves all possible chain heads (leaves of fork choice tree).
Expand Down Expand Up @@ -135,6 +141,13 @@ export type Endpoints = {
BeaconState,
ExecutionOptimisticFinalizedAndVersionMeta
>;
getHistoricalSummaries: Endpoint<
"GET",
StateArgs,
{params: {state_id: string}},
ValueOf<typeof HistoricalSummariesResponseType>,
nflaig marked this conversation as resolved.
Show resolved Hide resolved
EmptyMeta
>;
};

export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpoints> {
Expand Down Expand Up @@ -196,5 +209,20 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
timeoutMs: 5 * 60 * 1000,
},
},
getHistoricalSummaries: {
url: "/eth/v0/debug/historical_summaries/{state_id}",
method: "GET",
req: {
writeReq: ({stateId}) => ({params: {state_id: stateId.toString()}}),
parseReq: ({params}) => ({stateId: params.state_id}),
schema: {
params: {state_id: Schema.StringRequired},
},
},
resp: {
data: HistoricalSummariesResponseType,
meta: EmptyMetaCodec,
},
},
};
}
16 changes: 16 additions & 0 deletions packages/api/test/unit/beacon/genericServerTest/debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,21 @@ describe("beacon / debug", () => {
expect(res.wireFormat()).toBe(WireFormat.ssz);
expect(toHexString(res.ssz())).toBe(toHexString(stateSerialized));
});
it("getHistoricalSummaries", async () => {
nflaig marked this conversation as resolved.
Show resolved Hide resolved
const state = ssz.deneb.BeaconState.defaultValue();
mockApi.getHistoricalSummaries.mockResolvedValue({
data: {HistoricalSummaries: state.historicalSummaries, proof: []},
meta: undefined,
});

const httpClient = new HttpClient({baseUrl});
const client = getClient(config, httpClient);

const res = await client.getHistoricalSummaries({stateId: "head"}, {responseWireFormat: WireFormat.json});

expect(res.ok).toBe(true);
expect(res.wireFormat()).toBe(WireFormat.json);
expect(res.json()).toStrictEqual({data: {Historical_summaries: [], proof: []}});
});
});
});
28 changes: 26 additions & 2 deletions packages/beacon-node/src/api/impl/debug/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {routes} from "@lodestar/api";
import {ApplicationMethods} from "@lodestar/api/server";
import {ExecutionStatus} from "@lodestar/fork-choice";
import {ZERO_HASH_HEX} from "@lodestar/params";
import {BeaconState} from "@lodestar/types";
import {ForkName, ForkSeq, ZERO_HASH_HEX} from "@lodestar/params";
import {BeaconState, ssz} from "@lodestar/types";
import {isOptimisticBlock} from "../../../util/forkChoice.js";
import {getStateSlotFromBytes} from "../../../util/multifork.js";
import {getStateResponseWithRegen} from "../beacon/state/utils.js";
import {ApiModules} from "../types.js";
import {Tree} from "@chainsafe/persistent-merkle-tree";

export function getDebugApi({
chain,
Expand Down Expand Up @@ -85,5 +86,28 @@ export function getDebugApi({
},
};
},
async getHistoricalSummaries({stateId}, _context) {
const {state} = await getStateResponseWithRegen(chain, stateId);
let slot: number;
if (state instanceof Uint8Array) {
slot = getStateSlotFromBytes(state);
} else {
slot = state.slot;
}
if (config.getForkSeq(slot) < ForkSeq.capella) {
throw new Error("Historical summaries are not supported before Capella");
}
const fork = config.getForkName(slot) as Exclude<ForkName, "phase0" | "altair" | "bellatrix">;
const stateView = state instanceof Uint8Array ? ssz[fork].BeaconState.deserializeToViewDU(state) : state;
nflaig marked this conversation as resolved.
Show resolved Hide resolved
const gindex = ssz[fork].BeaconState.getPathInfo(["historicalSummaries"]);
const proof = new Tree(stateView.node).getSingleProof(gindex.gindex);

return {
data: {
HistoricalSummaries: (stateView as any).historicalSummaries.toValue(),
proof: proof,
},
};
},
};
}
6 changes: 5 additions & 1 deletion packages/types/src/capella/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export const HistoricalSummary = new ContainerType(
{typeName: "HistoricalSummary", jsonCase: "eth2"}
);

export const HistoricalSummaries = new ListCompositeType(HistoricalSummary, HISTORICAL_ROOTS_LIMIT, {
typeName: "HistoricalSummaries",
});

// we don't reuse bellatrix.BeaconState fields since we need to replace some keys
// and we cannot keep order doing that
export const BeaconState = new ContainerType(
Expand Down Expand Up @@ -168,7 +172,7 @@ export const BeaconState = new ContainerType(
nextWithdrawalIndex: WithdrawalIndex, // [New in Capella]
nextWithdrawalValidatorIndex: ValidatorIndex, // [New in Capella]
// Deep history valid from Capella onwards
historicalSummaries: new ListCompositeType(HistoricalSummary, HISTORICAL_ROOTS_LIMIT), // [New in Capella]
historicalSummaries: HistoricalSummaries, // [New in Capella]
},
{typeName: "BeaconState", jsonCase: "eth2"}
);
Expand Down
Loading