github.com/decred/dcrlnd@v0.7.6/lnwallet/parameters.go (about)

     1  package lnwallet
     2  
     3  import (
     4  	"decred.org/dcrwallet/v4/wallet/txrules"
     5  	"github.com/decred/dcrd/dcrutil/v4"
     6  	"github.com/decred/dcrlnd/input"
     7  	"github.com/decred/dcrlnd/lnwallet/chainfee"
     8  	"github.com/decred/dcrlnd/lnwire"
     9  )
    10  
    11  var (
    12  	// RoutingFee100PercentUpTo is the cut-off amount we allow 100% fees to
    13  	// be charged up to.
    14  	RoutingFee100PercentUpTo = lnwire.NewMAtomsFromAtoms(1_000)
    15  )
    16  
    17  const (
    18  	// DefaultRoutingFeePercentage is the default off-chain routing fee we
    19  	// allow to be charged for a payment over the RoutingFee100PercentUpTo
    20  	// size.
    21  	DefaultRoutingFeePercentage lnwire.MilliAtom = 5
    22  )
    23  
    24  // DefaultRoutingFeeLimitForAmount returns the default off-chain routing fee
    25  // limit lnd uses if the user does not specify a limit manually. The fee is
    26  // amount dependent because of the base routing fee that is set on many
    27  // channels. For example the default base fee is 1 satoshi. So sending a payment
    28  // of one satoshi will cost 1 satoshi in fees over most channels, which comes to
    29  // a fee of 100%. That's why for very small amounts we allow 100% fee.
    30  func DefaultRoutingFeeLimitForAmount(a lnwire.MilliAtom) lnwire.MilliAtom {
    31  	// Allow 100% fees up to a certain amount to accommodate for base fees.
    32  	if a <= RoutingFee100PercentUpTo {
    33  		return a
    34  	}
    35  
    36  	// Everything larger than the cut-off amount will get a default fee
    37  	// percentage.
    38  	return a * DefaultRoutingFeePercentage / 100
    39  }
    40  
    41  // DustLimitForSize retrieves the dust limit for a given pkscript size.  It
    42  // must be called with a proper size parameter or else a panic occurs.
    43  func DustLimitForSize(scriptSize int64) dcrutil.Amount {
    44  	// With the size of the script, determine which type of pkscript to
    45  	// create. This will be used in the call to GetDustThreshold. We pass
    46  	// in an empty byte slice since the contents of the script itself don't
    47  	// matter.
    48  	switch scriptSize {
    49  	case input.P2PKHPkScriptSize, input.P2SHPkScriptSize:
    50  	default:
    51  		panic("invalid script size")
    52  
    53  	}
    54  
    55  	return defaultDustLimit()
    56  }
    57  
    58  // defaultDustLimit is used to calculate the default dust threshold limit,
    59  // assuming the network uses the defaultRelayFeePerKb of the wallet.
    60  func defaultDustLimit() dcrutil.Amount {
    61  	return DustThresholdForRelayFee(chainfee.AtomPerKByte(txrules.DefaultRelayFeePerKb))
    62  }
    63  
    64  // DustThresholdForRelayFee returns the minimum amount an output of the given
    65  // size should have in order to not be considered a dust output for networks
    66  // using the provided relay fee.
    67  //
    68  // It is assumed the output is paying to a P2PKH script.
    69  func DustThresholdForRelayFee(relayFeeRate chainfee.AtomPerKByte) dcrutil.Amount {
    70  	// Size to redeem a p2pkh script is the size of an input + size of a
    71  	// serialized p2pkh signature script (varint length + OP_DATA_73 + 73 +
    72  	// OP_DATA_33 + 33)
    73  	//
    74  	// This adds up to 165 bytes.
    75  	inputRedeemSize := input.InputSize + input.P2PKHSigScriptSize
    76  
    77  	// Calculate the total (estimated) cost to the network.  This is
    78  	// calculated using the serialize size of the output plus the serial
    79  	// size of a transaction input which redeems it.  The output is assumed
    80  	// to be compressed P2PKH as this is the most common script type. The serialized
    81  	// varint size of a P2PKH script is 1.
    82  	//
    83  	// This adds up to 201 bytes.
    84  	scriptSize := input.P2PKHPkScriptSize
    85  	totalSize := input.OutputSize + 1 + scriptSize + inputRedeemSize
    86  
    87  	// Calculate the relay fee for this test tx in atoms, given its
    88  	// estimated totalSize and the provided relayFeeRate in atoms/kB.
    89  	//
    90  	// With the currently standard min relay fee rate of 10000 atoms/kB,
    91  	// this adds up to 2010.
    92  	relayFee := totalSize * int64(relayFeeRate) / 1000
    93  
    94  	// Threshold for dustiness is determined as 3 times the relay fee.  The
    95  	// final result for the currently standard fee rate is 6030 atoms.
    96  	return dcrutil.Amount(3 * relayFee)
    97  }