Skip to content

Commit

Permalink
WIP: multi-version protobuf conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
mzabaluev committed Oct 19, 2022
1 parent 0ced5c2 commit 6845f84
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 106 deletions.
95 changes: 50 additions & 45 deletions tendermint/src/abci/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,59 +139,64 @@ impl<K: Into<String>, V: Into<String>> From<(K, V)> for EventAttribute {
// Protobuf conversions
// =============================================================================

use core::convert::{TryFrom, TryInto};

use tendermint_proto::{abci as pb, Protobuf};

impl From<EventAttribute> for pb::EventAttribute {
fn from(event: EventAttribute) -> Self {
Self {
key: event.key.into(),
value: event.value.into(),
index: event.index,
mod v0_34 {
use super::{Event, EventAttribute};
use crate::prelude::*;
use core::convert::{TryFrom, TryInto};

use tendermint_proto::v0_34::abci as pb;
use tendermint_proto::Protobuf;

impl From<EventAttribute> for pb::EventAttribute {
fn from(event: EventAttribute) -> Self {
Self {
key: event.key.into(),
value: event.value.into(),
index: event.index,
}
}
}
}

impl TryFrom<pb::EventAttribute> for EventAttribute {
type Error = crate::Error;

fn try_from(event: pb::EventAttribute) -> Result<Self, Self::Error> {
// We insist that keys and values are strings, like tm 0.35 did.
Ok(Self {
key: String::from_utf8(event.key.to_vec())
.map_err(|e| crate::Error::parse(e.to_string()))?,
value: String::from_utf8(event.value.to_vec())
.map_err(|e| crate::Error::parse(e.to_string()))?,
index: event.index,
})
impl TryFrom<pb::EventAttribute> for EventAttribute {
type Error = crate::Error;

fn try_from(event: pb::EventAttribute) -> Result<Self, Self::Error> {
// We insist that keys and values are strings, like tm 0.35 did.
Ok(Self {
key: String::from_utf8(event.key.to_vec())
.map_err(|e| crate::Error::parse(e.to_string()))?,
value: String::from_utf8(event.value.to_vec())
.map_err(|e| crate::Error::parse(e.to_string()))?,
index: event.index,
})
}
}
}

impl Protobuf<pb::EventAttribute> for EventAttribute {}
impl Protobuf<pb::EventAttribute> for EventAttribute {}

impl From<Event> for pb::Event {
fn from(event: Event) -> Self {
Self {
r#type: event.kind,
attributes: event.attributes.into_iter().map(Into::into).collect(),
impl From<Event> for pb::Event {
fn from(event: Event) -> Self {
Self {
r#type: event.kind,
attributes: event.attributes.into_iter().map(Into::into).collect(),
}
}
}
}

impl TryFrom<pb::Event> for Event {
type Error = crate::Error;

fn try_from(event: pb::Event) -> Result<Self, Self::Error> {
Ok(Self {
kind: event.r#type,
attributes: event
.attributes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
impl TryFrom<pb::Event> for Event {
type Error = crate::Error;

fn try_from(event: pb::Event) -> Result<Self, Self::Error> {
Ok(Self {
kind: event.r#type,
attributes: event
.attributes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
}

impl Protobuf<pb::Event> for Event {}
impl Protobuf<pb::Event> for Event {}
}
103 changes: 56 additions & 47 deletions tendermint/src/abci/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// 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, TryInto};
use core::convert::TryFrom;

// bring into scope for doc links
#[allow(unused)]
Expand Down Expand Up @@ -257,56 +257,65 @@ impl TryFrom<Request> for SnapshotRequest {
// Protobuf conversions
// =============================================================================

use tendermint_proto::{abci as pb, Protobuf};
mod v0_34 {
use super::Request;
use crate::{prelude::*, Error};
use tendermint_proto::v0_34::abci as pb;
use tendermint_proto::Protobuf;

impl From<Request> 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 From<Request> 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<pb::Request> for Request {
type Error = Error;
impl TryFrom<pb::Request> for Request {
type Error = Error;

fn try_from(request: pb::Request) -> Result<Self, Self::Error> {
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()),
fn try_from(request: pb::Request) -> Result<Self, Self::Error> {
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<pb::Request> for Request {}
impl Protobuf<pb::Request> for Request {}
}
58 changes: 44 additions & 14 deletions tendermint/src/abci/request/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,56 @@ pub struct Echo {
// Protobuf conversions
// =============================================================================

use core::convert::TryFrom;
mod v0_34 {
use tendermint_proto::v0_34::abci as pb;
use tendermint_proto::Protobuf;

use tendermint_proto::{abci as pb, Protobuf};
use super::Echo;

impl From<Echo> for pb::RequestEcho {
fn from(echo: Echo) -> Self {
Self {
message: echo.message,
impl From<Echo> for pb::RequestEcho {
fn from(echo: Echo) -> Self {
Self {
message: echo.message,
}
}
}
}

impl TryFrom<pb::RequestEcho> for Echo {
type Error = crate::Error;
impl TryFrom<pb::RequestEcho> for Echo {
type Error = crate::Error;

fn try_from(echo: pb::RequestEcho) -> Result<Self, Self::Error> {
Ok(Self {
message: echo.message,
})
fn try_from(echo: pb::RequestEcho) -> Result<Self, Self::Error> {
Ok(Self {
message: echo.message,
})
}
}

impl Protobuf<pb::RequestEcho> for Echo {}
}

impl Protobuf<pb::RequestEcho> for Echo {}
mod v0_37 {
use tendermint_proto::v0_37::abci as pb;
use tendermint_proto::Protobuf;

use super::Echo;

impl From<Echo> for pb::RequestEcho {
fn from(echo: Echo) -> Self {
Self {
message: echo.message,
}
}
}

impl TryFrom<pb::RequestEcho> for Echo {
type Error = crate::Error;

fn try_from(echo: pb::RequestEcho) -> Result<Self, Self::Error> {
Ok(Self {
message: echo.message,
})
}
}

impl Protobuf<pb::RequestEcho> for Echo {}
}

0 comments on commit 6845f84

Please sign in to comment.