using dust value from rust-bitcoin

This commit is contained in:
James Taylor 2021-12-19 02:55:24 -05:00
parent 77bce06caf
commit ca682819b3
No known key found for this signature in database
GPG Key ID: 74670007569E1EDC
2 changed files with 10 additions and 12 deletions

View File

@ -53,7 +53,7 @@ use address_validator::AddressValidator;
use coin_selection::DefaultCoinSelectionAlgorithm; use coin_selection::DefaultCoinSelectionAlgorithm;
use signer::{SignOptions, Signer, SignerOrdering, SignersContainer}; use signer::{SignOptions, Signer, SignerOrdering, SignersContainer};
use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxParams}; use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxParams};
use utils::{check_nlocktime, check_nsequence_rbf, After, Older, SecpCtx, DUST_LIMIT_SATOSHI}; use utils::{check_nlocktime, check_nsequence_rbf, After, Older, SecpCtx};
use crate::blockchain::{Blockchain, Progress}; use crate::blockchain::{Blockchain, Progress};
use crate::database::memory::MemoryDatabase; use crate::database::memory::MemoryDatabase;
@ -601,7 +601,7 @@ where
let recipients = params.recipients.iter().map(|(r, v)| (r, *v)); let recipients = params.recipients.iter().map(|(r, v)| (r, *v));
for (index, (script_pubkey, value)) in recipients.enumerate() { for (index, (script_pubkey, value)) in recipients.enumerate() {
if value.is_dust() && !script_pubkey.is_provably_unspendable() { if value.is_dust(script_pubkey) && !script_pubkey.is_provably_unspendable() {
return Err(Error::OutputBelowDustLimit(index)); return Err(Error::OutputBelowDustLimit(index));
} }
@ -677,9 +677,9 @@ where
if tx.output.is_empty() { if tx.output.is_empty() {
if params.drain_to.is_some() { if params.drain_to.is_some() {
if drain_val.is_dust() { if drain_val.is_dust(&drain_output.script_pubkey) {
return Err(Error::InsufficientFunds { return Err(Error::InsufficientFunds {
needed: DUST_LIMIT_SATOSHI, needed: drain_output.script_pubkey.dust_value().as_sat(),
available: drain_val, available: drain_val,
}); });
} }
@ -688,7 +688,7 @@ where
} }
} }
if drain_val.is_dust() { if drain_val.is_dust(&drain_output.script_pubkey) {
fee_amount += drain_val; fee_amount += drain_val;
} else { } else {
drain_output.value = drain_val; drain_output.value = drain_val;
@ -3424,7 +3424,7 @@ pub(crate) mod test {
.unwrap(); .unwrap();
let mut builder = wallet.build_fee_bump(txid).unwrap(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb(140.0)); builder.fee_rate(FeeRate::from_sat_per_vb(141.0));
let (psbt, details) = builder.finish().unwrap(); let (psbt, details) = builder.finish().unwrap();
assert_eq!( assert_eq!(

View File

@ -9,13 +9,11 @@
// You may not use this file except in accordance with one or both of these // You may not use this file except in accordance with one or both of these
// licenses. // licenses.
use bitcoin::blockdata::script::Script;
use bitcoin::secp256k1::{All, Secp256k1}; use bitcoin::secp256k1::{All, Secp256k1};
use miniscript::{MiniscriptKey, Satisfier, ToPublicKey}; use miniscript::{MiniscriptKey, Satisfier, ToPublicKey};
// De-facto standard "dust limit" (even though it should change based on the output type)
pub const DUST_LIMIT_SATOSHI: u64 = 546;
// MSB of the nSequence. If set there's no consensus-constraint, so it must be disabled when // MSB of the nSequence. If set there's no consensus-constraint, so it must be disabled when
// spending using CSV in order to enforce CSV rules // spending using CSV in order to enforce CSV rules
pub(crate) const SEQUENCE_LOCKTIME_DISABLE_FLAG: u32 = 1 << 31; pub(crate) const SEQUENCE_LOCKTIME_DISABLE_FLAG: u32 = 1 << 31;
@ -34,12 +32,12 @@ pub(crate) const BLOCKS_TIMELOCK_THRESHOLD: u32 = 500000000;
// encourage the usage of this trait. // encourage the usage of this trait.
pub trait IsDust { pub trait IsDust {
/// Check whether or not a value is below dust limit /// Check whether or not a value is below dust limit
fn is_dust(&self) -> bool; fn is_dust(&self, script: &Script) -> bool;
} }
impl IsDust for u64 { impl IsDust for u64 {
fn is_dust(&self) -> bool { fn is_dust(&self, script: &Script) -> bool {
*self <= DUST_LIMIT_SATOSHI *self <= script.dust_value().as_sat()
} }
} }