cometbft_rpc/
method.rs

1//! JSON-RPC request methods
2
3use core::{
4    fmt::{self, Display},
5    str::FromStr,
6};
7
8use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
9
10use crate::{prelude::*, Error};
11
12/// JSON-RPC request methods.
13///
14/// Serialized as the "method" field of JSON-RPC/HTTP requests.
15#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
16pub enum Method {
17    /// Get ABCI info
18    AbciInfo,
19
20    /// Get ABCI query
21    AbciQuery,
22
23    /// Get block info
24    Block,
25
26    /// Get block info by hash
27    BlockByHash,
28
29    /// Get ABCI results for a particular block
30    BlockResults,
31
32    /// Search for blocks by their BeginBlock and EndBlock events
33    BlockSearch,
34
35    /// Get blockchain info
36    Blockchain,
37
38    /// Broadcast transaction asynchronously
39    BroadcastTxAsync,
40
41    /// Broadcast transaction synchronously
42    BroadcastTxSync,
43
44    /// Broadcast transaction commit
45    BroadcastTxCommit,
46
47    /// Get commit info for a block
48    Commit,
49
50    /// Get consensus parameters
51    ConsensusParams,
52
53    /// Get consensus state
54    ConsensusState,
55
56    /// Get genesis file
57    Genesis,
58
59    /// Get block header
60    Header,
61
62    /// Get block header by hash
63    HeaderByHash,
64
65    /// Get health info
66    Health,
67
68    /// Get network info
69    NetInfo,
70
71    /// Get node status
72    Status,
73
74    /// Find transaction by hash
75    Tx,
76
77    /// Search for transactions with their results
78    TxSearch,
79
80    /// Get validator info for a block
81    Validators,
82
83    /// Subscribe to events
84    Subscribe,
85
86    /// Unsubscribe from events
87    Unsubscribe,
88
89    /// Broadcast evidence
90    BroadcastEvidence,
91}
92
93impl Method {
94    /// Get a static string which represents this method name
95    pub fn as_str(self) -> &'static str {
96        match self {
97            Method::AbciInfo => "abci_info",
98            Method::AbciQuery => "abci_query",
99            Method::Block => "block",
100            Method::BlockByHash => "block_by_hash",
101            Method::BlockResults => "block_results",
102            Method::BlockSearch => "block_search",
103            Method::Blockchain => "blockchain",
104            Method::BroadcastEvidence => "broadcast_evidence",
105            Method::BroadcastTxAsync => "broadcast_tx_async",
106            Method::BroadcastTxSync => "broadcast_tx_sync",
107            Method::BroadcastTxCommit => "broadcast_tx_commit",
108            Method::Commit => "commit",
109            Method::ConsensusParams => "consensus_params",
110            Method::ConsensusState => "consensus_state",
111            Method::Genesis => "genesis",
112            Method::Header => "header",
113            Method::HeaderByHash => "header_by_hash",
114            Method::Health => "health",
115            Method::NetInfo => "net_info",
116            Method::Status => "status",
117            Method::Subscribe => "subscribe",
118            Method::Tx => "tx",
119            Method::TxSearch => "tx_search",
120            Method::Unsubscribe => "unsubscribe",
121            Method::Validators => "validators",
122        }
123    }
124}
125
126impl FromStr for Method {
127    type Err = Error;
128
129    fn from_str(s: &str) -> Result<Self, Error> {
130        Ok(match s {
131            "abci_info" => Method::AbciInfo,
132            "abci_query" => Method::AbciQuery,
133            "block" => Method::Block,
134            "block_by_hash" => Method::BlockByHash,
135            "block_results" => Method::BlockResults,
136            "header" => Method::Header,
137            "header_by_hash" => Method::HeaderByHash,
138            "block_search" => Method::BlockSearch,
139            "blockchain" => Method::Blockchain,
140            "broadcast_evidence" => Method::BroadcastEvidence,
141            "broadcast_tx_async" => Method::BroadcastTxAsync,
142            "broadcast_tx_sync" => Method::BroadcastTxSync,
143            "broadcast_tx_commit" => Method::BroadcastTxCommit,
144            "commit" => Method::Commit,
145            "consensus_params" => Method::ConsensusParams,
146            "consensus_state" => Method::ConsensusState,
147            "genesis" => Method::Genesis,
148            "health" => Method::Health,
149            "net_info" => Method::NetInfo,
150            "status" => Method::Status,
151            "subscribe" => Method::Subscribe,
152            "tx" => Method::Tx,
153            "tx_search" => Method::TxSearch,
154            "unsubscribe" => Method::Unsubscribe,
155            "validators" => Method::Validators,
156            other => return Err(Error::method_not_found(other.to_string())),
157        })
158    }
159}
160
161impl Display for Method {
162    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163        write!(f, "{}", self.as_str())
164    }
165}
166
167impl Serialize for Method {
168    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
169        self.as_str().serialize(serializer)
170    }
171}
172
173impl<'de> Deserialize<'de> for Method {
174    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
175        Self::from_str(&String::deserialize(deserializer)?)
176            .map_err(|e| D::Error::custom(format!("{e}")))
177    }
178}