Skip to content

Commit

Permalink
Use Amount type for dust value calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeisler committed Jun 12, 2021
1 parent 9b20985 commit 9981da2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/blockdata/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ impl Script {

/// Gets the minimum value an output with this script should have in order to be
/// broadcastable on today's bitcoin network.
pub fn dust_value(&self) -> u64 {
pub fn dust_value(&self) -> ::Amount {
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
// otherwise allow users to create transactions which likely can never be
// broadcasted/confirmed.
DUST_RELAY_TX_FEE as u64 / 1000 * // The default dust relay fee is 3000 satoshi/kB (ie 3 sat/vByte)
let sats = DUST_RELAY_TX_FEE as u64 / 1000 * // The default dust relay fee is 3000 satoshi/kB (ie 3 sat/vByte)
if self.is_op_return() {
0
} else if self.is_witness_program() {
Expand All @@ -424,7 +424,9 @@ impl Script {
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field
self.consensus_encode(&mut ::std::io::sink()).unwrap() as u64 // The serialized size of this script_pubkey
}
};

::Amount::from_sat(sats)
}

/// Iterate over the script in the form of `Instruction`s, which are an enum covering
Expand Down Expand Up @@ -1260,7 +1262,7 @@ mod test {
// well-known scriptPubKey types.
let script_p2wpkh = Builder::new().push_int(0).push_slice(&[42; 20]).into_script();
assert!(script_p2wpkh.is_v0_p2wpkh());
assert_eq!(script_p2wpkh.dust_value(), 294);
assert_eq!(script_p2wpkh.dust_value(), ::Amount::from_sat(294));

let script_p2pkh = Builder::new()
.push_opcode(opcodes::all::OP_DUP)
Expand All @@ -1270,7 +1272,7 @@ mod test {
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.dust_value(), 546);
assert_eq!(script_p2pkh.dust_value(), ::Amount::from_sat(546));
}
}

0 comments on commit 9981da2

Please sign in to comment.