diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 45ab355c..9f737eb6 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -9,24 +9,20 @@ //! these blocks and the blockchain. //! -use crate::prelude::*; - use core::fmt; -use crate::merkle_tree; -use crate::error::Error::{self, BlockBadTarget, BlockBadProofOfWork}; -use crate::hashes::{Hash, HashEngine}; -use crate::hash_types::{Wtxid, TxMerkleNode, WitnessMerkleNode, WitnessCommitment}; -use crate::consensus::{encode, Encodable, Decodable}; -use crate::blockdata::transaction::Transaction; -use crate::blockdata::script; -use crate::pow::{CompactTarget, Target, Work}; -use crate::VarInt; -use crate::internal_macros::impl_consensus_encoding; -use crate::io; use super::Weight; - +use crate::blockdata::script; +use crate::blockdata::transaction::Transaction; +use crate::consensus::{encode, Decodable, Encodable}; +use crate::error::Error::{self, BlockBadProofOfWork, BlockBadTarget}; pub use crate::hash_types::BlockHash; +use crate::hash_types::{TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxid}; +use crate::hashes::{Hash, HashEngine}; +use crate::internal_macros::impl_consensus_encoding; +use crate::pow::{CompactTarget, Target, Work}; +use crate::prelude::*; +use crate::{io, merkle_tree, VarInt}; /// Bitcoin block header. /// @@ -67,14 +63,10 @@ impl Header { } /// Computes the target (range [0, T] inclusive) that a blockhash must land in to be valid. - pub fn target(&self) -> Target { - self.bits.into() - } + pub fn target(&self) -> Target { self.bits.into() } /// Computes the popular "difficulty" measure for mining. - pub fn difficulty(&self) -> u128 { - self.target().difficulty() - } + pub fn difficulty(&self) -> u128 { self.target().difficulty() } /// Checks that the proof-of-work for the block is valid, returning the block hash. pub fn validate_pow(&self, required_target: Target) -> Result { @@ -91,9 +83,7 @@ impl Header { } /// Returns the total work of the block. - pub fn work(&self) -> Work { - self.target().to_work() - } + pub fn work(&self) -> Work { self.target().to_work() } } /// Bitcoin block version number. @@ -135,16 +125,12 @@ impl Version { /// Creates a [`Version`] from a signed 32 bit integer value. /// /// This is the data type used in consensus code in Bitcoin Core. - pub fn from_consensus(v: i32) -> Self { - Version(v) - } + pub fn from_consensus(v: i32) -> Self { Version(v) } /// Returns the inner `i32` value. /// /// This is the data type used in consensus code in Bitcoin Core. - pub fn to_consensus(self) -> i32 { - self.0 - } + pub fn to_consensus(self) -> i32 { self.0 } /// Checks whether the version number is signalling a soft fork at the given bit. /// @@ -167,9 +153,7 @@ impl Version { } impl Default for Version { - fn default() -> Version { - Self::NO_SOFT_FORK_SIGNALLING - } + fn default() -> Version { Self::NO_SOFT_FORK_SIGNALLING } } impl Encodable for Version { @@ -202,16 +186,14 @@ pub struct Block { /// The block header pub header: Header, /// List of transactions contained in the block - pub txdata: Vec + pub txdata: Vec, } impl_consensus_encoding!(Block, header, txdata); impl Block { /// Returns the block hash. - pub fn block_hash(&self) -> BlockHash { - self.header.block_hash() - } + pub fn block_hash(&self) -> BlockHash { self.header.block_hash() } /// Checks if merkle root of header matches merkle root of the transaction list. pub fn check_merkle_root(&self) -> bool { @@ -239,15 +221,21 @@ impl Block { } // Commitment is in the last output that starts with magic bytes. - if let Some(pos) = coinbase.output.iter() - .rposition(|o| o.script_pubkey.len () >= 38 && o.script_pubkey.as_bytes()[0..6] == MAGIC) + if let Some(pos) = coinbase + .output + .iter() + .rposition(|o| o.script_pubkey.len() >= 38 && o.script_pubkey.as_bytes()[0..6] == MAGIC) { - let commitment = WitnessCommitment::from_slice(&coinbase.output[pos].script_pubkey.as_bytes()[6..38]).unwrap(); + let commitment = WitnessCommitment::from_slice( + &coinbase.output[pos].script_pubkey.as_bytes()[6..38], + ) + .unwrap(); // Witness reserved value is in coinbase input witness. let witness_vec: Vec<_> = coinbase.input[0].witness.iter().collect(); if witness_vec.len() == 1 && witness_vec[0].len() == 32 { if let Some(witness_root) = self.witness_root() { - return commitment == Self::compute_witness_commitment(&witness_root, witness_vec[0]); + return commitment + == Self::compute_witness_commitment(&witness_root, witness_vec[0]); } } } @@ -262,7 +250,10 @@ impl Block { } /// Computes the witness commitment for the block's transaction list. - pub fn compute_witness_commitment(witness_root: &WitnessMerkleNode, witness_reserved_value: &[u8]) -> WitnessCommitment { + pub fn compute_witness_commitment( + witness_root: &WitnessMerkleNode, + witness_reserved_value: &[u8], + ) -> WitnessCommitment { let mut encoder = WitnessCommitment::engine(); witness_root.consensus_encode(&mut encoder).expect("engines don't error"); encoder.input(witness_reserved_value); @@ -283,9 +274,7 @@ impl Block { } /// base_size == size of header + size of encoded transaction count. - fn base_size(&self) -> usize { - 80 + VarInt(self.txdata.len() as u64).len() - } + fn base_size(&self) -> usize { 80 + VarInt(self.txdata.len() as u64).len() } /// Returns the size of the block. /// @@ -309,9 +298,7 @@ impl Block { } /// Returns the coinbase transaction, if one is present. - pub fn coinbase(&self) -> Option<&Transaction> { - self.txdata.first() - } + pub fn coinbase(&self) -> Option<&Transaction> { self.txdata.first() } /// Returns the block height, as encoded in the coinbase transaction according to BIP34. pub fn bip34_block_height(&self) -> Result { @@ -334,7 +321,8 @@ impl Block { match push.map_err(|_| Bip34Error::NotPresent)? { script::Instruction::PushBytes(b) => { // Check that the number is encoded in the minimal way. - let h = script::read_scriptint(b.as_bytes()).map_err(|_e| Bip34Error::UnexpectedPush(b.as_bytes().to_vec()))?; + let h = script::read_scriptint(b.as_bytes()) + .map_err(|_e| Bip34Error::UnexpectedPush(b.as_bytes().to_vec()))?; if h < 0 { Err(Bip34Error::NegativeHeight) } else { @@ -380,44 +368,32 @@ impl std::error::Error for Bip34Error { use self::Bip34Error::*; match self { - Unsupported | - NotPresent | - UnexpectedPush(_) | - NegativeHeight => None, + Unsupported | NotPresent | UnexpectedPush(_) | NegativeHeight => None, } } } impl From
for BlockHash { - fn from(header: Header) -> BlockHash { - header.block_hash() - } + fn from(header: Header) -> BlockHash { header.block_hash() } } impl From<&Header> for BlockHash { - fn from(header: &Header) -> BlockHash { - header.block_hash() - } + fn from(header: &Header) -> BlockHash { header.block_hash() } } impl From for BlockHash { - fn from(block: Block) -> BlockHash { - block.block_hash() - } + fn from(block: Block) -> BlockHash { block.block_hash() } } impl From<&Block> for BlockHash { - fn from(block: &Block) -> BlockHash { - block.block_hash() - } + fn from(block: &Block) -> BlockHash { block.block_hash() } } #[cfg(test)] mod tests { use super::*; - - use crate::hashes::hex::FromHex; use crate::consensus::encode::{deserialize, serialize}; + use crate::hashes::hex::FromHex; use crate::internal_macros::hex; #[test] @@ -431,7 +407,6 @@ mod tests { assert_eq!(block.bip34_block_height(), Ok(100_000)); - // block with 9-byte bip34 push const BAD_HEX: &str = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3d09a08601112233445566000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; let bad: Block = deserialize(&hex!(BAD_HEX)).unwrap(); @@ -464,13 +439,19 @@ mod tests { assert_eq!(real_decode.header.bits, CompactTarget::from_consensus(486604799)); assert_eq!(real_decode.header.nonce, 2067413810); assert_eq!(real_decode.header.work(), work); - assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash()); + assert_eq!( + real_decode.header.validate_pow(real_decode.header.target()).unwrap(), + real_decode.block_hash() + ); assert_eq!(real_decode.header.difficulty(), 1); // [test] TODO: check the transaction data assert_eq!(real_decode.size(), some_block.len()); assert_eq!(real_decode.strippedsize(), some_block.len()); - assert_eq!(real_decode.weight(), Weight::from_non_witness_data_size(some_block.len() as u64)); + assert_eq!( + real_decode.weight(), + Weight::from_non_witness_data_size(some_block.len() as u64) + ); // should be also ok for a non-witness block as commitment is optional in that case assert!(real_decode.check_witness_commitment()); @@ -491,7 +472,7 @@ mod tests { assert!(decode.is_ok()); let real_decode = decode.unwrap(); - assert_eq!(real_decode.header.version, Version(Version::USE_VERSION_BITS as i32)); // VERSIONBITS but no bits set + assert_eq!(real_decode.header.version, Version(Version::USE_VERSION_BITS as i32)); // VERSIONBITS but no bits set assert_eq!(serialize(&real_decode.header.prev_blockhash), prevhash); assert_eq!(serialize(&real_decode.header.merkle_root), merkle); assert_eq!(real_decode.header.merkle_root, real_decode.compute_merkle_root().unwrap()); @@ -499,7 +480,10 @@ mod tests { assert_eq!(real_decode.header.bits, CompactTarget::from_consensus(0x1a06d450)); assert_eq!(real_decode.header.nonce, 1879759182); assert_eq!(real_decode.header.work(), work); - assert_eq!(real_decode.header.validate_pow(real_decode.header.target()).unwrap(), real_decode.block_hash()); + assert_eq!( + real_decode.header.validate_pow(real_decode.header.target()).unwrap(), + real_decode.block_hash() + ); assert_eq!(real_decode.header.difficulty(), 2456598); // [test] TODO: check the transaction data @@ -530,8 +514,12 @@ mod tests { #[test] fn validate_pow_test() { let some_header = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b"); - let some_header: Header = deserialize(&some_header).expect("Can't deserialize correct block header"); - assert_eq!(some_header.validate_pow(some_header.target()).unwrap(), some_header.block_hash()); + let some_header: Header = + deserialize(&some_header).expect("Can't deserialize correct block header"); + assert_eq!( + some_header.validate_pow(some_header.target()).unwrap(), + some_header.block_hash() + ); // test with zero target match some_header.validate_pow(Target::ZERO) { @@ -552,7 +540,8 @@ mod tests { fn compact_roundrtip_test() { let some_header = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b"); - let header: Header = deserialize(&some_header).expect("Can't deserialize correct block header"); + let header: Header = + deserialize(&some_header).expect("Can't deserialize correct block header"); assert_eq!(header.bits, header.target().to_compact_lossy()); } @@ -560,7 +549,7 @@ mod tests { #[test] fn soft_fork_signalling() { for i in 0..31 { - let version_int = (0x20000000u32 ^ 1< Transaction { }; // Inputs - let in_script = script::Builder::new().push_int(486604799) - .push_int_non_minimal(4) - .push_slice(b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks") - .into_script(); + let in_script = script::Builder::new() + .push_int(486604799) + .push_int_non_minimal(4) + .push_slice(b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks") + .into_script(); ret.input.push(TxIn { previous_output: OutPoint::null(), script_sig: in_script, @@ -86,14 +87,9 @@ fn bitcoin_genesis_tx() -> Transaction { // Outputs let script_bytes = hex!("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); - let out_script = script::Builder::new() - .push_slice(script_bytes) - .push_opcode(OP_CHECKSIG) - .into_script(); - ret.output.push(TxOut { - value: 50 * COIN_VALUE, - script_pubkey: out_script - }); + let out_script = + script::Builder::new().push_slice(script_bytes).push_opcode(OP_CHECKSIG).into_script(); + ret.output.push(TxOut { value: 50 * COIN_VALUE, script_pubkey: out_script }); // end ret @@ -105,58 +101,50 @@ pub fn genesis_block(network: Network) -> Block { let hash: sha256d::Hash = txdata[0].txid().into(); let merkle_root = hash.into(); match network { - Network::Bitcoin => { - Block { - header: block::Header { - version: block::Version::ONE, - prev_blockhash: Hash::all_zeros(), - merkle_root, - time: 1231006505, - bits: CompactTarget::from_consensus(0x1d00ffff), - nonce: 2083236893 - }, - txdata, - } - } - Network::Testnet => { - Block { - header: block::Header { - version: block::Version::ONE, - prev_blockhash: Hash::all_zeros(), - merkle_root, - time: 1296688602, - bits: CompactTarget::from_consensus(0x1d00ffff), - nonce: 414098458 - }, - txdata, - } - } - Network::Signet => { - Block { - header: block::Header { - version: block::Version::ONE, - prev_blockhash: Hash::all_zeros(), - merkle_root, - time: 1598918400, - bits: CompactTarget::from_consensus(0x1e0377ae), - nonce: 52613770 - }, - txdata, - } - } - Network::Regtest => { - Block { - header: block::Header { - version: block::Version::ONE, - prev_blockhash: Hash::all_zeros(), - merkle_root, - time: 1296688602, - bits: CompactTarget::from_consensus(0x207fffff), - nonce: 2 - }, - txdata, - } - } + Network::Bitcoin => Block { + header: block::Header { + version: block::Version::ONE, + prev_blockhash: Hash::all_zeros(), + merkle_root, + time: 1231006505, + bits: CompactTarget::from_consensus(0x1d00ffff), + nonce: 2083236893, + }, + txdata, + }, + Network::Testnet => Block { + header: block::Header { + version: block::Version::ONE, + prev_blockhash: Hash::all_zeros(), + merkle_root, + time: 1296688602, + bits: CompactTarget::from_consensus(0x1d00ffff), + nonce: 414098458, + }, + txdata, + }, + Network::Signet => Block { + header: block::Header { + version: block::Version::ONE, + prev_blockhash: Hash::all_zeros(), + merkle_root, + time: 1598918400, + bits: CompactTarget::from_consensus(0x1e0377ae), + nonce: 52613770, + }, + txdata, + }, + Network::Regtest => Block { + header: block::Header { + version: block::Version::ONE, + prev_blockhash: Hash::all_zeros(), + merkle_root, + time: 1296688602, + bits: CompactTarget::from_consensus(0x207fffff), + nonce: 2, + }, + txdata, + }, } } @@ -169,13 +157,25 @@ impl_bytes_newtype!(ChainHash, 32); impl ChainHash { // Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md /// `ChainHash` for mainnet bitcoin. - pub const BITCOIN: Self = Self([111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0]); + pub const BITCOIN: Self = Self([ + 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, + 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, + ]); /// `ChainHash` for testnet bitcoin. - pub const TESTNET: Self = Self([67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0]); + pub const TESTNET: Self = Self([ + 67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, + 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0, + ]); /// `ChainHash` for signet bitcoin. - pub const SIGNET: Self = Self([246, 30, 238, 59, 99, 163, 128, 164, 119, 160, 99, 175, 50, 178, 187, 201, 124, 159, 249, 240, 31, 44, 66, 37, 233, 115, 152, 129, 8, 0, 0, 0]); + pub const SIGNET: Self = Self([ + 246, 30, 238, 59, 99, 163, 128, 164, 119, 160, 99, 175, 50, 178, 187, 201, 124, 159, 249, + 240, 31, 44, 66, 37, 233, 115, 152, 129, 8, 0, 0, 0, + ]); /// `ChainHash` for regtest bitcoin. - pub const REGTEST: Self = Self([6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15]); + pub const REGTEST: Self = Self([ + 6, 34, 110, 70, 17, 26, 11, 89, 202, 175, 18, 96, 67, 235, 91, 191, 40, 195, 79, 58, 94, + 51, 42, 31, 199, 178, 183, 60, 241, 136, 145, 15, + ]); /// Returns the hash of the `network` genesis block for use as a chain hash. /// @@ -190,10 +190,10 @@ impl ChainHash { #[cfg(test)] mod test { use super::*; - use crate::network::constants::Network; - use crate::consensus::encode::serialize; use crate::blockdata::locktime::absolute; + use crate::consensus::encode::serialize; use crate::internal_macros::hex; + use crate::network::constants::Network; #[test] fn bitcoin_genesis_first_transaction() { @@ -213,7 +213,10 @@ mod test { assert_eq!(gen.output[0].value, 50 * COIN_VALUE); assert_eq!(gen.lock_time, absolute::LockTime::ZERO); - assert_eq!(gen.wtxid().to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!( + gen.wtxid().to_string(), + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" + ); } #[test] @@ -222,12 +225,18 @@ mod test { assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!( + gen.header.merkle_root.to_string(), + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" + ); assert_eq!(gen.header.time, 1231006505); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header.nonce, 2083236893); - assert_eq!(gen.header.block_hash().to_string(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); + assert_eq!( + gen.header.block_hash().to_string(), + "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" + ); } #[test] @@ -235,11 +244,17 @@ mod test { let gen = genesis_block(Network::Testnet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!( + gen.header.merkle_root.to_string(), + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" + ); assert_eq!(gen.header.time, 1296688602); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header.nonce, 414098458); - assert_eq!(gen.header.block_hash().to_string(), "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"); + assert_eq!( + gen.header.block_hash().to_string(), + "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943" + ); } #[test] @@ -247,11 +262,17 @@ mod test { let gen = genesis_block(Network::Signet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!( + gen.header.merkle_root.to_string(), + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" + ); assert_eq!(gen.header.time, 1598918400); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1e0377ae)); assert_eq!(gen.header.nonce, 52613770); - assert_eq!(gen.header.block_hash().to_string(), "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"); + assert_eq!( + gen.header.block_hash().to_string(), + "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6" + ); } // The *_chain_hash tests are sanity/regression tests, they verify that the const byte array diff --git a/bitcoin/src/blockdata/fee_rate.rs b/bitcoin/src/blockdata/fee_rate.rs index 23e986b1..a0686bda 100644 --- a/bitcoin/src/blockdata/fee_rate.rs +++ b/bitcoin/src/blockdata/fee_rate.rs @@ -1,11 +1,11 @@ //! Implements `FeeRate` and assoctiated features. use core::fmt; -use core::ops::{Mul, Div}; +use core::ops::{Div, Mul}; +use super::Weight; use crate::prelude::*; use crate::Amount; -use super::Weight; /// Represents fee rate. /// @@ -40,9 +40,7 @@ impl FeeRate { pub const DUST: FeeRate = FeeRate::from_sat_per_vb_unchecked(3); /// Constructs `FeeRate` from satoshis per 1000 weight units. - pub const fn from_sat_per_kwu(sat_kwu: u64) -> Self { - FeeRate(sat_kwu) - } + pub const fn from_sat_per_kwu(sat_kwu: u64) -> Self { FeeRate(sat_kwu) } /// Constructs `FeeRate` from satoshis per virtual bytes. /// @@ -57,40 +55,28 @@ impl FeeRate { } /// Constructs `FeeRate` from satoshis per virtual bytes without overflow check. - pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { - FeeRate(sat_vb * (1000 / 4)) - } + pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { FeeRate(sat_vb * (1000 / 4)) } /// Returns raw fee rate. /// /// Can be used instead of `into()` to avoid inference issues. - pub const fn to_sat_per_kwu(self) -> u64 { - self.0 - } + pub const fn to_sat_per_kwu(self) -> u64 { self.0 } /// Converts to sat/vB rounding down. - pub const fn to_sat_per_vb_floor(self) -> u64 { - self.0 / (1000 / 4) - } + pub const fn to_sat_per_vb_floor(self) -> u64 { self.0 / (1000 / 4) } /// Converts to sat/vB rounding up. - pub const fn to_sat_per_vb_ceil(self) -> u64 { - (self.0 + (1000 / 4 - 1)) / (1000 / 4) - } + pub const fn to_sat_per_vb_ceil(self) -> u64 { (self.0 + (1000 / 4 - 1)) / (1000 / 4) } /// Checked multiplication. /// /// Computes `self * rhs` returning `None` if overflow occurred. - pub fn checked_mul(self, rhs: u64) -> Option { - self.0.checked_mul(rhs).map(Self) - } + pub fn checked_mul(self, rhs: u64) -> Option { self.0.checked_mul(rhs).map(Self) } /// Checked division. /// /// Computes `self / rhs` returning `None` if `rhs == 0`. - pub fn checked_div(self, rhs: u64) -> Option { - self.0.checked_div(rhs).map(Self) - } + pub fn checked_div(self, rhs: u64) -> Option { self.0.checked_div(rhs).map(Self) } } /// Alternative will display the unit. @@ -105,9 +91,7 @@ impl fmt::Display for FeeRate { } impl From for u64 { - fn from(value: FeeRate) -> Self { - value.to_sat_per_kwu() - } + fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() } } /// Computes ceiling so that fee computation is conservative. @@ -122,26 +106,23 @@ impl Mul for Weight { impl Mul for FeeRate { type Output = Amount; - fn mul(self, rhs: Weight) -> Self::Output { - rhs * self - } + fn mul(self, rhs: Weight) -> Self::Output { rhs * self } } impl Div for Amount { type Output = FeeRate; - fn div(self, rhs: Weight) -> Self::Output { - FeeRate(self.to_sat() * 1000 / rhs.to_wu()) - } + fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) } } crate::parse::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu); #[cfg(test)] mod tests { - use super::*; use std::u64; + use super::*; + #[test] fn fee_rate_const_test() { assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu()); @@ -171,9 +152,7 @@ mod tests { #[test] #[should_panic] - fn from_sat_per_vb_unchecked_panic_test() { - FeeRate::from_sat_per_vb_unchecked(u64::MAX); - } + fn from_sat_per_vb_unchecked_panic_test() { FeeRate::from_sat_per_vb_unchecked(u64::MAX); } #[test] fn raw_feerate_test() { diff --git a/bitcoin/src/blockdata/locktime/absolute.rs b/bitcoin/src/blockdata/locktime/absolute.rs index 1f2fad50..53546093 100644 --- a/bitcoin/src/blockdata/locktime/absolute.rs +++ b/bitcoin/src/blockdata/locktime/absolute.rs @@ -7,24 +7,22 @@ //! whether `LockTime < LOCKTIME_THRESHOLD`. //! -use core::{mem, fmt}; -use core::cmp::{PartialOrd, Ordering}; +use core::cmp::{Ordering, PartialOrd}; +use core::{fmt, mem}; use bitcoin_internals::write_err; - #[cfg(all(test, mutate))] use mutagen::mutate; +#[cfg(doc)] +use crate::absolute; use crate::consensus::encode::{self, Decodable, Encodable}; use crate::error::ParseIntError; use crate::io::{self, Read, Write}; -use crate::parse::{impl_parse_str_from_int_infallible, impl_parse_str_from_int_fallible}; +use crate::parse::{impl_parse_str_from_int_fallible, impl_parse_str_from_int_infallible}; use crate::prelude::*; use crate::string::FromHexStr; -#[cfg(doc)] -use crate::absolute; - /// The Threshold for deciding whether a lock time value is a height or a time (see [Bitcoin Core]). /// /// `LockTime` values _below_ the threshold are interpreted as block heights, values _above_ (or @@ -173,9 +171,7 @@ impl LockTime { /// Returns true if this lock time value is a block time (UNIX timestamp). #[inline] - pub fn is_block_time(&self) -> bool { - !self.is_block_height() - } + pub fn is_block_time(&self) -> bool { !self.is_block_height() } /// Returns true if this timelock constraint is satisfied by the respective `height`/`time`. /// @@ -278,16 +274,12 @@ impl_parse_str_from_int_infallible!(LockTime, u32, from_consensus); impl From for LockTime { #[inline] - fn from(h: Height) -> Self { - LockTime::Blocks(h) - } + fn from(h: Height) -> Self { LockTime::Blocks(h) } } impl From