forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 118
feat(tendermint): validators RPC #2310
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9fc3319
save dev state
onur-ozkan a6ebe1c
save dev state
onur-ozkan 9198049
make proto types serializable for RPC endpoint
onur-ozkan b48c1ba
remove dummy test
onur-ozkan 2470594
add RPC error type
onur-ozkan c4df52a
add TODO
onur-ozkan 95d008b
fix status filtering
onur-ozkan 61b1654
fix clippy warn
onur-ozkan 8d59f4d
resolve `todo!()`s
onur-ozkan 05eb07e
remove inline attribute
onur-ozkan 838bee4
improve `validators_rpc`
onur-ozkan ee20b9a
add coverage for tendermint_validators RPC
onur-ozkan 4c63f6e
apply nit changes
onur-ozkan 43dbf48
document `ValidatorStatus`
onur-ozkan db5cff6
use proper error variant on coin filtering
onur-ozkan 96306cc
apply nits
onur-ozkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| use common::{HttpStatusCode, PagingOptions, StatusCode}; | ||
| use cosmrs::staking::{Commission, Description, Validator}; | ||
| use mm2_core::mm_ctx::MmArc; | ||
| use mm2_err_handle::prelude::MmError; | ||
|
|
||
| use crate::{lp_coinfind_or_err, tendermint::TendermintCoinRpcError, MmCoinEnum}; | ||
|
|
||
| /// Represents current status of the validator. | ||
| #[derive(Default, Deserialize)] | ||
| pub(crate) enum ValidatorStatus { | ||
| All, | ||
| /// Validator is in the active set and participates in consensus. | ||
| #[default] | ||
| Bonded, | ||
| /// Validator is not in the active set and does not participate in consensus. | ||
| /// Accordingly, they do not receive rewards and cannot be slashed. | ||
| /// It is still possible to delegate tokens to a validator in this state. | ||
| Unbonded, | ||
| } | ||
|
|
||
| impl ToString for ValidatorStatus { | ||
| fn to_string(&self) -> String { | ||
| match self { | ||
| // An empty string doesn't filter any validators and we get an unfiltered result. | ||
| ValidatorStatus::All => String::default(), | ||
| ValidatorStatus::Bonded => "BOND_STATUS_BONDED".into(), | ||
| ValidatorStatus::Unbonded => "BOND_STATUS_UNBONDED".into(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct ValidatorsRPC { | ||
| #[serde(rename = "ticker")] | ||
| coin: String, | ||
| #[serde(flatten)] | ||
| paging: PagingOptions, | ||
|
mariocynicys marked this conversation as resolved.
|
||
| #[serde(default)] | ||
| filter_by_status: ValidatorStatus, | ||
| } | ||
|
|
||
| #[derive(Clone, Serialize)] | ||
| pub struct ValidatorsRPCResponse { | ||
| validators: Vec<serde_json::Value>, | ||
| } | ||
|
|
||
| #[derive(Clone, Debug, Display, Serialize, SerializeErrorType, PartialEq)] | ||
| #[serde(tag = "error_type", content = "error_data")] | ||
| pub enum ValidatorsRPCError { | ||
| #[display(fmt = "Coin '{ticker}' could not be found in coins configuration.")] | ||
| CoinNotFound { ticker: String }, | ||
| #[display(fmt = "'{ticker}' is not a Cosmos coin.")] | ||
| UnexpectedCoinType { ticker: String }, | ||
| #[display(fmt = "Transport error: {}", _0)] | ||
| Transport(String), | ||
| #[display(fmt = "Internal error: {}", _0)] | ||
| InternalError(String), | ||
| } | ||
|
|
||
| impl HttpStatusCode for ValidatorsRPCError { | ||
| fn status_code(&self) -> common::StatusCode { | ||
| match self { | ||
| ValidatorsRPCError::Transport(_) => StatusCode::SERVICE_UNAVAILABLE, | ||
| ValidatorsRPCError::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR, | ||
| ValidatorsRPCError::CoinNotFound { .. } => StatusCode::NOT_FOUND, | ||
| ValidatorsRPCError::UnexpectedCoinType { .. } => StatusCode::BAD_REQUEST, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<TendermintCoinRpcError> for ValidatorsRPCError { | ||
| fn from(e: TendermintCoinRpcError) -> Self { | ||
| match e { | ||
| TendermintCoinRpcError::InvalidResponse(e) | ||
| | TendermintCoinRpcError::PerformError(e) | ||
| | TendermintCoinRpcError::RpcClientError(e) => ValidatorsRPCError::Transport(e), | ||
| TendermintCoinRpcError::Prost(e) | TendermintCoinRpcError::InternalError(e) => ValidatorsRPCError::InternalError(e), | ||
| TendermintCoinRpcError::UnexpectedAccountType { .. } => ValidatorsRPCError::InternalError( | ||
| "RPC client got an unexpected error 'TendermintCoinRpcError::UnexpectedAccountType', this isn't normal." | ||
| .into(), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub async fn validators_rpc( | ||
| ctx: MmArc, | ||
| req: ValidatorsRPC, | ||
| ) -> Result<ValidatorsRPCResponse, MmError<ValidatorsRPCError>> { | ||
| fn maybe_jsonize_description(description: Option<Description>) -> Option<serde_json::Value> { | ||
| description.map(|d| { | ||
| json!({ | ||
| "moniker": d.moniker, | ||
| "identity": d.identity, | ||
| "website": d.website, | ||
| "security_contact": d.security_contact, | ||
| "details": d.details, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| fn maybe_jsonize_commission(commission: Option<Commission>) -> Option<serde_json::Value> { | ||
| commission.map(|c| { | ||
| let rates = c.commission_rates.map(|cr| { | ||
| json!({ | ||
| "rate": cr.rate, | ||
| "max_rate": cr.max_rate, | ||
| "max_change_rate": cr.max_change_rate | ||
| }) | ||
| }); | ||
|
|
||
| json!({ | ||
| "commission_rates": rates, | ||
| "update_time": c.update_time | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| fn jsonize_validator(v: Validator) -> serde_json::Value { | ||
| json!({ | ||
| "operator_address": v.operator_address, | ||
| "consensus_pubkey": v.consensus_pubkey, | ||
| "jailed": v.jailed, | ||
| "status": v.status, | ||
| "tokens": v.tokens, | ||
| "delegator_shares": v.delegator_shares, | ||
| "description": maybe_jsonize_description(v.description), | ||
| "unbonding_height": v.unbonding_height, | ||
| "unbonding_time": v.unbonding_time, | ||
| "commission": maybe_jsonize_commission(v.commission), | ||
| "min_self_delegation": v.min_self_delegation, | ||
| }) | ||
| } | ||
|
|
||
| let validators = match lp_coinfind_or_err(&ctx, &req.coin).await { | ||
| Ok(MmCoinEnum::Tendermint(coin)) => coin.validators_list(req.filter_by_status, req.paging).await?, | ||
| Ok(MmCoinEnum::TendermintToken(token)) => { | ||
| token | ||
| .platform_coin | ||
| .validators_list(req.filter_by_status, req.paging) | ||
| .await? | ||
| }, | ||
| Ok(_) => return MmError::err(ValidatorsRPCError::UnexpectedCoinType { ticker: req.coin }), | ||
| Err(_) => return MmError::err(ValidatorsRPCError::CoinNotFound { ticker: req.coin }), | ||
| }; | ||
|
|
||
| Ok(ValidatorsRPCResponse { | ||
| validators: validators.into_iter().map(jsonize_validator).collect(), | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.