Skip to content

Commit

Permalink
Use core and alloc crates for no_std compatibility (#1156)
Browse files Browse the repository at this point in the history
* Use core and alloc crates for no_std compatibility

* Revert use of core:: back to std:: in ibc-proto crate

* Refactor some new uses of std

* Refactor more use of std.

* Fix formatting

* Fix more use of `std`

* Remove stale changes

* Remove some more use of std

* Use #![no_std] in ibc crate, with extern crate std and custom prelude

* Add changelog
  • Loading branch information
soareschen authored Sep 15, 2021
1 parent 5d38857 commit 09420bc
Show file tree
Hide file tree
Showing 170 changed files with 585 additions and 424 deletions.
3 changes: 3 additions & 0 deletions .changelog/unreleased/improvements/1156-use-core-alloc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Use `core` and `alloc` crates for `no_std` compatibility ([#1156])

[#1156]: https://github.com/informalsystems/ibc-rs/issues/1156
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ics04_channel::error as channel_error;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::{ChannelId, PortId};
use crate::prelude::*;
use flex_error::define_error;

define_error! {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This is the definition of a transfer messages that an application submits to a chain.
use std::convert::{TryFrom, TryInto};

use crate::prelude::*;
use core::convert::{TryFrom, TryInto};
use tendermint_proto::Protobuf;

use ibc_proto::ibc::apps::transfer::v1::MsgTransfer as RawMsgTransfer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::handler::HandlerOutput;
use crate::ics04_channel::handler::send_packet::send_packet;
use crate::ics04_channel::packet::Packet;
use crate::ics04_channel::packet::PacketResult;
use crate::prelude::*;

pub(crate) fn send_transfer<Ctx>(
ctx: &Ctx,
Expand Down
7 changes: 4 additions & 3 deletions modules/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::prelude::*;
use core::fmt;
use flex_error::{define_error, TraceError};
use prost::alloc::fmt::Formatter;
use serde_derive::{Deserialize, Serialize};

use crate::ics02_client::error as client_error;
Expand All @@ -12,9 +16,6 @@ use crate::ics04_channel::events::Attributes as ChannelAttributes;
use crate::ics24_host::error::ValidationError;
use crate::timestamp::ParseTimestampError;
use crate::Height;
use flex_error::{define_error, TraceError};
use prost::alloc::fmt::Formatter;
use std::fmt;

define_error! {
Error {
Expand Down
7 changes: 4 additions & 3 deletions modules/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::events::IbcEvent;
use std::marker::PhantomData;
use crate::prelude::*;
use core::marker::PhantomData;

pub type HandlerResult<T, E> = Result<HandlerOutput<T>, E>;

Expand All @@ -26,8 +27,8 @@ pub struct HandlerOutputBuilder<T> {
impl<T> HandlerOutputBuilder<T> {
pub fn new() -> Self {
Self {
log: vec![],
events: vec![],
log: Vec::new(),
events: Vec::new(),
marker: PhantomData,
}
}
Expand Down
13 changes: 6 additions & 7 deletions modules/src/ics02_client/client_consensus.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use core::marker::{Send, Sync};
use std::convert::TryFrom;

use crate::prelude::*;
use chrono::{DateTime, Utc};
use core::convert::Infallible;
use core::convert::TryFrom;
use core::marker::{Send, Sync};
use ibc_proto::ibc::core::client::v1::ConsensusStateWithHeight;
use prost_types::Any;
use serde::Serialize;
use std::convert::Infallible;
use tendermint_proto::Protobuf;

use ibc_proto::ibc::core::client::v1::ConsensusStateWithHeight;

use crate::events::IbcEventType;
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::Error;
Expand All @@ -26,7 +25,7 @@ pub const TENDERMINT_CONSENSUS_STATE_TYPE_URL: &str =

pub const MOCK_CONSENSUS_STATE_TYPE_URL: &str = "/ibc.mock.ConsensusState";

pub trait ConsensusState: Clone + std::fmt::Debug + Send + Sync {
pub trait ConsensusState: Clone + core::fmt::Debug + Send + Sync {
type Error;

/// Type of client associated with this consensus state (eg. Tendermint)
Expand Down
1 change: 1 addition & 0 deletions modules/src/ics02_client/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::ics04_channel::packet::Sequence;
use crate::ics07_tendermint::client_def::TendermintClient;
use crate::ics23_commitment::commitment::{CommitmentPrefix, CommitmentProofBytes, CommitmentRoot};
use crate::ics24_host::identifier::{ChannelId, ClientId, ConnectionId, PortId};
use crate::prelude::*;
use crate::Height;

#[cfg(any(test, feature = "mocks"))]
Expand Down
9 changes: 5 additions & 4 deletions modules/src/ics02_client/client_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;
use core::convert::{TryFrom, TryInto};
use core::marker::{Send, Sync};
use std::convert::{TryFrom, TryInto};
use std::time::Duration;
use core::time::Duration;

use prost_types::Any;
use serde::{Deserialize, Serialize};
Expand All @@ -21,7 +22,7 @@ use crate::Height;
pub const TENDERMINT_CLIENT_STATE_TYPE_URL: &str = "/ibc.lightclients.tendermint.v1.ClientState";
pub const MOCK_CLIENT_STATE_TYPE_URL: &str = "/ibc.mock.ClientState";

pub trait ClientState: Clone + std::fmt::Debug + Send + Sync {
pub trait ClientState: Clone + core::fmt::Debug + Send + Sync {
/// Return the chain identifier which this client is serving (i.e., the client is verifying
/// consensus states from this chain).
fn chain_id(&self) -> ChainId;
Expand Down Expand Up @@ -216,7 +217,7 @@ impl From<IdentifiedAnyClientState> for IdentifiedClientState {

#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use core::convert::TryFrom;
use test_env_log::test;

use prost_types::Any;
Expand Down
8 changes: 4 additions & 4 deletions modules/src/ics02_client/client_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;

use crate::prelude::*;
use core::fmt;
use serde_derive::{Deserialize, Serialize};

use super::error::Error;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl fmt::Display for ClientType {
}
}

impl std::str::FromStr for ClientType {
impl core::str::FromStr for ClientType {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -53,7 +53,7 @@ impl std::str::FromStr for ClientType {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use core::str::FromStr;
use test_env_log::test;

use super::ClientType;
Expand Down
11 changes: 6 additions & 5 deletions modules/src/ics02_client/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::prelude::*;
use core::num::TryFromIntError;

use flex_error::{define_error, DisplayOnly, TraceError};
use tendermint_proto::Error as TendermintError;

use crate::ics02_client::client_type::ClientType;
use crate::ics07_tendermint::error::Error as Ics07Error;
use crate::ics23_commitment::error::Error as Ics23Error;
use crate::ics24_host::error::ValidationError;
use crate::ics24_host::identifier::ClientId;
use crate::Height;

use std::num::TryFromIntError;

use flex_error::{define_error, DisplayOnly, TraceError};
use tendermint_proto::Error as TendermintError;

define_error! {
#[derive(Debug, PartialEq, Eq)]
Error {
Expand Down
12 changes: 6 additions & 6 deletions modules/src/ics02_client/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl Default for Attributes {
}
}

impl std::fmt::Display for Attributes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Display for Attributes {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(
f,
"h: {}, cs_h: {}({})",
Expand Down Expand Up @@ -166,8 +166,8 @@ impl From<CreateClient> for IbcEvent {
}
}

impl std::fmt::Display for CreateClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Display for CreateClient {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{}", self.0)
}
}
Expand Down Expand Up @@ -215,8 +215,8 @@ impl From<UpdateClient> for IbcEvent {
}
}

impl std::fmt::Display for UpdateClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Display for UpdateClient {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{}", self.common)
}
}
Expand Down
7 changes: 4 additions & 3 deletions modules/src/ics02_client/handler/create_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Protocol logic specific to processing ICS2 messages of type `MsgCreateAnyClient`.
use crate::prelude::*;

use crate::events::IbcEvent;
use crate::handler::{HandlerOutput, HandlerResult};
Expand All @@ -11,7 +12,6 @@ use crate::ics02_client::events::Attributes;
use crate::ics02_client::handler::ClientResult;
use crate::ics02_client::msgs::create_client::MsgCreateAnyClient;
use crate::ics24_host::identifier::ClientId;

/// The result following the successful processing of a `MsgCreateAnyClient` message. Preferably
/// this data type should be used with a qualified name `create_client::Result` to avoid ambiguity.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -57,8 +57,9 @@ pub fn process(

#[cfg(test)]
mod tests {
use std::convert::TryInto;
use std::time::Duration;
use crate::prelude::*;
use core::convert::TryInto;
use core::time::Duration;
use test_env_log::test;

use crate::events::IbcEvent;
Expand Down
4 changes: 3 additions & 1 deletion modules/src/ics02_client/handler/update_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::ics02_client::events::Attributes;
use crate::ics02_client::handler::ClientResult;
use crate::ics02_client::msgs::update_client::MsgUpdateAnyClient;
use crate::ics24_host::identifier::ClientId;
use crate::prelude::*;

/// The result following the successful processing of a `MsgUpdateAnyClient` message. Preferably
/// this data type should be used with a qualified name `update_client::Result` to avoid ambiguity.
Expand Down Expand Up @@ -68,7 +69,7 @@ pub fn process(

#[cfg(test)]
mod tests {
use std::str::FromStr;
use core::str::FromStr;
use test_env_log::test;

use crate::events::IbcEvent;
Expand All @@ -84,6 +85,7 @@ mod tests {
use crate::mock::client_state::MockClientState;
use crate::mock::context::MockContext;
use crate::mock::header::MockHeader;
use crate::prelude::*;
use crate::test_utils::get_dummy_account_id;
use crate::Height;

Expand Down
5 changes: 4 additions & 1 deletion modules/src/ics02_client/handler/upgrade_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::ics02_client::events::Attributes;
use crate::ics02_client::handler::ClientResult;
use crate::ics02_client::msgs::upgrade_client::MsgUpgradeAnyClient;
use crate::ics24_host::identifier::ClientId;
use crate::prelude::*;

/// The result following the successful processing of a `MsgUpgradeAnyClient` message.
/// This data type should be used with a qualified name `upgrade_client::Result` to avoid ambiguity.
Expand Down Expand Up @@ -74,7 +75,9 @@ pub fn process(

#[cfg(test)]
mod tests {
use std::{convert::TryFrom, str::FromStr};
use crate::prelude::*;
use core::convert::TryFrom;
use core::str::FromStr;

use crate::events::IbcEvent;
use crate::handler::HandlerOutput;
Expand Down
13 changes: 7 additions & 6 deletions modules/src/ics02_client/header.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use std::convert::TryFrom;
use std::ops::Deref;
use crate::prelude::*;
use core::convert::TryFrom;
use core::ops::Deref;
use prost_types::Any;
use serde_derive::{Deserialize, Serialize};
use tendermint_proto::Protobuf;

use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::Error;
use crate::ics07_tendermint::header::{decode_header, Header as TendermintHeader};
#[cfg(any(test, feature = "mocks"))]
use crate::mock::header::MockHeader;
use crate::Height;
use prost_types::Any;
use serde_derive::{Deserialize, Serialize};
use tendermint_proto::Protobuf;

pub const TENDERMINT_HEADER_TYPE_URL: &str = "/ibc.lightclients.tendermint.v1.Header";
pub const MOCK_HEADER_TYPE_URL: &str = "/ibc.mock.Header";

/// Abstract of consensus state update information
pub trait Header: Clone + std::fmt::Debug + Send + Sync {
pub trait Header: Clone + core::fmt::Debug + Send + Sync {
/// The type of client (eg. Tendermint)
fn client_type(&self) -> ClientType;

Expand Down
17 changes: 9 additions & 8 deletions modules/src/ics02_client/height.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::num::ParseIntError;
use std::str::FromStr;
use crate::prelude::*;
use core::cmp::Ordering;
use core::convert::TryFrom;
use core::num::ParseIntError;
use core::str::FromStr;

use flex_error::{define_error, TraceError};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -121,8 +122,8 @@ impl From<Height> for RawHeight {
}
}

impl std::fmt::Debug for Height {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Debug for Height {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
f.debug_struct("Height")
.field("revision", &self.revision_number)
.field("height", &self.revision_height)
Expand All @@ -131,8 +132,8 @@ impl std::fmt::Debug for Height {
}

/// Custom debug output to omit the packet data
impl std::fmt::Display for Height {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Display for Height {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(f, "{}-{}", self.revision_number, self.revision_height)
}
}
Expand Down
10 changes: 5 additions & 5 deletions modules/src/ics02_client/misbehaviour.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::convert::TryFrom;

use crate::prelude::*;
use core::convert::TryFrom;
use prost_types::Any;
use tendermint_proto::Protobuf;

Expand All @@ -19,7 +19,7 @@ pub const TENDERMINT_MISBEHAVIOR_TYPE_URL: &str = "/ibc.lightclients.tendermint.
#[cfg(any(test, feature = "mocks"))]
pub const MOCK_MISBEHAVIOUR_TYPE_URL: &str = "/ibc.mock.Misbehavior";

pub trait Misbehaviour: Clone + std::fmt::Debug + Send + Sync {
pub trait Misbehaviour: Clone + core::fmt::Debug + Send + Sync {
/// The type of client (eg. Tendermint)
fn client_id(&self) -> &ClientId;

Expand Down Expand Up @@ -103,8 +103,8 @@ impl From<AnyMisbehaviour> for Any {
}
}

impl std::fmt::Display for AnyMisbehaviour {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
impl core::fmt::Display for AnyMisbehaviour {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
match self {
AnyMisbehaviour::Tendermint(tm) => write!(f, "{}", tm),

Expand Down
5 changes: 3 additions & 2 deletions modules/src/ics02_client/msgs/create_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Definition of domain type message `MsgCreateAnyClient`.
use std::convert::TryFrom;
use crate::prelude::*;
use core::convert::TryFrom;

use tendermint_proto::Protobuf;

Expand Down Expand Up @@ -97,7 +98,7 @@ impl From<MsgCreateAnyClient> for RawMsgCreateClient {

#[cfg(test)]
mod tests {
use std::convert::{TryFrom, TryInto};
use core::convert::{TryFrom, TryInto};
use test_env_log::test;

use ibc_proto::ibc::core::client::v1::MsgCreateClient;
Expand Down
3 changes: 2 additions & 1 deletion modules/src/ics02_client/msgs/misbehavior.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryFrom;
use crate::prelude::*;
use core::convert::TryFrom;

use tendermint_proto::Protobuf;

Expand Down
Loading

0 comments on commit 09420bc

Please sign in to comment.