Skip to content

Commit

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

* 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 0ba0a5d commit 1062dc8
Show file tree
Hide file tree
Showing 72 changed files with 152 additions and 135 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
2 changes: 1 addition & 1 deletion relayer-cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fn is_ci_release() -> bool {
}

mod git {
use core::marker::PhantomData;
use std::ffi::OsStr;
use std::marker::PhantomData;
use std::process::{Command, Output};

// A wrapper over a git shell command that is only constructable if git is available & the
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/cli_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;
use tokio::runtime::Runtime as TokioRuntime;

use ibc::ics02_client::client_state::ClientState;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/create/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use abscissa_core::{Command, Options, Runnable};

Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::str::FromStr;
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
};

use abscissa_core::{Command, Options, Runnable};
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/keys/list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use alloc::collections::btree_map::BTreeMap as HashMap;

use abscissa_core::{Command, Options, Runnable};

Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/keys/restore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use core::str::FromStr;

use abscissa_core::{Command, Options, Runnable};

Expand Down
4 changes: 3 additions & 1 deletion relayer-cli/src/commands/listen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{fmt, ops::Deref, str::FromStr, sync::Arc, thread};
use alloc::sync::Arc;
use core::{fmt, ops::Deref, str::FromStr};
use std::thread;

use abscissa_core::{application::fatal_error, Command, Options, Runnable};
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/channel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;

use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/channels.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{Debug, Error, Formatter};
use core::fmt::{Debug, Error, Formatter};

use abscissa_core::{Options, Runnable};
use serde::Serialize;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;

use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/clients.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;

use abscissa_core::{Command, Options, Runnable};
use serde::Serialize;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;

use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/query/connections.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use alloc::sync::Arc;

use abscissa_core::{Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
2 changes: 0 additions & 2 deletions relayer-cli/src/commands/query/packet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//! `query packet` subcommands

use abscissa_core::{Command, Options, Runnable};

mod ack;
Expand Down
4 changes: 2 additions & 2 deletions relayer-cli/src/commands/query/tx/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::str::FromStr;
use std::sync::Arc;
use alloc::sync::Arc;
use core::str::FromStr;

use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
3 changes: 2 additions & 1 deletion relayer-cli/src/commands/start.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::sync::Arc;
use std::error::Error;
use std::io;
use std::sync::{Arc, RwLock};
use std::sync::RwLock;

use abscissa_core::{Command, Options, Runnable};
use crossbeam_channel::Sender;
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/tx/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use core::fmt;

use abscissa_core::{config, Command, Options, Runnable};

Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/commands/tx/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl TxIcs20MsgTransferCmd {
denom,
receiver: self.receiver.clone(),
timeout_height_offset: self.timeout_height_offset,
timeout_seconds: std::time::Duration::from_secs(self.timeout_seconds),
timeout_seconds: core::time::Duration::from_secs(self.timeout_seconds),
number_msgs,
};

Expand Down
4 changes: 2 additions & 2 deletions relayer-cli/src/commands/tx/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::Arc;
use std::time::Duration;
use alloc::sync::Arc;
use core::time::Duration;

use abscissa_core::{Command, Options, Runnable};
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
8 changes: 4 additions & 4 deletions relayer-cli/src/conclude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
//! Output::success(h).with_result(end).exit();
//! ```

use std::fmt;
use core::fmt;

use serde::Serialize;
use tracing::warn;
Expand Down Expand Up @@ -170,7 +170,7 @@ impl Output {
/// Builder-style method for attaching a result to an output object.
pub fn with_result<R>(mut self, result: R) -> Self
where
R: Serialize + std::fmt::Debug + 'static,
R: Serialize + core::fmt::Debug + 'static,
{
if json() {
self.result = Result::Json(serialize_result(result));
Expand All @@ -191,7 +191,7 @@ impl Output {
/// input `result`.
pub fn success<R>(result: R) -> Self
where
R: Serialize + std::fmt::Debug + 'static,
R: Serialize + core::fmt::Debug + 'static,
{
Output::with_success().with_result(result)
}
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Output {
}

/// Helper to serialize a result into a `serde_json::Value`.
fn serialize_result(res: impl Serialize + std::fmt::Debug) -> serde_json::Value {
fn serialize_result(res: impl Serialize + core::fmt::Debug) -> serde_json::Value {
let last_resort = format!("{:#?}", res);

match serde_json::to_value(res) {
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! application's configuration file and/or command-line options
//! for specifying it.

use std::collections::BTreeSet;
use alloc::collections::BTreeSet;
use std::path::PathBuf;

use flex_error::{define_error, TraceError};
Expand Down
2 changes: 2 additions & 0 deletions relayer-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
unused_qualifications
)]

extern crate alloc;

pub mod application;
pub mod commands;
pub mod config;
Expand Down
3 changes: 1 addition & 2 deletions relayer/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::sync::Arc;

use alloc::sync::Arc;
use prost_types::Any;
use tendermint::block::Height;
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
10 changes: 5 additions & 5 deletions relayer/src/chain/cosmos.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{
use alloc::sync::Arc;
use core::{
cmp::min,
convert::{TryFrom, TryInto},
future::Future,
str::FromStr,
sync::Arc,
thread,
time::{Duration, Instant},
time::Duration,
};
use std::{thread, time::Instant};

use bech32::{ToBase32, Variant};
use bitcoin::hashes::hex::ToHex;
Expand Down Expand Up @@ -96,7 +96,7 @@ pub const GENESIS_MAX_BYTES_MAX_FRACTION: f64 = 0.9;

mod retry_strategy {
use crate::util::retry::Fixed;
use std::time::Duration;
use core::time::Duration;

pub fn wait_for_block_commits(max_total_wait: Duration) -> impl Iterator<Item = Duration> {
let backoff_millis = 300; // The periodic backoff
Expand Down
4 changes: 2 additions & 2 deletions relayer/src/chain/cosmos/compatibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct AppInfo {
git_commit: String,
}

impl std::fmt::Display for AppInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for AppInfo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}:{}-{}", self.app_name, self.version, self.git_commit)
}
}
Expand Down
6 changes: 2 additions & 4 deletions relayer/src/chain/handle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
fmt::{self, Debug},
sync::Arc,
};
use alloc::sync::Arc;
use core::fmt::{self, Debug};

use crossbeam_channel as channel;
use ibc::ics03_connection::connection::IdentifiedConnectionEnd;
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/chain/handle/prod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Debug;
use core::fmt::Debug;

use crossbeam_channel as channel;
use serde::{Serialize, Serializer};
Expand Down
10 changes: 5 additions & 5 deletions relayer/src/chain/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Add;
use std::sync::Arc;
use std::time::Duration;
use alloc::sync::Arc;
use core::ops::Add;
use core::time::Duration;

use crossbeam_channel as channel;
use prost_types::Any;
Expand Down Expand Up @@ -396,8 +396,8 @@ impl ChainEndpoint for MockChain {
// For integration tests with the modules
#[cfg(test)]
pub mod test_utils {
use std::str::FromStr;
use std::time::Duration;
use core::str::FromStr;
use core::time::Duration;

use ibc::ics24_host::identifier::ChainId;

Expand Down
3 changes: 2 additions & 1 deletion relayer/src/chain/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{sync::Arc, thread};
use alloc::sync::Arc;
use std::thread;

use crossbeam_channel as channel;
use tokio::runtime::Runtime as TokioRuntime;
Expand Down
4 changes: 2 additions & 2 deletions relayer/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![allow(clippy::borrowed_box)]

use core::time::Duration;
use prost_types::Any;
use serde::Serialize;
use std::time::Duration;
use tracing::{debug, error, info, warn};

use ibc::events::IbcEvent;
Expand Down Expand Up @@ -31,7 +31,7 @@ pub mod error;
pub use error::ChannelError;

mod retry_strategy {
use std::time::Duration;
use core::time::Duration;

use retry::delay::Fibonacci;

Expand Down
2 changes: 1 addition & 1 deletion relayer/src/channel/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::time::Duration;
use flex_error::define_error;
use ibc::events::IbcEvent;
use ibc::ics02_client::error::Error as ClientError;
use ibc::ics04_channel::channel::State;
use ibc::ics24_host::identifier::{ChainId, ChannelId, ClientId, PortChannelId, PortId};
use std::time::Duration;

use crate::error::Error;
use crate::foreign_client::ForeignClientError;
Expand Down
6 changes: 4 additions & 2 deletions relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
pub mod reload;
pub mod types;

use std::collections::{HashMap, HashSet};
use std::{fmt, fs, fs::File, io::Write, path::Path, time::Duration};
use alloc::collections::BTreeMap as HashMap;
use alloc::collections::BTreeSet as HashSet;
use core::{fmt, time::Duration};
use std::{fs, fs::File, io::Write, path::Path};

use serde_derive::{Deserialize, Serialize};
use tendermint_light_client::types::TrustThreshold;
Expand Down
6 changes: 2 additions & 4 deletions relayer/src/config/reload.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Facility for reloading the relayer configuration.

use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
use alloc::sync::Arc;
use std::{path::PathBuf, sync::RwLock};

use crossbeam_channel::Sender;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Duration;
use core::time::Duration;

use crate::chain::counterparty::connection_state_on_destination;
use crate::util::retry::RetryResult;
Expand Down
4 changes: 2 additions & 2 deletions relayer/src/event/bus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::VecDeque;
use alloc::collections::VecDeque;

use crossbeam_channel as channel;

Expand Down Expand Up @@ -49,8 +49,8 @@ impl<T> EventBus<T> {
mod tests {
use super::EventBus;

use core::sync::atomic::{AtomicUsize, Ordering};
use serial_test::serial;
use std::sync::atomic::{AtomicUsize, Ordering};
use test_env_log::test;

static COUNTER: AtomicUsize = AtomicUsize::new(0);
Expand Down
Loading

0 comments on commit 1062dc8

Please sign in to comment.