Skip to content

Commit

Permalink
Decouple tendermint-rpc from tendermint-proto (informalsystems#1234)
Browse files Browse the repository at this point in the history
* rpc: remove use of tendermint_proto::serializers

Import tendermint::serializers instead. This removes almost all
need to depend on tendermint_proto directly.

* Rename merkle::Proof to merkle::ProofOps

The protobuf message name is actually ProofOps, and
the crypto package also defines Proof, for which we now
want to have a domain type.

* tendermint: add domain type merkle::Proof

Corresponds to the protobuf message crypto.Proof.

* tendermint: mod tx and domain type tx::Proof

Corresponds to protobuf types.TxProof

* rpc: Change /tx response field type to tx::Proof

Thus removing the last direct use of tendermint_proto.

* rpc: remove dependency on tendermint-proto

* tendermint: slap some doc on tx::Proof

* Changelog entries for informalsystems#1234
  • Loading branch information
mzabaluev authored Nov 16, 2022
1 parent 6ae3f25 commit c5f76fb
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- `[tendermint]` Rename `merkle::proof::Proof` to `ProofOps`
([#1234](https://github.com/informalsystems/tendermint-rs/pull/1234))
- `[tendermint-rpc]` Change the type of `/tx` response field `proof`
to `tendermint::tx::Proof`
([#1233](https://github.com/informalsystems/tendermint-rs/issues/1233))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[tendermint]` Add domain types `merkle::Proof` and `tx::Proof`,
to represent protobuf messages `crypto.Proof` and `types.TxProof` respectively
([#1234](https://github.com/informalsystems/tendermint-rs/pull/1234))
1 change: 0 additions & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ serde_bytes = { version = "0.11", default-features = false }
serde_json = { version = "1", default-features = false, features = ["std"] }
tendermint-config = { version = "0.26.0", path = "../config", default-features = false }
tendermint = { version = "0.26.0", default-features = false, path = "../tendermint" }
tendermint-proto = { version = "0.26.0", default-features = false, path = "../proto" }
thiserror = { version = "1", default-features = false }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }
uuid = { version = "0.8", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/endpoint/abci_query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! `/abci_query` endpoint JSON-RPC wrapper
use serde::{Deserialize, Serialize};
use tendermint::{abci::Code, block, merkle::proof::Proof, serializers};
use tendermint::{abci::Code, block, merkle::proof::ProofOps, serializers};

use crate::prelude::*;

Expand Down Expand Up @@ -87,7 +87,7 @@ pub struct AbciQuery {

/// Proof (might be explicit null)
#[serde(alias = "proofOps")]
pub proof: Option<Proof>,
pub proof: Option<ProofOps>,

/// Block height
pub height: block::Height,
Expand Down
8 changes: 4 additions & 4 deletions rpc/src/endpoint/block_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use serde::{Deserialize, Serialize};

pub use super::{block, block_results};
use crate::{prelude::*, Method, Order};
use crate::{prelude::*, serializers, Method, Order};

/// Request for searching for blocks by their BeginBlock and EndBlock events.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Request {
pub query: String,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub page: u32,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub per_page: u8,
pub order_by: Order,
}
Expand Down Expand Up @@ -41,7 +41,7 @@ impl crate::SimpleRequest for Request {}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
pub blocks: Vec<block::Response>,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub total_count: u32,
}

Expand Down
5 changes: 2 additions & 3 deletions rpc/src/endpoint/tx.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! `/tx` endpoint JSON-RPC wrapper
use serde::{Deserialize, Serialize};
use tendermint::{abci, block, Hash};
use tendermint_proto::types::TxProof;
use tendermint::{abci, block, tx, Hash};

use crate::{prelude::*, serializers, Method};

Expand Down Expand Up @@ -51,7 +50,7 @@ pub struct Response {
#[serde(with = "serializers::bytes::base64string")]
pub tx: Vec<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub proof: Option<TxProof>,
pub proof: Option<tx::Proof>,
}

impl crate::Response for Response {}
8 changes: 4 additions & 4 deletions rpc/src/endpoint/tx_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
use serde::{Deserialize, Serialize};

pub use super::tx;
use crate::{prelude::*, Method, Order};
use crate::{prelude::*, serializers, Method, Order};

/// Request for searching for transactions with their results.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Request {
pub query: String,
pub prove: bool,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub page: u32,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub per_page: u8,
pub order_by: Order,
}
Expand Down Expand Up @@ -49,7 +49,7 @@ impl crate::SimpleRequest for Request {}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Response {
pub txs: Vec<tx::Response>,
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub total_count: u32,
}

Expand Down
8 changes: 4 additions & 4 deletions rpc/src/endpoint/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use serde::{Deserialize, Serialize};
use tendermint::{block, validator};

use crate::{prelude::*, PageNumber, PerPage};
use crate::{prelude::*, serializers, PageNumber, PerPage};

/// The default number of validators to return per page.
pub const DEFAULT_VALIDATORS_PER_PAGE: u8 = 30;
Expand All @@ -16,10 +16,10 @@ pub struct Request {
/// defaults to the latest height.
pub height: Option<block::Height>,
/// The number of the page to fetch.
#[serde(with = "tendermint_proto::serializers::optional_from_str")]
#[serde(with = "serializers::optional_from_str")]
pub page: Option<PageNumber>,
/// The number of validators to fetch per page.
#[serde(with = "tendermint_proto::serializers::optional_from_str")]
#[serde(with = "serializers::optional_from_str")]
pub per_page: Option<PerPage>,
}

Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Response {
pub validators: Vec<validator::Info>,

/// Total number of validators for this block height.
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub total: i32,
}

Expand Down
6 changes: 3 additions & 3 deletions rpc/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::collections::BTreeMap as HashMap;
use serde::{Deserialize, Serialize};
use tendermint::{abci, Block};

use crate::{prelude::*, query::EventType, response::Wrapper, Response};
use crate::{prelude::*, query::EventType, response::Wrapper, serializers, Response};

/// An incoming event produced by a [`Subscription`].
///
Expand Down Expand Up @@ -59,10 +59,10 @@ pub enum EventData {
/// Transaction result info.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct TxInfo {
#[serde(with = "tendermint_proto::serializers::from_str")]
#[serde(with = "serializers::from_str")]
pub height: i64,
pub index: Option<i64>,
#[serde(with = "tendermint_proto::serializers::bytes::base64string")]
#[serde(with = "serializers::bytes::base64string")]
pub tx: Vec<u8>,
pub result: TxResult,
}
Expand Down
3 changes: 1 addition & 2 deletions rpc/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
use core::{fmt, str::FromStr};

use tendermint_proto::serializers::timestamp;
use time::{
format_description::well_known::Rfc3339,
macros::{format_description, offset},
Date, OffsetDateTime,
};

use crate::{prelude::*, Error};
use crate::{prelude::*, serializers::timestamp, Error};

/// A structured query for use in interacting with the Tendermint RPC event
/// subscription system.
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! CAUTION: There are no guarantees for backwards compatibility, this module should be considered
//! an internal implementation detail which can vanish without further warning. Use at your own
//! risk.
pub use tendermint_proto::serializers::*;
pub use tendermint::serializers::*;

pub mod opt_tm_hash_base64;
pub mod tm_hash_base64;
Expand Down
5 changes: 3 additions & 2 deletions rpc/tests/kvstore_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,9 @@ fn incoming_fixtures() {
assert!(tx.tx_result.log.is_empty());
let proof = tx.proof.unwrap();
assert_eq!(proof.data, tx.tx);
assert!(proof.proof.is_some());
assert_ne!(proof.root_hash, [0; 32]);
assert_eq!(proof.proof.total, 1);
assert_eq!(proof.proof.index, 0);
assert_ne!(proof.root_hash.as_bytes(), [0; 32]);
}
},
_ => {
Expand Down
2 changes: 1 addition & 1 deletion tendermint/src/abci/response/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Query {
pub value: Bytes,
/// Serialized proof for the value data, if requested, to be verified against
/// the app hash for the given `height`.
pub proof: Option<merkle::Proof>,
pub proof: Option<merkle::ProofOps>,
/// The block height from which data was derived.
///
/// Note that this is the height of the block containing the application's
Expand Down
8 changes: 8 additions & 0 deletions tendermint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ define_error! {

TrustThresholdTooSmall
|_| { "trust threshold too small (must be >= 1/3)" },

NegativeProofTotal
[ DisplayOnly<TryFromIntError> ]
|_| { "negative number of items in proof" },

NegativeProofIndex
[ DisplayOnly<TryFromIntError> ]
|_| { "negative item index in proof" },
}
}

Expand Down
1 change: 1 addition & 0 deletions tendermint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod signature;
pub mod time;
mod timeout;
pub mod trust_threshold;
pub mod tx;
pub mod validator;
mod version;
pub mod vote;
Expand Down
2 changes: 2 additions & 0 deletions tendermint/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pub mod proof;

pub use proof::Proof;

use sha2::{Digest, Sha256};

use crate::prelude::*;
Expand Down
72 changes: 62 additions & 10 deletions tendermint/src/merkle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ use core::convert::TryFrom;

use serde::{Deserialize, Serialize};
use tendermint_proto::{
crypto::{ProofOp as RawProofOp, ProofOps as RawProofOps},
crypto::{Proof as RawProof, ProofOp as RawProofOp, ProofOps as RawProofOps},
Protobuf,
};

use crate::{prelude::*, serializers, Error};
use crate::{prelude::*, serializers, Error, Hash};

/// Proof is Merkle proof defined by the list of ProofOps
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "RawProof", into = "RawProof")]
pub struct Proof {
// Total number of items.
pub total: u64,
// Index of the item to prove.
pub index: u64,
// Hash of item value.
pub leaf_hash: Hash,
// Hashes from leaf's sibling to a root's child.
pub aunts: Vec<Hash>,
}

/// Merkle proof defined by the list of ProofOps
/// <https://github.com/tendermint/tendermint/blob/c8483531d8e756f7fbb812db1dd16d841cdf298a/crypto/merkle/merkle.proto#L26>
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Proof {
pub struct ProofOps {
/// The list of ProofOps
pub ops: Vec<ProofOp>,
}
Expand All @@ -34,6 +47,45 @@ pub struct ProofOp {
pub data: Vec<u8>,
}

impl Protobuf<RawProof> for Proof {}

impl TryFrom<RawProof> for Proof {
type Error = Error;

fn try_from(message: RawProof) -> Result<Self, Self::Error> {
Ok(Self {
total: message
.total
.try_into()
.map_err(Error::negative_proof_total)?,
index: message
.index
.try_into()
.map_err(Error::negative_proof_index)?,
leaf_hash: message.leaf_hash.try_into()?,
aunts: message
.aunts
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}

impl From<Proof> for RawProof {
fn from(value: Proof) -> Self {
Self {
total: value
.total
.try_into()
.expect("number of items is too large"),
index: value.index.try_into().expect("index is too large"),
leaf_hash: value.leaf_hash.into(),
aunts: value.aunts.into_iter().map(Into::into).collect(),
}
}
}

impl Protobuf<RawProofOp> for ProofOp {}

impl TryFrom<RawProofOp> for ProofOp {
Expand All @@ -58,9 +110,9 @@ impl From<ProofOp> for RawProofOp {
}
}

impl Protobuf<RawProofOps> for Proof {}
impl Protobuf<RawProofOps> for ProofOps {}

impl TryFrom<RawProofOps> for Proof {
impl TryFrom<RawProofOps> for ProofOps {
type Error = Error;

fn try_from(value: RawProofOps) -> Result<Self, Self::Error> {
Expand All @@ -70,8 +122,8 @@ impl TryFrom<RawProofOps> for Proof {
}
}

impl From<Proof> for RawProofOps {
fn from(value: Proof) -> Self {
impl From<ProofOps> for RawProofOps {
fn from(value: ProofOps) -> Self {
let ops: Vec<RawProofOp> = value.ops.into_iter().map(RawProofOp::from).collect();

RawProofOps { ops }
Expand All @@ -80,7 +132,7 @@ impl From<Proof> for RawProofOps {

#[cfg(test)]
mod test {
use super::Proof;
use super::ProofOps;
use crate::test::test_serialization_roundtrip;

#[test]
Expand All @@ -100,6 +152,6 @@ mod test {
}
]
}"#;
test_serialization_roundtrip::<Proof>(payload);
test_serialization_roundtrip::<ProofOps>(payload);
}
}
3 changes: 3 additions & 0 deletions tendermint/src/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod proof;

pub use proof::Proof;
Loading

0 comments on commit c5f76fb

Please sign in to comment.