Skip to content

Commit

Permalink
no_std ergo-lib, make ergo-merkle-tree and ergo-nipopow optional
Browse files Browse the repository at this point in the history
  • Loading branch information
SethDusek committed Nov 26, 2024
1 parent 51c632d commit fb2f92b
Show file tree
Hide file tree
Showing 34 changed files with 243 additions and 182 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ derive_more = { version = "0.99", features = [
] }
num-derive = "0.4.2"
thiserror = { version = "2.0.1", default-features = false }
bounded-vec = { git = "https://github.com/SethDusek/bounded-vec", branch = "no_std" } # TODO: merge no_std support in bounded-vec
bounded-vec = { git = "https://github.com/SethDusek/bounded-vec", rev = "0eb104e114c1387f33e88bc48b33951900767249" } # TODO: merge no_std support in bounded-vec upstream
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
blake2 = { version = "0.10.6", default-features = false }
sha2 = { version = "0.10", default-features = false }
Expand Down Expand Up @@ -93,7 +93,7 @@ core2 = { version = "0.4.0", default-features = false, features = ["alloc"] }
proptest = { version = "1.5.0", default-features = false, features = [
"alloc",
"std",
] } # TODO: consider enabling std
] }
proptest-derive = "0.3"
pretty_assertions = "1.3"
wasm-bindgen-test = "0.3.37"
Expand Down
2 changes: 1 addition & 1 deletion bindings/ergo-lib-c-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ futures-util = "0.3"
url = { workspace = true }
bounded-integer = { workspace = true }
serde_with = { workspace = true }
bounded-vec = { workspace = true, features=["serde"] }
bounded-vec = { workspace = true, features = ["serde"] }

[features]
default = ["mnemonic_gen", "ergo-lib/compiler"]
Expand Down
4 changes: 2 additions & 2 deletions bindings/ergo-lib-c-core/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub unsafe fn hints_bag_get(
}

/// TransactionHintsBag
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);
pub type TransactionHintsBagPtr = *mut TransactionHintsBag;
pub type ConstTransactionHintsBagPtr = *const TransactionHintsBag;

Expand All @@ -90,7 +90,7 @@ pub unsafe fn transaction_hints_bag_empty(
let transaction_hints_bag_out =
mut_ptr_as_mut(transaction_hints_bag_out, "transaction_hints_bag_out")?;
*transaction_hints_bag_out = Box::into_raw(Box::new(TransactionHintsBag(
ergo_lib::wallet::multi_sig::TransactionHintsBag::empty(),
ergo_lib::wallet::TransactionHintsBag::empty(),
)));
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions bindings/ergo-lib-wasm/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ impl From<ergo_lib::ergotree_interpreter::sigma_protocol::prover::hint::HintsBag

/// TransactionHintsBag
#[wasm_bindgen]
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::multi_sig::TransactionHintsBag);
pub struct TransactionHintsBag(pub(crate) ergo_lib::wallet::TransactionHintsBag);

#[wasm_bindgen]
impl TransactionHintsBag {
/// Empty TransactionHintsBag
pub fn empty() -> TransactionHintsBag {
TransactionHintsBag(ergo_lib::wallet::multi_sig::TransactionHintsBag::empty())
TransactionHintsBag(ergo_lib::wallet::TransactionHintsBag::empty())
}

/// Adding hints for input
Expand All @@ -106,8 +106,8 @@ impl TransactionHintsBag {
}
}

impl From<ergo_lib::wallet::multi_sig::TransactionHintsBag> for TransactionHintsBag {
fn from(t: ergo_lib::wallet::multi_sig::TransactionHintsBag) -> Self {
impl From<ergo_lib::wallet::TransactionHintsBag> for TransactionHintsBag {
fn from(t: ergo_lib::wallet::TransactionHintsBag) -> Self {
TransactionHintsBag(t)
}
}
Expand Down
21 changes: 13 additions & 8 deletions ergo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ sigma-util = { workspace = true }
ergo-chain-types = { workspace = true }
ergotree-ir = { workspace = true }
ergotree-interpreter = { workspace = true }
ergo-nipopow = { workspace = true }
ergo-nipopow = { workspace = true, optional = true }
ergoscript-compiler = { workspace = true, optional = true }
ergo-merkle-tree = { workspace = true }
ergo-merkle-tree = { workspace = true, optional = true }
ergo-rest = { workspace = true, optional = true }
indexmap = { workspace = true }
base16 = { workspace = true }
Expand All @@ -28,39 +28,44 @@ serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }
derive_more = { workspace = true }
bounded-vec = { workspace = true }
num-bigint = { workspace = true, features = ["serde"] }
proptest-derive = { workspace = true, optional = true }
k256 = { workspace = true }
sha2 = { workspace = true }
hmac = { version = "0.12" }
pbkdf2 = "0.11"
rand = { workspace = true }
rand = { workspace = true, optional = true }
bitvec = { workspace = true, optional = true }
unicode-normalization = "0.1.19"
unicode-normalization = { version = "0.1.19", default-features = false }
lazy_static = { workspace = true }
proptest = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true }
itertools = { workspace = true }
hashbrown = { workspace = true }


[features]
default = ["json"]
default = ["std", "json", "nipopow", "merkle"]
std = ["rand", "ergotree-ir/std", "ergotree-interpreter/std"]
json = [
"serde",
"serde_json",
"serde_with",
"bounded-vec/serde",
"ergotree-ir/json",
"ergotree-interpreter/json",
"ergo-merkle-tree?/json",
]
compiler = ["ergoscript-compiler"]
arbitrary = [
"std",
"proptest",
"proptest-derive",
"ergotree-ir/arbitrary",
"ergo-chain-types/arbitrary",
"ergotree-interpreter/arbitrary",
]
mnemonic_gen = ["bitvec"]
merkle = ["ergo-merkle-tree"]
nipopow = ["ergo-nipopow"]
mnemonic_gen = ["bitvec", "rand"]
rest = ["ergo-rest"]

[dev-dependencies]
Expand Down
7 changes: 2 additions & 5 deletions ergo-lib/src/chain/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ impl Contract {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "compiler"))]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;

#[cfg(feature = "compiler")]
#[test]
fn compile() {
let contract =
Contract::compile("HEIGHT", ergoscript_compiler::script_env::ScriptEnv::new()).unwrap();
dbg!(&contract);
Contract::compile("HEIGHT", ergoscript_compiler::script_env::ScriptEnv::new()).unwrap();
}
}
7 changes: 5 additions & 2 deletions ergo-lib/src/chain/ergo_box/box_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! ErgoBoxCandidate builder
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use hashbrown::HashMap;

use ergotree_ir::chain::address::AddressEncoderError;
use ergotree_ir::chain::ergo_box::box_value::BoxValue;
Expand Down
2 changes: 2 additions & 0 deletions ergo-lib/src/chain/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! JSON serialization
use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use ergotree_interpreter::sigma_protocol::prover::ProofBytes;
Expand Down
7 changes: 4 additions & 3 deletions ergo-lib/src/chain/json/hint.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashMap;
use alloc::vec::Vec;
use hashbrown::HashMap;

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

use crate::wallet::multi_sig::TransactionHintsBag;
use crate::wallet::TransactionHintsBag;

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

#[cfg(test)]
mod tests {
use crate::wallet::multi_sig::TransactionHintsBag;
use crate::wallet::TransactionHintsBag;
use proptest::prelude::*;

proptest! {
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/json/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::chain::parameters::{Parameter, Parameters};
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct ParametersJson {
Expand Down
4 changes: 3 additions & 1 deletion ergo-lib/src/chain/json/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::convert::TryFrom;
use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryFrom;
use thiserror::Error;

use crate::chain::transaction::unsigned::UnsignedTransaction;
Expand Down
4 changes: 2 additions & 2 deletions ergo-lib/src/chain/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Blockchain parameters. This module defines adjustable blockchain parameters that can be voted on by miners
use std::collections::HashMap;
use hashbrown::HashMap;

#[repr(i8)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Parameters {
}
}

impl std::default::Default for Parameters {
impl Default for Parameters {
/// Default blockchain parameters
// Taken from https://github.com/ergoplatform/ergo/blob/master/ergo-core/src/main/scala/org/ergoplatform/settings/Parameters.scala#L291
fn default() -> Self {
Expand Down
15 changes: 9 additions & 6 deletions ergo-lib/src/chain/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub mod reduced;
pub(crate) mod storage_rent;
pub mod unsigned;

use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use bounded_vec::BoundedVec;
use ergo_chain_types::blake2b256_hash;
use ergotree_interpreter::eval::env::Env;
Expand All @@ -25,6 +28,7 @@ use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
use ergotree_ir::chain::token::TokenId;
pub use ergotree_ir::chain::tx_id::TxId;
use ergotree_ir::chain::IndexSet;
use ergotree_ir::ergo_tree::ErgoTreeError;
use thiserror::Error;

Expand All @@ -46,11 +50,9 @@ use self::ergo_transaction::TxValidationError;
use self::storage_rent::try_spend_storage_rent;
use self::unsigned::UnsignedTransaction;

use indexmap::IndexSet;

use std::convert::TryFrom;
use std::convert::TryInto;
use std::iter::FromIterator;
use core::convert::TryFrom;
use core::convert::TryInto;
use core::iter::FromIterator;

use super::ergo_state_context::ErgoStateContext;

Expand Down Expand Up @@ -303,7 +305,8 @@ impl SigmaSerializable for Transaction {
"too many tokens in transaction".to_string(),
));
}
let mut token_ids = IndexSet::with_capacity(tokens_count as usize);
let mut token_ids =
IndexSet::with_capacity_and_hasher(tokens_count as usize, Default::default());
for _ in 0..tokens_count {
token_ids.insert(TokenId::sigma_parse(r)?);
}
Expand Down
3 changes: 1 addition & 2 deletions ergo-lib/src/chain/transaction/ergo_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ergotree_ir::{
},
serialization::SigmaSerializationError,
};
use itertools::Itertools;
use thiserror::Error;

use crate::wallet::tx_context::TransactionContextError;
Expand Down Expand Up @@ -104,7 +103,7 @@ pub trait ErgoTransaction {

// Check if there are no double-spends in input (one BoxId being spent more than once)
let len = inputs.len();
let unique_count = inputs.unique().count();
let unique_count = inputs.collect::<hashbrown::HashSet<_>>().len();
if unique_count != len {
return Err(TxValidationError::DoubleSpend(unique_count, len));
}
Expand Down
4 changes: 3 additions & 1 deletion ergo-lib/src/chain/transaction/input/prover_result/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::str::FromStr;
use core::str::FromStr;

use alloc::string::String;
use alloc::vec::Vec;
use ergotree_ir::chain::context_extension::ContextExtension;
use serde::ser::SerializeStruct;
use serde::Serialize;
Expand Down
5 changes: 3 additions & 2 deletions ergo-lib/src/chain/transaction/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ use super::DataInput;
use super::Transaction;
use super::TxIoVec;
use super::{distinct_token_ids, TransactionError};
use alloc::vec::Vec;
use bounded_vec::BoundedVec;
use ergo_chain_types::blake2b256_hash;

use core::convert::TryInto;
use ergotree_ir::chain::ergo_box::ErgoBox;
use ergotree_ir::chain::ergo_box::ErgoBoxCandidate;
use ergotree_ir::chain::token::TokenId;
use ergotree_ir::chain::tx_id::TxId;
use ergotree_ir::chain::IndexSet;
use ergotree_ir::serialization::SigmaSerializationError;
use indexmap::IndexSet;
use std::convert::TryInto;

/// Unsigned (inputs without proofs) transaction
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
Expand Down
8 changes: 8 additions & 0 deletions ergo-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! ErgoTree IR
#![cfg_attr(not(feature = "std"), no_std)]
// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
Expand All @@ -20,6 +21,11 @@
#![deny(clippy::unreachable)]
#![deny(clippy::panic)]

#[cfg(all(not(feature = "std"), any(feature = "nipopow", feature = "merkle")))]
compile_error!("ergo-nipopow and ergo-merkle-tree are not supported without std");

#[macro_use]
extern crate alloc;
pub mod chain;
pub mod constants;
mod utils;
Expand All @@ -30,8 +36,10 @@ pub mod wallet;
/// Ergo blockchain types
pub extern crate ergo_chain_types;
/// Ergo Merkle Tree and Merkle verification tools
#[cfg(feature = "merkle")]
pub extern crate ergo_merkle_tree;
/// Ergo NiPoPoW implementation
#[cfg(feature = "nipopow")]
pub extern crate ergo_nipopow;
/// Re-exported types from dependencies
#[cfg(feature = "rest")]
Expand Down
Loading

0 comments on commit fb2f92b

Please sign in to comment.