Skip to content

Commit e334c9d

Browse files
authored
Merge pull request rust-bitcoin#616 from sgeisler/2021-06-dust-value-amount
Use `Amount` type for dust value calculation
2 parents b0ae2a6 + 9981da2 commit e334c9d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/blockdata/script.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,11 @@ impl Script {
411411

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

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

12671269
let script_p2pkh = Builder::new()
12681270
.push_opcode(opcodes::all::OP_DUP)
@@ -1272,7 +1274,7 @@ mod test {
12721274
.push_opcode(opcodes::all::OP_CHECKSIG)
12731275
.into_script();
12741276
assert!(script_p2pkh.is_p2pkh());
1275-
assert_eq!(script_p2pkh.dust_value(), 546);
1277+
assert_eq!(script_p2pkh.dust_value(), ::Amount::from_sat(546));
12761278
}
12771279
}
12781280

0 commit comments

Comments
 (0)