Skip to content

Commit

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

* Disable default-features for all dependencies

* Fix features

* Fix more features

* Fix lightclient-js test

* Disable "ed25519-dalek/serde" feature as it does not support std

* Remove "subtle-encoding/std" feature from tendermint-proto

* Merge latest changes from master and fix conflicts

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* testgen: Remove unnecessary return

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* testgen: Enable serde_json/std feature to fix error

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* proto: Fix formatting in docstring

Signed-off-by: Thane Thomson <connect@thanethomson.com>

* Remove default std features in tendermint crates

* Add default-features = false for tendermint-config

* Fix build errors and update flex-error version

* Disable use of std in tendermint and tendermint-proto crates

* Fix formatting

* Enable flex-error/std and flex-error/eyre_tracer by default in crates

* proto: Restructure serializer docs into table

Signed-off-by: Thane Thomson <connect@thanethomson.com>

Co-authored-by: Thane Thomson <connect@thanethomson.com>
  • Loading branch information
soareschen and thanethomson authored Oct 26, 2021
1 parent ce55f22 commit 5bf6bae
Show file tree
Hide file tree
Showing 128 changed files with 477 additions and 316 deletions.
8 changes: 2 additions & 6 deletions abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,17 @@ path = "src/application/kvstore/main.rs"
required-features = [ "binary", "kvstore-app" ]

[features]
default = ["std", "eyre_tracer"]
eyre_tracer = ["flex-error/eyre_tracer"]
default = ["flex-error/std", "flex-error/eyre_tracer"]
client = []
echo-app = []
kvstore-app = []
binary = [ "structopt", "tracing-subscriber" ]
std = [
"flex-error/std"
]

[dependencies]
bytes = { version = "1.0", default-features = false }
prost = { version = "0.9", default-features = false }
tendermint-proto = { version = "0.23.0-internal", default-features = false, path = "../proto" }
tracing = { version = "0.1", default-features = false }
flex-error = { version = "0.4.3", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
structopt = { version = "0.3", optional = true, default-features = false }
tracing-subscriber = { version = "0.2", optional = true, default-features = false }
9 changes: 1 addition & 8 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
tendermint = { version = "0.23.0-internal", default-features = false, path = "../tendermint" }
flex-error = { version = "0.4.1", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = { version = "0.5" }
url = { version = "2.2" }

[dev-dependencies]
pretty_assertions = "0.7.2"

[features]
default = ["std", "eyre_tracer"]
eyre_tracer = ["flex-error/eyre_tracer"]
std = [
"flex-error/std"
]
13 changes: 6 additions & 7 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ use crate::net;
use crate::node_key::NodeKey;
use crate::Error;

use crate::prelude::*;
use alloc::collections::{btree_map, BTreeMap};
use core::{fmt, str::FromStr};
use serde::{de, de::Error as _, ser, Deserialize, Serialize};
use std::{
collections::BTreeMap,
fmt, fs,
path::{Path, PathBuf},
str::FromStr,
};
use std::fs;
use std::path::{Path, PathBuf};
use tendermint::{genesis::Genesis, node, Moniker, Timeout};

/// Tendermint `config.toml` file
Expand Down Expand Up @@ -188,7 +187,7 @@ impl LogLevel {
}

/// Iterator over log levels
pub type LogLevelIter<'a> = std::collections::btree_map::Iter<'a, String, String>;
pub type LogLevelIter<'a> = btree_map::Iter<'a, String, String>;

impl FromStr for LogLevel {
type Err = Error;
Expand Down
3 changes: 3 additions & 0 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
)]
#![forbid(unsafe_code)]

extern crate alloc;

pub mod net;

mod config;
mod error;
mod node_key;
mod prelude;
mod priv_validator_key;

pub use config::*;
Expand Down
5 changes: 3 additions & 2 deletions config/src/net.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Remote addresses (`tcp://` or `unix://`)

use crate::error::Error;
use crate::prelude::*;

use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use std::{
use core::{
fmt::{self, Display},
str::{self, FromStr},
};
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use tendermint::node::{self, info::ListenAddress};
use url::Url;

Expand Down
1 change: 1 addition & 0 deletions config/src/node_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Node keys

use crate::prelude::*;
use serde::{Deserialize, Serialize};
use std::{fs, path::Path};
use tendermint::{node, private_key::PrivateKey, public_key::PublicKey};
Expand Down
11 changes: 11 additions & 0 deletions config/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub use core::prelude::v1::*;

// Re-export according to alloc::prelude::v1 because it is not yet stabilized
// https://doc.rust-lang.org/src/alloc/prelude/v1.rs.html
pub use alloc::borrow::ToOwned;
pub use alloc::boxed::Box;
pub use alloc::string::{String, ToString};
pub use alloc::vec::Vec;

pub use alloc::format;
pub use alloc::vec;
1 change: 1 addition & 0 deletions config/src/priv_validator_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Validator private keys

use crate::prelude::*;
use serde::{Deserialize, Serialize};
use std::{fs, path::Path};
use tendermint::public_key::TendermintKey;
Expand Down
18 changes: 9 additions & 9 deletions light-client-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]

[dependencies]
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
serde = { version = "1.0", default-features = false, features = [ "derive" ] }
serde_json = { version = "1.0", default-features = false }
# TODO(thane): Remove once https://github.com/rustwasm/wasm-bindgen/issues/2508 is resolved
syn = "=1.0.65"
tendermint = { version = "0.23.0-internal", path = "../tendermint" }
tendermint-light-client = { version = "0.23.0-internal", path = "../light-client", default-features = false }
wasm-bindgen = { version = "0.2.63", features = [ "serde-serialize" ] }
syn = { version = "=1.0.65", default-features = false }
tendermint = { version = "0.23.0-internal", default-features = false, path = "../tendermint" }
tendermint-light-client = { version = "0.23.0-internal", default-features = false, path = "../light-client" }
wasm-bindgen = { version = "0.2.63", default-features = false, features = [ "serde-serialize" ] }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }
console_error_panic_hook = { version = "0.1.6", default-features = false, optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
wee_alloc = { version = "0.4.5", default-features = false, optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
wasm-bindgen-test = { version = "0.3.13", default-features = false }
46 changes: 23 additions & 23 deletions light-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,40 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["std", "eyre_tracer", "rpc-client"]
eyre_tracer = ["flex-error/eyre_tracer"]
default = ["rpc-client", "flex-error/std", "flex-error/eyre_tracer"]
rpc-client = ["tokio", "tendermint-rpc/http-client"]
secp256k1 = ["tendermint/secp256k1", "tendermint-rpc/secp256k1"]
lightstore-sled = ["sled"]
unstable = []
std = [
"flex-error/std"
]
# Enable to execute long-running model-based tests
mbt = []

[dependencies]
tendermint = { version = "0.23.0-internal", path = "../tendermint" }
tendermint = { version = "0.23.0-internal", path = "../tendermint", default-features = false }
tendermint-rpc = { version = "0.23.0-internal", path = "../rpc", default-features = false }

contracts = { version = "0.4.0", default-features = false }
chrono = { version = "0.4", default-features = false, features = ["clock"] }
contracts = "0.4.0"
crossbeam-channel = "0.4.2"
derive_more = "0.99.5"
futures = "0.3.4"
serde = "1.0.106"
serde_cbor = "0.11.1"
serde_derive = "1.0.106"
sled = { version = "0.34.3", optional = true }
static_assertions = "1.1.0"
tokio = { version = "1.0", features = ["rt"], optional = true }
flex-error = { version = "0.4.1", default-features = false }
crossbeam-channel = { version = "0.4.2", default-features = false }
derive_more = { version = "0.99.5", default-features = false, features = ["display"] }
futures = { version = "0.3.4", default-features = false }
serde = { version = "1.0.106", default-features = false }
serde_cbor = { version = "0.11.1", default-features = false, features = ["alloc", "std"] }
serde_derive = { version = "1.0.106", default-features = false }
sled = { version = "0.34.3", optional = true, default-features = false }
static_assertions = { version = "1.1.0", default-features = false }
tokio = { version = "1.0", default-features = false, features = ["rt"], optional = true }
flex-error = { version = "0.4.4", default-features = false }

[dev-dependencies]
tendermint-testgen = { path = "../testgen" }
tendermint-testgen = { path = "../testgen", default-features = false }

serde_json = { version = "1.0.51", default-features = false }
gumdrop = { version = "0.8.0", default-features = false }
rand = { version = "0.7.3", default-features = false }
tempfile = { version = "3.2.0", default-features = false }
proptest = { version = "0.10.1", default-features = false, features = ["std"] }

serde_json = "1.0.51"
gumdrop = "0.8.0"
rand = "0.7.3"
tempfile = "3.2.0"
proptest = "0.10.1"
[[example]]
name = "light_client"
required-features = ["rpc-client", "tendermint-rpc/http-client", "flex-error/std"]
14 changes: 5 additions & 9 deletions p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ description = """
test = false

[features]
default = ["std", "eyre_tracer"]
eyre_tracer = ["flex-error/eyre_tracer"]
default = ["flex-error/std", "flex-error/eyre_tracer"]
amino = ["prost-derive"]
std = [
"flex-error/std"
]

[dependencies]
chacha20poly1305 = { version = "0.8", default-features = false, features = ["reduced-round"] }
Expand All @@ -45,12 +41,12 @@ x25519-dalek = { version = "1.1", default-features = false }
zeroize = { version = "1", default-features = false }
signature = { version = "1.3.0", default-features = false }
aead = { version = "0.4.1", default-features = false }
flex-error = { version = "0.4.3", default-features = false }
flex-error = { version = "0.4.4", default-features = false }

# path dependencies
tendermint = { path = "../tendermint", version = "0.23.0-internal" }
tendermint-proto = { path = "../proto", version = "0.23.0-internal" }
tendermint-std-ext = { path = "../std-ext", version = "0.23.0-internal" }
tendermint = { path = "../tendermint", version = "0.23.0-internal", default-features = false }
tendermint-proto = { path = "../proto", version = "0.23.0-internal", default-features = false }
tendermint-std-ext = { path = "../std-ext", version = "0.23.0-internal", default-features = false }

# optional dependencies
prost-derive = { version = "0.9", optional = true }
4 changes: 2 additions & 2 deletions pbt-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ default = ["time"]
time = ["chrono"]

[dependencies]
chrono = { version = "0.4", features = ["serde"], optional = true}
proptest = "0.10.1"
chrono = { version = "0.4", default-features = false, features = ["serde"], optional = true}
proptest = { version = "0.10.1", default-features = false, features = ["std"] }
11 changes: 2 additions & 9 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ subtle-encoding = { version = "0.5", default-features = false, features = ["hex"
num-traits = { version = "0.2", default-features = false }
num-derive = { version = "0.3", default-features = false }
chrono = { version = "0.4", default-features = false, features = ["serde", "alloc"] }
flex-error = { version = "0.4.3", default-features = false }
flex-error = { version = "0.4.4", default-features = false }

[dev-dependencies]
serde_json = "1.0"

[features]
default = ["std", "eyre_tracer"]
eyre_tracer = ["flex-error/eyre_tracer"]
std = [
"flex-error/std"
]
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
7 changes: 4 additions & 3 deletions proto/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! This module defines the various errors that be raised during Protobuf conversions.

use crate::prelude::*;
use core::convert::TryFrom;
use core::fmt::Display;
use core::num::TryFromIntError;
use flex_error::{define_error, DisplayOnly};
use prost::{DecodeError, EncodeError};
use std::convert::TryFrom;
use std::fmt::Display;
use std::num::TryFromIntError;

define_error! {
Error {
Expand Down
13 changes: 10 additions & 3 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! tendermint-proto library gives the developer access to the Tendermint proto-defined structs.

#![no_std]
#![deny(warnings, trivial_casts, trivial_numeric_casts, unused_import_braces)]
#![allow(clippy::large_enum_variant)]
#![forbid(unsafe_code)]
#![doc(html_root_url = "https://docs.rs/tendermint-proto/0.23.0-internal")]

extern crate alloc;

mod prelude;

/// Built-in prost_types with slight customization to enable JSON-encoding
#[allow(warnings)]
pub mod google {
Expand All @@ -23,13 +28,15 @@ pub use error::Error;
pub use tendermint::*;

use bytes::{Buf, BufMut};
use core::convert::{TryFrom, TryInto};
use core::fmt::Display;
use prost::encoding::encoded_len_varint;
use prost::Message;
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;

pub mod serializers;

use prelude::*;

/// Allows for easy Google Protocol Buffers encoding and decoding of domain
/// types with validation.
///
Expand All @@ -38,7 +45,7 @@ pub mod serializers;
/// ```rust
/// use bytes::BufMut;
/// use prost::Message;
/// use std::convert::TryFrom;
/// use core::convert::TryFrom;
/// use tendermint_proto::Protobuf;
///
/// // This struct would ordinarily be automatically generated by prost.
Expand Down
11 changes: 11 additions & 0 deletions proto/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub use core::prelude::v1::*;

// Re-export according to alloc::prelude::v1 because it is not yet stabilized
// https://doc.rust-lang.org/src/alloc/prelude/v1.rs.html
pub use alloc::borrow::ToOwned;
pub use alloc::boxed::Box;
pub use alloc::string::{String, ToString};
pub use alloc::vec::Vec;

pub use alloc::format;
pub use alloc::vec;
20 changes: 14 additions & 6 deletions proto/src/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,25 @@
//! ```
//!
//! Available serializers:
//! i64 <-> string: #[serde(with="serializers::from_str")]
//! u64 <-> string: #[serde(with="serializers::from_str")]
//! std::time::Duration <-> nanoseconds as string #[serde(with="serializers::time_duration")]
//! Vec<u8> <-> HexString: #[serde(with="serializers::bytes::hexstring")]
//! Vec<u8> <-> Base64String: #[serde(with="serializers::bytes::base64string")]
//! Vec<u8> <-> String: #[serde(with="serializers::bytes::string")]
//!
//! | Field Type | String Format | Serializer |
//! |--------------|-------------------------------|-------------------|
//! | `i64` | e.g. `-5` | [`from_str`] |
//! | `u64` | e.g. `100` | [`from_str`] |
//! | [`Duration`] | Nanoseconds (e.g. `100`) | [`time_duration`] |
//! | `Vec<u8>` | Hexadecimal (e.g. `1AF2B3C4`) | [`hexstring`] |
//! | `Vec<u8>` | Base64-encoded | [`base64string`] |
//! | `Vec<u8>` | Raw bytes in string | [`string`] |
//!
//! Notes:
//! * Any type that has the "FromStr" trait can be serialized into a string with
//! serializers::primitives::string.
//! * serializers::bytes::* deserializes a null value into an empty vec![].
//!
//! [`Duration`]: core::time::Duration
//! [`hexstring`]: bytes::hexstring
//! [`base64string`]: bytes::base64string
//! [`string`]: bytes::string

// Todo: remove dead_code allowance as soon as more types are implemented
#![allow(dead_code)]
Expand Down
Loading

0 comments on commit 5bf6bae

Please sign in to comment.