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