Skip to content

Commit

Permalink
[BROKEN] Add fee outputs to functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 20, 2019
1 parent b48aea8 commit 6113bf8
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 45 deletions.
3 changes: 3 additions & 0 deletions test/functional/feature_bip68_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_disable_flag(self):
sequence_value = SEQUENCE_LOCKTIME_DISABLE_FLAG | 1
tx1.vin = [CTxIn(COutPoint(int(utxo["txid"], 16), utxo["vout"]), nSequence=sequence_value)]
tx1.vout = [CTxOut(value, CScript([b'a']))]
tx1.vout.append(CTxOut(int(self.relayfee*COIN)))

tx1_signed = self.nodes[0].signrawtransactionwithwallet(ToHex(tx1))["hex"]
tx1_id = self.nodes[0].sendrawtransaction(tx1_signed)
Expand All @@ -90,6 +91,7 @@ def test_disable_flag(self):
sequence_value = sequence_value & 0x7fffffff
tx2.vin = [CTxIn(COutPoint(tx1_id, 0), nSequence=sequence_value)]
tx2.vout = [CTxOut(int(value - self.relayfee * COIN), CScript([b'a' * 35]))]
tx2.vout.append(CTxOut(int(self.relayfee*COIN)))
tx2.rehash()

assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx2))
Expand Down Expand Up @@ -185,6 +187,7 @@ def test_sequence_lock_confirmed_inputs(self):
# Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output
tx_size = len(ToHex(tx))//2 + 120*num_inputs + 50
tx.vout.append(CTxOut(int(value-self.relayfee*tx_size*COIN/1000), CScript([b'a'])))
tx.vout.append(CTxOut(int(self.relayfee*tx_size*COIN/1000)))
rawtx = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))["hex"]

if (using_sequence_locks and not should_pass):
Expand Down
82 changes: 47 additions & 35 deletions test/functional/feature_rbf.py

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions test/functional/feature_segwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
)
from test_framework.blocktools import witness_script, send_to_witness
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, sha256, ToHex
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP, OP_RETURN
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error, bytes_to_hex_str, connect_nodes, hex_str_to_bytes, sync_blocks, try_rpc, BITCOIN_ASSET

Expand Down Expand Up @@ -229,6 +229,7 @@ def run_test(self):
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(txid1, 16), 0), b''))
tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
tx.vout.append(CTxOut(int(49.996*COIN - 49.99*COIN)))
tx2_hex = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))['hex']
txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
tx = FromHex(CTransaction(), tx2_hex)
Expand All @@ -238,6 +239,7 @@ def run_test(self):
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int(txid2, 16), 0), b""))
tx.vout.append(CTxOut(int(49.95 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE]))) # Huge fee
tx.vout.append(CTxOut(int(49.99*COIN - 49.95*COIN)))
tx.calc_sha256()
txid3 = self.nodes[0].sendrawtransaction(ToHex(tx))
assert(tx.wit.is_null())
Expand Down Expand Up @@ -587,8 +589,11 @@ def mine_and_test_listunspent(self, script_list, ismine):
utxo = find_spendable_utxo(self.nodes[0], 50)
tx = CTransaction()
tx.vin.append(CTxIn(COutPoint(int('0x'+utxo['txid'],0), utxo['vout'])))
remaining = 50*COIN
for i in script_list:
tx.vout.append(CTxOut(10000000, i))
remaining -= 10000000
tx.vout.append(CTxOut(remaining))
tx.rehash()
signresults = self.nodes[0].signrawtransactionwithwallet(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
txid = self.nodes[0].sendrawtransaction(signresults, True)
Expand Down Expand Up @@ -633,14 +638,19 @@ def p2pkh_address_to_script(self,v):

def create_and_mine_tx_from_txids(self, txids, success = True):
tx = CTransaction()
total_in = 0
for i in txids:
txtmp = CTransaction()
txraw = self.nodes[0].getrawtransaction(i)
f = BytesIO(hex_str_to_bytes(txraw))
txtmp.deserialize(f)
for j in range(len(txtmp.vout)):
if txtmp.vout[j].is_fee():
continue
total_in += txtmp.vout[j].nValue.getAmount()
tx.vin.append(CTxIn(COutPoint(int('0x'+i,0), j)))
tx.vout.append(CTxOut(0, CScript()))
tx.vout.append(CTxOut(0, CScript([OP_RETURN])))
tx.vout.append(CTxOut(total_in))
tx.rehash()
signresults = self.nodes[0].signrawtransactionwithwallet(bytes_to_hex_str(tx.serialize_without_witness()))['hex']
self.nodes[0].sendrawtransaction(signresults, True)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/mempool_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def run_test(self):
self.log.info('Create a mempool tx that will be evicted')
us0 = utxos.pop()
inputs = [{ "txid" : us0["txid"], "vout" : us0["vout"]}]
outputs = {self.nodes[0].getnewaddress() : 0.0001}
outputs = {self.nodes[0].getnewaddress() : 0.0001, "fee": us0["amount"] - Decimal('0.0001')}
tx = self.nodes[0].createrawtransaction(inputs, outputs)
self.nodes[0].settxfee(relayfee) # specifically fund this tx with low fee
txF = self.nodes[0].fundrawtransaction(tx)
Expand All @@ -58,7 +58,7 @@ def run_test(self):
self.log.info('Create a mempool tx that will not pass mempoolminfee')
us0 = utxos.pop()
inputs = [{ "txid" : us0["txid"], "vout" : us0["vout"]}]
outputs = {self.nodes[0].getnewaddress() : 0.0001}
outputs = {self.nodes[0].getnewaddress() : 0.0001, "fee": us0["amount"] - Decimal('0.0001')}
tx = self.nodes[0].createrawtransaction(inputs, outputs)
# specifically fund this tx with a fee < mempoolminfee, >= than minrelaytxfee
txF = self.nodes[0].fundrawtransaction(tx, {'feeRate': relayfee})
Expand Down
6 changes: 3 additions & 3 deletions test/functional/rpc_txoutproof.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def run_test(self):
assert_equal(self.nodes[2].getbalance()['bitcoin'], 0)

node0utxos = self.nodes[0].listunspent(1)
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99, "fee": 0.01})
txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx1)["hex"])
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99})
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 49.99, "fee": 0.01})
txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx2)["hex"])
# This will raise an exception because the transaction is not yet in a block
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1])
Expand All @@ -58,7 +58,7 @@ def run_test(self):
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid1, txid2], blockhash)), txlist)

txin_spent = self.nodes[1].listunspent(1).pop()
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98})
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 49.98, "fee": 0.01})
txid3 = self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransactionwithwallet(tx3)["hex"])
self.nodes[0].generate(1)
self.sync_all()
Expand Down
4 changes: 3 additions & 1 deletion test/functional/test_framework/blocktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
if not encode_p2sh:
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
return node.createrawtransaction([utxo], {addr: amount})
if "amount" not in utxo:
utxo["amount"] = node.gettxout(utxo["txid"], utxo["vout"])["value"]
return node.createrawtransaction([utxo], {addr: amount, "fee": utxo["amount"]-amount})

def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):
"""Create a transaction spending a given utxo to a segwit output.
Expand Down
3 changes: 3 additions & 0 deletions test/functional/test_framework/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ def __init__(self, nValue=0, scriptPubKey=b""):
self.nValue = nValue
self.scriptPubKey = scriptPubKey

def is_fee(self):
return len(self.scriptPubKey) == 0

def deserialize(self, f):
self.nValue = struct.unpack("<q", f.read(8))[0]
self.scriptPubKey = deser_string(f)
Expand Down
4 changes: 3 additions & 1 deletion test/functional/test_framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def create_confirmed_utxos(fee, node, count):
send_value = t['amount'] - fee
outputs[addr1] = satoshi_round(send_value / 2)
outputs[addr2] = satoshi_round(send_value / 2)
outputs["fee"] = fee
raw_tx = node.createrawtransaction(inputs, outputs)
signed_tx = node.signrawtransactionwithwallet(raw_tx)["hex"]
node.sendrawtransaction(signed_tx)
Expand All @@ -539,7 +540,7 @@ def gen_return_txouts():
script_pubkey = script_pubkey + "01"
# concatenate 128 txouts of above script_pubkey which we'll insert before the txout for change
txouts = []
from .messages import CTxOut
from .messages import CTxOut, CTxOutValue
for k in range(128):
txout = CTxOut()
txout.nValue = 0
Expand All @@ -559,6 +560,7 @@ def create_lots_of_big_transactions(node, txouts, utxos, num, fee):
outputs = {}
change = t['amount'] - fee
outputs[addr] = satoshi_round(change)
outputs["fee"] = fee
rawtx = node.createrawtransaction(inputs, outputs)
tx = CTransaction()
tx.deserialize(BytesIO(hex_str_to_bytes(rawtx)))
Expand Down
2 changes: 1 addition & 1 deletion test/functional/wallet_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def run_test(self):
self.nodes[0].generate(1)
node0_balance = self.nodes[0].getbalance()['bitcoin']
# Split into two chains
rawtx = self.nodes[0].createrawtransaction([{"txid": singletxid, "vout": 0}], {chain_addrs[0]: node0_balance / 2 - Decimal('0.01'), chain_addrs[1]: node0_balance / 2 - Decimal('0.01')})
rawtx = self.nodes[0].createrawtransaction([{"txid": singletxid, "vout": 0}], {chain_addrs[0]: node0_balance / 2 - Decimal('0.01'), chain_addrs[1]: node0_balance / 2 - Decimal('0.01'), "fee": Decimal('0.02')})
signedtx = self.nodes[0].signrawtransactionwithwallet(rawtx)
singletxid = self.nodes[0].sendrawtransaction(signedtx["hex"])
self.nodes[0].generate(1)
Expand Down
2 changes: 2 additions & 0 deletions test/functional/wallet_txn_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def run_test(self):
clone_inputs = [{"txid": rawtx1["vin"][0]["txid"], "vout": rawtx1["vin"][0]["vout"]}]
clone_outputs = {rawtx1["vout"][0]["scriptPubKey"]["addresses"][0]: rawtx1["vout"][0]["value"],
rawtx1["vout"][1]["scriptPubKey"]["addresses"][0]: rawtx1["vout"][1]["value"]}
clone_valuein = self.nodes[0].getrawtransaction(rawtx1["vin"][0]["txid"], 1)["vout"][rawtx1["vin"][0]["vout"]]["value"]
clone_outputs["fee"] = clone_valuein - rawtx1["vout"][0]["value"] - rawtx1["vout"][1]["value"]
clone_locktime = rawtx1["locktime"]
clone_raw = self.nodes[0].createrawtransaction(clone_inputs, clone_outputs, clone_locktime)

Expand Down

0 comments on commit 6113bf8

Please sign in to comment.