[tests] Fix style warnings in feature_fee_estimation.py

This commit is contained in:
John Newbery 2018-02-02 10:08:37 -05:00
parent 4cad91663d
commit d119f2ec1a

View file

@ -3,35 +3,37 @@
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test fee estimation code.""" """Test fee estimation code."""
from decimal import Decimal
import random
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, COIN from test_framework.mininode import CTransaction, CTxIn, CTxOut, COutPoint, ToHex, COIN
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 satoshi_round, sync_mempools, sync_blocks, connect_nodes, assert_greater_than
# Construct 2 trivial P2SH's and the ScriptSigs that spend them # Construct 2 trivial P2SH's and the ScriptSigs that spend them
# So we can create many transactions without needing to spend # So we can create many transactions without needing to spend
# time signing. # time signing.
redeem_script_1 = CScript([OP_1, OP_DROP]) REDEEM_SCRIPT_1 = CScript([OP_1, OP_DROP])
redeem_script_2 = CScript([OP_2, OP_DROP]) REDEEM_SCRIPT_2 = CScript([OP_2, OP_DROP])
P2SH_1 = CScript([OP_HASH160, hash160(redeem_script_1), OP_EQUAL]) P2SH_1 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_1), OP_EQUAL])
P2SH_2 = CScript([OP_HASH160, hash160(redeem_script_2), OP_EQUAL]) P2SH_2 = CScript([OP_HASH160, hash160(REDEEM_SCRIPT_2), OP_EQUAL])
# Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2 # Associated ScriptSig's to spend satisfy P2SH_1 and P2SH_2
SCRIPT_SIG = [CScript([OP_TRUE, redeem_script_1]), CScript([OP_TRUE, redeem_script_2])] SCRIPT_SIG = [CScript([OP_TRUE, REDEEM_SCRIPT_1]), CScript([OP_TRUE, REDEEM_SCRIPT_2])]
global log global log
def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee_increment): def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee_increment):
""" """Create and send a transaction with a random fee.
Create and send a transaction with a random fee.
The transaction pays to a trivial P2SH script, and assumes that its inputs The transaction pays to a trivial P2SH script, and assumes that its inputs
are of the same form. are of the same form.
The function takes a list of confirmed outputs and unconfirmed outputs The function takes a list of confirmed outputs and unconfirmed outputs
and attempts to use the confirmed list first for its inputs. and attempts to use the confirmed list first for its inputs.
It adds the newly created outputs to the unconfirmed list. It adds the newly created outputs to the unconfirmed list.
Returns (raw transaction, fee) Returns (raw transaction, fee)."""
"""
# It's best to exponentially distribute our random fees # It's best to exponentially distribute our random fees
# because the buckets are exponentially spaced. # because the buckets are exponentially spaced.
# Exponentially distributed from 1-128 * fee_increment # Exponentially distributed from 1-128 * fee_increment
@ -64,13 +66,13 @@ def small_txpuzzle_randfee(from_node, conflist, unconflist, amount, min_fee, fee
return (ToHex(tx), fee) return (ToHex(tx), fee)
def split_inputs(from_node, txins, txouts, initial_split=False): def split_inputs(from_node, txins, txouts, initial_split=False):
""" """Generate a lot of inputs so we can generate a ton of transactions.
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 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. 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 Previously this was designed to be small inputs so they wouldn't have
a high coin age when the notion of priority still existed. a high coin age when the notion of priority still existed."""
"""
prevtxout = txins.pop() prevtxout = txins.pop()
tx = CTransaction() tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(prevtxout["txid"], 16), prevtxout["vout"]), b"")) tx.vin.append(CTxIn(COutPoint(int(prevtxout["txid"], 16), prevtxout["vout"]), b""))
@ -92,10 +94,8 @@ def split_inputs(from_node, txins, txouts, initial_split = False):
txouts.append({"txid": txid, "vout": 1, "amount": rem_change}) txouts.append({"txid": txid, "vout": 1, "amount": rem_change})
def check_estimates(node, fees_seen, max_invalid, print_estimates=True): def check_estimates(node, fees_seen, max_invalid, print_estimates=True):
""" """Call estimatefee and verify that the estimates meet certain invariants."""
This function calls estimatefee and verifies that the estimates
meet certain invariants.
"""
all_estimates = [node.estimatefee(i) for i in range(1, 26)] all_estimates = [node.estimatefee(i) for i in range(1, 26)]
if print_estimates: if print_estimates:
log.info([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]])
@ -117,7 +117,7 @@ def check_estimates(node, fees_seen, max_invalid, print_estimates = True):
if e >= 0: if e >= 0:
valid_estimate = True valid_estimate = True
if i >= 13: # for n>=14 estimatesmartfee(n/2) should be at least as high as estimatefee(n) if i >= 13: # for n>=14 estimatesmartfee(n/2) should be at least as high as estimatefee(n)
assert(node.estimatesmartfee((i+1)//2)["feerate"] > float(e) - delta) assert_greater_than(node.estimatesmartfee((i + 1) // 2)["feerate"], float(e) - delta)
else: else:
invalid_estimates += 1 invalid_estimates += 1
@ -125,8 +125,8 @@ def check_estimates(node, fees_seen, max_invalid, print_estimates = True):
# estimatesmartfee should still be valid # estimatesmartfee should still be valid
approx_estimate = node.estimatesmartfee(i + 1)["feerate"] approx_estimate = node.estimatesmartfee(i + 1)["feerate"]
answer_found = node.estimatesmartfee(i + 1)["blocks"] answer_found = node.estimatesmartfee(i + 1)["blocks"]
assert(approx_estimate > 0) assert_greater_than(approx_estimate, 0)
assert(answer_found > i+1) assert_greater_than(answer_found, i + 1)
# Once we're at a high enough confirmation count that we can give an estimate # Once we're at a high enough confirmation count that we can give an estimate
# We should have estimates for all higher confirmation counts # We should have estimates for all higher confirmation counts
@ -160,7 +160,6 @@ class EstimateFeeTest(BitcoinTestFramework):
# Node2 is a stingy miner, that # Node2 is a stingy miner, that
# produces too small blocks (room for only 55 or so transactions) # produces too small blocks (room for only 55 or so transactions)
def transact_and_mine(self, numblocks, mining_node): def transact_and_mine(self, numblocks, mining_node):
min_fee = Decimal("0.00001") min_fee = Decimal("0.00001")
# We will now mine numblocks blocks generating on average 100 transactions between each block # We will now mine numblocks blocks generating on average 100 transactions between each block