tendermint_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 genesis file in multiple chunks
60    GenesisChunked,
61
62    /// Get block header
63    Header,
64
65    /// Get block header by hash
66    HeaderByHash,
67
68    /// Get health info
69    Health,
70
71    /// Get network info
72    NetInfo,
73
74    /// Get node status
75    Status,
76
77    /// Find transaction by hash
78    Tx,
79
80    /// Search for transactions with their results
81    TxSearch,
82
83    /// Get validator info for a block
84    Validators,
85
86    /// Subscribe to events
87    Subscribe,
88
89    /// Unsubscribe from events
90    Unsubscribe,
91
92    /// Broadcast evidence
93    BroadcastEvidence,
94}
95
96impl Method {
97    /// Get a static string which represents this method name
98    pub fn as_str(self) -> &'static str {
99        match self {
100            Method::AbciInfo => "abci_info",
101            Method::AbciQuery => "abci_query",
102            Method::Block => "block",
103            Method::BlockByHash => "block_by_hash",
104            Method::BlockResults => "block_results",
105            Method::BlockSearch => "block_search",
106            Method::Blockchain => "blockchain",
107            Method::BroadcastEvidence => "broadcast_evidence",
108            Method::BroadcastTxAsync => "broadcast_tx_async",
109            Method::BroadcastTxSync => "broadcast_tx_sync",
110            Method::BroadcastTxCommit => "broadcast_tx_commit",
111            Method::Commit => "commit",
112            Method::ConsensusParams => "consensus_params",
113            Method::ConsensusState => "consensus_state",
114            Method::Genesis => "genesis",
115            Method::GenesisChunked => "genesis_chunked",
116            Method::Header => "header",
117            Method::HeaderByHash => "header_by_hash",
118            Method::Health => "health",
119            Method::NetInfo => "net_info",
120            Method::Status => "status",
121            Method::Subscribe => "subscribe",
122            Method::Tx => "tx",
123            Method::TxSearch => "tx_search",
124            Method::Unsubscribe => "unsubscribe",
125            Method::Validators => "validators",
126        }
127    }
128}
129
130impl FromStr for Method {
131    type Err = Error;
132
133    fn from_str(s: &str) -> Result<Self, Error> {
134        Ok(match s {
135            "abci_info" => Method::AbciInfo,
136            "abci_query" => Method::AbciQuery,
137            "block" => Method::Block,
138            "block_by_hash" => Method::BlockByHash,
139            "block_results" => Method::BlockResults,
140            "header" => Method::Header,
141            "header_by_hash" => Method::HeaderByHash,
142            "block_search" => Method::BlockSearch,
143            "blockchain" => Method::Blockchain,
144            "broadcast_evidence" => Method::BroadcastEvidence,
145            "broadcast_tx_async" => Method::BroadcastTxAsync,
146            "broadcast_tx_sync" => Method::BroadcastTxSync,
147            "broadcast_tx_commit" => Method::BroadcastTxCommit,
148            "commit" => Method::Commit,
149            "consensus_params" => Method::ConsensusParams,
150            "consensus_state" => Method::ConsensusState,
151            "genesis" => Method::Genesis,
152            "health" => Method::Health,
153            "net_info" => Method::NetInfo,
154            "status" => Method::Status,
155            "subscribe" => Method::Subscribe,
156            "tx" => Method::Tx,
157            "tx_search" => Method::TxSearch,
158            "unsubscribe" => Method::Unsubscribe,
159            "validators" => Method::Validators,
160            other => return Err(Error::method_not_found(other.to_string())),
161        })
162    }
163}
164
165impl Display for Method {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        write!(f, "{}", self.as_str())
168    }
169}
170
171impl Serialize for Method {
172    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
173        self.as_str().serialize(serializer)
174    }
175}
176
177impl<'de> Deserialize<'de> for Method {
178    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
179        Self::from_str(&String::deserialize(deserializer)?)
180            .map_err(|e| D::Error::custom(format!("{e}")))
181    }
182}