Use `Amount` type for dust value calculation

This commit is contained in:
Sebastian Geisler 2021-06-12 21:15:13 +02:00
parent 9b2098517e
commit 9981da2ec8
1 changed files with 7 additions and 5 deletions

View File

@ -409,11 +409,11 @@ impl Script {
/// Gets the minimum value an output with this script should have in order to be /// Gets the minimum value an output with this script should have in order to be
/// broadcastable on today's bitcoin network. /// 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 // 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 // otherwise allow users to create transactions which likely can never be
// broadcasted/confirmed. // 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() { if self.is_op_return() {
0 0
} else if self.is_witness_program() { } else if self.is_witness_program() {
@ -424,7 +424,9 @@ impl Script {
32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core 32 + 4 + 1 + 107 + 4 + // The spend cost copied from Core
8 + // The serialized size of the TxOut's amount field 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 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 /// Iterate over the script in the form of `Instruction`s, which are an enum covering
@ -1260,7 +1262,7 @@ mod test {
// well-known scriptPubKey types. // well-known scriptPubKey types.
let script_p2wpkh = Builder::new().push_int(0).push_slice(&[42; 20]).into_script(); let script_p2wpkh = Builder::new().push_int(0).push_slice(&[42; 20]).into_script();
assert!(script_p2wpkh.is_v0_p2wpkh()); 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() let script_p2pkh = Builder::new()
.push_opcode(opcodes::all::OP_DUP) .push_opcode(opcodes::all::OP_DUP)
@ -1270,7 +1272,7 @@ mod test {
.push_opcode(opcodes::all::OP_CHECKSIG) .push_opcode(opcodes::all::OP_CHECKSIG)
.into_script(); .into_script();
assert!(script_p2pkh.is_p2pkh()); assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.dust_value(), 546); assert_eq!(script_p2pkh.dust_value(), ::Amount::from_sat(546));
} }
} }