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

Add get_delegators_for_validator #3219

Merged
merged 5 commits into from
May 9, 2024
Merged
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
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you update the doc comment on SYNC_LENIENCY?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you like it to say?

Copy link
Contributor Author

@vicsn vicsn May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have a specific preference, but for example:

/// The maximum number of blocks the client can be behind it's latest peer before it skips
/// processing incoming transactions, solutions and GET delegator requests.

Or:

/// The maximum number of blocks the client can be behind it's latest peer before it skips
/// processing incoming transactions, solutions and heavy REST requests.

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 {
iamalwaysuncomfortable marked this conversation as resolved.
Show resolved Hide resolved
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