Skip to content

Commit

Permalink
Support no-std
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Sep 3, 2023
1 parent 3e586b5 commit 4ceb2bb
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ jobs:
run: rustup component add clippy
- name: Run clippy
run: cargo clippy -- -D warnings
no-std:
name: no-std lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install clippy
run: rustup component add clippy
- name: Run clippy dlc
run: cargo clippy --no-default-features --features no-std -p dlc -- -D warnings
- name: Run clippy dlc-messages
run: cargo clippy --no-default-features --features no-std -p dlc-messages -- -D warnings
- name: Run clippy dlc-trie
run: cargo clippy --no-default-features --features no-std -p dlc-trie -- -D warnings
unit-tests:
name: unit-tests
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion bitcoin-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ name = "bitcoin-test-utils"
version = "0.1.0"

[dependencies]
bitcoin = {version = "0.29.2"}
bitcoin = { version = "0.29.2", default-features = false }
bitcoincore-rpc = {version = "0.16"}
bitcoincore-rpc-json = {version = "0.16"}
16 changes: 9 additions & 7 deletions dlc-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc-manager
version = "0.4.0"

[features]
default = ["std"]
std = ["dlc/std", "dlc-messages/std", "dlc-trie/std", "bitcoin/std", "lightning/std"]
fuzztarget = ["rand_chacha"]
parallel = ["dlc-trie/parallel"]
use-serde = ["serde", "dlc/use-serde", "dlc-messages/serde", "dlc-trie/use-serde"]

[dependencies]
async-trait = "0.1.50"
bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
dlc-messages = {version = "0.4.0", path = "../dlc-messages"}
dlc-trie = {version = "0.4.0", path = "../dlc-trie"}
lightning = {version = "0.0.116" }
bitcoin = { version = "0.29.2", default-features = false, features = ["secp-recovery", "rand"] }
dlc = { version = "0.4.0", default-features = false, path = "../dlc" }
dlc-messages = { version = "0.4.0", default-features = false, path = "../dlc-messages" }
dlc-trie = { version = "0.4.0", default-features = false, path = "../dlc-trie" }
lightning = { version = "0.0.116", default-features = false, features = ["grind_signatures"] }
log = "0.4.14"
rand_chacha = {version = "0.3.1", optional = true}
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]}
Expand All @@ -31,8 +33,8 @@ bitcoin-test-utils = {path = "../bitcoin-test-utils"}
bitcoincore-rpc = {version = "0.16.0"}
bitcoincore-rpc-json = {version = "0.16.0"}
criterion = "0.4.0"
dlc-manager = {path = ".", features = ["use-serde"]}
dlc-messages = {path = "../dlc-messages", features = ["serde"]}
dlc-manager = { path = ".", default-features = false, features = ["use-serde"] }
dlc-messages = { path = "../dlc-messages", default-features = false, features = ["serde"] }
electrs-blockchain-provider = {path = "../electrs-blockchain-provider"}
env_logger = "0.9.1"
mocks = {path = "../mocks"}
Expand Down
28 changes: 14 additions & 14 deletions dlc-manager/src/contract/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ use dlc_trie::multi_oracle_trie::{MultiOracleTrie, MultiOracleTrieDump};
use dlc_trie::multi_oracle_trie_with_diff::{MultiOracleTrieWithDiff, MultiOracleTrieWithDiffDump};
use dlc_trie::multi_trie::{MultiTrieDump, MultiTrieNodeData, TrieNodeInfo};
use dlc_trie::{OracleNumericInfo, RangeInfo};
use lightning::io::Read;
use lightning::ln::msgs::DecodeError;
use lightning::util::ser::{Readable, Writeable, Writer};
use std::io::Read;

/// Trait used to de/serialize an object to/from a vector of bytes.
pub trait Serializable
where
Self: Sized,
{
/// Serialize the object.
fn serialize(&self) -> Result<Vec<u8>, ::std::io::Error>;
fn serialize(&self) -> Result<Vec<u8>, lightning::io::Error>;
/// Deserialize the object.
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, DecodeError>;
}
Expand All @@ -45,7 +45,7 @@ impl<T> Serializable for T
where
T: Writeable + Readable,
{
fn serialize(&self) -> Result<Vec<u8>, ::std::io::Error> {
fn serialize(&self) -> Result<Vec<u8>, lightning::io::Error> {
let mut buffer = Vec::new();
self.write(&mut buffer)?;
Ok(buffer)
Expand Down Expand Up @@ -160,8 +160,8 @@ impl_dlc_writeable_external!(TrieNodeInfo, trie_node_info, { (trie_index, usize)
fn write_digit_node_data_trie<W: Writer>(
input: &DigitNodeData<Vec<TrieNodeInfo>>,
writer: &mut W,
) -> Result<(), ::std::io::Error> {
let cb = |x: &Vec<TrieNodeInfo>, writer: &mut W| -> Result<(), ::std::io::Error> {
) -> Result<(), lightning::io::Error> {
let cb = |x: &Vec<TrieNodeInfo>, writer: &mut W| -> Result<(), lightning::io::Error> {
write_vec_cb(x, writer, &trie_node_info::write)
};
write_digit_node_data(input, writer, &cb)
Expand All @@ -179,7 +179,7 @@ fn read_digit_node_data_trie<R: Read>(
fn write_digit_node_data_range<W: Writer>(
input: &DigitNodeData<RangeInfo>,
writer: &mut W,
) -> Result<(), ::std::io::Error> {
) -> Result<(), lightning::io::Error> {
write_digit_node_data(input, writer, &range_info::write)
}

Expand All @@ -192,8 +192,8 @@ fn read_digit_node_data_range<R: Read>(
fn write_digit_node_data_vec_range<W: Writer>(
input: &DigitNodeData<Vec<RangeInfo>>,
writer: &mut W,
) -> Result<(), ::std::io::Error> {
let cb = |x: &Vec<RangeInfo>, writer: &mut W| -> Result<(), ::std::io::Error> {
) -> Result<(), lightning::io::Error> {
let cb = |x: &Vec<RangeInfo>, writer: &mut W| -> Result<(), lightning::io::Error> {
write_vec_cb(x, writer, &range_info::write)
};
write_digit_node_data(input, writer, &cb)
Expand All @@ -212,14 +212,14 @@ fn write_digit_node_data<W: Writer, T, F>(
input: &DigitNodeData<T>,
writer: &mut W,
cb: &F,
) -> Result<(), ::std::io::Error>
) -> Result<(), lightning::io::Error>
where
F: Fn(&T, &mut W) -> Result<(), ::std::io::Error>,
F: Fn(&T, &mut W) -> Result<(), lightning::io::Error>,
{
write_option_cb(&input.data, writer, &cb)?;
write_vec_cb(&input.prefix, writer, &write_usize)?;
let cb = |x: &Vec<Option<usize>>, writer: &mut W| -> Result<(), ::std::io::Error> {
let cb = |y: &Option<usize>, writer: &mut W| -> Result<(), ::std::io::Error> {
let cb = |x: &Vec<Option<usize>>, writer: &mut W| -> Result<(), lightning::io::Error> {
let cb = |y: &Option<usize>, writer: &mut W| -> Result<(), lightning::io::Error> {
write_option_cb(y, writer, &write_usize)
};
write_vec_cb(x, writer, &cb)
Expand Down Expand Up @@ -252,7 +252,7 @@ where
fn write_multi_oracle_trie<W: Writer>(
trie: &MultiOracleTrie,
w: &mut W,
) -> Result<(), ::std::io::Error> {
) -> Result<(), lightning::io::Error> {
multi_oracle_trie_dump::write(&trie.dump(), w)
}

Expand All @@ -264,7 +264,7 @@ fn read_multi_oracle_trie<R: Read>(reader: &mut R) -> Result<MultiOracleTrie, De
fn write_multi_oracle_trie_with_diff<W: Writer>(
trie: &MultiOracleTrieWithDiff,
w: &mut W,
) -> Result<(), ::std::io::Error> {
) -> Result<(), lightning::io::Error> {
multi_oracle_trie_with_diff_dump::write(&trie.dump(), w)
}

Expand Down
6 changes: 3 additions & 3 deletions dlc-manager/src/conversion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use dlc_messages::{
oracle_msgs::EventDescriptor,
};
use dlc_trie::OracleNumericInfo;
use std::error;
use std::fmt;

pub(crate) const BITCOIN_CHAINHASH: [u8; 32] = [
Expand All @@ -54,8 +53,9 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
#[cfg(not(feature = "no-std"))]
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
match *self {
Error::BitcoinEncoding(ref e) => Some(e),
Error::InvalidParameters => None,
Expand Down
7 changes: 4 additions & 3 deletions dlc-manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum Error {
/// representation.
Conversion(crate::conversion_utils::Error),
/// An IO error.
IOError(std::io::Error),
IOError(lightning::io::Error),
/// Some invalid parameters were provided.
InvalidParameters(String),
/// An invalid state was encounter, likely to indicate a bug.
Expand Down Expand Up @@ -44,8 +44,8 @@ impl fmt::Display for Error {
}
}

impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
impl From<lightning::io::Error> for Error {
fn from(e: lightning::io::Error) -> Error {
Error::IOError(e)
}
}
Expand Down Expand Up @@ -74,6 +74,7 @@ impl From<secp256k1_zkp::UpstreamError> for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
14 changes: 8 additions & 6 deletions dlc-messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ repository = "https://github.com/p2pderivatives/rust-dlc/tree/master/dlc-message
version = "0.4.0"

[features]
default = ["std"]
std = ["dlc/std", "bitcoin/std", "lightning/std"]
no-std = ["bitcoin/no-std", "dlc/no-std", "lightning/no-std"]
use-serde = ["serde", "secp256k1-zkp/use-serde"]

[dependencies]
bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
lightning = {version = "0.0.116" }
bitcoin = { version = "0.29.2", default-features = false, features = ["secp-recovery", "rand"] }
dlc = { version = "0.4.0", path = "../dlc", default-features = false }
lightning = { version = "0.0.116", default-features = false, features = ["grind_signatures"] }
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]}
serde = {version = "1.0", features = ["derive"], optional = true}

[dev-dependencies]
bitcoin = {version = "0.29.2"}
bitcoin-test-utils = {path = "../bitcoin-test-utils"}
dlc-messages = {path = "./", features = ["use-serde"]}
bitcoin = { version = "0.29.2", default-features = false, features = ["serde"] }
dlc-messages = {path = "./", default-features = false, features = ["use-serde"]}
secp256k1-zkp = {version = "0.7.0", features = ["use-serde", "global-context"]}
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
4 changes: 1 addition & 3 deletions dlc-messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ extern crate secp256k1_zkp;
pub mod ser_macros;
pub mod ser_impls;

#[cfg(test)]
extern crate bitcoin_test_utils;
#[cfg(any(test, feature = "serde"))]
extern crate serde;

Expand Down Expand Up @@ -532,7 +530,7 @@ macro_rules! impl_type_writeable_for_enum {
}

impl Writeable for $type_name {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::lightning::io::Error> {
match self {
$($type_name::$variant_name(v) => v.write(writer),)*
}
Expand Down
10 changes: 4 additions & 6 deletions dlc-messages/src/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use std::{
collections::{HashMap, VecDeque},
fmt::Display,
io::Cursor,
sync::Mutex,
};

use lightning::ln::features::{InitFeatures, NodeFeatures};
use lightning::{
io::Cursor,
ln::{
msgs::{DecodeError, LightningError},
peer_handler::CustomMessageHandler,
Expand Down Expand Up @@ -94,7 +94,7 @@ macro_rules! handle_read_dlc_messages {
}};
}

fn read_dlc_message<R: ::std::io::Read>(
fn read_dlc_message<R: ::lightning::io::Read>(
msg_type: u16,
mut buffer: &mut R,
) -> Result<Option<WireMessage>, DecodeError> {
Expand Down Expand Up @@ -123,7 +123,7 @@ fn read_dlc_message<R: ::std::io::Read>(
/// custom messages in the LDK.
impl CustomMessageReader for MessageHandler {
type CustomMessage = WireMessage;
fn read<R: ::std::io::Read>(
fn read<R: ::lightning::io::Read>(
&self,
msg_type: u16,
mut buffer: &mut R,
Expand Down Expand Up @@ -151,9 +151,7 @@ impl CustomMessageHandler for MessageHandler {
org: &PublicKey,
) -> Result<(), LightningError> {
let mut segment_readers = self.segment_readers.lock().unwrap();
let segment_reader = segment_readers
.entry(*org)
.or_insert_with(SegmentReader::new);
let segment_reader = segment_readers.entry(*org).or_default();

if segment_reader.expecting_chunk() {
match msg {
Expand Down
1 change: 1 addition & 0 deletions dlc-messages/src/segmentation/segment_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl std::fmt::Display for Error {
}
}

#[cfg(not(feature = "no-std"))]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down
Loading

0 comments on commit 4ceb2bb

Please sign in to comment.