txrules: add threshold function in order to reuse it in lnd

This commit is contained in:
Andrey Samokhvalov 2016-11-23 17:51:33 +03:00 committed by Olaoluwa Osuntokun
parent 504b045332
commit 5785ca17cc

View file

@ -17,10 +17,9 @@ import (
// DefaultRelayFeePerKb is the default minimum relay fee policy for a mempool. // DefaultRelayFeePerKb is the default minimum relay fee policy for a mempool.
const DefaultRelayFeePerKb btcutil.Amount = 1e3 const DefaultRelayFeePerKb btcutil.Amount = 1e3
// IsDustAmount determines whether a transaction output value and script length would // GetDustThreshold is used to define the amount below which output will be
// cause the output to be considered dust. Transactions with dust outputs are // determined as dust. Threshold is determined as 3 times the relay fee.
// not standard and are rejected by mempools with default policies. func GetDustThreshold(scriptSize int, relayFeePerKb btcutil.Amount) btcutil.Amount {
func IsDustAmount(amount btcutil.Amount, scriptSize int, relayFeePerKb btcutil.Amount) bool {
// Calculate the total (estimated) cost to the network. This is // Calculate the total (estimated) cost to the network. This is
// calculated using the serialize size of the output plus the serial // calculated using the serialize size of the output plus the serial
// size of a transaction input which redeems it. The output is assumed // size of a transaction input which redeems it. The output is assumed
@ -30,9 +29,16 @@ func IsDustAmount(amount btcutil.Amount, scriptSize int, relayFeePerKb btcutil.A
totalSize := 8 + wire.VarIntSerializeSize(uint64(scriptSize)) + totalSize := 8 + wire.VarIntSerializeSize(uint64(scriptSize)) +
scriptSize + 148 scriptSize + 148
// Dust is defined as an output value where the total cost to the network byteFee := relayFeePerKb / 1000
// (output size + input size) is greater than 1/3 of the relay fee. relayFee := btcutil.Amount(totalSize) * byteFee
return int64(amount)*1000/(3*int64(totalSize)) < int64(relayFeePerKb) return 3 * relayFee
}
// IsDustAmount determines whether a transaction output value and script length would
// cause the output to be considered dust. Transactions with dust outputs are
// not standard and are rejected by mempools with default policies.
func IsDustAmount(amount btcutil.Amount, scriptSize int, relayFeePerKb btcutil.Amount) bool {
return amount < GetDustThreshold(scriptSize, relayFeePerKb)
} }
// IsDustOutput determines whether a transaction output is considered dust. // IsDustOutput determines whether a transaction output is considered dust.