Skip to content

Commit

Permalink
WIP: Adapt domain types to v0.37 and add multi-version
Browse files Browse the repository at this point in the history
conversions.
  • Loading branch information
mzabaluev committed Oct 19, 2022
1 parent 6845f84 commit fae22f9
Show file tree
Hide file tree
Showing 10 changed files with 612 additions and 210 deletions.
1 change: 0 additions & 1 deletion proto/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
// Todo: remove dead_code allowance as soon as more types are implemented
#![allow(dead_code)]
pub mod bytes;
pub mod evidence;
pub mod from_str;
pub mod nullable;
pub mod optional;
Expand Down
73 changes: 0 additions & 73 deletions proto/src/serializers/evidence.rs

This file was deleted.

1 change: 1 addition & 0 deletions tendermint/src/abci/doc/request-prepareproposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ABCI documentation](https://github.com/tendermint/tendermint/blob/main/spec/abci/abci++_methods.md#prepareproposal)
1 change: 1 addition & 0 deletions tendermint/src/abci/doc/request-processproposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ABCI documentation](https://github.com/tendermint/tendermint/blob/main/spec/abci/abci++_methods.md#processproposal)
84 changes: 84 additions & 0 deletions tendermint/src/abci/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ mod info;
mod init_chain;
mod load_snapshot_chunk;
mod offer_snapshot;
mod prepare_proposal;
mod process_proposal;
mod query;
mod set_option;

Expand All @@ -49,6 +51,8 @@ pub use info::Info;
pub use init_chain::InitChain;
pub use load_snapshot_chunk::LoadSnapshotChunk;
pub use offer_snapshot::OfferSnapshot;
pub use prepare_proposal::PrepareProposal;
pub use process_proposal::ProcessProposal;
pub use query::Query;
pub use set_option::SetOption;

Expand Down Expand Up @@ -86,6 +90,10 @@ pub enum Request {
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 {
Expand All @@ -99,6 +107,8 @@ impl Request {
DeliverTx(_) => MethodKind::Consensus,
EndBlock(_) => MethodKind::Consensus,
Commit => MethodKind::Consensus,
PrepareProposal(_) => MethodKind::Consensus,
ProcessProposal(_) => MethodKind::Consensus,
CheckTx(_) => MethodKind::Mempool,
ListSnapshots => MethodKind::Snapshot,
OfferSnapshot(_) => MethodKind::Snapshot,
Expand Down Expand Up @@ -282,6 +292,12 @@ mod v0_34 {
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) => {
panic!("PrepareProposal should not be used with Tendermint 0.34")
},
Request::ProcessProposal(x) => {
panic!("ProcessProposal should not be used with Tendermint 0.34")
},
};
pb::Request { value }
}
Expand Down Expand Up @@ -319,3 +335,71 @@ mod v0_34 {

impl Protobuf<pb::Request> 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<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::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(x) => {
panic!("SetOption should not be used with Tendermint 0.37")
},
};
pb::Request { value }
}
}

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::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<pb::Request> for Request {}
}
76 changes: 76 additions & 0 deletions tendermint/src/abci/request/prepare_proposal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::prelude::*;
use crate::{
abci::types::{CommitInfo, Misbehavior},
account, block, Error, Hash, Time,
};

use bytes::Bytes;

#[doc = include_str!("../doc/request-prepareproposal.md")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PrepareProposal {
/// the modified transactions cannot exceed this size.
pub max_tx_bytes: i64,
/// txs is an array of transactions that will be included in a block,
/// sent to the app for possible modifications.
pub txs: Vec<Bytes>,
pub local_last_commit: Option<CommitInfo>,
pub misbehavior: Vec<Misbehavior>,
pub height: block::Height,
pub time: Time,
pub next_validators_hash: Hash,
/// address of the public key of the validator proposing the block.
pub proposer_address: account::Id,
}

// =============================================================================
// Protobuf conversions
// =============================================================================

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

impl From<PrepareProposal> for pb::RequestPrepareProposal {
fn from(value: PrepareProposal) -> Self {
Self {
max_tx_bytes: value.max_tx_bytes,
txs: value.txs,
local_last_commit: value.local_last_commit.map(Into::into),
misbehavior: value.misbehavior.into_iter().map(Into::into).collect(),
height: value.height.into(),
time: Some(value.time.into()),
next_validators_hash: value.next_validators_hash.into(),
proposer_address: value.proposer_address.into(),
}
}
}

impl TryFrom<pb::RequestPrepareProposal> for PrepareProposal {
type Error = Error;

fn try_from(message: pb::RequestPrepareProposal) -> Result<Self, Self::Error> {
let req = Self {
max_tx_bytes: message.max_tx_bytes,
txs: message.txs,
local_last_commit: message
.local_last_commit
.map(TryInto::try_into)
.transpose()?,
misbehavior: message
.misbehavior
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?,
height: message.height.try_into()?,
time: message
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
next_validators_hash: message.next_validators_hash.try_into()?,
proposer_address: message.proposer_address.try_into()?,
};
Ok(req)
}
}

impl Protobuf<pb::RequestPrepareProposal> for PrepareProposal {}
75 changes: 75 additions & 0 deletions tendermint/src/abci/request/process_proposal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::prelude::*;
use crate::{
abci::types::{CommitInfo, Misbehavior},
account, block, Error, Hash, Time,
};

use bytes::Bytes;

#[doc = include_str!("../doc/request-processproposal.md")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProcessProposal {
/// txs is an array of transactions that will be included in a block,
/// sent to the app for possible modifications.
pub txs: Vec<Bytes>,
pub proposed_last_commit: Option<CommitInfo>,
pub misbehavior: Vec<Misbehavior>,
pub hash: Hash,
pub height: block::Height,
pub time: Time,
pub next_validators_hash: Hash,
/// address of the public key of the validator proposing the block.
pub proposer_address: account::Id,
}

// =============================================================================
// Protobuf conversions
// =============================================================================

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

impl From<ProcessProposal> for pb::RequestProcessProposal {
fn from(value: ProcessProposal) -> Self {
Self {
txs: value.txs,
proposed_last_commit: value.proposed_last_commit.map(Into::into),
misbehavior: value.misbehavior.into_iter().map(Into::into).collect(),
hash: value.hash.into(),
height: value.height.into(),
time: Some(value.time.into()),
next_validators_hash: value.next_validators_hash.into(),
proposer_address: value.proposer_address.into(),
}
}
}

impl TryFrom<pb::RequestProcessProposal> for ProcessProposal {
type Error = Error;

fn try_from(message: pb::RequestProcessProposal) -> Result<Self, Self::Error> {
let req = Self {
txs: message.txs,
proposed_last_commit: message
.proposed_last_commit
.map(TryInto::try_into)
.transpose()?,
misbehavior: message
.misbehavior
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<_>, _>>()?,
hash: message.hash.try_into()?,
height: message.height.try_into()?,
time: message
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
next_validators_hash: message.next_validators_hash.try_into()?,
proposer_address: message.proposer_address.try_into()?,
};
Ok(req)
}
}

impl Protobuf<pb::RequestProcessProposal> for ProcessProposal {}
Loading

0 comments on commit fae22f9

Please sign in to comment.