Skip to content

Commit

Permalink
imp: minimize prost dependency (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani authored Dec 1, 2023
1 parent fe9a26b commit 2259108
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 70 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- [`cw-check`] More rigorous CosmWasm check by upgrading dependencies and
including `std` and `schema` features for `ibc-core`.
([\#992](https://github.com/cosmos/ibc-rs/pull/992))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [`ibc-primitives`] Minimize `prost` dependency by introducing `ToVec` trait
([\#997](https://github.com/cosmos/ibc-rs/issues/997))
2 changes: 0 additions & 2 deletions ibc-clients/ics07-tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ all-features = true

[dependencies]
# external dependencies
prost = { workspace = true }
serde = { workspace = true, optional = true }

# ibc dependencies
Expand All @@ -36,7 +35,6 @@ tendermint-light-client-verifier = { workspace = true, features = ["rust-crypto"
[features]
default = ["std"]
std = [
"prost/std",
"serde/std",
"ibc-client-tendermint-types/std",
"ibc-core-client/std",
Expand Down
16 changes: 3 additions & 13 deletions ibc-clients/ics07-tendermint/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use ibc_core_host::types::path::{
};
use ibc_core_host::ExecutionContext;
use ibc_primitives::prelude::*;
use prost::Message;
use ibc_primitives::ToVec;

use super::consensus_state::ConsensusState as TmConsensusState;
use crate::context::{
Expand Down Expand Up @@ -169,32 +169,22 @@ impl ClientStateCommon for ClientState {

let last_height = self.latest_height().revision_height();

let mut client_state_value = Vec::new();
upgraded_client_state
.encode(&mut client_state_value)
.map_err(ClientError::Encode)?;

// Verify the proof of the upgraded client state
self.verify_membership(
&upgrade_path_prefix,
&proof_upgrade_client,
root,
Path::UpgradeClient(UpgradeClientPath::UpgradedClientState(last_height)),
client_state_value,
upgraded_client_state.to_vec(),
)?;

let mut cons_state_value = Vec::new();
upgraded_consensus_state
.encode(&mut cons_state_value)
.map_err(ClientError::Encode)?;

// Verify the proof of the upgraded consensus state
self.verify_membership(
&upgrade_path_prefix,
&proof_upgrade_consensus_state,
root,
Path::UpgradeClient(UpgradeClientPath::UpgradedClientConsensusState(last_height)),
cons_state_value,
upgraded_consensus_state.to_vec(),
)?;

Ok(())
Expand Down
5 changes: 0 additions & 5 deletions ibc-core/ics02-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ description = """
all-features = true

[dependencies]
# external dependencies
prost = { workspace = true }

# ibc dependencies
ibc-core-client-types = { workspace = true }
ibc-core-client-context = { workspace = true }
ibc-core-commitment-types = { workspace = true }
Expand All @@ -31,7 +27,6 @@ ibc-primitives = { workspace = true }
[features]
default = ["std"]
std = [
"prost/std",
"ibc-core-client-types/std",
"ibc-core-client-context/std",
"ibc-core-commitment-types/std",
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics02-client/context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ all-features = true
# external dependencies
derive_more = { workspace = true }
displaydoc = { workspace = true }
prost = { workspace = true }
subtle-encoding = { workspace = true }

# ibc dependencies
Expand All @@ -39,7 +38,6 @@ tendermint = { workspace = true }
default = ["std"]
std = [
"displaydoc/std",
"prost/std",
"subtle-encoding/std",
"ibc-core-client-types/std",
"ibc-core-commitment-types/std",
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics02-client/src/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ibc_core_handler_types::error::ContextError;
use ibc_core_handler_types::events::{IbcEvent, MessageEvent};
use ibc_core_host::{ExecutionContext, ValidationContext};
use ibc_primitives::prelude::*;
use prost::Message;
use ibc_primitives::ToVec;

pub fn validate<Ctx>(ctx: &Ctx, msg: MsgUpdateOrMisbehaviour) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -108,7 +108,7 @@ where
client_state.client_type(),
*consensus_height,
consensus_heights,
header.encode_to_vec(),
header.to_vec(),
))
};
ctx.emit_ibc_event(IbcEvent::Message(MessageEvent::Client))?;
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics02-client/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ pub enum ClientError {
MissingRawConsensusState,
/// invalid client id in the update client message: `{0}`
InvalidMsgUpdateClientId(IdentifierError),
/// encode error: `{0}`
Encode(prost::EncodeError),
/// decode error: `{0}`
Decode(prost::DecodeError),
/// invalid client identifier error: `{0}`
Expand Down
5 changes: 0 additions & 5 deletions ibc-core/ics03-connection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ description = """
all-features = true

[dependencies]
# external dependencies
prost = { workspace = true }

# ibc dependencies
ibc-core-client = { workspace = true }
ibc-core-connection-types = { workspace = true }
ibc-core-host = { workspace = true }
Expand All @@ -30,7 +26,6 @@ ibc-primitives = { workspace = true }
[features]
default = ["std"]
std = [
"prost/std",
"ibc-core-client/std",
"ibc-core-connection-types/std",
"ibc-core-host/std",
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics03-connection/src/handler/conn_open_ack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ibc_core_host::types::path::{ClientConsensusStatePath, ClientStatePath, Conn
use ibc_core_host::{ExecutionContext, ValidationContext};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Protobuf;
use prost::Message;
use ibc_primitives::ToVec;

pub fn validate<Ctx>(ctx_a: &Ctx, msg: MsgConnectionOpenAck) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -106,7 +106,7 @@ where
&msg.proof_client_state_of_a_on_b,
consensus_state_of_b_on_a.root(),
Path::ClientState(ClientStatePath::new(vars.client_id_on_b())),
msg.client_state_of_a_on_b.encode_to_vec(),
msg.client_state_of_a_on_b.to_vec(),
)
.map_err(|e| ConnectionError::ClientStateVerificationFailure {
client_id: vars.client_id_on_b().clone(),
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics03-connection/src/handler/conn_open_try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ibc_core_host::types::path::{
use ibc_core_host::{ExecutionContext, ValidationContext};
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Protobuf;
use prost::Message;
use ibc_primitives::ToVec;

pub fn validate<Ctx>(ctx_b: &Ctx, msg: MsgConnectionOpenTry) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -101,7 +101,7 @@ where
&msg.proof_client_state_of_b_on_a,
consensus_state_of_a_on_b.root(),
Path::ClientState(ClientStatePath::new(client_id_on_a)),
msg.client_state_of_b_on_a.encode_to_vec(),
msg.client_state_of_b_on_a.to_vec(),
)
.map_err(|e| ConnectionError::ClientStateVerificationFailure {
client_id: msg.client_id_on_b.clone(),
Expand Down
5 changes: 0 additions & 5 deletions ibc-core/ics04-channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ description = """
all-features = true

[dependencies]
# external dependencies
prost = { workspace = true }

# ibc dependencies
ibc-core-client = { workspace = true }
ibc-core-connection = { workspace = true }
ibc-core-channel-types = { workspace = true }
Expand All @@ -33,7 +29,6 @@ ibc-primitives = { workspace = true }
[features]
default = ["std"]
std = [
"prost/std",
"ibc-core-client/std",
"ibc-core-connection/std",
"ibc-core-channel-types/std",
Expand Down
10 changes: 1 addition & 9 deletions ibc-core/ics04-channel/src/handler/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use ibc_core_host::types::path::{
use ibc_core_host::{ExecutionContext, ValidationContext};
use ibc_core_router::module::Module;
use ibc_primitives::prelude::*;
use prost::Message;

use super::timeout_on_close;

Expand Down Expand Up @@ -231,19 +230,12 @@ where
let seq_recv_path_on_b =
SeqRecvPath::new(&msg.packet.port_id_on_b, &msg.packet.chan_id_on_b);

let mut value = Vec::new();
u64::from(msg.packet.seq_on_a)
.encode(&mut value)
.map_err(|_| PacketError::CannotEncodeSequence {
sequence: msg.packet.seq_on_a,
})?;

client_state_of_b_on_a.verify_membership(
conn_end_on_a.counterparty().prefix(),
&msg.proof_unreceived_on_b,
consensus_state_of_b_on_a.root(),
Path::SeqRecv(seq_recv_path_on_b),
value,
msg.packet.seq_on_a.to_vec(),
)
} else {
let receipt_path_on_b = ReceiptPath::new(
Expand Down
10 changes: 1 addition & 9 deletions ibc-core/ics04-channel/src/handler/timeout_on_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use ibc_core_host::types::path::{
use ibc_core_host::ValidationContext;
use ibc_primitives::prelude::*;
use ibc_primitives::proto::Protobuf;
use prost::Message;

pub fn validate<Ctx>(ctx_a: &Ctx, msg: &MsgTimeoutOnClose) -> Result<(), ContextError>
where
Expand Down Expand Up @@ -135,19 +134,12 @@ where
}
let seq_recv_path_on_b = SeqRecvPath::new(&packet.port_id_on_b, &packet.chan_id_on_b);

let mut value = Vec::new();
u64::from(packet.seq_on_a).encode(&mut value).map_err(|_| {
PacketError::CannotEncodeSequence {
sequence: packet.seq_on_a,
}
})?;

client_state_of_b_on_a.verify_membership(
conn_end_on_a.counterparty().prefix(),
&msg.proof_unreceived_on_b,
consensus_state_of_b_on_a.root(),
Path::SeqRecv(seq_recv_path_on_b),
value,
packet.seq_on_a.to_vec(),
)
} else {
let receipt_path_on_b = ReceiptPath::new(
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics04-channel/types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ pub enum PacketError {
port_id: PortId,
channel_id: ChannelId,
},
/// Cannot encode sequence `{sequence}`
CannotEncodeSequence { sequence: Sequence },
/// other error: `{description}`
Other { description: String },
}
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics24-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ all-features = true
# external dependencies
derive_more = { workspace = true }
displaydoc = { workspace = true }
prost = { workspace = true }
subtle-encoding = { workspace = true }

# ibc dependencies
Expand All @@ -41,7 +40,6 @@ rstest = { workspace = true }
default = ["std"]
std = [
"displaydoc/std",
"prost/std",
"subtle-encoding/std",
"ibc-core-client-types/std",
"ibc-core-client-context/std",
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics24-host/cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ all-features = true
borsh = { workspace = true, optional = true}
derive_more = { workspace = true }
displaydoc = { workspace = true }
prost = { workspace = true }
serde = { workspace = true, optional = true }
sha2 = { workspace = true }
subtle-encoding = { workspace = true }
Expand Down Expand Up @@ -50,7 +49,6 @@ scale-info = { workspace = true, optional = true }
default = ["std"]
std = [
"displaydoc/std",
"prost/std",
"serde/std",
"sha2/std",
"subtle-encoding/std",
Expand Down
11 changes: 11 additions & 0 deletions ibc-core/ics24-host/types/src/identifiers/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ibc_primitives::prelude::*;
use ibc_primitives::ToVec;

use crate::error::IdentifierError;

Expand Down Expand Up @@ -34,16 +35,26 @@ impl core::str::FromStr for Sequence {
}

impl Sequence {
/// Gives the sequence number.
pub fn value(&self) -> u64 {
self.0
}

/// Returns `true` if the sequence number is zero.
pub fn is_zero(&self) -> bool {
self.0 == 0
}

/// Increments the sequence number by one.
pub fn increment(&self) -> Sequence {
Sequence(self.0 + 1)
}

/// Encodes the sequence number into a `Vec<u8>` using
/// `prost::Message::encode_to_vec`.
pub fn to_vec(&self) -> Vec<u8> {
self.0.to_vec()
}
}

impl From<u64> for Sequence {
Expand Down
2 changes: 0 additions & 2 deletions ibc-core/ics26-routing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ all-features = true
# external dependencies
derive_more = { workspace = true }
displaydoc = { workspace = true }
prost = { workspace = true }
subtle-encoding = { workspace = true }

# ibc dependencies
Expand All @@ -33,7 +32,6 @@ ibc-core-router-types = { workspace = true }
default = ["std"]
std = [
"displaydoc/std",
"prost/std",
"subtle-encoding/std",
"ibc-primitives/std",
"ibc-core-channel-types/std",
Expand Down
11 changes: 11 additions & 0 deletions ibc-primitives/src/traits/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ where
<Self as TryFrom<P>>::Error: Display,
{
}

/// Convenient trait for converting types to a raw Protobuf `Vec<u8>`.
pub trait ToVec {
fn to_vec(&self) -> Vec<u8>;
}

impl<T: prost::Message> ToVec for T {
fn to_vec(&self) -> Vec<u8> {
self.encode_to_vec()
}
}

0 comments on commit 2259108

Please sign in to comment.