Skip to content

Commit

Permalink
Merge #2153: [Tests] Don't create dust outputs in small_txpuzzle_randfee
Browse files Browse the repository at this point in the history
a3bfe80 [Tests] Don't create dust outputs in small_txpuzzle_randfee (random-zebra)

Pull request description:

  `small_txpuzzle_randfee` function (in `feature_fee_estimation` functional test) can rarely create raw transactions with dust outputs, which cause the test failure (https://github.com/PIVX-Project/PIVX/runs/1745765860).

  Prevent this by checking the change value, and adding it to the fee, if it is below the threshold, same as we do in internally in `CWallet::CreateTransaction`.

ACKs for top commit:
  furszy:
    utACK a3bfe80
  Fuzzbawls:
    ACK a3bfe80

Tree-SHA512: ce6358297842e3c1a151feefeddee71fe50d88894d3300d1178530c838937c664c0063d30cc82dcc2419dacc4101624f82e6ef3db3015019bbf6b815f1665fec
  • Loading branch information
random-zebra committed Feb 1, 2021
2 parents 3c60768 + a3bfe80 commit f6509c3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions test/functional/feature_fee_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from test_framework.mininode import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN

# Use as minTxFee
MIN_FEE = Decimal("0.0001")
MIN_FEE = Decimal("0.00001")

# Construct 2 trivial P2SH's and the ScriptSigs that spend them
# So we can create many transactions without needing to spend
Expand All @@ -35,6 +35,9 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
It adds the newly created outputs to the unconfirmed list.
Returns (raw transaction, fee)
"""
# Don't send dust (3 * dustRelayFee.GetFee(182))
DUST_THRESHOLD = 55
assert int(amount*COIN) > DUST_THRESHOLD
# It's best to exponentially distribute our random fees
# because the buckets are exponentially spaced.
# Exponentially distributed from 1-128 * fee_increment
Expand All @@ -54,15 +57,24 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
if total_in <= amount + fee:
raise RuntimeError("Insufficient funds: need %d, have %d"%(amount+fee, total_in))
tx.vout.append(CTxOut(int((total_in - amount - fee)*COIN), P2SH_1))
tx.vout.append(CTxOut(int(amount*COIN), P2SH_2))

tx.vout.append(CTxOut(int(amount * COIN), P2SH_1))
# Prevent the creation of dust change outputs (otherwise add the change value to the fee)
change = int((total_in - amount - fee)*COIN)
if change > DUST_THRESHOLD:
tx.vout.append(CTxOut(change, P2SH_2))
else:
fee += change

# These transactions don't need to be signed, but we still have to insert
# the ScriptSig that will satisfy the ScriptPubKey.
for inp in tx.vin:
inp.scriptSig = SCRIPT_SIG[inp.prevout.n]
txid = from_node.sendrawtransaction(ToHex(tx), True)
unconflist.append({ "txid" : txid, "vout" : 0 , "amount" : total_in - amount - fee})
unconflist.append({ "txid" : txid, "vout" : 1 , "amount" : amount})

unconflist.append({"txid": txid, "vout": 0, "amount": amount})
if change > DUST_THRESHOLD:
unconflist.append({ "txid" : txid, "vout" : 1 , "amount" : total_in - amount - fee})

return (ToHex(tx), fee)

Expand Down

0 comments on commit f6509c3

Please sign in to comment.