2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2014-10-23 03:48:19 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test fee estimation code."""
|
2018-02-08 15:26:20 +01:00
|
|
|
from decimal import Decimal
|
|
|
|
import random
|
2014-03-17 13:19:54 +01:00
|
|
|
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN
|
2018-02-08 15:26:20 +01:00
|
|
|
from test_framework.script import CScript, OP_1, OP_DROP, OP_2, OP_HASH160, OP_EQUAL, hash160, OP_TRUE
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_greater_than,
|
|
|
|
assert_greater_than_or_equal,
|
|
|
|
satoshi_round,
|
|
|
|
)
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
# Construct 2 trivial P2SH's and the ScriptSigs that spend them
|
2017-06-22 20:36:16 +02:00
|
|
|
# So we can create many transactions without needing to spend
|
2014-08-26 22:28:32 +02:00
|
|
|
# time signing.
|
2018-02-08 15:26:20 +01:00
|
|
|
REDEEM_SCRIPT_1 = CScript([OP_1, OP_DROP])
|
|
|
|
REDEEM_SCRIPT_2 = CScript([OP_2, OP_DROP])
|
|
|
|
P2SH_1 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_1), OP_EQUAL])
|
|
|
|
P2SH_2 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_2), OP_EQUAL])
|
2017-03-14 11:21:14 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
|
2018-02-08 15:26:20 +01:00
|
|
|
SCRIPT_SIG = [CScript([OP_TRUE, REDEEM_SCRIPT_1]), CScript([OP_TRUE, REDEEM_SCRIPT_2])]
|
2017-03-10 23:04:58 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee_increment):
|
2018-02-08 15:26:20 +01:00
|
|
|
"""Create and send a transaction with a random fee.
|
|
|
|
|
2016-04-30 23:16:06 +02:00
|
|
|
The transaction pays to a trivial P2SH script, and assumes that its inputs
|
2014-08-26 22:28:32 +02:00
|
|
|
are of the same form.
|
|
|
|
The function takes a list of confirmed outputs and unconfirmed outputs
|
|
|
|
and attempts to use the confirmed list first for its inputs.
|
|
|
|
It adds the newly created outputs to the unconfirmed list.
|
2018-02-08 15:26:20 +01:00
|
|
|
Returns (raw transaction, fee)."""
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
# It's best to exponentially distribute our random fees
|
|
|
|
# because the buckets are exponentially spaced.
|
|
|
|
# Exponentially distributed from 1-128 * fee_increment
|
2018-02-08 15:26:20 +01:00
|
|
|
rand_fee = float(fee_increment) * (1.1892 ** random.randint(0, 28))
|
2014-08-26 22:28:32 +02:00
|
|
|
# Total fee ranges from min_fee to min_fee + 127*fee_increment
|
|
|
|
fee = min_fee - fee_increment + satoshi_round(rand_fee)
|
2017-03-14 11:21:14 +01:00
|
|
|
tx = CTransaction()
|
2014-08-26 22:28:32 +02:00
|
|
|
total_in = Decimal("0.00000000")
|
|
|
|
while total_in <= (amount + fee) and len(conflist) > 0:
|
|
|
|
t = conflist.pop(0)
|
|
|
|
total_in += t["amount"]
|
2017-03-14 11:21:14 +01:00
|
|
|
tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
|
2014-08-26 22:28:32 +02:00
|
|
|
if total_in <= amount + fee:
|
|
|
|
while total_in <= (amount + fee) and len(unconflist) > 0:
|
|
|
|
t = unconflist.pop(0)
|
|
|
|
total_in += t["amount"]
|
2017-03-14 11:21:14 +01:00
|
|
|
tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
|
2014-08-26 22:28:32 +02:00
|
|
|
if total_in <= amount + fee:
|
2018-02-08 15:26:20 +01:00
|
|
|
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))
|
2017-03-14 11:21:14 +01:00
|
|
|
# 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]
|
2021-12-12 07:27:44 +01:00
|
|
|
txid = from_node.sendrawtransaction(hexstring=ToHex(tx), maxfeerate=0)
|
2018-02-08 15:26:20 +01:00
|
|
|
unconflist.append({"txid": txid, "vout": 0, "amount": total_in - amount - fee})
|
|
|
|
unconflist.append({"txid": txid, "vout": 1, "amount": amount})
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2017-03-14 11:21:14 +01:00
|
|
|
return (ToHex(tx), fee)
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2018-02-08 15:26:20 +01:00
|
|
|
def split_inputs(from_node, txins, txouts, initial_split=False):
|
|
|
|
"""Generate a lot of inputs so we can generate a ton of transactions.
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
This function takes an input from txins, and creates and sends a transaction
|
|
|
|
which splits the value into 2 outputs which are appended to txouts.
|
2019-03-14 15:44:42 +01:00
|
|
|
Previously this was designed to be small inputs so they wouldn't have
|
2018-02-08 15:26:20 +01:00
|
|
|
a high coin age when the notion of priority still existed."""
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
prevtxout = txins.pop()
|
2017-03-14 11:21:14 +01:00
|
|
|
tx = CTransaction()
|
|
|
|
tx.vin.append(CTxIn(COutPoint(int(prevtxout["txid"], 16), prevtxout["vout"]), b""))
|
|
|
|
|
2018-02-08 15:26:20 +01:00
|
|
|
half_change = satoshi_round(prevtxout["amount"] / 2)
|
|
|
|
rem_change = prevtxout["amount"] - half_change - Decimal("0.00001000")
|
|
|
|
tx.vout.append(CTxOut(int(half_change * COIN), P2SH_1))
|
|
|
|
tx.vout.append(CTxOut(int(rem_change * COIN), P2SH_2))
|
2017-03-14 11:21:14 +01:00
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
# If this is the initial split we actually need to sign the transaction
|
2017-03-14 11:21:14 +01:00
|
|
|
# Otherwise we just need to insert the proper ScriptSig
|
2018-02-08 15:26:20 +01:00
|
|
|
if (initial_split):
|
2018-02-20 03:29:22 +01:00
|
|
|
completetx = from_node.signrawtransactionwithwallet(ToHex(tx))["hex"]
|
2018-02-08 15:26:20 +01:00
|
|
|
else:
|
2017-03-14 11:21:14 +01:00
|
|
|
tx.vin[0].scriptSig = SCRIPT_SIG[prevtxout["vout"]]
|
|
|
|
completetx = ToHex(tx)
|
2021-12-12 07:27:44 +01:00
|
|
|
txid = from_node.sendrawtransaction(hexstring=completetx, maxfeerate=0)
|
2018-02-08 15:26:20 +01:00
|
|
|
txouts.append({"txid": txid, "vout": 0, "amount": half_change})
|
|
|
|
txouts.append({"txid": txid, "vout": 1, "amount": rem_change})
|
2015-11-16 21:26:57 +01:00
|
|
|
|
2020-03-11 20:54:42 +01:00
|
|
|
def check_raw_estimates(node, fees_seen):
|
|
|
|
"""Call estimaterawfee and verify that the estimates meet certain invariants."""
|
|
|
|
|
|
|
|
delta = 1.0e-6 # account for rounding error
|
|
|
|
for i in range(1, 26):
|
|
|
|
for _, e in node.estimaterawfee(i).items():
|
|
|
|
feerate = float(e["feerate"])
|
|
|
|
assert_greater_than(feerate, 0)
|
|
|
|
|
|
|
|
if feerate + delta < min(fees_seen) or feerate - delta > max(fees_seen):
|
|
|
|
raise AssertionError("Estimated fee (%f) out of range (%f,%f)"
|
|
|
|
% (feerate, min(fees_seen), max(fees_seen)))
|
|
|
|
|
|
|
|
def check_smart_estimates(node, fees_seen):
|
2018-02-08 15:26:20 +01:00
|
|
|
"""Call estimatesmartfee and verify that the estimates meet certain invariants."""
|
2015-11-16 21:26:57 +01:00
|
|
|
|
2018-02-08 15:26:20 +01:00
|
|
|
delta = 1.0e-6 # account for rounding error
|
|
|
|
last_feerate = float(max(fees_seen))
|
|
|
|
all_smart_estimates = [node.estimatesmartfee(i) for i in range(1, 26)]
|
|
|
|
for i, e in enumerate(all_smart_estimates): # estimate is for i+1
|
|
|
|
feerate = float(e["feerate"])
|
|
|
|
assert_greater_than(feerate, 0)
|
2015-11-16 21:26:57 +01:00
|
|
|
|
2018-02-08 15:26:20 +01:00
|
|
|
if feerate + delta < min(fees_seen) or feerate - delta > max(fees_seen):
|
|
|
|
raise AssertionError("Estimated fee (%f) out of range (%f,%f)"
|
|
|
|
% (feerate, min(fees_seen), max(fees_seen)))
|
|
|
|
if feerate - delta > last_feerate:
|
|
|
|
raise AssertionError("Estimated fee (%f) larger than last fee (%f) for lower number of confirms"
|
|
|
|
% (feerate, last_feerate))
|
|
|
|
last_feerate = feerate
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2018-02-08 15:26:20 +01:00
|
|
|
if i == 0:
|
|
|
|
assert_equal(e["blocks"], 2)
|
|
|
|
else:
|
|
|
|
assert_greater_than_or_equal(i + 1, e["blocks"])
|
2014-08-26 22:28:32 +02:00
|
|
|
|
2020-03-11 20:54:42 +01:00
|
|
|
def check_estimates(node, fees_seen):
|
|
|
|
check_raw_estimates(node, fees_seen)
|
|
|
|
check_smart_estimates(node, fees_seen)
|
2018-12-19 18:44:46 +01:00
|
|
|
|
2014-07-08 18:07:23 +02:00
|
|
|
class EstimateFeeTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
self.num_nodes = 3
|
2018-12-19 18:44:46 +01:00
|
|
|
# mine non-standard txs (e.g. txs with "dust" outputs)
|
|
|
|
self.extra_args = [
|
2020-02-28 05:18:57 +01:00
|
|
|
["-acceptnonstdtxn=1", "-maxorphantxsize=1000", "-whitelist=noban@127.0.0.1"],
|
|
|
|
["-acceptnonstdtxn=1", "-blockmaxsize=17000", "-maxorphantxsize=1000", "-whitelist=noban@127.0.0.1"],
|
|
|
|
["-acceptnonstdtxn=1", "-blockmaxsize=8000", "-maxorphantxsize=1000", "-whitelist=noban@127.0.0.1"]
|
2018-12-19 18:44:46 +01:00
|
|
|
]
|
2016-05-20 15:16:51 +02:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2014-10-20 14:14:04 +02:00
|
|
|
def setup_network(self):
|
2019-01-07 10:55:35 +01:00
|
|
|
"""
|
2014-08-26 22:28:32 +02:00
|
|
|
We'll setup the network to have 3 nodes that all mine with different parameters.
|
2019-03-14 15:44:42 +01:00
|
|
|
But first we need to use one node to create a lot of outputs
|
2014-08-26 22:28:32 +02:00
|
|
|
which we will use to generate our transactions.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""
|
2018-12-19 18:44:46 +01:00
|
|
|
self.add_nodes(3, extra_args=self.extra_args)
|
2014-08-26 22:28:32 +02:00
|
|
|
# Use node0 to mine blocks for input splitting
|
2019-03-14 15:44:42 +01:00
|
|
|
# Node1 mines small blocks but that are bigger than the expected transaction rate.
|
2014-07-08 18:07:23 +02:00
|
|
|
# NOTE: the CreateNewBlock code starts counting block size at 1,000 bytes,
|
2014-08-26 22:28:32 +02:00
|
|
|
# (17k is room enough for 110 or so transactions)
|
2014-07-08 18:07:23 +02:00
|
|
|
# Node2 is a stingy miner, that
|
2015-11-16 21:26:57 +01:00
|
|
|
# produces too small blocks (room for only 55 or so transactions)
|
2018-11-02 16:41:29 +01:00
|
|
|
self.start_nodes()
|
|
|
|
self.import_deterministic_coinbase_privkeys()
|
|
|
|
self.stop_nodes()
|
2014-08-26 22:28:32 +02:00
|
|
|
|
|
|
|
def transact_and_mine(self, numblocks, mining_node):
|
2016-03-06 16:14:39 +01:00
|
|
|
min_fee = Decimal("0.0001")
|
2014-08-26 22:28:32 +02:00
|
|
|
# We will now mine numblocks blocks generating on average 100 transactions between each block
|
|
|
|
# We shuffle our confirmed txout set before each set of transactions
|
|
|
|
# small_txpuzzle_randfee will use the transactions that have inputs already in the chain when possible
|
|
|
|
# resorting to tx's that depend on the mempool when those run out
|
|
|
|
for i in range(numblocks):
|
|
|
|
random.shuffle(self.confutxo)
|
2018-02-08 15:26:20 +01:00
|
|
|
for j in range(random.randrange(100 - 50, 100 + 50)):
|
|
|
|
from_index = random.randint(1, 2)
|
2014-08-26 22:28:32 +02:00
|
|
|
(txhex, fee) = small_txpuzzle_randfee(self.nodes[from_index], self.confutxo,
|
|
|
|
self.memutxo, Decimal("0.005"), min_fee, min_fee)
|
2016-03-19 21:36:32 +01:00
|
|
|
tx_kbytes = (len(txhex) // 2) / 1000.0
|
2018-02-08 15:26:20 +01:00
|
|
|
self.fees_per_kb.append(float(fee) / tx_kbytes)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_mempools(self.nodes[0:3], wait=.1)
|
2018-02-08 15:26:20 +01:00
|
|
|
mined = mining_node.getblock(mining_node.generate(1)[0], True)["tx"]
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks(self.nodes[0:3], wait=.1)
|
2016-04-30 23:16:06 +02:00
|
|
|
# update which txouts are confirmed
|
2014-08-26 22:28:32 +02:00
|
|
|
newmem = []
|
|
|
|
for utx in self.memutxo:
|
|
|
|
if utx["txid"] in mined:
|
|
|
|
self.confutxo.append(utx)
|
|
|
|
else:
|
|
|
|
newmem.append(utx)
|
|
|
|
self.memutxo = newmem
|
2014-07-08 18:07:23 +02:00
|
|
|
|
2014-10-20 14:14:04 +02:00
|
|
|
def run_test(self):
|
2017-09-01 18:47:13 +02:00
|
|
|
self.log.info("This test is time consuming, please be patient")
|
|
|
|
self.log.info("Splitting inputs so we can generate tx's")
|
|
|
|
|
|
|
|
# Start node0
|
|
|
|
self.start_node(0)
|
|
|
|
self.txouts = []
|
|
|
|
self.txouts2 = []
|
|
|
|
# Split a coinbase into two transaction puzzle outputs
|
|
|
|
split_inputs(self.nodes[0], self.nodes[0].listunspent(0), self.txouts, True)
|
|
|
|
|
|
|
|
# Mine
|
|
|
|
while (len(self.nodes[0].getrawmempool()) > 0):
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
|
|
|
|
# Repeatedly split those 2 outputs, doubling twice for each rep
|
|
|
|
# Use txouts to monitor the available utxo, since these won't be tracked in wallet
|
|
|
|
reps = 0
|
|
|
|
while (reps < 5):
|
2018-02-08 15:26:20 +01:00
|
|
|
# Double txouts to txouts2
|
|
|
|
while (len(self.txouts) > 0):
|
2017-09-01 18:47:13 +02:00
|
|
|
split_inputs(self.nodes[0], self.txouts, self.txouts2)
|
|
|
|
while (len(self.nodes[0].getrawmempool()) > 0):
|
|
|
|
self.nodes[0].generate(1)
|
2018-02-08 15:26:20 +01:00
|
|
|
# Double txouts2 to txouts
|
|
|
|
while (len(self.txouts2) > 0):
|
2017-09-01 18:47:13 +02:00
|
|
|
split_inputs(self.nodes[0], self.txouts2, self.txouts)
|
|
|
|
while (len(self.nodes[0].getrawmempool()) > 0):
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
reps += 1
|
|
|
|
self.log.info("Finished splitting")
|
|
|
|
|
|
|
|
# Now we can connect the other nodes, didn't want to connect them earlier
|
|
|
|
# so the estimates would not be affected by the splitting transactions
|
|
|
|
self.start_node(1)
|
|
|
|
self.start_node(2)
|
2022-09-24 14:36:35 +02:00
|
|
|
self.connect_nodes(1, 0)
|
|
|
|
self.connect_nodes(0, 2)
|
|
|
|
self.connect_nodes(2, 1)
|
2017-09-01 18:47:13 +02:00
|
|
|
|
|
|
|
self.sync_all()
|
|
|
|
|
2014-08-26 22:28:32 +02:00
|
|
|
self.fees_per_kb = []
|
|
|
|
self.memutxo = []
|
2018-02-08 15:26:20 +01:00
|
|
|
self.confutxo = self.txouts # Start with the set of confirmed txouts after splitting
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Will output estimates for 1/2/3/6/15/25 blocks")
|
2014-07-08 18:07:23 +02:00
|
|
|
|
2016-05-06 11:23:48 +02:00
|
|
|
for i in range(2):
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Creating transactions and mining them with a block size that can't keep up")
|
2015-11-16 21:26:57 +01:00
|
|
|
# Create transactions and mine 10 small blocks with node 2, but create txs faster than we can mine
|
|
|
|
self.transact_and_mine(10, self.nodes[2])
|
2018-03-21 17:00:27 +01:00
|
|
|
check_estimates(self.nodes[1], self.fees_per_kb)
|
2014-07-08 18:07:23 +02:00
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Creating transactions and mining them at a block size that is just big enough")
|
2015-11-16 21:26:57 +01:00
|
|
|
# Generate transactions while mining 10 more blocks, this time with node1
|
|
|
|
# which mines blocks with capacity just above the rate that transactions are being created
|
|
|
|
self.transact_and_mine(10, self.nodes[1])
|
2018-03-21 17:00:27 +01:00
|
|
|
check_estimates(self.nodes[1], self.fees_per_kb)
|
2014-07-08 18:07:23 +02:00
|
|
|
|
|
|
|
# Finish by mining a normal-sized block:
|
2014-08-26 22:28:32 +02:00
|
|
|
while len(self.nodes[1].getrawmempool()) > 0:
|
|
|
|
self.nodes[1].generate(1)
|
2014-03-17 13:19:54 +01:00
|
|
|
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks(self.nodes[0:3], wait=.1)
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Final estimates after emptying mempools")
|
2018-03-21 17:00:27 +01:00
|
|
|
check_estimates(self.nodes[1], self.fees_per_kb)
|
2014-03-17 13:19:54 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-07-08 18:07:23 +02:00
|
|
|
EstimateFeeTest().main()
|