Skip to content

Commit

Permalink
mempool: introduce GetDustThreshold to export dust limit calculation
Browse files Browse the repository at this point in the history
This commit modifies no behavior and would allow other projects to
retrieve the dust limit for a particular output type before the
amount of the output is known. This is particularly useful in the
Lightning Network for channel negotiation.
  • Loading branch information
Crypt-iQ authored and jcvernaleo committed Sep 16, 2021
1 parent 5e6736a commit f8e6854
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions mempool/policy.go
Expand Up @@ -172,17 +172,10 @@ func checkPkScriptStandard(pkScript []byte, scriptClass txscript.ScriptClass) er
return nil
}

// IsDust returns whether or not the passed transaction output amount is
// considered dust or not based on the passed minimum transaction relay fee.
// Dust is defined in terms of the minimum transaction relay fee. In
// particular, if the cost to the network to spend coins is more than 1/3 of the
// minimum transaction relay fee, it is considered dust.
func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
// Unspendable outputs are considered dust.
if txscript.IsUnspendable(txOut.PkScript) {
return true
}

// GetDustThreshold calculates the dust limit for a *wire.TxOut by taking the
// size of a typical spending transaction and multiplying it by 3 to account
// for the minimum dust relay fee of 3000sat/kvb.
func GetDustThreshold(txOut *wire.TxOut) int64 {
// The total serialized size consists of the output and the associated
// input script to redeem it. Since there is no input script
// to redeem it yet, use the minimum size of a typical input script.
Expand Down Expand Up @@ -253,6 +246,20 @@ func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
totalSize += 107
}

return 3 * int64(totalSize)
}

// IsDust returns whether or not the passed transaction output amount is
// considered dust or not based on the passed minimum transaction relay fee.
// Dust is defined in terms of the minimum transaction relay fee. In
// particular, if the cost to the network to spend coins is more than 1/3 of the
// minimum transaction relay fee, it is considered dust.
func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
// Unspendable outputs are considered dust.
if txscript.IsUnspendable(txOut.PkScript) {
return true
}

// The output is considered dust if the cost to the network to spend the
// coins is more than 1/3 of the minimum free transaction relay fee.
// minFreeTxRelayFee is in Satoshi/KB, so multiply by 1000 to
Expand All @@ -265,7 +272,7 @@ func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
//
// The following is equivalent to (value/totalSize) * (1/3) * 1000
// without needing to do floating point math.
return txOut.Value*1000/(3*int64(totalSize)) < int64(minRelayTxFee)
return txOut.Value*1000/GetDustThreshold(txOut) < int64(minRelayTxFee)
}

// checkTransactionStandard performs a series of checks on a transaction to
Expand Down

0 comments on commit f8e6854

Please sign in to comment.