Skip to content

Commit

Permalink
Make api/rpc interfaces no_std compatible (#384)
Browse files Browse the repository at this point in the history
* make rpc interface no-std compatible

* move feature flag below doc
  • Loading branch information
haerdib authored Dec 20, 2022
1 parent 4365929 commit add60a5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 25 deletions.
23 changes: 21 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ log = { version = "0.4.14", default-features = false }
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.79", default-features = false }

#FIXME: convert back to thiserror once possible #317
thiserror = { version = "1.0", package = "thiserror-core", default-features = false }

# crates.io std only
primitive-types = { version = "0.12.1", optional = true, features = ["codec"] }
thiserror = { version = "1.0.30", optional = true }
url = { version = "2.0.0", optional = true }

# websocket dependent features
Expand Down Expand Up @@ -70,9 +71,8 @@ std = [
"log/std",
"serde/std",
"serde_json/std",
"thiserror/std",
# crates.io std only
"primitive-types",
"thiserror",
"url",
# substrate no_std
"frame-metadata/std",
Expand Down
3 changes: 0 additions & 3 deletions compose-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

// re-export for macro resolution
pub use ac_primitives as primitives;
#[cfg(feature = "std")]
pub use codec;
#[cfg(feature = "std")]
pub use log;
pub use rpc::*;
pub use sp_core;
Expand Down Expand Up @@ -98,7 +96,6 @@ macro_rules! compose_extrinsic_offline {
/// * 'args' - Optional sequence of arguments of the call. They are not checked against the metadata.
/// As of now the user needs to check himself that the correct arguments are supplied.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! compose_extrinsic {
($api: expr,
$module: expr,
Expand Down
8 changes: 7 additions & 1 deletion primitives/src/rpc_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl RpcParams {
}

/// Insert a plain value into the builder.
#[cfg(feature = "std")]
pub fn insert<P: Serialize>(&mut self, value: P) -> Result<()> {
self.0.insert(value)
}
Expand Down Expand Up @@ -145,6 +144,13 @@ impl ParamsBuilder {
Ok(())
}

/// Insert a plain value into the builder with heap allocation. If available,
/// use the more efficient std version.
#[cfg(not(feature = "std"))]
pub(crate) fn insert<P: Serialize>(&mut self, value: P) -> Result<()> {
self.insert_with_allocation(value)
}

/// Insert a plain value into the builder with heap allocation. For better performance,
/// use the std version, if possible.
pub(crate) fn insert_with_allocation<P: Serialize>(&mut self, value: P) -> Result<()> {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
*/
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(assert_matches)]
#![feature(error_in_core)]

extern crate alloc;

pub use ac_compose_macros::*;
pub use ac_node_api::*;
pub use ac_primitives::*;
pub use utils::*;

pub mod rpc;
pub mod utils;

// std only features:
Expand All @@ -33,5 +37,3 @@ pub use api::*;
pub mod api;
#[cfg(feature = "std")]
pub mod extrinsic;
#[cfg(feature = "std")]
pub mod rpc;
50 changes: 41 additions & 9 deletions src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@
*/

use std::sync::mpsc::SendError;
use alloc::{boxed::Box, string::String};

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Serde json error: {0}")]
Serde(#[from] serde_json::error::Error),
Serde(serde_json::error::Error),
#[error("mpsc send Error: {0}")]
Send(String),
#[error("Could not convert to valid Url: {0}")]
Url(#[from] url::ParseError),
Url(String),
#[error("ChannelReceiveError, sender is disconnected: {0}")]
ChannelDisconnected(#[from] sp_std::sync::mpsc::RecvError),
ChannelDisconnected(String),
#[error("Failure during thread creation: {0}")]
Io(#[from] std::io::Error),
Io(String),
#[error("Exceeded maximum amount of connections")]
ConnectionAttemptsExceeded,
#[error("Websocket Connection was closed unexpectedly")]
ConnectionClosed,
#[error(transparent)]
Client(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
Client(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
}

impl From<SendError<String>> for Error {
fn from(error: SendError<String>) -> Self {
Self::Send(error.0)
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Self {
Self::Serde(error)
}
}

Expand All @@ -58,3 +58,35 @@ impl From<tungstenite::Error> for Error {
Self::Client(Box::new(error))
}
}

#[cfg(feature = "std")]
pub use std_only::*;
#[cfg(feature = "std")]
mod std_only {
use super::*;
use std::sync::mpsc::{RecvError, SendError};

impl From<SendError<String>> for Error {
fn from(error: SendError<String>) -> Self {
Self::Send(error.0)
}
}

impl From<RecvError> for Error {
fn from(error: RecvError) -> Self {
Self::ChannelDisconnected(format!("{:?}", error))
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Io(format!("{:?}", error))
}
}

impl From<url::ParseError> for Error {
fn from(error: url::ParseError) -> Self {
Self::Io(format!("{:?}", error))
}
}
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod error;
pub use error::*;

use ac_primitives::RpcParams;
use alloc::string::{String, ToString};
use serde::de::DeserializeOwned;

/// Trait to be implemented by the ws-client for sending rpc requests and extrinsic.
Expand Down
4 changes: 0 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

use hex::FromHexError;
use sp_core::{storage::StorageKey, twox_128, H256};

Expand Down

0 comments on commit add60a5

Please sign in to comment.