cometbft_rpc/endpoint/
validators.rs

1//! `/validators` endpoint JSON-RPC wrapper
2
3use cometbft::{block, validator};
4use serde::{Deserialize, Serialize};
5
6use crate::{
7    dialect::Dialect, prelude::*, request::RequestMessage, serializers, PageNumber, PerPage,
8};
9
10/// The default number of validators to return per page.
11pub const DEFAULT_VALIDATORS_PER_PAGE: u8 = 30;
12
13/// List validators for a specific block
14#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
15#[non_exhaustive]
16pub struct Request {
17    /// The height at which to retrieve the validator set. If not specified,
18    /// defaults to the latest height.
19    pub height: Option<block::Height>,
20    /// The number of the page to fetch.
21    #[serde(with = "serializers::optional_from_str")]
22    pub page: Option<PageNumber>,
23    /// The number of validators to fetch per page.
24    #[serde(with = "serializers::optional_from_str")]
25    pub per_page: Option<PerPage>,
26}
27
28impl Request {
29    /// List validators for a specific block.
30    ///
31    /// See the [CometBFT RPC] for the defaults for each option when set to
32    /// `None`.
33    ///
34    /// [CometBFT RPC]: https://docs.cometbft.com/v1/rpc/#/Info/validators
35    pub fn new(
36        height: Option<block::Height>,
37        page: Option<PageNumber>,
38        per_page: Option<PerPage>,
39    ) -> Self {
40        Self {
41            height,
42            page,
43            per_page,
44        }
45    }
46}
47
48impl RequestMessage for Request {
49    fn method(&self) -> crate::Method {
50        crate::Method::Validators
51    }
52}
53
54impl<S: Dialect> crate::Request<S> for Request {
55    type Response = Response;
56}
57
58impl<S: Dialect> crate::SimpleRequest<S> for Request {
59    type Output = Response;
60}
61
62/// Validator responses
63#[derive(Clone, Debug, Deserialize, Serialize)]
64#[non_exhaustive]
65pub struct Response {
66    /// Block height
67    pub block_height: block::Height,
68
69    /// Validator list
70    pub validators: Vec<validator::Info>,
71
72    /// Total number of validators for this block height.
73    #[serde(with = "serializers::from_str")]
74    pub total: i32,
75}
76
77impl crate::Response for Response {}
78
79impl Response {
80    /// Constructor.
81    pub fn new(block_height: block::Height, validators: Vec<validator::Info>, total: i32) -> Self {
82        Self {
83            block_height,
84            validators,
85            total,
86        }
87    }
88}