Skip to content

Commit

Permalink
Fix dependency issues
Browse files Browse the repository at this point in the history
1. Reorganize dependencies and remove requirements ("=") on wasm-bindgen
   and js-sys, due to conflicts that can occur when using this crate.
2. Replace wasm-bindgen's "serde-serialize" feature with
   serde-wasm-bindgen due to cyclical dependency issue.

Relates to:
tkaitchuck/aHash#95
rustwasm/wasm-bindgen#3031
  • Loading branch information
trevor-crypto committed Aug 26, 2022
1 parent 8b0a2b2 commit c880537
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
18 changes: 14 additions & 4 deletions rust/Cargo.lock

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

8 changes: 5 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ num-integer = "0.1.45"
# feature or this one
clear_on_drop = { version = "0.2", features = ["no_cc"] }
itertools = "0.10.1"
getrandom = { version = "0.2.3", features = ["js"] }
rand = "0.8.4"
schemars = "0.8.8"
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -41,12 +40,15 @@ serde = { version = "1.0", features = ["derive"] }
[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))'.dependencies]
rand_os = "0.1"
noop_proc_macro = "0.3.0"
getrandom = "0.2.3"

# wasm
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "=0.2.78", features = ["serde-serialize"] }
serde-wasm-bindgen = "0.4.3"
wasm-bindgen = "=0.2.78"
rand_os = { version = "0.1", features = ["wasm-bindgen"] }
js-sys = "=0.3.51"
js-sys = "0.3.51"
getrandom = { version = "0.2.3", features = ["js"] }

[profile.release]
# Tell `rustc` to optimize for small code size.
Expand Down
62 changes: 26 additions & 36 deletions rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cbor_event::{
se::{Serialize, Serializer},
};
use hex::FromHex;
use itertools::Itertools;
use num_bigint::Sign;
use serde_json;
use std::convert::TryFrom;
Expand All @@ -13,7 +14,6 @@ use std::{
io::{BufRead, Seek, Write},
ops::{Rem, Sub},
};
use itertools::Itertools;

use super::*;
use crate::error::{DeserializeError, DeserializeFailure};
Expand Down Expand Up @@ -43,7 +43,7 @@ macro_rules! to_from_json {

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
pub fn to_js_value(&self) -> Result<JsValue, JsError> {
JsValue::from_serde(&self)
serde_wasm_bindgen::to_value(&self)
.map_err(|e| JsError::from_str(&format!("to_js_value: {}", e)))
}

Expand Down Expand Up @@ -252,7 +252,11 @@ impl BigNum {
}

pub fn max(a: &BigNum, b: &BigNum) -> BigNum {
if a.less_than(b) { b.clone() } else { a.clone() }
if a.less_than(b) {
b.clone()
} else {
a.clone()
}
}
}

Expand Down Expand Up @@ -1351,55 +1355,44 @@ struct OutputSizeConstants {

// <TODO:REMOVE_AFTER_BABBAGE>
fn quot<T>(a: T, b: T) -> T
where T: Sub<Output=T> + Rem<Output=T> + Div<Output=T> + Copy + Clone + std::fmt::Display {
where
T: Sub<Output = T> + Rem<Output = T> + Div<Output = T> + Copy + Clone + std::fmt::Display,
{
(a - (a % b)) / b
}

// <TODO:REMOVE_AFTER_BABBAGE>
fn bundle_size(
assets: &Value,
constants: &OutputSizeConstants,
) -> usize {
fn bundle_size(assets: &Value, constants: &OutputSizeConstants) -> usize {
// based on https://github.com/input-output-hk/cardano-ledger-specs/blob/master/doc/explanations/min-utxo-alonzo.rst
match &assets.multiasset {
None => 2, // coinSize according the minimum value function
Some (assets) => {
let num_assets = assets.0
.values()
.fold(
0,
| acc, next| acc + next.len()
);
let sum_asset_name_lengths = assets.0
Some(assets) => {
let num_assets = assets.0.values().fold(0, |acc, next| acc + next.len());
let sum_asset_name_lengths = assets
.0
.values()
.flat_map(|assets| assets.0.keys())
.unique_by(|asset| asset.name())
.fold(
0,
| acc, next| acc + next.0.len()
);
let sum_policy_id_lengths = assets.0
.keys()
.fold(
0,
| acc, next| acc + next.0.len()
);
.fold(0, |acc, next| acc + next.0.len());
let sum_policy_id_lengths = assets.0.keys().fold(0, |acc, next| acc + next.0.len());
// converts bytes to 8-byte long words, rounding up
fn roundup_bytes_to_words(b: usize) -> usize {
quot(b + 7, 8)
}
constants.k0 + roundup_bytes_to_words(
(num_assets * constants.k1) + sum_asset_name_lengths +
(constants.k2 * sum_policy_id_lengths)
)
constants.k0
+ roundup_bytes_to_words(
(num_assets * constants.k1)
+ sum_asset_name_lengths
+ (constants.k2 * sum_policy_id_lengths),
)
}
}
}

// <TODO:REMOVE_AFTER_BABBAGE>
fn _min_ada_required_legacy(
assets: &Value,
has_data_hash: bool, // whether the output includes a data hash
has_data_hash: bool, // whether the output includes a data hash
coins_per_utxo_word: &BigNum, // protocol parameter (in lovelace)
) -> Result<BigNum, JsError> {
// based on https://github.com/input-output-hk/cardano-ledger-specs/blob/master/doc/explanations/min-utxo-alonzo.rst
Expand Down Expand Up @@ -1472,11 +1465,8 @@ impl MinOutputAdaCalculator {
coins_per_word: &Coin,
) -> Result<Coin, JsError> {
// <TODO:REMOVE_AFTER_BABBAGE>
let legacy_coin = _min_ada_required_legacy(
&output.amount(),
output.has_data_hash(),
coins_per_word,
)?;
let legacy_coin =
_min_ada_required_legacy(&output.amount(), output.has_data_hash(), coins_per_word)?;
//according to https://hydra.iohk.io/build/15339994/download/1/babbage-changes.pdf
//See on the page 9 getValue txout
let result = BigNum::from(output.to_bytes().len())
Expand Down

0 comments on commit c880537

Please sign in to comment.