Skip to content

Add new HTTP endpoint to get supply for a given state root #7102

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

Open
wants to merge 15 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ jobs:
bins: cargo-audit
- name: Check formatting with cargo fmt
run: make cargo-fmt
- name: Markdown-linter
run: make mdlint
- name: Lint code for quality and style with Clippy
run: make lint-full
- name: Certify Cargo.lock freshness
Expand All @@ -374,8 +376,6 @@ jobs:
run: make audit-CI
- name: Run cargo vendor to make sure dependencies can be vendored for packaging, reproducibility and archival purpose
run: CARGO_HOME=$(readlink -f $HOME) make vendor
- name: Markdown-linter
run: make mdlint
- name: Spell-check
uses: rojopolis/spellcheck-github-actions@v0
check-msrv:
Expand Down
29 changes: 29 additions & 0 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4666,6 +4666,34 @@ pub fn serve<T: BeaconChainTypes>(
})
});

// GET lighthouse/analysis/global_validator_supply/{state_root}
let get_lighthouse_global_validator_supply = warp::path("lighthouse")
.and(warp::path("analysis"))
.and(warp::path("global_validator_supply"))
.and(warp::path::param::<StateId>())
.and(warp::path::end())
.and(task_spawner_filter.clone())
.and(chain_filter.clone())
.then(
|state_id: StateId,
task_spawner: TaskSpawner<T::EthSpec>,
chain: Arc<BeaconChain<T>>| {
task_spawner.blocking_json_task(Priority::P1, move || {
state_id.map_state_and_execution_optimistic_and_finalized(
&chain,
|state, execution_optimistic, finalized| {
let response = state.balances().iter().sum::<u64>();
Ok(api_types::GenericResponse::from(response)
.add_execution_optimistic_finalized(
execution_optimistic,
finalized,
))
},
)
})
},
);

// GET lighthouse/analysis/attestation_performance/{index}
let get_lighthouse_attestation_performance = warp::path("lighthouse")
.and(warp::path("analysis"))
Expand Down Expand Up @@ -4939,6 +4967,7 @@ pub fn serve<T: BeaconChainTypes>(
.uor(get_lighthouse_staking)
.uor(get_lighthouse_database_info)
.uor(get_lighthouse_block_rewards)
.uor(get_lighthouse_global_validator_supply)
.uor(get_lighthouse_attestation_performance)
.uor(get_beacon_light_client_optimistic_update)
.uor(get_beacon_light_client_finality_update)
Expand Down
38 changes: 37 additions & 1 deletion beacon_node/http_api/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5857,6 +5857,34 @@ impl ApiTester {
self
}

pub async fn test_get_lighthouse_global_validator_supply(self) -> Self {
for state_id in self.interesting_state_ids() {
let state_opt = state_id
.state(&self.chain)
.ok()
.map(|(state, _execution_optimistic, _finalized)| state);

let global_validator_supply = state_opt
.as_ref()
.map(|state| state.balances().iter().sum::<u64>());

let api_global_validator_supply = self
.client
.get_global_validator_supply(state_id.0)
.await
.unwrap()
.map(|res| res.data);

assert_eq!(
global_validator_supply, api_global_validator_supply,
"{:?}",
state_id
);
}

self
}

pub async fn test_post_lighthouse_database_reconstruct(self) -> Self {
let response = self
.client
Expand Down Expand Up @@ -7478,7 +7506,7 @@ async fn post_validator_liveness_epoch() {
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lighthouse_endpoints() {
async fn lighthouse_get_endpoints() {
ApiTester::new()
.await
.test_get_lighthouse_health()
Expand All @@ -7498,6 +7526,14 @@ async fn lighthouse_endpoints() {
.test_get_lighthouse_eth1_deposit_cache()
.await
.test_get_lighthouse_staking()
.await
.test_get_lighthouse_global_validator_supply()
.await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lighthouse_post_endpoints() {
ApiTester::new()
.await
.test_post_lighthouse_database_reconstruct()
.await
Expand Down
16 changes: 16 additions & 0 deletions book/src/api_lighthouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,22 @@ Caveats:
This is because the state *prior* to the `start_epoch` needs to be loaded from the database, and
loading a state on a boundary is most efficient.

## `lighthouse/analysis/global_validator_supply/{state_root}`

Returns the sum of all validator balances for a given state root.

```bash
curl -X GET "http://localhost:5052/lighthouse/analysis/global_validator_supply/0x7e76880eb67bbdc86250aa578958e9d0675e64e714337855204fb5abaaf82c2b" -H "accept: application/json" | jq
```

```json
{
"execution_optimistic": false,
"finalized": true,
"data": 674144000000000
}
```

## `/lighthouse/logs`

This is a Server Side Event subscription endpoint. This allows a user to read
Expand Down
17 changes: 17 additions & 0 deletions common/eth2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,23 @@ impl BeaconNodeHttpClient {
Ok(())
}

/// `GET lighthouse/analysis/global_validator_supply/{state_root}`
pub async fn get_global_validator_supply(
&self,
state_id: StateId,
) -> Result<Option<ExecutionOptimisticFinalizedResponse<u64>>, Error> {
let mut path = self.server.full.clone();

path.path_segments_mut()
.map_err(|()| Error::InvalidUrl(self.server.clone()))?
.push("lighthouse")
.push("analysis")
.push("global_validator_supply")
.push(&state_id.to_string());

self.get_opt(path).await
}

/// `GET events?topics`
pub async fn get_events<E: EthSpec>(
&self,
Expand Down
Loading