From 950205c02cfb3f47277481e565bb19c44c0f0c2e Mon Sep 17 00:00:00 2001 From: Mikhail Zabaluev Date: Mon, 21 Nov 2022 15:45:27 +0200 Subject: [PATCH] WIP: tendermint: version-specific Request types --- tendermint/src/abci.rs | 7 +- tendermint/src/abci/request.rs | 330 ++------------------------- tendermint/src/abci/v0_34.rs | 1 + tendermint/src/abci/v0_34/request.rs | 229 +++++++++++++++++++ tendermint/src/abci/v0_37.rs | 1 + tendermint/src/abci/v0_37/request.rs | 233 +++++++++++++++++++ 6 files changed, 484 insertions(+), 317 deletions(-) create mode 100644 tendermint/src/abci/v0_34.rs create mode 100644 tendermint/src/abci/v0_34/request.rs create mode 100644 tendermint/src/abci/v0_37.rs create mode 100644 tendermint/src/abci/v0_37/request.rs diff --git a/tendermint/src/abci.rs b/tendermint/src/abci.rs index 2ea41b9d9..cdc138324 100644 --- a/tendermint/src/abci.rs +++ b/tendermint/src/abci.rs @@ -44,12 +44,17 @@ pub mod request; pub mod response; pub mod types; +pub mod v0_34; +pub mod v0_37; + +pub use v0_37::request::Request; + pub use event::{Event, EventAttribute, EventAttributeIndexExt}; #[doc(inline)] pub use self::{ code::Code, kind::MethodKind, - request::{ConsensusRequest, InfoRequest, MempoolRequest, Request, SnapshotRequest}, + request::{ConsensusRequest, InfoRequest, MempoolRequest, SnapshotRequest}, response::{ConsensusResponse, InfoResponse, MempoolResponse, Response, SnapshotResponse}, }; diff --git a/tendermint/src/abci/request.rs b/tendermint/src/abci/request.rs index f37388995..7e3e8c307 100644 --- a/tendermint/src/abci/request.rs +++ b/tendermint/src/abci/request.rs @@ -18,28 +18,25 @@ // This is also why certain submodules have #[allow(unused)] imports to bring // items into scope for doc links, rather than changing the doc links -- it // allows the doc comments to be copied without editing. -use core::convert::TryFrom; // bring into scope for doc links #[allow(unused)] use super::types::Snapshot; -use super::MethodKind; -use crate::{prelude::*, Error}; -mod apply_snapshot_chunk; -mod begin_block; -mod check_tx; -mod deliver_tx; -mod echo; -mod end_block; -mod info; -mod init_chain; -mod load_snapshot_chunk; -mod offer_snapshot; -mod prepare_proposal; -mod process_proposal; -mod query; -mod set_option; +pub(super) mod apply_snapshot_chunk; +pub(super) mod begin_block; +pub(super) mod check_tx; +pub(super) mod deliver_tx; +pub(super) mod echo; +pub(super) mod end_block; +pub(super) mod info; +pub(super) mod init_chain; +pub(super) mod load_snapshot_chunk; +pub(super) mod offer_snapshot; +pub(super) mod prepare_proposal; +pub(super) mod process_proposal; +pub(super) mod query; +pub(super) mod set_option; pub use apply_snapshot_chunk::ApplySnapshotChunk; pub use begin_block::BeginBlock; @@ -56,72 +53,6 @@ pub use process_proposal::ProcessProposal; pub use query::Query; pub use set_option::SetOption; -/// All possible ABCI requests. -#[allow(clippy::large_enum_variant)] -#[derive(Clone, PartialEq, Eq, Debug)] -pub enum Request { - #[doc = include_str!("doc/request-echo.md")] - Echo(Echo), - #[doc = include_str!("doc/request-flush.md")] - Flush, - #[doc = include_str!("doc/request-info.md")] - Info(Info), - #[doc = include_str!("doc/request-setoption.md")] - SetOption(SetOption), - #[doc = include_str!("doc/request-initchain.md")] - InitChain(InitChain), - #[doc = include_str!("doc/request-query.md")] - Query(Query), - #[doc = include_str!("doc/request-beginblock.md")] - BeginBlock(BeginBlock), - #[doc = include_str!("doc/request-checktx.md")] - CheckTx(CheckTx), - #[doc = include_str!("doc/request-delivertx.md")] - DeliverTx(DeliverTx), - #[doc = include_str!("doc/request-endblock.md")] - EndBlock(EndBlock), - #[doc = include_str!("doc/request-commit.md")] - Commit, - #[doc = include_str!("doc/request-listsnapshots.md")] - ListSnapshots, - #[doc = include_str!("doc/request-offersnapshot.md")] - OfferSnapshot(OfferSnapshot), - #[doc = include_str!("doc/request-loadsnapshotchunk.md")] - LoadSnapshotChunk(LoadSnapshotChunk), - #[doc = include_str!("doc/request-applysnapshotchunk.md")] - ApplySnapshotChunk(ApplySnapshotChunk), - #[doc = include_str!("doc/request-prepareproposal.md")] - PrepareProposal(PrepareProposal), - #[doc = include_str!("doc/request-processproposal.md")] - ProcessProposal(ProcessProposal), -} - -impl Request { - /// Get the method kind for this request. - pub fn kind(&self) -> MethodKind { - use Request::*; - match self { - Flush => MethodKind::Flush, - InitChain(_) => MethodKind::Consensus, - BeginBlock(_) => MethodKind::Consensus, - DeliverTx(_) => MethodKind::Consensus, - EndBlock(_) => MethodKind::Consensus, - Commit => MethodKind::Consensus, - PrepareProposal(_) => MethodKind::Consensus, - ProcessProposal(_) => MethodKind::Consensus, - CheckTx(_) => MethodKind::Mempool, - ListSnapshots => MethodKind::Snapshot, - OfferSnapshot(_) => MethodKind::Snapshot, - LoadSnapshotChunk(_) => MethodKind::Snapshot, - ApplySnapshotChunk(_) => MethodKind::Snapshot, - Info(_) => MethodKind::Info, - Query(_) => MethodKind::Info, - Echo(_) => MethodKind::Info, - SetOption(_) => MethodKind::Info, - } - } -} - /// The consensus category of ABCI requests. #[allow(clippy::large_enum_variant)] #[derive(Clone, PartialEq, Eq, Debug)] @@ -138,32 +69,6 @@ pub enum ConsensusRequest { Commit, } -impl From for Request { - fn from(req: ConsensusRequest) -> Self { - match req { - ConsensusRequest::InitChain(x) => Self::InitChain(x), - ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x), - ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x), - ConsensusRequest::EndBlock(x) => Self::EndBlock(x), - ConsensusRequest::Commit => Self::Commit, - } - } -} - -impl TryFrom for ConsensusRequest { - type Error = Error; - fn try_from(req: Request) -> Result { - match req { - Request::InitChain(x) => Ok(Self::InitChain(x)), - Request::BeginBlock(x) => Ok(Self::BeginBlock(x)), - Request::DeliverTx(x) => Ok(Self::DeliverTx(x)), - Request::EndBlock(x) => Ok(Self::EndBlock(x)), - Request::Commit => Ok(Self::Commit), - _ => Err(Error::invalid_abci_request_type()), - } - } -} - /// The mempool category of ABCI requests. #[derive(Clone, PartialEq, Eq, Debug)] pub enum MempoolRequest { @@ -171,24 +76,6 @@ pub enum MempoolRequest { CheckTx(CheckTx), } -impl From for Request { - fn from(req: MempoolRequest) -> Self { - match req { - MempoolRequest::CheckTx(x) => Self::CheckTx(x), - } - } -} - -impl TryFrom for MempoolRequest { - type Error = Error; - fn try_from(req: Request) -> Result { - match req { - Request::CheckTx(x) => Ok(Self::CheckTx(x)), - _ => Err(Error::invalid_abci_request_type()), - } - } -} - /// The info category of ABCI requests. #[derive(Clone, PartialEq, Eq, Debug)] pub enum InfoRequest { @@ -202,30 +89,6 @@ pub enum InfoRequest { SetOption(SetOption), } -impl From for Request { - fn from(req: InfoRequest) -> Self { - match req { - InfoRequest::Info(x) => Self::Info(x), - InfoRequest::Query(x) => Self::Query(x), - InfoRequest::Echo(x) => Self::Echo(x), - InfoRequest::SetOption(x) => Self::SetOption(x), - } - } -} - -impl TryFrom for InfoRequest { - type Error = Error; - fn try_from(req: Request) -> Result { - match req { - Request::Info(x) => Ok(Self::Info(x)), - Request::Query(x) => Ok(Self::Query(x)), - Request::Echo(x) => Ok(Self::Echo(x)), - Request::SetOption(x) => Ok(Self::SetOption(x)), - _ => Err(Error::invalid_abci_request_type()), - } - } -} - /// The snapshot category of ABCI requests. #[derive(Clone, PartialEq, Eq, Debug)] pub enum SnapshotRequest { @@ -238,168 +101,3 @@ pub enum SnapshotRequest { #[doc = include_str!("doc/request-applysnapshotchunk.md")] ApplySnapshotChunk(ApplySnapshotChunk), } - -impl From for Request { - fn from(req: SnapshotRequest) -> Self { - match req { - SnapshotRequest::ListSnapshots => Self::ListSnapshots, - SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x), - SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x), - SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x), - } - } -} - -impl TryFrom for SnapshotRequest { - type Error = Error; - fn try_from(req: Request) -> Result { - match req { - Request::ListSnapshots => Ok(Self::ListSnapshots), - Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)), - Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)), - Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)), - _ => Err(Error::invalid_abci_request_type()), - } - } -} - -// ============================================================================= -// Protobuf conversions -// ============================================================================= - -mod v0_34 { - use super::Request; - use crate::{prelude::*, Error}; - use tendermint_proto::v0_34::abci as pb; - use tendermint_proto::Protobuf; - - impl From for pb::Request { - fn from(request: Request) -> pb::Request { - use pb::request::Value; - let value = match request { - Request::Echo(x) => Some(Value::Echo(x.into())), - Request::Flush => Some(Value::Flush(Default::default())), - Request::Info(x) => Some(Value::Info(x.into())), - Request::SetOption(x) => Some(Value::SetOption(x.into())), - Request::InitChain(x) => Some(Value::InitChain(x.into())), - Request::Query(x) => Some(Value::Query(x.into())), - Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())), - Request::CheckTx(x) => Some(Value::CheckTx(x.into())), - Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())), - Request::EndBlock(x) => Some(Value::EndBlock(x.into())), - Request::Commit => Some(Value::Commit(Default::default())), - Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())), - Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), - Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), - Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), - Request::PrepareProposal(_) => { - panic!("PrepareProposal should not be used with Tendermint 0.34") - }, - Request::ProcessProposal(_) => { - panic!("ProcessProposal should not be used with Tendermint 0.34") - }, - }; - pb::Request { value } - } - } - - impl TryFrom for Request { - type Error = Error; - - fn try_from(request: pb::Request) -> Result { - use pb::request::Value; - match request.value { - Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)), - Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush), - Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)), - Some(Value::SetOption(x)) => Ok(Request::SetOption(x.try_into()?)), - Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)), - Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)), - Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)), - Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)), - Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)), - Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)), - Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit), - Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => { - Ok(Request::ListSnapshots) - }, - Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)), - Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)), - Some(Value::ApplySnapshotChunk(x)) => { - Ok(Request::ApplySnapshotChunk(x.try_into()?)) - }, - None => Err(crate::Error::missing_data()), - } - } - } - - impl Protobuf for Request {} -} - -mod v0_37 { - use super::Request; - use crate::{prelude::*, Error}; - use tendermint_proto::v0_37::abci as pb; - use tendermint_proto::Protobuf; - - impl From for pb::Request { - fn from(request: Request) -> pb::Request { - use pb::request::Value; - let value = match request { - Request::Echo(x) => Some(Value::Echo(x.into())), - Request::Flush => Some(Value::Flush(Default::default())), - Request::Info(x) => Some(Value::Info(x.into())), - Request::InitChain(x) => Some(Value::InitChain(x.into())), - Request::Query(x) => Some(Value::Query(x.into())), - Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())), - Request::CheckTx(x) => Some(Value::CheckTx(x.into())), - Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())), - Request::EndBlock(x) => Some(Value::EndBlock(x.into())), - Request::Commit => Some(Value::Commit(Default::default())), - Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())), - Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), - Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), - Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), - Request::PrepareProposal(x) => Some(Value::PrepareProposal(x.into())), - Request::ProcessProposal(x) => Some(Value::ProcessProposal(x.into())), - Request::SetOption(_) => { - panic!("SetOption should not be used with Tendermint 0.37") - }, - }; - pb::Request { value } - } - } - - impl TryFrom for Request { - type Error = Error; - - fn try_from(request: pb::Request) -> Result { - use pb::request::Value; - match request.value { - Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)), - Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush), - Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)), - Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)), - Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)), - Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)), - Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)), - Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)), - Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)), - Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit), - Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => { - Ok(Request::ListSnapshots) - }, - Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)), - Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)), - Some(Value::ApplySnapshotChunk(x)) => { - Ok(Request::ApplySnapshotChunk(x.try_into()?)) - }, - Some(Value::PrepareProposal(x)) => Ok(Request::PrepareProposal(x.try_into()?)), - Some(Value::ProcessProposal(x)) => Ok(Request::ProcessProposal(x.try_into()?)), - None => Err(crate::Error::missing_data()), - } - } - } - - impl Protobuf for Request {} -} diff --git a/tendermint/src/abci/v0_34.rs b/tendermint/src/abci/v0_34.rs new file mode 100644 index 000000000..be9378d97 --- /dev/null +++ b/tendermint/src/abci/v0_34.rs @@ -0,0 +1 @@ +pub mod request; diff --git a/tendermint/src/abci/v0_34/request.rs b/tendermint/src/abci/v0_34/request.rs new file mode 100644 index 000000000..c6a9517a5 --- /dev/null +++ b/tendermint/src/abci/v0_34/request.rs @@ -0,0 +1,229 @@ +use tendermint_proto::v0_34::abci as pb; +use tendermint_proto::Protobuf; + +use crate::abci::request::{ConsensusRequest, InfoRequest, MempoolRequest, SnapshotRequest}; +use crate::abci::MethodKind; +use crate::Error; + +pub use crate::abci::request::apply_snapshot_chunk::ApplySnapshotChunk; +pub use crate::abci::request::begin_block::BeginBlock; +pub use crate::abci::request::check_tx::{CheckTx, CheckTxKind}; +pub use crate::abci::request::deliver_tx::DeliverTx; +pub use crate::abci::request::echo::Echo; +pub use crate::abci::request::end_block::EndBlock; +pub use crate::abci::request::info::Info; +pub use crate::abci::request::init_chain::InitChain; +pub use crate::abci::request::load_snapshot_chunk::LoadSnapshotChunk; +pub use crate::abci::request::offer_snapshot::OfferSnapshot; +pub use crate::abci::request::prepare_proposal::PrepareProposal; +pub use crate::abci::request::process_proposal::ProcessProposal; +pub use crate::abci::request::query::Query; +pub use crate::abci::request::set_option::SetOption; + +/// All possible ABCI requests. +#[allow(clippy::large_enum_variant)] +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Request { + #[doc = include_str!("../doc/request-echo.md")] + Echo(Echo), + #[doc = include_str!("../doc/request-flush.md")] + Flush, + #[doc = include_str!("../doc/request-info.md")] + Info(Info), + #[doc = include_str!("../doc/request-setoption.md")] + SetOption(SetOption), + #[doc = include_str!("../doc/request-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("../doc/request-query.md")] + Query(Query), + #[doc = include_str!("../doc/request-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("../doc/request-checktx.md")] + CheckTx(CheckTx), + #[doc = include_str!("../doc/request-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("../doc/request-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("../doc/request-commit.md")] + Commit, + #[doc = include_str!("../doc/request-listsnapshots.md")] + ListSnapshots, + #[doc = include_str!("../doc/request-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("../doc/request-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("../doc/request-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), +} + +impl Request { + /// Get the method kind for this request. + pub fn kind(&self) -> MethodKind { + use Request::*; + match self { + Flush => MethodKind::Flush, + InitChain(_) => MethodKind::Consensus, + BeginBlock(_) => MethodKind::Consensus, + DeliverTx(_) => MethodKind::Consensus, + EndBlock(_) => MethodKind::Consensus, + Commit => MethodKind::Consensus, + CheckTx(_) => MethodKind::Mempool, + ListSnapshots => MethodKind::Snapshot, + OfferSnapshot(_) => MethodKind::Snapshot, + LoadSnapshotChunk(_) => MethodKind::Snapshot, + ApplySnapshotChunk(_) => MethodKind::Snapshot, + Info(_) => MethodKind::Info, + Query(_) => MethodKind::Info, + Echo(_) => MethodKind::Info, + SetOption(_) => MethodKind::Info, + } + } +} + +impl From for Request { + fn from(req: ConsensusRequest) -> Self { + match req { + ConsensusRequest::InitChain(x) => Self::InitChain(x), + ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x), + ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x), + ConsensusRequest::EndBlock(x) => Self::EndBlock(x), + ConsensusRequest::Commit => Self::Commit, + } + } +} + +impl TryFrom for ConsensusRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::InitChain(x) => Ok(Self::InitChain(x)), + Request::BeginBlock(x) => Ok(Self::BeginBlock(x)), + Request::DeliverTx(x) => Ok(Self::DeliverTx(x)), + Request::EndBlock(x) => Ok(Self::EndBlock(x)), + Request::Commit => Ok(Self::Commit), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: MempoolRequest) -> Self { + match req { + MempoolRequest::CheckTx(x) => Self::CheckTx(x), + } + } +} + +impl TryFrom for MempoolRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::CheckTx(x) => Ok(Self::CheckTx(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: InfoRequest) -> Self { + match req { + InfoRequest::Info(x) => Self::Info(x), + InfoRequest::Query(x) => Self::Query(x), + InfoRequest::Echo(x) => Self::Echo(x), + InfoRequest::SetOption(x) => Self::SetOption(x), + } + } +} + +impl TryFrom for InfoRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::Info(x) => Ok(Self::Info(x)), + Request::Query(x) => Ok(Self::Query(x)), + Request::Echo(x) => Ok(Self::Echo(x)), + Request::SetOption(x) => Ok(Self::SetOption(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: SnapshotRequest) -> Self { + match req { + SnapshotRequest::ListSnapshots => Self::ListSnapshots, + SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x), + SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x), + SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x), + } + } +} + +impl TryFrom for SnapshotRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::ListSnapshots => Ok(Self::ListSnapshots), + Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)), + Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)), + Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +impl From for pb::Request { + fn from(request: Request) -> pb::Request { + use pb::request::Value; + let value = match request { + Request::Echo(x) => Some(Value::Echo(x.into())), + Request::Flush => Some(Value::Flush(Default::default())), + Request::Info(x) => Some(Value::Info(x.into())), + Request::SetOption(x) => Some(Value::SetOption(x.into())), + Request::InitChain(x) => Some(Value::InitChain(x.into())), + Request::Query(x) => Some(Value::Query(x.into())), + Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())), + Request::CheckTx(x) => Some(Value::CheckTx(x.into())), + Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())), + Request::EndBlock(x) => Some(Value::EndBlock(x.into())), + Request::Commit => Some(Value::Commit(Default::default())), + Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())), + Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), + Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), + Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), + }; + pb::Request { value } + } +} + +impl TryFrom for Request { + type Error = Error; + + fn try_from(request: pb::Request) -> Result { + use pb::request::Value; + match request.value { + Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)), + Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush), + Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)), + Some(Value::SetOption(x)) => Ok(Request::SetOption(x.try_into()?)), + Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)), + Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)), + Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)), + Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)), + Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)), + Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)), + Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit), + Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => Ok(Request::ListSnapshots), + Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)), + Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)), + Some(Value::ApplySnapshotChunk(x)) => Ok(Request::ApplySnapshotChunk(x.try_into()?)), + None => Err(crate::Error::missing_data()), + } + } +} + +impl Protobuf for Request {} diff --git a/tendermint/src/abci/v0_37.rs b/tendermint/src/abci/v0_37.rs new file mode 100644 index 000000000..be9378d97 --- /dev/null +++ b/tendermint/src/abci/v0_37.rs @@ -0,0 +1 @@ +pub mod request; diff --git a/tendermint/src/abci/v0_37/request.rs b/tendermint/src/abci/v0_37/request.rs new file mode 100644 index 000000000..25e1a4769 --- /dev/null +++ b/tendermint/src/abci/v0_37/request.rs @@ -0,0 +1,233 @@ +use tendermint_proto::v0_37::abci as pb; +use tendermint_proto::Protobuf; + +use crate::abci::request::{ConsensusRequest, InfoRequest, MempoolRequest, SnapshotRequest}; +use crate::abci::MethodKind; +use crate::Error; + +pub use crate::abci::request::apply_snapshot_chunk::ApplySnapshotChunk; +pub use crate::abci::request::begin_block::BeginBlock; +pub use crate::abci::request::check_tx::{CheckTx, CheckTxKind}; +pub use crate::abci::request::deliver_tx::DeliverTx; +pub use crate::abci::request::echo::Echo; +pub use crate::abci::request::end_block::EndBlock; +pub use crate::abci::request::info::Info; +pub use crate::abci::request::init_chain::InitChain; +pub use crate::abci::request::load_snapshot_chunk::LoadSnapshotChunk; +pub use crate::abci::request::offer_snapshot::OfferSnapshot; +pub use crate::abci::request::prepare_proposal::PrepareProposal; +pub use crate::abci::request::process_proposal::ProcessProposal; +pub use crate::abci::request::query::Query; +pub use crate::abci::request::set_option::SetOption; + +/// All possible ABCI requests. +#[allow(clippy::large_enum_variant)] +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum Request { + #[doc = include_str!("../doc/request-echo.md")] + Echo(Echo), + #[doc = include_str!("../doc/request-flush.md")] + Flush, + #[doc = include_str!("../doc/request-info.md")] + Info(Info), + #[doc = include_str!("../doc/request-initchain.md")] + InitChain(InitChain), + #[doc = include_str!("../doc/request-query.md")] + Query(Query), + #[doc = include_str!("../doc/request-beginblock.md")] + BeginBlock(BeginBlock), + #[doc = include_str!("../doc/request-checktx.md")] + CheckTx(CheckTx), + #[doc = include_str!("../doc/request-delivertx.md")] + DeliverTx(DeliverTx), + #[doc = include_str!("../doc/request-endblock.md")] + EndBlock(EndBlock), + #[doc = include_str!("../doc/request-commit.md")] + Commit, + #[doc = include_str!("../doc/request-listsnapshots.md")] + ListSnapshots, + #[doc = include_str!("../doc/request-offersnapshot.md")] + OfferSnapshot(OfferSnapshot), + #[doc = include_str!("../doc/request-loadsnapshotchunk.md")] + LoadSnapshotChunk(LoadSnapshotChunk), + #[doc = include_str!("../doc/request-applysnapshotchunk.md")] + ApplySnapshotChunk(ApplySnapshotChunk), + #[doc = include_str!("../doc/request-prepareproposal.md")] + PrepareProposal(PrepareProposal), + #[doc = include_str!("../doc/request-processproposal.md")] + ProcessProposal(ProcessProposal), +} + +impl Request { + /// Get the method kind for this request. + pub fn kind(&self) -> MethodKind { + use Request::*; + match self { + Flush => MethodKind::Flush, + InitChain(_) => MethodKind::Consensus, + BeginBlock(_) => MethodKind::Consensus, + DeliverTx(_) => MethodKind::Consensus, + EndBlock(_) => MethodKind::Consensus, + Commit => MethodKind::Consensus, + PrepareProposal(_) => MethodKind::Consensus, + ProcessProposal(_) => MethodKind::Consensus, + CheckTx(_) => MethodKind::Mempool, + ListSnapshots => MethodKind::Snapshot, + OfferSnapshot(_) => MethodKind::Snapshot, + LoadSnapshotChunk(_) => MethodKind::Snapshot, + ApplySnapshotChunk(_) => MethodKind::Snapshot, + Info(_) => MethodKind::Info, + Query(_) => MethodKind::Info, + Echo(_) => MethodKind::Info, + } + } +} + +impl From for Request { + fn from(req: ConsensusRequest) -> Self { + match req { + ConsensusRequest::InitChain(x) => Self::InitChain(x), + ConsensusRequest::BeginBlock(x) => Self::BeginBlock(x), + ConsensusRequest::DeliverTx(x) => Self::DeliverTx(x), + ConsensusRequest::EndBlock(x) => Self::EndBlock(x), + ConsensusRequest::Commit => Self::Commit, + } + } +} + +impl TryFrom for ConsensusRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::InitChain(x) => Ok(Self::InitChain(x)), + Request::BeginBlock(x) => Ok(Self::BeginBlock(x)), + Request::DeliverTx(x) => Ok(Self::DeliverTx(x)), + Request::EndBlock(x) => Ok(Self::EndBlock(x)), + Request::Commit => Ok(Self::Commit), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: MempoolRequest) -> Self { + match req { + MempoolRequest::CheckTx(x) => Self::CheckTx(x), + } + } +} + +impl TryFrom for MempoolRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::CheckTx(x) => Ok(Self::CheckTx(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: InfoRequest) -> Self { + match req { + InfoRequest::Info(x) => Self::Info(x), + InfoRequest::Query(x) => Self::Query(x), + InfoRequest::Echo(x) => Self::Echo(x), + InfoRequest::SetOption(_) => panic!("cannot be used with v0.37"), + } + } +} + +impl TryFrom for InfoRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::Info(x) => Ok(Self::Info(x)), + Request::Query(x) => Ok(Self::Query(x)), + Request::Echo(x) => Ok(Self::Echo(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +impl From for Request { + fn from(req: SnapshotRequest) -> Self { + match req { + SnapshotRequest::ListSnapshots => Self::ListSnapshots, + SnapshotRequest::OfferSnapshot(x) => Self::OfferSnapshot(x), + SnapshotRequest::LoadSnapshotChunk(x) => Self::LoadSnapshotChunk(x), + SnapshotRequest::ApplySnapshotChunk(x) => Self::ApplySnapshotChunk(x), + } + } +} + +impl TryFrom for SnapshotRequest { + type Error = Error; + fn try_from(req: Request) -> Result { + match req { + Request::ListSnapshots => Ok(Self::ListSnapshots), + Request::OfferSnapshot(x) => Ok(Self::OfferSnapshot(x)), + Request::LoadSnapshotChunk(x) => Ok(Self::LoadSnapshotChunk(x)), + Request::ApplySnapshotChunk(x) => Ok(Self::ApplySnapshotChunk(x)), + _ => Err(Error::invalid_abci_request_type()), + } + } +} + +// ============================================================================= +// Protobuf conversions +// ============================================================================= + +impl From for pb::Request { + fn from(request: Request) -> pb::Request { + use pb::request::Value; + let value = match request { + Request::Echo(x) => Some(Value::Echo(x.into())), + Request::Flush => Some(Value::Flush(Default::default())), + Request::Info(x) => Some(Value::Info(x.into())), + Request::InitChain(x) => Some(Value::InitChain(x.into())), + Request::Query(x) => Some(Value::Query(x.into())), + Request::BeginBlock(x) => Some(Value::BeginBlock(x.into())), + Request::CheckTx(x) => Some(Value::CheckTx(x.into())), + Request::DeliverTx(x) => Some(Value::DeliverTx(x.into())), + Request::EndBlock(x) => Some(Value::EndBlock(x.into())), + Request::Commit => Some(Value::Commit(Default::default())), + Request::ListSnapshots => Some(Value::ListSnapshots(Default::default())), + Request::OfferSnapshot(x) => Some(Value::OfferSnapshot(x.into())), + Request::LoadSnapshotChunk(x) => Some(Value::LoadSnapshotChunk(x.into())), + Request::ApplySnapshotChunk(x) => Some(Value::ApplySnapshotChunk(x.into())), + Request::PrepareProposal(x) => Some(Value::PrepareProposal(x.into())), + Request::ProcessProposal(x) => Some(Value::ProcessProposal(x.into())), + }; + pb::Request { value } + } +} + +impl TryFrom for Request { + type Error = Error; + + fn try_from(request: pb::Request) -> Result { + use pb::request::Value; + match request.value { + Some(Value::Echo(x)) => Ok(Request::Echo(x.try_into()?)), + Some(Value::Flush(pb::RequestFlush {})) => Ok(Request::Flush), + Some(Value::Info(x)) => Ok(Request::Info(x.try_into()?)), + Some(Value::InitChain(x)) => Ok(Request::InitChain(x.try_into()?)), + Some(Value::Query(x)) => Ok(Request::Query(x.try_into()?)), + Some(Value::BeginBlock(x)) => Ok(Request::BeginBlock(x.try_into()?)), + Some(Value::CheckTx(x)) => Ok(Request::CheckTx(x.try_into()?)), + Some(Value::DeliverTx(x)) => Ok(Request::DeliverTx(x.try_into()?)), + Some(Value::EndBlock(x)) => Ok(Request::EndBlock(x.try_into()?)), + Some(Value::Commit(pb::RequestCommit {})) => Ok(Request::Commit), + Some(Value::ListSnapshots(pb::RequestListSnapshots {})) => Ok(Request::ListSnapshots), + Some(Value::OfferSnapshot(x)) => Ok(Request::OfferSnapshot(x.try_into()?)), + Some(Value::LoadSnapshotChunk(x)) => Ok(Request::LoadSnapshotChunk(x.try_into()?)), + Some(Value::ApplySnapshotChunk(x)) => Ok(Request::ApplySnapshotChunk(x.try_into()?)), + Some(Value::PrepareProposal(x)) => Ok(Request::PrepareProposal(x.try_into()?)), + Some(Value::ProcessProposal(x)) => Ok(Request::ProcessProposal(x.try_into()?)), + None => Err(crate::Error::missing_data()), + } + } +} + +impl Protobuf for Request {}