aboutsummaryrefslogtreecommitdiff
path: root/qa/rpc-tests/smartfees.py
diff options
context:
space:
mode:
Diffstat (limited to 'qa/rpc-tests/smartfees.py')
-rwxr-xr-xqa/rpc-tests/smartfees.py117
1 files changed, 58 insertions, 59 deletions
diff --git a/qa/rpc-tests/smartfees.py b/qa/rpc-tests/smartfees.py
index 2c56f954a2..49f2df5c37 100755
--- a/qa/rpc-tests/smartfees.py
+++ b/qa/rpc-tests/smartfees.py
@@ -2,26 +2,29 @@
# Copyright (c) 2014-2016 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
-
-#
-# Test fee estimation code
-#
+"""Test fee estimation code."""
from collections import OrderedDict
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *
+from test_framework.script import CScript, OP_1, OP_DROP, OP_2, OP_HASH160, OP_EQUAL, hash160, OP_TRUE
+from test_framework.mininode import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, FromHex, COIN
# Construct 2 trivial P2SH's and the ScriptSigs that spend them
# So we can create many many transactions without needing to spend
# time signing.
-P2SH_1 = "2MySexEGVzZpRgNQ1JdjdP5bRETznm3roQ2" # P2SH of "OP_1 OP_DROP"
-P2SH_2 = "2NBdpwq8Aoo1EEKEXPNrKvr5xQr3M9UfcZA" # P2SH of "OP_2 OP_DROP"
+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])
+
# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
-# 4 bytes of OP_TRUE and push 2-byte redeem script of "OP_1 OP_DROP" or "OP_2 OP_DROP"
-SCRIPT_SIG = ["0451025175", "0451025275"]
+SCRIPT_SIG = [CScript([OP_TRUE, redeem_script_1]), CScript([OP_TRUE, redeem_script_2])]
+
+global log
def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee_increment):
- '''
+ """
Create and send a transaction with a random fee.
The transaction pays to a trivial P2SH script, and assumes that its inputs
are of the same form.
@@ -29,79 +32,74 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
and attempts to use the confirmed list first for its inputs.
It adds the newly created outputs to the unconfirmed list.
Returns (raw transaction, fee)
- '''
+ """
# It's best to exponentially distribute our random fees
# because the buckets are exponentially spaced.
# Exponentially distributed from 1-128 * fee_increment
rand_fee = float(fee_increment)*(1.1892**random.randint(0,28))
# Total fee ranges from min_fee to min_fee + 127*fee_increment
fee = min_fee - fee_increment + satoshi_round(rand_fee)
- inputs = []
+ tx = CTransaction()
total_in = Decimal("0.00000000")
while total_in <= (amount + fee) and len(conflist) > 0:
t = conflist.pop(0)
total_in += t["amount"]
- inputs.append({ "txid" : t["txid"], "vout" : t["vout"]} )
+ tx.vin.append(CTxIn(COutPoint(int(t["txid"], 16), t["vout"]), b""))
if total_in <= amount + fee:
while total_in <= (amount + fee) and len(unconflist) > 0:
t = unconflist.pop(0)
total_in += t["amount"]
- inputs.append({ "txid" : t["txid"], "vout" : t["vout"]} )
+ 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))
- outputs = {}
- outputs = OrderedDict([(P2SH_1, total_in - amount - fee),
- (P2SH_2, amount)])
- rawtx = from_node.createrawtransaction(inputs, outputs)
- # createrawtransaction constructs a transaction that is ready to be signed.
- # These transactions don't need to be signed, but we still have to insert the ScriptSig
- # that will satisfy the ScriptPubKey.
- completetx = rawtx[0:10]
- inputnum = 0
- for inp in inputs:
- completetx += rawtx[10+82*inputnum:82+82*inputnum]
- completetx += SCRIPT_SIG[inp["vout"]]
- completetx += rawtx[84+82*inputnum:92+82*inputnum]
- inputnum += 1
- completetx += rawtx[10+82*inputnum:]
- txid = from_node.sendrawtransaction(completetx, True)
+ tx.vout.append(CTxOut(int((total_in - amount - fee)*COIN), P2SH_1))
+ tx.vout.append(CTxOut(int(amount*COIN), P2SH_2))
+ # 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})
- return (completetx, fee)
+ return (ToHex(tx), fee)
def split_inputs(from_node, txins, txouts, initial_split = False):
- '''
- We need to generate a lot of very small inputs so we can generate a ton of transactions
- and they will have low priority.
+ """
+ We need to generate a lot of inputs so we can generate a ton of transactions.
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.
- '''
+ Previously this was designed to be small inputs so they wouldn't have
+ a high coin age when the notion of priority still existed.
+ """
prevtxout = txins.pop()
- inputs = []
- inputs.append({ "txid" : prevtxout["txid"], "vout" : prevtxout["vout"] })
+ tx = CTransaction()
+ tx.vin.append(CTxIn(COutPoint(int(prevtxout["txid"], 16), prevtxout["vout"]), b""))
+
half_change = satoshi_round(prevtxout["amount"]/2)
rem_change = prevtxout["amount"] - half_change - Decimal("0.00001000")
- outputs = OrderedDict([(P2SH_1, half_change), (P2SH_2, rem_change)])
- rawtx = from_node.createrawtransaction(inputs, outputs)
+ tx.vout.append(CTxOut(int(half_change*COIN), P2SH_1))
+ tx.vout.append(CTxOut(int(rem_change*COIN), P2SH_2))
+
# If this is the initial split we actually need to sign the transaction
- # Otherwise we just need to insert the property ScriptSig
+ # Otherwise we just need to insert the proper ScriptSig
if (initial_split) :
- completetx = from_node.signrawtransaction(rawtx)["hex"]
+ completetx = from_node.signrawtransaction(ToHex(tx))["hex"]
else :
- completetx = rawtx[0:82] + SCRIPT_SIG[prevtxout["vout"]] + rawtx[84:]
+ tx.vin[0].scriptSig = SCRIPT_SIG[prevtxout["vout"]]
+ completetx = ToHex(tx)
txid = from_node.sendrawtransaction(completetx, True)
txouts.append({ "txid" : txid, "vout" : 0 , "amount" : half_change})
txouts.append({ "txid" : txid, "vout" : 1 , "amount" : rem_change})
def check_estimates(node, fees_seen, max_invalid, print_estimates = True):
- '''
+ """
This function calls estimatefee and verifies that the estimates
meet certain invariants.
- '''
+ """
all_estimates = [ node.estimatefee(i) for i in range(1,26) ]
if print_estimates:
- print([str(all_estimates[e-1]) for e in [1,2,3,6,15,25]])
+ log.info([str(all_estimates[e-1]) for e in [1,2,3,6,15,25]])
delta = 1.0e-6 # account for rounding error
last_e = max(fees_seen)
for e in [x for x in all_estimates if x >= 0]:
@@ -151,18 +149,18 @@ class EstimateFeeTest(BitcoinTestFramework):
self.setup_clean_chain = False
def setup_network(self):
- '''
+ """
We'll setup the network to have 3 nodes that all mine with different parameters.
- But first we need to use one node to create a lot of small low priority outputs
+ But first we need to use one node to create a lot of outputs
which we will use to generate our transactions.
- '''
+ """
self.nodes = []
# Use node0 to mine blocks for input splitting
self.nodes.append(start_node(0, self.options.tmpdir, ["-maxorphantx=1000",
"-whitelist=127.0.0.1"]))
- print("This test is time consuming, please be patient")
- print("Splitting inputs to small size so we can generate low priority tx's")
+ self.log.info("This test is time consuming, please be patient")
+ self.log.info("Splitting inputs so we can generate tx's")
self.txouts = []
self.txouts2 = []
# Split a coinbase into two transaction puzzle outputs
@@ -187,22 +185,20 @@ class EstimateFeeTest(BitcoinTestFramework):
while (len(self.nodes[0].getrawmempool()) > 0):
self.nodes[0].generate(1)
reps += 1
- print("Finished splitting")
+ 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
- # Node1 mines small blocks but that are bigger than the expected transaction rate,
- # and allows free transactions.
+ # Node1 mines small blocks but that are bigger than the expected transaction rate.
# NOTE: the CreateNewBlock code starts counting block size at 1,000 bytes,
# (17k is room enough for 110 or so transactions)
self.nodes.append(start_node(1, self.options.tmpdir,
- ["-blockprioritysize=1500", "-blockmaxsize=17000",
- "-maxorphantx=1000", "-debug=estimatefee"]))
+ ["-blockmaxsize=17000", "-maxorphantx=1000"]))
connect_nodes(self.nodes[1], 0)
# Node2 is a stingy miner, that
# produces too small blocks (room for only 55 or so transactions)
- node2args = ["-blockprioritysize=0", "-blockmaxsize=8000", "-maxorphantx=1000"]
+ node2args = ["-blockmaxsize=8000", "-maxorphantx=1000"]
self.nodes.append(start_node(2, self.options.tmpdir, node2args))
connect_nodes(self.nodes[0], 2)
@@ -238,18 +234,21 @@ class EstimateFeeTest(BitcoinTestFramework):
self.memutxo = newmem
def run_test(self):
+ # Make log handler available to helper functions
+ global log
+ log = self.log
self.fees_per_kb = []
self.memutxo = []
self.confutxo = self.txouts # Start with the set of confirmed txouts after splitting
- print("Will output estimates for 1/2/3/6/15/25 blocks")
+ self.log.info("Will output estimates for 1/2/3/6/15/25 blocks")
for i in range(2):
- print("Creating transactions and mining them with a block size that can't keep up")
+ self.log.info("Creating transactions and mining them with a block size that can't keep up")
# 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])
check_estimates(self.nodes[1], self.fees_per_kb, 14)
- print("Creating transactions and mining them at a block size that is just big enough")
+ self.log.info("Creating transactions and mining them at a block size that is just big enough")
# 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])
@@ -260,7 +259,7 @@ class EstimateFeeTest(BitcoinTestFramework):
self.nodes[1].generate(1)
sync_blocks(self.nodes[0:3], wait=.1)
- print("Final estimates after emptying mempools")
+ self.log.info("Final estimates after emptying mempools")
check_estimates(self.nodes[1], self.fees_per_kb, 2)
if __name__ == '__main__':