tendermint/v0_34/abci/
request.rs

1use tendermint_proto::v0_34::abci as pb;
2use tendermint_proto::Protobuf;
3
4use crate::abci::MethodKind;
5use crate::Error;
6
7pub use crate::abci::request::{
8    ApplySnapshotChunk, BeginBlock, CheckTx, CheckTxKind, DeliverTx, Echo, EndBlock, Info,
9    InitChain, LoadSnapshotChunk, OfferSnapshot, Query, SetOption,
10};
11
12/// All possible ABCI requests in CometBFT 0.34.
13#[allow(clippy::large_enum_variant)]
14#[derive(Clone, PartialEq, Eq, Debug)]
15pub enum Request {
16    #[doc = include_str!("../../abci/doc/request-echo.md")]
17    Echo(Echo),
18    #[doc = include_str!("../../abci/doc/request-flush.md")]
19    Flush,
20    #[doc = include_str!("../../abci/doc/request-info.md")]
21    Info(Info),
22    #[doc = include_str!("../../abci/doc/request-setoption.md")]
23    SetOption(SetOption),
24    #[doc = include_str!("../../abci/doc/request-initchain.md")]
25    InitChain(InitChain),
26    #[doc = include_str!("../../abci/doc/request-query.md")]
27    Query(Query),
28    #[doc = include_str!("../../abci/doc/request-beginblock.md")]
29    BeginBlock(BeginBlock),
30    #[doc = include_str!("../../abci/doc/request-checktx.md")]
31    CheckTx(CheckTx),
32    #[doc = include_str!("../../abci/doc/request-delivertx.md")]
33    DeliverTx(DeliverTx),
34    #[doc = include_str!("../../abci/doc/request-endblock.md")]
35    EndBlock(EndBlock),
36    #[doc = include_str!("../../abci/doc/request-commit.md")]
37    Commit,
38    #[doc = include_str!("../../abci/doc/request-listsnapshots.md")]
39    ListSnapshots,
40    #[doc = include_str!("../../abci/doc/request-offersnapshot.md")]
41    OfferSnapshot(OfferSnapshot),
42    #[doc = include_str!("../../abci/doc/request-loadsnapshotchunk.md")]
43    LoadSnapshotChunk(LoadSnapshotChunk),
44    #[doc = include_str!("../../abci/doc/request-applysnapshotchunk.md")]
45    ApplySnapshotChunk(ApplySnapshotChunk),
46}
47
48/// The consensus category of ABCI requests.
49#[allow(clippy::large_enum_variant)]
50#[derive(Clone, PartialEq, Eq, Debug)]
51pub enum ConsensusRequest {
52    #[doc = include_str!("../../abci/doc/request-initchain.md")]
53    InitChain(InitChain),
54    #[doc = include_str!("../../abci/doc/request-beginblock.md")]
55    BeginBlock(BeginBlock),
56    #[doc = include_str!("../../abci/doc/request-delivertx.md")]
57    DeliverTx(DeliverTx),
58    #[doc = include_str!("../../abci/doc/request-endblock.md")]
59    EndBlock(EndBlock),
60    #[doc = include_str!("../../abci/doc/request-commit.md")]
61    Commit,
62}
63
64/// The mempool category of ABCI requests.
65#[derive(Clone, PartialEq, Eq, Debug)]
66pub enum MempoolRequest {
67    #[doc = include_str!("../../abci/doc/request-checktx.md")]
68    CheckTx(CheckTx),
69}
70
71/// The info category of ABCI requests.
72#[derive(Clone, PartialEq, Eq, Debug)]
73pub enum InfoRequest {
74    #[doc = include_str!("../../abci/doc/request-info.md")]
75    Info(Info),
76    #[doc = include_str!("../../abci/doc/request-query.md")]
77    Query(Query),
78    #[doc = include_str!("../../abci/doc/request-echo.md")]
79    Echo(Echo),
80    #[doc = include_str!("../../abci/doc/request-setoption.md")]
81    SetOption(SetOption),
82}
83
84/// The snapshot category of ABCI requests.
85#[derive(Clone, PartialEq, Eq, Debug)]
86pub enum SnapshotRequest {
87    #[doc = include_str!("../../abci/doc/request-listsnapshots.md")]
88    ListSnapshots,
89    #[doc = include_str!("../../abci/doc/request-offersnapshot.md")]
90    OfferSnapshot(OfferSnapshot),
91    #[doc = include_str!("../../abci/doc/request-loadsnapshotchunk.md")]
92    LoadSnapshotChunk(LoadSnapshotChunk),
93    #[doc = include_str!("../../abci/doc/request-applysnapshotchunk.md")]
94    ApplySnapshotChunk(ApplySnapshotChunk),
95}
96
97impl Request {
98    /// Get the method kind for this request.
99    pub fn kind(&self) -> MethodKind {
100        use Request::*;
101        match self {
102            Flush => MethodKind::Flush,
103            InitChain(_) => MethodKind::Consensus,
104            BeginBlock(_) => MethodKind::Consensus,
105            DeliverTx(_) => MethodKind::Consensus,
106            EndBlock(_) => MethodKind::Consensus,
107            Commit => MethodKind::Consensus,
108            CheckTx(_) => MethodKind::Mempool,
109            ListSnapshots => MethodKind::Snapshot,
110            OfferSnapshot(_) => MethodKind::Snapshot,
111            LoadSnapshotChunk(_) => MethodKind::Snapshot,
112            ApplySnapshotChunk(_) => MethodKind::Snapshot,
113            Info(_) => MethodKind::Info,
114            Query(_) => MethodKind::Info,
115            Echo(_) => MethodKind::Info,
116            SetOption(_) => MethodKind::Info,
117        }
118    }
119}
120
121impl From<ConsensusRequest> for Request {
122    fn from(req: ConsensusRequest) -> Self {
123        match req {
124            ConsensusRequest::InitChain(x) => Self::InitChain(x),
125            ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x),
126            ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x),
127            ConsensusRequest::EndBlock(x) => Self::EndBlock(x),
128            ConsensusRequest::Commit => Self::Commit,
129        }
130    }
131}
132
133impl TryFrom<Request> for ConsensusRequest {
134    type Error = Error;
135    fn try_from(req: Request) -> Result<Self, Self::Error> {
136        match req {
137            Request::InitChain(x) => Ok(Self::InitChain(x)),
138            Request::BeginBlock(x) => Ok(Self::BeginBlock(x)),
139            Request::DeliverTx(x) => Ok(Self::DeliverTx(x)),
140            Request::EndBlock(x) => Ok(Self::EndBlock(x)),
141            Request::Commit => Ok(Self::Commit),
142            _ => Err(Error::invalid_abci_request_type()),
143        }
144    }
145}
146
147impl From<MempoolRequest> for Request {
148    fn from(req: MempoolRequest) -> Self {
149        match req {
150            MempoolRequest::CheckTx(x) => Self::CheckTx(x),
151        }
152    }
153}
154
155impl TryFrom<Request> for MempoolRequest {
156    type Error = Error;
157    fn try_from(req: Request) -> Result<Self, Self::Error> {
158        match req {
159            Request::CheckTx(x) => Ok(Self::CheckTx(x)),
160            _ => Err(Error::invalid_abci_request_type()),
161        }
162    }
163}
164
165impl From<InfoRequest> for Request {
166    fn from(req: InfoRequest) -> Self {
167        match req {
168            InfoRequest::Info(x) => Self::Info(x),
169            InfoRequest::Query(x) => Self::Query(x),
170            InfoRequest::Echo(x) => Self::Echo(x),
171            InfoRequest::SetOption(x) => Self::SetOption(x),
172        }
173    }
174}
175
176impl TryFrom<Request> for InfoRequest {
177    type Error = Error;
178    fn try_from(req: Request) -> Result<Self, Self::Error> {
179        match req {
180            Request::Info(x) => Ok(Self::Info(x)),
181            Request::Query(x) => Ok(Self::Query(x)),
182            Request::Echo(x) => Ok(Self::Echo(x)),
183            Request::SetOption(x) => Ok(Self::SetOption(x)),
184            _ => Err(Error::invalid_abci_request_type()),
185        }
186    }
187}
188
189impl From<SnapshotRequest> for Request {
190    fn from(req: SnapshotRequest) -> Self {
191        match req {
192            SnapshotRequest::ListSnapshots => Self::ListSnapshots,
193            SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x),
194            SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x),
195            SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x),
196        }
197    }
198}
199
200impl TryFrom<Request> for SnapshotRequest {
201    type Error = Error;
202    fn try_from(req: Request) -> Result<Self, Self::Error> {
203        match req {
204            Request::ListSnapshots => Ok(Self::ListSnapshots),
205            Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)),
206            Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)),
207            Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)),
208            _ => Err(Error::invalid_abci_request_type()),
209        }
210    }
211}
212
213// =============================================================================
214// Protobuf conversions
215// =============================================================================
216
217impl From<Request> for pb::Request {
218    fn from(request: Request) -> pb::Request {
219        use pb::request::Value;
220        let value = match request {
221            Request::Echo(x) => Some(Value::Echo(x.into())),
222            Request::Flush => Some(Value::Flush(Default::default())),
223            Request::Info(x) => Some(Value::Info(x.into())),
224            Request::SetOption(x) => Some(Value::SetOption(x.into())),
225            Request::InitChain(x) => Some(Value::InitChain(x.into())),
226            Request::Query(x) => Some(Value::Query(x.into())),
227            Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())),
228            Request::CheckTx(x) => Some(Value::CheckTx(x.into())),
229            Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())),
230            Request::EndBlock(x) => Some(Value::EndBlock(x.into())),
231            Request::Commit => Some(Value::Commit(Default::default())),
232            Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())),
233            Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())),
234            Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())),
235            Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())),
236        };
237        pb::Request { value }
238    }
239}
240
241impl TryFrom<pb::Request> for Request {
242    type Error = Error;
243
244    fn try_from(request: pb::Request) -> Result<Self, Self::Error> {
245        use pb::request::Value;
246        match request.value {
247            Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)),
248            Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush),
249            Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)),
250            Some(Value::SetOption(x)) => Ok(Request::SetOption(x.try_into()?)),
251            Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)),
252            Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)),
253            Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)),
254            Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)),
255            Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)),
256            Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)),
257            Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit),
258            Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => Ok(Request::ListSnapshots),
259            Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)),
260            Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)),
261            Some(Value::ApplySnapshotChunk(x)) => Ok(Request::ApplySnapshotChunk(x.try_into()?)),
262            None => Err(Error::missing_data()),
263        }
264    }
265}
266
267impl Protobuf<pb::Request> for Request {}