Skip to content

Commit fa0d0bb

Browse files
committed
no_std ergo-lib, make ergo-merkle-tree and ergo-nipopow optional
1 parent 51c632d commit fa0d0bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+267
-207
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ derive_more = { version = "0.99", features = [
5555
] }
5656
num-derive = "0.4.2"
5757
thiserror = { version = "2.0.1", default-features = false }
58-
bounded-vec = { git = "https://github.com/SethDusek/bounded-vec", branch = "no_std" } # TODO: merge no_std support in bounded-vec
58+
bounded-vec = { git = "https://github.com/SethDusek/bounded-vec", branch = "no_std" } # TODO: merge no_std support in bounded-vec upstream
5959
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
6060
blake2 = { version = "0.10.6", default-features = false }
6161
sha2 = { version = "0.10", default-features = false }
@@ -93,7 +93,7 @@ core2 = { version = "0.4.0", default-features = false, features = ["alloc"] }
9393
proptest = { version = "1.5.0", default-features = false, features = [
9494
"alloc",
9595
"std",
96-
] } # TODO: consider enabling std
96+
] }
9797
proptest-derive = "0.3"
9898
pretty_assertions = "1.3"
9999
wasm-bindgen-test = "0.3.37"

bindings/ergo-lib-c-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ futures-util = "0.3"
2222
url = { workspace = true }
2323
bounded-integer = { workspace = true }
2424
serde_with = { workspace = true }
25-
bounded-vec = { workspace = true, features=["serde"] }
25+
bounded-vec = { workspace = true, features = ["serde"] }
2626

2727
[features]
2828
default = ["mnemonic_gen", "ergo-lib/compiler"]

bindings/ergo-lib-c-core/src/transaction.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub unsafe fn hints_bag_get(
7979
}
8080

8181
/// TransactionHintsBag
82-
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
82+
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);
8383
pub type TransactionHintsBagPtr = *mut TransactionHintsBag;
8484
pub type ConstTransactionHintsBagPtr = *const TransactionHintsBag;
8585

@@ -90,7 +90,7 @@ pub unsafe fn transaction_hints_bag_empty(
9090
let transaction_hints_bag_out =
9191
mut_ptr_as_mut(transaction_hints_bag_out, "transaction_hints_bag_out")?;
9292
*transaction_hints_bag_out = Box::into_raw(Box::new(TransactionHintsBag(
93-
ergo_lib::wallet::multi_sig::TransactionHintsBag::empty(),
93+
ergo_lib::wallet::TransactionHintsBag::empty(),
9494
)));
9595
Ok(())
9696
}

bindings/ergo-lib-wasm/src/transaction.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ impl From<ergo_lib::ergotree_interpreter::sigma_protocol::prover::hint::HintsBag
7676

7777
/// TransactionHintsBag
7878
#[wasm_bindgen]
79-
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
79+
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);
8080

8181
#[wasm_bindgen]
8282
impl TransactionHintsBag {
8383
/// Empty TransactionHintsBag
8484
pub fn empty() -> TransactionHintsBag {
85-
TransactionHintsBag(ergo_lib::wallet::multi_sig::TransactionHintsBag::empty())
85+
TransactionHintsBag(ergo_lib::wallet::TransactionHintsBag::empty())
8686
}
8787

8888
/// Adding hints for input
@@ -106,8 +106,8 @@ impl TransactionHintsBag {
106106
}
107107
}
108108

109-
impl From<ergo_lib::wallet::multi_sig::TransactionHintsBag> for TransactionHintsBag {
110-
fn from(t: ergo_lib::wallet::multi_sig::TransactionHintsBag) -> Self {
109+
impl From<ergo_lib::wallet::TransactionHintsBag> for TransactionHintsBag {
110+
fn from(t: ergo_lib::wallet::TransactionHintsBag) -> Self {
111111
TransactionHintsBag(t)
112112
}
113113
}

ergo-lib/Cargo.toml

+21-8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ sigma-util = { workspace = true }
1717
ergo-chain-types = { workspace = true }
1818
ergotree-ir = { workspace = true }
1919
ergotree-interpreter = { workspace = true }
20-
ergo-nipopow = { workspace = true }
20+
ergo-nipopow = { workspace = true, optional = true }
2121
ergoscript-compiler = { workspace = true, optional = true }
22-
ergo-merkle-tree = { workspace = true }
22+
ergo-merkle-tree = { workspace = true, optional = true }
2323
ergo-rest = { workspace = true, optional = true }
2424
indexmap = { workspace = true }
2525
base16 = { workspace = true }
@@ -28,39 +28,44 @@ serde_json = { workspace = true, optional = true }
2828
thiserror = { workspace = true }
2929
derive_more = { workspace = true }
3030
bounded-vec = { workspace = true }
31-
num-bigint = { workspace = true, features = ["serde"] }
3231
proptest-derive = { workspace = true, optional = true }
3332
k256 = { workspace = true }
3433
sha2 = { workspace = true }
3534
hmac = { version = "0.12" }
3635
pbkdf2 = "0.11"
37-
rand = { workspace = true }
36+
rand = { workspace = true, optional = true }
3837
bitvec = { workspace = true, optional = true }
39-
unicode-normalization = "0.1.19"
38+
unicode-normalization = { version = "0.1.19", default-features = false }
4039
lazy_static = { workspace = true }
4140
proptest = { workspace = true, optional = true }
4241
serde_with = { workspace = true, optional = true }
43-
itertools = { workspace = true }
42+
hashbrown = { workspace = true }
4443

4544

4645
[features]
47-
default = ["json"]
46+
default = ["std", "json", "nipopow", "merkle"]
47+
std = ["rand", "ergotree-ir/std", "ergotree-interpreter/std"]
4848
json = [
4949
"serde",
5050
"serde_json",
5151
"serde_with",
5252
"bounded-vec/serde",
5353
"ergotree-ir/json",
54+
"ergotree-interpreter/json",
55+
"ergo-merkle-tree?/json",
5456
]
5557
compiler = ["ergoscript-compiler"]
5658
arbitrary = [
59+
"std",
5760
"proptest",
5861
"proptest-derive",
5962
"ergotree-ir/arbitrary",
6063
"ergo-chain-types/arbitrary",
6164
"ergotree-interpreter/arbitrary",
6265
]
63-
mnemonic_gen = ["bitvec"]
66+
merkle = ["ergo-merkle-tree"]
67+
nipopow = ["ergo-nipopow"]
68+
mnemonic_gen = ["bitvec", "rand"]
6469
rest = ["ergo-rest"]
6570

6671
[dev-dependencies]
@@ -73,3 +78,11 @@ pretty_assertions = { workspace = true }
7378
bs58 = { workspace = true }
7479
byteorder = { workspace = true }
7580
expect-test = { workspace = true }
81+
82+
83+
# docs.rs-specific configuration
84+
[package.metadata.docs.rs]
85+
# document all features
86+
all-features = true
87+
# enable docsrs flag, which lets us enable doc_auto_cfg to mark feature-gated functionality in documentation
88+
rustdoc-args = ["--cfg", "docsrs"]

ergo-lib/src/chain/contract.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@ impl Contract {
3737
}
3838
}
3939

40-
#[cfg(test)]
40+
#[cfg(all(test, feature = "compiler"))]
4141
#[allow(clippy::unwrap_used)]
4242
mod tests {
4343
use super::*;
4444

45-
#[cfg(feature = "compiler")]
4645
#[test]
4746
fn compile() {
48-
let contract =
49-
Contract::compile("HEIGHT", ergoscript_compiler::script_env::ScriptEnv::new()).unwrap();
50-
dbg!(&contract);
47+
Contract::compile("HEIGHT", ergoscript_compiler::script_env::ScriptEnv::new()).unwrap();
5148
}
5249
}

ergo-lib/src/chain/ergo_box/box_builder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! ErgoBoxCandidate builder
22
3-
use std::collections::HashMap;
4-
use std::convert::{TryFrom, TryInto};
3+
use alloc::string::String;
4+
use alloc::string::ToString;
5+
use alloc::vec::Vec;
6+
use core::convert::{TryFrom, TryInto};
7+
use hashbrown::HashMap;
58

69
use ergotree_ir::chain::address::AddressEncoderError;
710
use ergotree_ir::chain::ergo_box::box_value::BoxValue;

ergo-lib/src/chain/json.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! JSON serialization
22
3+
use alloc::string::String;
4+
use alloc::vec::Vec;
35
use serde::{Deserialize, Serialize};
46

57
use ergotree_interpreter::sigma_protocol::prover::ProofBytes;

ergo-lib/src/chain/json/hint.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use std::collections::HashMap;
1+
use alloc::vec::Vec;
2+
use hashbrown::HashMap;
23

34
use ergotree_interpreter::sigma_protocol::prover::hint::{Hint, HintsBag};
45
use serde::{Deserialize, Serialize};
56

6-
use crate::wallet::multi_sig::TransactionHintsBag;
7+
use crate::wallet::TransactionHintsBag;
78

89
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
910
pub struct TransactionHintsBagJson {
@@ -51,7 +52,7 @@ impl From<TransactionHintsBagJson> for TransactionHintsBag {
5152

5253
#[cfg(test)]
5354
mod tests {
54-
use crate::wallet::multi_sig::TransactionHintsBag;
55+
use crate::wallet::TransactionHintsBag;
5556
use proptest::prelude::*;
5657

5758
proptest! {

ergo-lib/src/chain/json/parameters.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::chain::parameters::{Parameter, Parameters};
2+
use hashbrown::HashMap;
23
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
44

55
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
66
pub struct ParametersJson {

ergo-lib/src/chain/json/transaction.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::convert::TryFrom;
1+
use alloc::string::String;
2+
use alloc::vec::Vec;
3+
use core::convert::TryFrom;
24
use thiserror::Error;
35

46
use crate::chain::transaction::unsigned::UnsignedTransaction;

ergo-lib/src/chain/parameters.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Blockchain parameters. This module defines adjustable blockchain parameters that can be voted on by miners
2-
use std::collections::HashMap;
2+
use hashbrown::HashMap;
33

44
#[repr(i8)]
55
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
@@ -103,7 +103,7 @@ impl Parameters {
103103
}
104104
}
105105

106-
impl std::default::Default for Parameters {
106+
impl Default for Parameters {
107107
/// Default blockchain parameters
108108
// Taken from https://github.com/ergoplatform/ergo/blob/master/ergo-core/src/main/scala/org/ergoplatform/settings/Parameters.scala#L291
109109
fn default() -> Self {

ergo-lib/src/chain/transaction.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ pub mod reduced;
77
pub(crate) mod storage_rent;
88
pub mod unsigned;
99

10+
use alloc::string::String;
11+
use alloc::string::ToString;
12+
use alloc::vec::Vec;
1013
use bounded_vec::BoundedVec;
1114
use ergo_chain_types::blake2b256_hash;
1215
use ergotree_interpreter::eval::env::Env;
@@ -25,6 +28,7 @@ use ergotree_ir::chain::ergo_box::ErgoBox;
2528
use ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
2629
use ergotree_ir::chain::token::TokenId;
2730
pub use ergotree_ir::chain::tx_id::TxId;
31+
use ergotree_ir::chain::IndexSet;
2832
use ergotree_ir::ergo_tree::ErgoTreeError;
2933
use thiserror::Error;
3034

@@ -46,11 +50,9 @@ use self::ergo_transaction::TxValidationError;
4650
use self::storage_rent::try_spend_storage_rent;
4751
use self::unsigned::UnsignedTransaction;
4852

49-
use indexmap::IndexSet;
50-
51-
use std::convert::TryFrom;
52-
use std::convert::TryInto;
53-
use std::iter::FromIterator;
53+
use core::convert::TryFrom;
54+
use core::convert::TryInto;
55+
use core::iter::FromIterator;
5456

5557
use super::ergo_state_context::ErgoStateContext;
5658

@@ -303,7 +305,8 @@ impl SigmaSerializable for Transaction {
303305
"too many tokens in transaction".to_string(),
304306
));
305307
}
306-
let mut token_ids = IndexSet::with_capacity(tokens_count as usize);
308+
let mut token_ids =
309+
IndexSet::with_capacity_and_hasher(tokens_count as usize, Default::default());
307310
for _ in 0..tokens_count {
308311
token_ids.insert(TokenId::sigma_parse(r)?);
309312
}

ergo-lib/src/chain/transaction/ergo_transaction.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use ergotree_ir::{
88
},
99
serialization::SigmaSerializationError,
1010
};
11-
use itertools::Itertools;
1211
use thiserror::Error;
1312

1413
use crate::wallet::tx_context::TransactionContextError;
@@ -104,7 +103,7 @@ pub trait ErgoTransaction {
104103

105104
// Check if there are no double-spends in input (one BoxId being spent more than once)
106105
let len = inputs.len();
107-
let unique_count = inputs.unique().count();
106+
let unique_count = inputs.collect::<hashbrown::HashSet<_>>().len();
108107
if unique_count != len {
109108
return Err(TxValidationError::DoubleSpend(unique_count, len));
110109
}

ergo-lib/src/chain/transaction/input/prover_result/json.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use std::str::FromStr;
1+
use core::str::FromStr;
22

3+
use alloc::string::String;
4+
use alloc::vec::Vec;
35
use ergotree_ir::chain::context_extension::ContextExtension;
46
use serde::ser::SerializeStruct;
57
use serde::Serialize;

ergo-lib/src/chain/transaction/unsigned.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ use super::DataInput;
66
use super::Transaction;
77
use super::TxIoVec;
88
use super::{distinct_token_ids, TransactionError};
9+
use alloc::vec::Vec;
910
use bounded_vec::BoundedVec;
1011
use ergo_chain_types::blake2b256_hash;
1112

13+
use core::convert::TryInto;
1214
use ergotree_ir::chain::ergo_box::ErgoBox;
1315
use ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
1416
use ergotree_ir::chain::token::TokenId;
1517
use ergotree_ir::chain::tx_id::TxId;
18+
use ergotree_ir::chain::IndexSet;
1619
use ergotree_ir::serialization::SigmaSerializationError;
17-
use indexmap::IndexSet;
18-
use std::convert::TryInto;
1920

2021
/// Unsigned (inputs without proofs) transaction
2122
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]

ergo-lib/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! ErgoTree IR
22
3+
#![cfg_attr(not(feature = "std"), no_std)]
4+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
35
// Coding conventions
46
#![forbid(unsafe_code)]
57
#![deny(non_upper_case_globals)]
@@ -20,6 +22,11 @@
2022
#![deny(clippy::unreachable)]
2123
#![deny(clippy::panic)]
2224

25+
#[cfg(all(not(feature = "std"), any(feature = "nipopow", feature = "merkle")))]
26+
compile_error!("ergo-nipopow and ergo-merkle-tree are not supported without std");
27+
28+
#[macro_use]
29+
extern crate alloc;
2330
pub mod chain;
2431
pub mod constants;
2532
mod utils;
@@ -30,8 +37,10 @@ pub mod wallet;
3037
/// Ergo blockchain types
3138
pub extern crate ergo_chain_types;
3239
/// Ergo Merkle Tree and Merkle verification tools
40+
#[cfg(feature = "merkle")]
3341
pub extern crate ergo_merkle_tree;
3442
/// Ergo NiPoPoW implementation
43+
#[cfg(feature = "nipopow")]
3544
pub extern crate ergo_nipopow;
3645
/// Re-exported types from dependencies
3746
#[cfg(feature = "rest")]

0 commit comments

Comments
 (0)