Update minimum fee calculations to match Core.

Calculate rate*fee first before dividing by 1000.
Add more unit tests around the fee calculations.
This commit is contained in:
Ben Echols 2015-10-29 01:08:27 -06:00 committed by Ben Echols
parent 7811770d31
commit 489ba8d31d
2 changed files with 46 additions and 11 deletions

View file

@ -26,11 +26,14 @@ const (
func calcMinRequiredTxRelayFee(serializedSize int64, minRelayTxFee btcutil.Amount) int64 { func calcMinRequiredTxRelayFee(serializedSize int64, minRelayTxFee btcutil.Amount) int64 {
// Calculate the minimum fee for a transaction to be allowed into the // Calculate the minimum fee for a transaction to be allowed into the
// mempool and relayed by scaling the base fee (which is the minimum // mempool and relayed by scaling the base fee (which is the minimum
// free transaction relay fee). minRelayTxFee is in Satoshi/KB, so // free transaction relay fee). minTxRelayFee is in Satoshi/kB so
// divide the transaction size by 1000 to convert to kilobytes. Also, // multiply by serializedSize (which is in bytes) and divide by 1000 to get
// integer division is used so fees only increase on full kilobyte // minimum Satoshis.
// boundaries. minFee := (serializedSize * int64(minRelayTxFee)) / 1000
minFee := (1 + serializedSize/1000) * int64(minRelayTxFee)
if minFee == 0 && minRelayTxFee > 0 {
minFee = int64(minRelayTxFee)
}
// Set the minimum fee to the maximum possible value if the calculated // Set the minimum fee to the maximum possible value if the calculated
// fee is not in the valid range for monetary amounts. // fee is not in the valid range for monetary amounts.

View file

@ -22,22 +22,24 @@ func TestCalcMinRequiredTxRelayFee(t *testing.T) {
want int64 // Expected fee. want int64 // Expected fee.
}{ }{
{ {
"zero value with default minimum relay fee", // Ensure combination of size and fee that are less than 1000
0, // produce a non-zero fee.
defaultMinRelayTxFee, "250 bytes with relay fee of 3",
int64(defaultMinRelayTxFee), 250,
3,
3,
}, },
{ {
"100 bytes with default minimum relay fee", "100 bytes with default minimum relay fee",
100, 100,
defaultMinRelayTxFee, defaultMinRelayTxFee,
int64(defaultMinRelayTxFee), 100,
}, },
{ {
"max standard tx size with default minimum relay fee", "max standard tx size with default minimum relay fee",
maxStandardTxSize, maxStandardTxSize,
defaultMinRelayTxFee, defaultMinRelayTxFee,
101000, 100000,
}, },
{ {
"max standard tx size with max satoshi relay fee", "max standard tx size with max satoshi relay fee",
@ -45,6 +47,36 @@ func TestCalcMinRequiredTxRelayFee(t *testing.T) {
btcutil.MaxSatoshi, btcutil.MaxSatoshi,
btcutil.MaxSatoshi, btcutil.MaxSatoshi,
}, },
{
"1500 bytes with 5000 relay fee",
1500,
5000,
7500,
},
{
"1500 bytes with 3000 relay fee",
1500,
3000,
4500,
},
{
"782 bytes with 5000 relay fee",
782,
5000,
3910,
},
{
"782 bytes with 3000 relay fee",
782,
3000,
2346,
},
{
"782 bytes with 2550 relay fee",
782,
2550,
1994,
},
} }
for _, test := range tests { for _, test := range tests {