Repository: ergoplatform/sigma-rust (language: rust)

Issue:
Shorten import by exporting types on a higher level (BoxId, BoxValue)
Export BoxId, BoxValue in ergo_box module, and make their modules private.


Discussion / hints:

Fix patch:
diff --git a/bindings/ergo-lib-wasm/src/ergo_box.rs b/bindings/ergo-lib-wasm/src/ergo_box.rs
index 9b4197421..a5398a597 100644
--- a/bindings/ergo-lib-wasm/src/ergo_box.rs
+++ b/bindings/ergo-lib-wasm/src/ergo_box.rs
@@ -18,7 +18,7 @@
 
 use std::convert::TryFrom;
 
-use chain::ergo_box::register::NonMandatoryRegisters;
+use chain::ergo_box::NonMandatoryRegisters;
 use ergo_lib::chain;
 use wasm_bindgen::prelude::*;
 
@@ -31,7 +31,7 @@ pub mod box_builder;
 /// Box id (32-byte digest)
 #[wasm_bindgen]
 #[derive(PartialEq, Eq, Debug, Clone)]
-pub struct BoxId(chain::ergo_box::box_id::BoxId);
+pub struct BoxId(chain::ergo_box::BoxId);
 
 #[wasm_bindgen]
 impl BoxId {
@@ -41,13 +41,13 @@ impl BoxId {
     }
 }
 
-impl From<chain::ergo_box::box_id::BoxId> for BoxId {
-    fn from(b: chain::ergo_box::box_id::BoxId) -> Self {
+impl From<chain::ergo_box::BoxId> for BoxId {
+    fn from(b: chain::ergo_box::BoxId) -> Self {
         BoxId(b)
     }
 }
 
-impl From<BoxId> for chain::ergo_box::box_id::BoxId {
+impl From<BoxId> for chain::ergo_box::BoxId {
     fn from(b: BoxId) -> Self {
         b.0
     }
@@ -152,7 +152,7 @@ impl From<chain::ergo_box::ErgoBox> for ErgoBox {
 /// Box value in nanoERGs with bound checks
 #[wasm_bindgen]
 #[derive(PartialEq, Eq, Debug, Clone)]
-pub struct BoxValue(pub(crate) chain::ergo_box::box_value::BoxValue);
+pub struct BoxValue(pub(crate) chain::ergo_box::BoxValue);
 
 #[wasm_bindgen]
 impl BoxValue {
@@ -160,13 +160,13 @@ impl BoxValue {
     /// Allows box size upto 2777 bytes with current min box value per byte of 360 nanoERGs
     #[allow(non_snake_case)]
     pub fn SAFE_USER_MIN() -> BoxValue {
-        BoxValue(chain::ergo_box::box_value::BoxValue::SAFE_USER_MIN)
+        BoxValue(chain::ergo_box::BoxValue::SAFE_USER_MIN)
     }
 
     /// Create from u32 with bounds check
     pub fn from_u32(v: u32) -> Result<BoxValue, JsValue> {
         Ok(BoxValue(
-            chain::ergo_box::box_value::BoxValue::try_from(v as u64)
+            chain::ergo_box::BoxValue::try_from(v as u64)
                 .map_err(|e| JsValue::from_str(&format!("{}", e)))?,
         ))
     }
@@ -177,14 +177,14 @@ impl BoxValue {
     }
 }
 
-impl From<BoxValue> for chain::ergo_box::box_value::BoxValue {
+impl From<BoxValue> for chain::ergo_box::BoxValue {
     fn from(v: BoxValue) -> Self {
         v.0
     }
 }
 
-impl From<chain::ergo_box::box_value::BoxValue> for BoxValue {
-    fn from(v: chain::ergo_box::box_value::BoxValue) -> Self {
+impl From<chain::ergo_box::BoxValue> for BoxValue {
+    fn from(v: chain::ergo_box::BoxValue) -> Self {
         BoxValue(v)
     }
 }
@@ -210,9 +210,9 @@ pub enum NonMandatoryRegisterId {
 
 impl NonMandatoryRegisterId {}
 
-impl From<NonMandatoryRegisterId> for chain::ergo_box::register::NonMandatoryRegisterId {
+impl From<NonMandatoryRegisterId> for chain::ergo_box::NonMandatoryRegisterId {
     fn from(v: NonMandatoryRegisterId) -> Self {
-        use chain::ergo_box::register::NonMandatoryRegisterId::*;
+        use chain::ergo_box::NonMandatoryRegisterId::*;
         match v {
             NonMandatoryRegisterId::R4 => R4,
             NonMandatoryRegisterId::R5 => R5,
@@ -224,16 +224,16 @@ impl From<NonMandatoryRegisterId> for chain::ergo_box::register::NonMandatoryReg
     }
 }
 
-impl From<chain::ergo_box::register::NonMandatoryRegisterId> for NonMandatoryRegisterId {
-    fn from(v: chain::ergo_box::register::NonMandatoryRegisterId) -> Self {
+impl From<chain::ergo_box::NonMandatoryRegisterId> for NonMandatoryRegisterId {
+    fn from(v: chain::ergo_box::NonMandatoryRegisterId) -> Self {
         use NonMandatoryRegisterId::*;
         match v {
-            chain::ergo_box::register::NonMandatoryRegisterId::R4 => R4,
-            chain::ergo_box::register::NonMandatoryRegisterId::R5 => R5,
-            chain::ergo_box::register::NonMandatoryRegisterId::R6 => R6,
-            chain::ergo_box::register::NonMandatoryRegisterId::R7 => R7,
-            chain::ergo_box::register::NonMandatoryRegisterId::R8 => R8,
-            chain::ergo_box::register::NonMandatoryRegisterId::R9 => R9,
+            chain::ergo_box::NonMandatoryRegisterId::R4 => R4,
+            chain::ergo_box::NonMandatoryRegisterId::R5 => R5,
+            chain::ergo_box::NonMandatoryRegisterId::R6 => R6,
+            chain::ergo_box::NonMandatoryRegisterId::R7 => R7,
+            chain::ergo_box::NonMandatoryRegisterId::R8 => R8,
+            chain::ergo_box::NonMandatoryRegisterId::R9 => R9,
         }
     }
 }
diff --git a/bindings/ergo-lib-wasm/src/token.rs b/bindings/ergo-lib-wasm/src/token.rs
index 3d600b298..26f546d4c 100644
--- a/bindings/ergo-lib-wasm/src/token.rs
+++ b/bindings/ergo-lib-wasm/src/token.rs
@@ -19,7 +19,7 @@ pub struct TokenId(chain::token::TokenId);
 impl TokenId {
     /// Create token id from erbo box id (32 byte digest)
     pub fn from_box_id(box_id: &BoxId) -> TokenId {
-        let box_id: chain::ergo_box::box_id::BoxId = box_id.clone().into();
+        let box_id: chain::ergo_box::BoxId = box_id.clone().into();
         TokenId(chain::token::TokenId::from(box_id))
     }
 
diff --git a/ergo-lib/src/chain/data_input.rs b/ergo-lib/src/chain/data_input.rs
index 752e4c857..3c2e4be9f 100644
--- a/ergo-lib/src/chain/data_input.rs
+++ b/ergo-lib/src/chain/data_input.rs
@@ -2,7 +2,7 @@
 
 use std::io;
 
-use super::ergo_box::box_id::BoxId;
+use super::ergo_box::BoxId;
 use crate::serialization::{
     sigma_byte_reader::SigmaByteRead, sigma_byte_writer::SigmaByteWrite, SerializationError,
     SigmaSerializable,
diff --git a/ergo-lib/src/chain/ergo_box.rs b/ergo-lib/src/chain/ergo_box.rs
index 04ded5d2e..1aeec943a 100644
--- a/ergo-lib/src/chain/ergo_box.rs
+++ b/ergo-lib/src/chain/ergo_box.rs
@@ -1,9 +1,13 @@
 //! Ergo box
 
 pub mod box_builder;
-pub mod box_id;
-pub mod box_value;
-pub mod register;
+mod box_id;
+mod box_value;
+mod register;
+
+pub use box_id::*;
+pub use box_value::*;
+pub use register::*;
 
 #[cfg(feature = "json")]
 use super::json;
@@ -23,10 +27,7 @@ use crate::{
         SerializationError, SigmaSerializable,
     },
 };
-use box_id::BoxId;
-use box_value::BoxValue;
 use indexmap::IndexSet;
-use register::NonMandatoryRegisters;
 #[cfg(feature = "json")]
 use serde::{Deserialize, Serialize};
 use std::collections::HashMap;
diff --git a/ergo-lib/src/chain/input.rs b/ergo-lib/src/chain/input.rs
index b9df9df56..bbdca69de 100644
--- a/ergo-lib/src/chain/input.rs
+++ b/ergo-lib/src/chain/input.rs
@@ -3,7 +3,7 @@ use std::io;
 
 use super::{
     context_extension::ContextExtension,
-    ergo_box::box_id::BoxId,
+    ergo_box::BoxId,
     ergo_box::ErgoBoxId,
     prover_result::{ProofBytes, ProverResult},
 };
diff --git a/ergo-lib/src/chain/json.rs b/ergo-lib/src/chain/json.rs
index ef001de60..6d783c53e 100644
--- a/ergo-lib/src/chain/json.rs
+++ b/ergo-lib/src/chain/json.rs
@@ -38,12 +38,11 @@ pub mod ergo_tree {
 }
 
 pub mod ergo_box {
+    use crate::chain::ergo_box::BoxId;
+    use crate::chain::ergo_box::BoxValue;
+    use crate::chain::ergo_box::NonMandatoryRegisters;
     use crate::{
-        chain::{
-            ergo_box::{box_id::BoxId, box_value::BoxValue, register::NonMandatoryRegisters},
-            token::Token,
-            transaction::TxId,
-        },
+        chain::{token::Token, transaction::TxId},
         ErgoTree,
     };
     use serde::Deserialize;
@@ -128,7 +127,6 @@ mod tests {
     use super::*;
     use crate::chain::context_extension::ContextExtension;
     use proptest::prelude::*;
-    use register::NonMandatoryRegisters;
 
     proptest! {
 
diff --git a/ergo-lib/src/chain/token.rs b/ergo-lib/src/chain/token.rs
index 24833eeef..511cf7ab3 100644
--- a/ergo-lib/src/chain/token.rs
+++ b/ergo-lib/src/chain/token.rs
@@ -8,7 +8,7 @@ use std::convert::TryFrom;
 use std::io;
 
 use super::digest32::Digest32;
-use super::ergo_box::box_id::BoxId;
+use super::ergo_box::BoxId;
 #[cfg(test)]
 use proptest_derive::Arbitrary;
 #[cfg(feature = "json")]
diff --git a/ergo-lib/src/serialization/ergo_box.rs b/ergo-lib/src/serialization/ergo_box.rs
index a91311bfb..51aa3e8b1 100644
--- a/ergo-lib/src/serialization/ergo_box.rs
+++ b/ergo-lib/src/serialization/ergo_box.rs
@@ -5,7 +5,7 @@ use crate::serialization::{
 use crate::{
     ast::Constant,
     chain::{
-        ergo_box::{box_value::BoxValue, register::NonMandatoryRegisters, ErgoBoxCandidate},
+        ergo_box::{BoxValue, ErgoBoxCandidate, NonMandatoryRegisters},
         token::{Token, TokenId},
     },
     ErgoTree,
diff --git a/ergo-lib/src/wallet/box_selector.rs b/ergo-lib/src/wallet/box_selector.rs
index c0c7368ca..9a3fa86ae 100644
--- a/ergo-lib/src/wallet/box_selector.rs
+++ b/ergo-lib/src/wallet/box_selector.rs
@@ -3,10 +3,10 @@
 mod simple;
 pub use simple::*;
 
-use crate::chain::ergo_box::box_value::BoxValueError;
+use crate::chain::ergo_box::BoxValueError;
 use crate::chain::ergo_box::ErgoBoxAssetsData;
 use crate::chain::{
-    ergo_box::{box_value::BoxValue, ErgoBoxAssets},
+    ergo_box::{BoxValue, ErgoBoxAssets},
     token::Token,
 };
 use thiserror::Error;
diff --git a/ergo-lib/src/wallet/box_selector/simple.rs b/ergo-lib/src/wallet/box_selector/simple.rs
index 9f0df3c52..73de29886 100644
--- a/ergo-lib/src/wallet/box_selector/simple.rs
+++ b/ergo-lib/src/wallet/box_selector/simple.rs
@@ -4,9 +4,9 @@ use std::cmp::min;
 use std::collections::HashMap;
 use std::convert::TryInto;
 
-use crate::chain::ergo_box::box_value::BoxValue;
 use crate::chain::ergo_box::sum_tokens;
 use crate::chain::ergo_box::sum_tokens_from_boxes;
+use crate::chain::ergo_box::BoxValue;
 use crate::chain::ergo_box::ErgoBoxAssets;
 use crate::chain::ergo_box::ErgoBoxAssetsData;
 use crate::chain::token::Token;
@@ -146,7 +146,7 @@ impl Default for SimpleBoxSelector {
 mod tests {
     use std::convert::TryFrom;
 
-    use crate::chain::ergo_box::box_value;
+    use crate::chain::ergo_box::checked_sum;
     use crate::chain::ergo_box::sum_value;
     use crate::chain::ergo_box::ErgoBox;
     use crate::chain::ergo_box::ErgoBoxAssetsData;
@@ -169,7 +169,7 @@ mod tests {
                                         vec(any_with::<ErgoBoxAssetsData>(
                                             (BoxValue::MIN_RAW * 1000 .. BoxValue::MIN_RAW * 10000).into()), 1..10)) {
             let s = SimpleBoxSelector::new();
-            let all_inputs_val = box_value::checked_sum(inputs.iter().map(|b| b.value)).unwrap();
+            let all_inputs_val = checked_sum(inputs.iter().map(|b| b.value)).unwrap();
 
             let balance_too_much = all_inputs_val.checked_add(&BoxValue::SAFE_USER_MIN).unwrap();
             prop_assert!(s.select(inputs, balance_too_much, vec![].as_slice()).is_err());
@@ -241,7 +241,7 @@ mod tests {
                                   vec(any_with::<ErgoBoxAssetsData>(
                                       (BoxValue::MIN_RAW * 1000 .. BoxValue::MIN_RAW * 10000).into()), 1..10)) {
             let s = SimpleBoxSelector::new();
-            let all_inputs_val = box_value::checked_sum(inputs.iter().map(|b| b.value)).unwrap();
+            let all_inputs_val = checked_sum(inputs.iter().map(|b| b.value)).unwrap();
             let balance_less = all_inputs_val.checked_sub(&BoxValue::SAFE_USER_MIN).unwrap();
             let selection_less = s.select(inputs.clone(), balance_less, vec![].as_slice()).unwrap();
             prop_assert!(selection_less.boxes == inputs);
diff --git a/ergo-lib/src/wallet/signing.rs b/ergo-lib/src/wallet/signing.rs
index 993ad182e..3cae9e239 100644
--- a/ergo-lib/src/wallet/signing.rs
+++ b/ergo-lib/src/wallet/signing.rs
@@ -71,12 +71,10 @@ mod tests {
     use proptest::prelude::*;
 
     use crate::chain::ergo_box::box_builder::ErgoBoxCandidateBuilder;
-    use crate::chain::ergo_box::box_value::BoxValue;
+    use crate::chain::ergo_box::BoxValue;
     use crate::{
         ast::{Constant, Expr},
-        chain::{
-            ergo_box::register::NonMandatoryRegisters, input::UnsignedInput, transaction::TxId,
-        },
+        chain::{ergo_box::NonMandatoryRegisters, input::UnsignedInput, transaction::TxId},
         sigma_protocol::{
             prover::TestProver,
             verifier::{TestVerifier, Verifier, VerifierError},
diff --git a/ergo-lib/src/wallet/tx_builder.rs b/ergo-lib/src/wallet/tx_builder.rs
index cff2abc57..93e28094b 100644
--- a/ergo-lib/src/wallet/tx_builder.rs
+++ b/ergo-lib/src/wallet/tx_builder.rs
@@ -2,7 +2,6 @@
 
 use std::collections::HashSet;
 
-use box_value::BoxValueError;
 use thiserror::Error;
 
 use crate::chain::address::Address;
@@ -12,10 +11,11 @@ use crate::chain::contract::Contract;
 use crate::chain::data_input::DataInput;
 use crate::chain::ergo_box::box_builder::ErgoBoxCandidateBuilder;
 use crate::chain::ergo_box::box_builder::ErgoBoxCandidateBuilderError;
-use crate::chain::ergo_box::box_id::BoxId;
-use crate::chain::ergo_box::box_value;
 use crate::chain::ergo_box::sum_tokens_from_boxes;
 use crate::chain::ergo_box::sum_value;
+use crate::chain::ergo_box::BoxId;
+use crate::chain::ergo_box::BoxValue;
+use crate::chain::ergo_box::BoxValueError;
 use crate::chain::input::Input;
 use crate::chain::prover_result::ProofBytes;
 use crate::chain::prover_result::ProverResult;
@@ -23,10 +23,7 @@ use crate::chain::token::Token;
 use crate::chain::token::TokenId;
 use crate::chain::transaction::Transaction;
 use crate::chain::{
-    ergo_box::ErgoBoxAssets,
-    ergo_box::ErgoBoxId,
-    ergo_box::{box_value::BoxValue, ErgoBoxCandidate},
-    input::UnsignedInput,
+    ergo_box::ErgoBoxAssets, ergo_box::ErgoBoxCandidate, ergo_box::ErgoBoxId, input::UnsignedInput,
     transaction::unsigned::UnsignedTransaction,
 };
 use crate::constants::MINERS_FEE_MAINNET_ADDRESS;
@@ -257,8 +254,9 @@ mod tests {
 
     use proptest::{collection::vec, prelude::*};
 
-    use crate::chain::ergo_box::register::NonMandatoryRegisters;
+    use crate::chain::ergo_box::checked_sum;
     use crate::chain::ergo_box::ErgoBox;
+    use crate::chain::ergo_box::NonMandatoryRegisters;
     use crate::chain::token::tests::ArbTokenIdParam;
     use crate::chain::token::Token;
     use crate::chain::token::TokenAmount;
@@ -534,12 +532,12 @@ mod tests {
                          data_inputs in vec(any::<DataInput>(), 0..2)) {
             prop_assume!(sum_tokens_from_boxes(outputs.as_slice()).is_empty());
             let min_change_value = BoxValue::SAFE_USER_MIN;
-            let all_outputs = box_value::checked_sum(outputs.iter().map(|b| b.value)).unwrap()
+            let all_outputs = checked_sum(outputs.iter().map(|b| b.value)).unwrap()
                                                                              .checked_add(&miners_fee)
                                                                              .unwrap();
-            let all_inputs = box_value::checked_sum(inputs.iter().map(|b| b.value)).unwrap();
+            let all_inputs = checked_sum(inputs.iter().map(|b| b.value)).unwrap();
             prop_assume!(all_outputs < all_inputs);
-            let total_output_value: BoxValue = box_value::checked_sum(outputs.iter().map(|b| b.value)).unwrap()
+            let total_output_value: BoxValue = checked_sum(outputs.iter().map(|b| b.value)).unwrap()
                                                                                                       .checked_add(&miners_fee).unwrap();
             let mut tx_builder = TxBuilder::new(
                 SimpleBoxSelector::new().select(inputs.clone(), total_output_value, &[]).unwrap(),
@@ -557,7 +555,7 @@ mod tests {
                                                              .map(|i| inputs.iter()
                                                                   .find(|ib| ib.box_id() == i.box_id).unwrap().value)
                                                              .collect();
-            let tx_all_inputs_sum = box_value::checked_sum(tx_all_inputs_vals.into_iter()).unwrap();
+            let tx_all_inputs_sum = checked_sum(tx_all_inputs_vals.into_iter()).unwrap();
             let expected_change = tx_all_inputs_sum.checked_sub(&all_outputs).unwrap();
             prop_assert!(tx.output_candidates.iter().any(|b| {
                 b.value == expected_change && b.ergo_tree == change_address.script().unwrap()


Test patch: