Skip to content

Commit

Permalink
Merge pull request #3219 from AleoHQ/get_delegators_for_validator
Browse files Browse the repository at this point in the history
Add get_delegators_for_validator
  • Loading branch information
howardwu authored May 9, 2024
2 parents 209d0c6 + 1c74880 commit d6a441b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions node/rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
.route(&format!("/{network}/stateRoot/latest"), get(Self::get_state_root_latest))
.route(&format!("/{network}/stateRoot/:height"), get(Self::get_state_root))
.route(&format!("/{network}/committee/latest"), get(Self::get_committee_latest))
.route(&format!("/{network}/delegators/:validator"), get(Self::get_delegators_for_validator))

// Pass in `Rest` to make things convenient.
.with_state(self.clone())
Expand Down
20 changes: 19 additions & 1 deletion node/rest/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::*;
use snarkos_node_router::{messages::UnconfirmedSolution, SYNC_LENIENCY};
use snarkvm::{
ledger::puzzle::Solution,
prelude::{block::Transaction, Identifier, LimitedWriter, Plaintext, ToBytes},
prelude::{block::Transaction, Address, Identifier, LimitedWriter, Plaintext, ToBytes},
};

use indexmap::IndexMap;
Expand Down Expand Up @@ -264,6 +264,24 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
Ok(ErasedJson::pretty(rest.ledger.latest_committee()?))
}

// GET /<network>/delegators/{validator}
pub(crate) async fn get_delegators_for_validator(
State(rest): State<Self>,
Path(validator): Path<Address<N>>,
) -> Result<ErasedJson, RestError> {
// Do not process the request if the node is too far behind to avoid sending outdated data.
if rest.routing.num_blocks_behind() > SYNC_LENIENCY {
return Err(RestError("Unable to request delegators (node is syncing)".to_string()));
}

// Return the delegators for the given validator.
match tokio::task::spawn_blocking(move || rest.ledger.get_delegators_for_validator(&validator)).await {
Ok(Ok(delegators)) => Ok(ErasedJson::pretty(delegators)),
Ok(Err(err)) => Err(RestError(format!("Unable to request delegators - {err}"))),
Err(err) => Err(RestError(format!("Unable to request delegators - {err}"))),
}
}

// GET /<network>/peers/count
pub(crate) async fn get_peers_count(State(rest): State<Self>) -> ErasedJson {
ErasedJson::pretty(rest.routing.router().number_of_connected_peers())
Expand Down

0 comments on commit d6a441b

Please sign in to comment.