Use `crate::prelude::*` rather than specific imports
[rust-lightning] / lightning / src / util / transaction_utils.rs
index 12768543783cd148b7874a0009aa6b44650cd71a..59b7be6073c1dd9a1516adb25f3a41af1d0222f5 100644 (file)
@@ -8,14 +8,16 @@
 // licenses.
 
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
-use bitcoin::blockdata::script::Script;
+use bitcoin::blockdata::script::ScriptBuf;
 use bitcoin::consensus::Encodable;
 use bitcoin::consensus::encode::VarInt;
 
-use ln::msgs::MAX_VALUE_MSAT;
+use crate::ln::msgs::MAX_VALUE_MSAT;
 
-use prelude::*;
-use io_extras::sink;
+#[allow(unused_imports)]
+use crate::prelude::*;
+
+use crate::io_extras::sink;
 use core::cmp::Ordering;
 
 pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
@@ -33,10 +35,10 @@ pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>
 /// Assumes at least one input will have a witness (ie spends a segwit output).
 /// Returns an Err(()) if the requested feerate cannot be met.
 /// Returns the expected maximum weight of the fully signed transaction on success.
-pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, witness_max_weight: usize, feerate_sat_per_1000_weight: u32, change_destination_script: Script) -> Result<usize, ()> {
+pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, witness_max_weight: u64, feerate_sat_per_1000_weight: u32, change_destination_script: ScriptBuf) -> Result<u64, ()> {
        if input_value > MAX_VALUE_MSAT / 1000 { return Err(()); }
 
-       const WITNESS_FLAG_BYTES: i64 = 2;
+       const WITNESS_FLAG_BYTES: u64 = 2;
 
        let mut output_value = 0;
        for output in tx.output.iter() {
@@ -50,16 +52,16 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
                value: 0,
        };
        let change_len = change_output.consensus_encode(&mut sink()).unwrap();
-       let starting_weight = tx.weight() + WITNESS_FLAG_BYTES as usize + witness_max_weight;
+       let starting_weight = tx.weight().to_wu() + WITNESS_FLAG_BYTES + witness_max_weight as u64;
        let mut weight_with_change: i64 = starting_weight as i64 + change_len as i64 * 4;
        // Include any extra bytes required to push an extra output.
        weight_with_change += (VarInt(tx.output.len() as u64 + 1).len() - VarInt(tx.output.len() as u64).len()) as i64 * 4;
        // When calculating weight, add two for the flag bytes
        let change_value: i64 = (input_value - output_value) as i64 - weight_with_change * feerate_sat_per_1000_weight as i64 / 1000;
-       if change_value >= dust_value.as_sat() as i64 {
+       if change_value >= dust_value.to_sat() as i64 {
                change_output.value = change_value as u64;
                tx.output.push(change_output);
-               Ok(weight_with_change as usize)
+               Ok(weight_with_change as u64)
        } else if (input_value - output_value) as i64 - (starting_weight as i64) * feerate_sat_per_1000_weight as i64 / 1000 < 0 {
                Err(())
        } else {
@@ -71,15 +73,13 @@ pub(crate) fn maybe_add_change_output(tx: &mut Transaction, input_value: u64, wi
 mod tests {
        use super::*;
 
-       use bitcoin::blockdata::transaction::{Transaction, TxOut, TxIn, OutPoint};
-       use bitcoin::blockdata::script::{Script, Builder};
+       use bitcoin::blockdata::locktime::absolute::LockTime;
+       use bitcoin::blockdata::transaction::{TxIn, OutPoint};
+       use bitcoin::blockdata::script::Builder;
        use bitcoin::hash_types::{PubkeyHash, Txid};
-
-       use bitcoin::hashes::sha256d::Hash as Sha256dHash;
        use bitcoin::hashes::Hash;
-       use bitcoin::Witness;
-
-       use hex::decode;
+       use bitcoin::hashes::hex::FromHex;
+       use bitcoin::{Sequence, Witness};
 
        use alloc::vec;
 
@@ -170,8 +170,8 @@ mod tests {
                );
        }
 
-       fn script_from_hex(hex_str: &str) -> Script {
-               Script::from(decode(hex_str).unwrap())
+       fn script_from_hex(hex_str: &str) -> ScriptBuf {
+               ScriptBuf::from(<Vec<u8>>::from_hex(hex_str).unwrap())
        }
 
        macro_rules! bip_txout_tests {
@@ -215,23 +215,23 @@ mod tests {
        #[test]
        fn test_tx_value_overrun() {
                // If we have a bogus input amount or outputs valued more than inputs, we should fail
-               let mut tx = Transaction { version: 2, lock_time: 0, input: Vec::new(), output: vec![TxOut {
-                       script_pubkey: Script::new(), value: 1000
+               let mut tx = Transaction { version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: vec![TxOut {
+                       script_pubkey: ScriptBuf::new(), value: 1000
                }] };
-               assert!(maybe_add_change_output(&mut tx, 21_000_000_0000_0001, 0, 253, Script::new()).is_err());
-               assert!(maybe_add_change_output(&mut tx, 400, 0, 253, Script::new()).is_err());
-               assert!(maybe_add_change_output(&mut tx, 4000, 0, 253, Script::new()).is_ok());
+               assert!(maybe_add_change_output(&mut tx, 21_000_000_0000_0001, 0, 253, ScriptBuf::new()).is_err());
+               assert!(maybe_add_change_output(&mut tx, 400, 0, 253, ScriptBuf::new()).is_err());
+               assert!(maybe_add_change_output(&mut tx, 4000, 0, 253, ScriptBuf::new()).is_ok());
        }
 
        #[test]
        fn test_tx_change_edge() {
                // Check that we never add dust outputs
-               let mut tx = Transaction { version: 2, lock_time: 0, input: Vec::new(), output: Vec::new() };
+               let mut tx = Transaction { version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
                let orig_wtxid = tx.wtxid();
-               let output_spk = Script::new_p2pkh(&PubkeyHash::hash(&[0; 0]));
-               assert_eq!(output_spk.dust_value().as_sat(), 546);
+               let output_spk = ScriptBuf::new_p2pkh(&PubkeyHash::hash(&[0; 0]));
+               assert_eq!(output_spk.dust_value().to_sat(), 546);
                // 9 sats isn't enough to pay fee on a dummy transaction...
-               assert_eq!(tx.weight() as u64, 40); // ie 10 vbytes
+               assert_eq!(tx.weight().to_wu(), 40); // ie 10 vbytes
                assert!(maybe_add_change_output(&mut tx, 9, 0, 250, output_spk.clone()).is_err());
                assert_eq!(tx.wtxid(), orig_wtxid); // Failure doesn't change the transaction
                // but 10-564 is, just not enough to add a change output...
@@ -251,7 +251,7 @@ mod tests {
                assert_eq!(tx.output.len(), 1);
                assert_eq!(tx.output[0].value, 546);
                assert_eq!(tx.output[0].script_pubkey, output_spk);
-               assert_eq!(tx.weight() / 4, 590-546); // New weight is exactly the fee we wanted.
+               assert_eq!(tx.weight().to_wu() / 4, 590-546); // New weight is exactly the fee we wanted.
 
                tx.output.pop();
                assert_eq!(tx.wtxid(), orig_wtxid); // The only change is the addition of one output.
@@ -260,16 +260,16 @@ mod tests {
        #[test]
        fn test_tx_extra_outputs() {
                // Check that we correctly handle existing outputs
-               let mut tx = Transaction { version: 2, lock_time: 0, input: vec![TxIn {
-                       previous_output: OutPoint::new(Txid::from_hash(Sha256dHash::default()), 0), script_sig: Script::new(), witness: Witness::new(), sequence: 0,
+               let mut tx = Transaction { version: 2, lock_time: LockTime::ZERO, input: vec![TxIn {
+                       previous_output: OutPoint::new(Txid::all_zeros(), 0), script_sig: ScriptBuf::new(), witness: Witness::new(), sequence: Sequence::ZERO,
                }], output: vec![TxOut {
                        script_pubkey: Builder::new().push_int(1).into_script(), value: 1000
                }] };
                let orig_wtxid = tx.wtxid();
-               let orig_weight = tx.weight();
+               let orig_weight = tx.weight().to_wu();
                assert_eq!(orig_weight / 4, 61);
 
-               assert_eq!(Builder::new().push_int(2).into_script().dust_value().as_sat(), 474);
+               assert_eq!(Builder::new().push_int(2).into_script().dust_value().to_sat(), 474);
 
                // Input value of the output value + fee - 1 should fail:
                assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100 - 1, 400, 250, Builder::new().push_int(2).into_script()).is_err());
@@ -285,7 +285,7 @@ mod tests {
                assert_eq!(tx.output.len(), 2);
                assert_eq!(tx.output[1].value, 474);
                assert_eq!(tx.output[1].script_pubkey, Builder::new().push_int(2).into_script());
-               assert_eq!(tx.weight() - orig_weight, 40); // Weight difference matches what we had to add above
+               assert_eq!(tx.weight().to_wu() - orig_weight, 40); // Weight difference matches what we had to add above
                tx.output.pop();
                assert_eq!(tx.wtxid(), orig_wtxid); // The only change is the addition of one output.
        }