From b84e1d46c03f32c61ea0aa34af21788e910ae813 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 21 Oct 2022 09:12:44 +1100 Subject: [PATCH] Move amount module out of util Done as part of flattening the `util` module. Simply move the `amount` module out of the `util` module and to the crate root. Justified by the fact that the `Amount` type is more-or-less a "primitive" bitcoin type. --- bitcoin/src/{util => }/amount.rs | 28 ++++++++++++------------- bitcoin/src/lib.rs | 3 ++- bitcoin/src/util/mod.rs | 1 - fuzz/fuzz_targets/deserialize_amount.rs | 8 +++---- 4 files changed, 20 insertions(+), 20 deletions(-) rename bitcoin/src/{util => }/amount.rs (98%) diff --git a/bitcoin/src/util/amount.rs b/bitcoin/src/amount.rs similarity index 98% rename from bitcoin/src/util/amount.rs rename to bitcoin/src/amount.rs index 805fbe01..5425da0c 100644 --- a/bitcoin/src/util/amount.rs +++ b/bitcoin/src/amount.rs @@ -1241,13 +1241,13 @@ pub mod serde { //! #[derive(Serialize, Deserialize)] //! # #[serde(crate = "actual_serde")] //! pub struct HasAmount { - //! #[serde(with = "bitcoin::util::amount::serde::as_btc")] + //! #[serde(with = "bitcoin::amount::serde::as_btc")] //! pub amount: Amount, //! } //! ``` use serde::{Deserialize, Deserializer, Serialize, Serializer}; - use crate::util::amount::{Amount, Denomination, SignedAmount}; + use crate::amount::{Amount, Denomination, SignedAmount}; /// This trait is used only to avoid code duplication and naming collisions /// of the different serde serialization crates. @@ -1334,7 +1334,7 @@ pub mod serde { //! Use with `#[serde(with = "amount::serde::as_sat")]`. use serde::{Deserializer, Serializer}; - use crate::util::amount::serde::SerdeAmount; + use crate::amount::serde::SerdeAmount; pub fn serialize(a: &A, s: S) -> Result { a.ser_sat(s) @@ -1349,7 +1349,7 @@ pub mod serde { //! Use with `#[serde(default, with = "amount::serde::as_sat::opt")]`. use serde::{Deserializer, Serializer, de}; - use crate::util::amount::serde::SerdeAmountForOpt; + use crate::amount::serde::SerdeAmountForOpt; use core::fmt; use core::marker::PhantomData; @@ -1398,7 +1398,7 @@ pub mod serde { //! Use with `#[serde(with = "amount::serde::as_btc")]`. use serde::{Deserializer, Serializer}; - use crate::util::amount::serde::SerdeAmount; + use crate::amount::serde::SerdeAmount; pub fn serialize(a: &A, s: S) -> Result { a.ser_btc(s) @@ -1413,7 +1413,7 @@ pub mod serde { //! Use with `#[serde(default, with = "amount::serde::as_btc::opt")]`. use serde::{Deserializer, Serializer, de}; - use crate::util::amount::serde::SerdeAmountForOpt; + use crate::amount::serde::SerdeAmountForOpt; use core::fmt; use core::marker::PhantomData; @@ -1957,9 +1957,9 @@ mod tests { #[derive(Serialize, Deserialize, PartialEq, Debug)] #[serde(crate = "actual_serde")] struct T { - #[serde(with = "crate::util::amount::serde::as_sat")] + #[serde(with = "crate::amount::serde::as_sat")] pub amt: Amount, - #[serde(with = "crate::util::amount::serde::as_sat")] + #[serde(with = "crate::amount::serde::as_sat")] pub samt: SignedAmount, } @@ -1988,9 +1988,9 @@ mod tests { #[derive(Serialize, Deserialize, PartialEq, Debug)] #[serde(crate = "actual_serde")] struct T { - #[serde(with = "crate::util::amount::serde::as_btc")] + #[serde(with = "crate::amount::serde::as_btc")] pub amt: Amount, - #[serde(with = "crate::util::amount::serde::as_btc")] + #[serde(with = "crate::amount::serde::as_btc")] pub samt: SignedAmount, } @@ -2024,9 +2024,9 @@ mod tests { #[derive(Serialize, Deserialize, PartialEq, Debug, Eq)] #[serde(crate = "actual_serde")] struct T { - #[serde(default, with = "crate::util::amount::serde::as_btc::opt")] + #[serde(default, with = "crate::amount::serde::as_btc::opt")] pub amt: Option, - #[serde(default, with = "crate::util::amount::serde::as_btc::opt")] + #[serde(default, with = "crate::amount::serde::as_btc::opt")] pub samt: Option, } @@ -2069,9 +2069,9 @@ mod tests { #[derive(Serialize, Deserialize, PartialEq, Debug, Eq)] #[serde(crate = "actual_serde")] struct T { - #[serde(default, with = "crate::util::amount::serde::as_sat::opt")] + #[serde(default, with = "crate::amount::serde::as_sat::opt")] pub amt: Option, - #[serde(default, with = "crate::util::amount::serde::as_sat::opt")] + #[serde(default, with = "crate::amount::serde::as_sat::opt")] pub samt: Option, } diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs index 8fd276d2..dc42c2fb 100644 --- a/bitcoin/src/lib.rs +++ b/bitcoin/src/lib.rs @@ -80,6 +80,7 @@ mod serde_utils; #[macro_use] pub mod network; pub mod address; +pub mod amount; pub mod bip152; pub mod bip158; pub mod bip32; @@ -109,7 +110,7 @@ pub use crate::consensus::encode::VarInt; pub use crate::hash_types::*; pub use crate::network::constants::Network; pub use crate::pow::{CompactTarget, Target, Work}; -pub use crate::util::amount::{Amount, Denomination, SignedAmount}; +pub use crate::amount::{Amount, Denomination, SignedAmount}; pub use crate::util::ecdsa::{self, EcdsaSig, EcdsaSigError}; pub use crate::util::key::{KeyPair, PrivateKey, PublicKey, XOnlyPublicKey}; pub use crate::util::merkleblock::MerkleBlock; diff --git a/bitcoin/src/util/mod.rs b/bitcoin/src/util/mod.rs index a34b8212..c6884c46 100644 --- a/bitcoin/src/util/mod.rs +++ b/bitcoin/src/util/mod.rs @@ -9,7 +9,6 @@ pub mod key; pub mod ecdsa; pub mod schnorr; -pub mod amount; pub mod base58; pub mod hash; pub mod merkleblock; diff --git a/fuzz/fuzz_targets/deserialize_amount.rs b/fuzz/fuzz_targets/deserialize_amount.rs index 2eb79572..7d265c62 100644 --- a/fuzz/fuzz_targets/deserialize_amount.rs +++ b/fuzz/fuzz_targets/deserialize_amount.rs @@ -4,22 +4,22 @@ fn do_test(data: &[u8]) { let data_str = String::from_utf8_lossy(data); // signed - let samt = match bitcoin::util::amount::SignedAmount::from_str(&data_str) { + let samt = match bitcoin::amount::SignedAmount::from_str(&data_str) { Ok(amt) => amt, Err(_) => return, }; - let samt_roundtrip = match bitcoin::util::amount::SignedAmount::from_str(&samt.to_string()) { + let samt_roundtrip = match bitcoin::amount::SignedAmount::from_str(&samt.to_string()) { Ok(amt) => amt, Err(_) => return, }; assert_eq!(samt, samt_roundtrip); // unsigned - let amt = match bitcoin::util::amount::Amount::from_str(&data_str) { + let amt = match bitcoin::amount::Amount::from_str(&data_str) { Ok(amt) => amt, Err(_) => return, }; - let amt_roundtrip = match bitcoin::util::amount::Amount::from_str(&amt.to_string()) { + let amt_roundtrip = match bitcoin::amount::Amount::from_str(&amt.to_string()) { Ok(amt) => amt, Err(_) => return, };