Skip to content

Commit

Permalink
Apply changes from #6655
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Apr 28, 2024
1 parent 1721a5d commit 8218615
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
84 changes: 84 additions & 0 deletions packages/api/src/beacon/routes/beacon/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
ArrayOf,
ExecutionOptimisticAndFinalizedCodec,
ExecutionOptimisticAndFinalizedMeta,
JsonOnlyReq,
} from "../../../utils/codecs.js";
import {fromU64Str, toU64Str} from "../../../utils/serdes.js";
import {WireFormat} from "../../../utils/headers.js";
import {RootResponse, RootResponseType} from "./block.js";

Expand Down Expand Up @@ -176,6 +178,23 @@ export type Endpoints = {
ExecutionOptimisticAndFinalizedMeta
>;

/**
* Get validators from state
* Returns filterable list of validators with their balance, status and index.
* @param stateId State identifier.
* Can be one of: "head" (canonical head in node's view), "genesis", "finalized", "justified", \<slot\>, \<hex encoded stateRoot with 0x prefix\>.
* @param id Either hex encoded public key (with 0x prefix) or validator index
* @param status [Validator status specification](https://hackmd.io/ofFJ5gOmQpu1jjHilHbdQQ)
*/
postStateValidators: Endpoint<
//
"POST",
{stateId: StateId; validatorIds?: ValidatorId[]; statuses?: ValidatorStatus[]},
{params: {state_id: string}; body: {ids?: string[]; statuses?: ValidatorStatus[]}},
ValidatorResponseList,
ExecutionOptimisticAndFinalizedMeta
>;

/**
* Get validator balances from state
* Returns filterable list of validator balances.
Expand All @@ -192,6 +211,22 @@ export type Endpoints = {
ExecutionOptimisticAndFinalizedMeta
>;

/**
* Get validator balances from state
* Returns filterable list of validator balances.
* @param stateId State identifier.
* Can be one of: "head" (canonical head in node's view), "genesis", "finalized", "justified", \<slot\>, \<hex encoded stateRoot with 0x prefix\>.
* @param id Either hex encoded public key (with 0x prefix) or validator index
*/
postStateValidatorBalances: Endpoint<
//
"POST",
{stateId: StateId; validatorIds?: ValidatorId[]},
{params: {state_id: string}; body: string[]},
ValidatorBalanceList,
ExecutionOptimisticAndFinalizedMeta
>;

/**
* Get all committees for a state.
* Retrieves the committees for the given state.
Expand Down Expand Up @@ -344,6 +379,33 @@ export const definitions: RouteDefinitions<Endpoints> = {
meta: ExecutionOptimisticAndFinalizedCodec,
},
},
postStateValidators: {
url: "/eth/v1/beacon/states/{state_id}/validators",
method: "POST",
req: JsonOnlyReq({
writeReqJson: ({stateId, validatorIds, statuses}) => ({
params: {state_id: stateId.toString()},
body: {
ids: validatorIds?.map((id) => (typeof id === "string" ? id : toU64Str(id))),
statuses,
},
}),
parseReqJson: ({params, body}) => ({
stateId: params.state_id,
validatorIds: body.ids?.map((id) => (typeof id === "string" && id.startsWith("0x") ? id : fromU64Str(id))),
statuses: body.statuses,
}),
schema: {
params: {state_id: Schema.StringRequired},
body: Schema.Object,
},
}),
resp: {
onlySupport: WireFormat.json,
data: ValidatorResponseListType,
meta: ExecutionOptimisticAndFinalizedCodec,
},
},
getStateValidatorBalances: {
url: "/eth/v1/beacon/states/{state_id}/validator_balances",
method: "GET",
Expand All @@ -360,4 +422,26 @@ export const definitions: RouteDefinitions<Endpoints> = {
meta: ExecutionOptimisticAndFinalizedCodec,
},
},
postStateValidatorBalances: {
url: "/eth/v1/beacon/states/{state_id}/validator_balances",
method: "POST",
req: JsonOnlyReq({
writeReqJson: ({stateId, validatorIds}) => ({
params: {state_id: stateId.toString()},
body: validatorIds?.map((id) => (typeof id === "string" ? id : toU64Str(id))) || [],
}),
parseReqJson: ({params, body = []}) => ({
stateId: params.state_id,
validatorIds: body.map((id) => (typeof id === "string" && id.startsWith("0x") ? id : fromU64Str(id))),
}),
schema: {
params: {state_id: Schema.StringRequired},
body: Schema.UintOrStringArray,
},
}),
resp: {
data: ValidatorBalanceListType,
meta: ExecutionOptimisticAndFinalizedCodec,
},
},
};
8 changes: 8 additions & 0 deletions packages/api/test/unit/beacon/testData/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export const testData: GenericServerTestCases<Endpoints> = {
args: {stateId: "head", id: [pubkeyHex, "1300"], status: ["active_ongoing"]},
res: {data: [validatorResponse], meta: {executionOptimistic: true, finalized: false}},
},
postStateValidators: {
args: {stateId: "head", validatorIds: [pubkeyHex, 1300], statuses: ["active_ongoing"]},
res: {data: [validatorResponse], meta: {executionOptimistic: true, finalized: false}},
},
getStateValidator: {
args: {stateId: "head", validatorId: pubkeyHex},
res: {data: validatorResponse, meta: {executionOptimistic: true, finalized: false}},
Expand All @@ -166,6 +170,10 @@ export const testData: GenericServerTestCases<Endpoints> = {
args: {stateId: "head", indices: ["1300"]},
res: {data: [{index: 1300, balance}], meta: {executionOptimistic: true, finalized: false}},
},
postStateValidatorBalances: {
args: {stateId: "head", validatorIds: [1300]},
res: {data: [{index: 1300, balance}], meta: {executionOptimistic: true, finalized: false}},
},
getEpochCommittees: {
args: {stateId: "head", index: 1, slot: 2, epoch: 3},
res: {data: [{index: 1, slot: 2, validators: [1300]}], meta: {executionOptimistic: true, finalized: false}},
Expand Down
8 changes: 8 additions & 0 deletions packages/beacon-node/src/api/impl/beacon/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ export function getBeaconStateApi({
};
},

async postStateValidators(stateId, filters) {
return this.getStateValidators(stateId, filters);
},

async getStateValidator({stateId, validatorId}) {
const {state, executionOptimistic, finalized} = await resolveStateId(chain, stateId);
const {pubkey2index} = chain.getHeadState().epochCtx;
Expand Down Expand Up @@ -183,6 +187,10 @@ export function getBeaconStateApi({
};
},

async postStateValidatorBalances(stateId, indices) {
return this.getStateValidatorBalances(stateId, indices);
},

async getEpochCommittees({stateId, ...filters}) {
const {state, executionOptimistic, finalized} = await resolveStateId(chain, stateId);

Expand Down

0 comments on commit 8218615

Please sign in to comment.