Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_fundrawtransaction.py
Show All 38 Lines | def setup_network(self): | ||||
self.setup_nodes() | self.setup_nodes() | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
connect_nodes(self.nodes[1], self.nodes[2]) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
connect_nodes(self.nodes[0], self.nodes[2]) | connect_nodes(self.nodes[0], self.nodes[2]) | ||||
connect_nodes(self.nodes[0], self.nodes[3]) | connect_nodes(self.nodes[0], self.nodes[3]) | ||||
def run_test(self): | def run_test(self): | ||||
min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee'] | self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee'] | ||||
# This test is not meant to test fee estimation and we'd like | # This test is not meant to test fee estimation and we'd like | ||||
# to be sure all txs are sent at a consistent desired feerate | # to be sure all txs are sent at a consistent desired feerate | ||||
for node in self.nodes: | for node in self.nodes: | ||||
node.settxfee(min_relay_tx_fee) | node.settxfee(self.min_relay_tx_fee) | ||||
# if the fee's positive delta is higher than this value tests will fail, | # if the fee's positive delta is higher than this value tests will fail, | ||||
# neg. delta always fail the tests. | # neg. delta always fail the tests. | ||||
# The size of the signature of every input may be at most 2 bytes larger | # The size of the signature of every input may be at most 2 bytes larger | ||||
# than a minimum sized signature. | # than a minimum sized signature. | ||||
# = 2 bytes * minRelayTxFeePerByte | # = 2 bytes * minRelayTxFeePerByte | ||||
feeTolerance = 2 * min_relay_tx_fee / 1000 | self.fee_tolerance = 2 * self.min_relay_tx_fee / 1000 | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[0].generate(121) | self.nodes[0].generate(121) | ||||
self.sync_all() | self.sync_all() | ||||
self.test_change_position() | |||||
self.test_simple() | |||||
self.test_simple_two_coins() | |||||
self.test_simple_two_outputs() | |||||
self.test_change() | |||||
self.test_no_change() | |||||
self.test_invalid_option() | |||||
self.test_invalid_change_address() | |||||
self.test_valid_change_address() | |||||
self.test_coin_selection() | |||||
self.test_two_vin() | |||||
self.test_two_vin_two_vout() | |||||
self.test_invalid_input() | |||||
self.test_fee_p2pkh() | |||||
self.test_fee_p2pkh_multi_out() | |||||
self.test_fee_p2sh() | |||||
self.test_fee_4of5() | |||||
self.test_spend_2of2() | |||||
self.test_locked_wallet() | |||||
self.test_many_inputs_fee() | |||||
self.test_many_inputs_send() | |||||
self.test_op_return() | |||||
self.test_watchonly() | |||||
self.test_all_watched_funds() | |||||
self.test_option_feerate() | |||||
self.test_address_reuse() | |||||
self.test_option_subtract_fee_from_outputs() | |||||
def test_change_position(self): | |||||
# ensure that setting changePosition in fundraw with an exact match is | # ensure that setting changePosition in fundraw with an exact match is | ||||
# handled properly | # handled properly | ||||
rawmatch = self.nodes[2].createrawtransaction( | rawmatch = self.nodes[2].createrawtransaction( | ||||
[], {self.nodes[2].getnewaddress(): 50}) | [], {self.nodes[2].getnewaddress(): 50}) | ||||
rawmatch = self.nodes[2].fundrawtransaction( | rawmatch = self.nodes[2].fundrawtransaction( | ||||
rawmatch, {"changePosition": 1, "subtractFeeFromOutputs": [0]}) | rawmatch, {"changePosition": 1, "subtractFeeFromOutputs": [0]}) | ||||
assert_equal(rawmatch["changepos"], -1) | assert_equal(rawmatch["changepos"], -1) | ||||
watchonly_address = self.nodes[0].getnewaddress() | watchonly_address = self.nodes[0].getnewaddress() | ||||
watchonly_pubkey = self.nodes[ | watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)[ | ||||
0].getaddressinfo(watchonly_address)["pubkey"] | "pubkey"] | ||||
watchonly_amount = Decimal(200) | self.watchonly_amount = Decimal(200) | ||||
self.nodes[3].importpubkey(watchonly_pubkey, "", True) | self.nodes[3].importpubkey(watchonly_pubkey, "", True) | ||||
watchonly_txid = self.nodes[0].sendtoaddress( | self.watchonly_txid = self.nodes[0].sendtoaddress( | ||||
watchonly_address, watchonly_amount) | watchonly_address, self.watchonly_amount) | ||||
# Lock UTXO so nodes[0] doesn't accidentally spend it | # Lock UTXO so nodes[0] doesn't accidentally spend it | ||||
watchonly_vout = find_vout_for_address( | self.watchonly_vout = find_vout_for_address( | ||||
self.nodes[0], watchonly_txid, watchonly_address) | self.nodes[0], self.watchonly_txid, watchonly_address) | ||||
self.nodes[0].lockunspent( | self.nodes[0].lockunspent( | ||||
False, [{"txid": watchonly_txid, "vout": watchonly_vout}]) | False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) | ||||
self.nodes[0].sendtoaddress( | self.nodes[0].sendtoaddress( | ||||
self.nodes[3].getnewaddress(), watchonly_amount / 10) | self.nodes[3].getnewaddress(), | ||||
self.watchonly_amount / 10) | |||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5) | ||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) | ||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
def test_simple(self): | |||||
# | # | ||||
# simple test # | # simple test # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
# test that we have enough inputs | # test that we have enough inputs | ||||
assert len(dec_tx['vin']) > 0 | assert len(dec_tx['vin']) > 0 | ||||
def test_simple_two_coins(self): | |||||
# | # | ||||
# simple test with two coins # | # simple test with two coins # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 2.2} | outputs = {self.nodes[0].getnewaddress(): 2.2} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
# test if we have enough inputs | # test if we have enough inputs | ||||
assert len(dec_tx['vin']) > 0 | assert len(dec_tx['vin']) > 0 | ||||
# | |||||
# simple test with two coins # | |||||
# | |||||
inputs = [] | |||||
outputs = {self.nodes[0].getnewaddress(): 2.6} | |||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | |||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | |||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | |||||
assert len(dec_tx['vin']) > 0 | |||||
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | ||||
def test_simple_two_outputs(self): | |||||
# | # | ||||
# simple test with two outputs # | # simple test with two outputs # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 2.6, self.nodes[1].getnewaddress(): 2.5} | self.nodes[0].getnewaddress(): 2.6, self.nodes[1].getnewaddress(): 2.5} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
for out in dec_tx['vout']: | for out in dec_tx['vout']: | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
assert len(dec_tx['vin']) > 0 | assert len(dec_tx['vin']) > 0 | ||||
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | ||||
def test_change(self): | |||||
# | # | ||||
# test a fundrawtransaction with a VIN greater than the required amount # | # test a fundrawtransaction with a VIN greater than the required amount # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | fee = rawtxfund['fee'] | ||||
# Use the same fee for the next tx | |||||
self.test_no_change_fee = fee | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
for out in dec_tx['vout']: | for out in dec_tx['vout']: | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
# compare vin total and totalout+fee | # compare vin total and totalout+fee | ||||
assert_equal(fee + totalOut, utx['amount']) | assert_equal(fee + totalOut, utx['amount']) | ||||
def test_no_change(self): | |||||
# | # | ||||
# test a fundrawtransaction with which will not get a change output # | # test a fundrawtransaction with which will not get a change output # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): Decimal(5.0) - fee - feeTolerance} | self.nodes[0].getnewaddress(): Decimal(5.0) - | ||||
self.test_no_change_fee - | |||||
self.fee_tolerance} | |||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | fee = rawtxfund['fee'] | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
for out in dec_tx['vout']: | for out in dec_tx['vout']: | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
assert_equal(rawtxfund['changepos'], -1) | assert_equal(rawtxfund['changepos'], -1) | ||||
assert_equal(fee + totalOut, utx['amount']) | |||||
# compare vin total and totalout+fee | # compare vin total and totalout+fee | ||||
assert_equal(fee + totalOut, utx['amount']) | |||||
def test_invalid_option(self): | |||||
# | # | ||||
# test a fundrawtransaction with an invalid option # | # test a fundrawtransaction with an invalid option # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
assert_raises_rpc_error(-3, "Unexpected key foo", self.nodes[ | assert_raises_rpc_error(-3, "Unexpected key foo", self.nodes[ | ||||
2].fundrawtransaction, rawTx, {'foo': 'bar'}) | 2].fundrawtransaction, rawTx, {'foo': 'bar'}) | ||||
def test_invalid_change_address(self): | |||||
# | # | ||||
# test a fundrawtransaction with an invalid change address # | # test a fundrawtransaction with an invalid change address # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-5, "changeAddress must be a valid bitcoin address", | -5, "changeAddress must be a valid bitcoin address", | ||||
self.nodes[2].fundrawtransaction, rawTx, {'changeAddress': 'foobar'}) | self.nodes[2].fundrawtransaction, rawTx, {'changeAddress': 'foobar'}) | ||||
def test_valid_change_address(self): | |||||
# | # | ||||
# test a fundrawtransaction with a provided change address # | # test a fundrawtransaction with a provided change address # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | outputs = {self.nodes[0].getnewaddress(): Decimal(4.0)} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
change = self.nodes[2].getnewaddress() | change = self.nodes[2].getnewaddress() | ||||
assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[ | assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[ | ||||
2].fundrawtransaction, rawTx, {'changeAddress': change, 'changePosition': 2}) | 2].fundrawtransaction, rawTx, {'changeAddress': change, 'changePosition': 2}) | ||||
rawtxfund = self.nodes[2].fundrawtransaction( | rawtxfund = self.nodes[2].fundrawtransaction( | ||||
rawTx, {'changeAddress': change, 'changePosition': 0}) | rawTx, {'changeAddress': change, 'changePosition': 0}) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
out = dec_tx['vout'][0] | out = dec_tx['vout'][0] | ||||
assert_equal(change, out['scriptPubKey']['addresses'][0]) | assert_equal(change, out['scriptPubKey']['addresses'][0]) | ||||
def test_coin_selection(self): | |||||
# | # | ||||
# test a fundrawtransaction with a VIN smaller than the required amount # | # test a fundrawtransaction with a VIN smaller than the required amount # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
# 4-byte version + 1-byte vin count + 36-byte prevout then script_len | # 4-byte version + 1-byte vin count + 36-byte prevout then script_len | ||||
rawTx = rawTx[:82] + "0100" + rawTx[84:] | rawTx = rawTx[:82] + "0100" + rawTx[84:] | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) | assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
matchingOuts = 0 | matchingOuts = 0 | ||||
for i, out in enumerate(dec_tx['vout']): | for i, out in enumerate(dec_tx['vout']): | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
if out['scriptPubKey']['addresses'][0] in outputs: | if out['scriptPubKey']['addresses'][0] in outputs: | ||||
matchingOuts += 1 | matchingOuts += 1 | ||||
else: | else: | ||||
assert_equal(i, rawtxfund['changepos']) | assert_equal(i, rawtxfund['changepos']) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) | assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex']) | ||||
assert_equal(matchingOuts, 1) | assert_equal(matchingOuts, 1) | ||||
assert_equal(len(dec_tx['vout']), 2) | assert_equal(len(dec_tx['vout']), 2) | ||||
def test_two_vin(self): | |||||
# | # | ||||
# test a fundrawtransaction with two VINs # | # test a fundrawtransaction with two VINs # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1) | ||||
utx2 = get_unspent(self.nodes[2].listunspent(), 5) | utx2 = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}, | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}, | ||||
{'txid': utx2['txid'], 'vout': utx2['vout']}] | {'txid': utx2['txid'], 'vout': utx2['vout']}] | ||||
outputs = {self.nodes[0].getnewaddress(): 6.0} | outputs = {self.nodes[0].getnewaddress(): 6.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
matchingOuts = 0 | matchingOuts = 0 | ||||
for out in dec_tx['vout']: | for out in dec_tx['vout']: | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
if out['scriptPubKey']['addresses'][0] in outputs: | if out['scriptPubKey']['addresses'][0] in outputs: | ||||
matchingOuts += 1 | matchingOuts += 1 | ||||
assert_equal(matchingOuts, 1) | assert_equal(matchingOuts, 1) | ||||
assert_equal(len(dec_tx['vout']), 2) | assert_equal(len(dec_tx['vout']), 2) | ||||
matchingIns = 0 | matchingIns = 0 | ||||
for vinOut in dec_tx['vin']: | for vinOut in dec_tx['vin']: | ||||
for vinIn in inputs: | for vinIn in inputs: | ||||
if vinIn['txid'] == vinOut['txid']: | if vinIn['txid'] == vinOut['txid']: | ||||
matchingIns += 1 | matchingIns += 1 | ||||
# we now must see two vins identical to vins given as params | # we now must see two vins identical to vins given as params | ||||
assert_equal(matchingIns, 2) | assert_equal(matchingIns, 2) | ||||
def test_two_vin_two_vout(self): | |||||
# | # | ||||
# test a fundrawtransaction with two VINs and two vOUTs # | # test a fundrawtransaction with two VINs and two vOUTs # | ||||
# | # | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1) | ||||
utx2 = get_unspent(self.nodes[2].listunspent(), 5) | utx2 = get_unspent(self.nodes[2].listunspent(), 5) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}, | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}, | ||||
{'txid': utx2['txid'], 'vout': utx2['vout']}] | {'txid': utx2['txid'], 'vout': utx2['vout']}] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 6.0, self.nodes[0].getnewaddress(): 1.0} | self.nodes[0].getnewaddress(): 6.0, self.nodes[0].getnewaddress(): 1.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
fee = rawtxfund['fee'] | |||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
totalOut = 0 | totalOut = 0 | ||||
matchingOuts = 0 | matchingOuts = 0 | ||||
for out in dec_tx['vout']: | for out in dec_tx['vout']: | ||||
totalOut += out['value'] | totalOut += out['value'] | ||||
if out['scriptPubKey']['addresses'][0] in outputs: | if out['scriptPubKey']['addresses'][0] in outputs: | ||||
matchingOuts += 1 | matchingOuts += 1 | ||||
assert_equal(matchingOuts, 2) | assert_equal(matchingOuts, 2) | ||||
assert_equal(len(dec_tx['vout']), 3) | assert_equal(len(dec_tx['vout']), 3) | ||||
def test_invalid_input(self): | |||||
# | # | ||||
# test a fundrawtransaction with invalid vin # | # test a fundrawtransaction with invalid vin # | ||||
# | # | ||||
# invalid vin! | |||||
inputs = [ | inputs = [ | ||||
{'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | {'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | ||||
# invalid vin! | |||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1.0} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | |||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawTx) | -4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawTx) | ||||
def test_fee_p2pkh(self): | |||||
# | # | ||||
# compare fee of a standard pubkeyhash transaction | # compare fee of a standard pubkeyhash transaction | ||||
# | |||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[1].getnewaddress(): 1.1} | outputs = {self.nodes[1].getnewaddress(): 1.1} | ||||
rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[0].fundrawtransaction(rawTx) | fundedTx = self.nodes[0].fundrawtransaction(rawTx) | ||||
# create same transaction over sendtoaddress | # create same transaction over sendtoaddress | ||||
txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1) | txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1.1) | ||||
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | ||||
# compare fee | # compare fee | ||||
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | ||||
assert feeDelta >= 0 and feeDelta <= feeTolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
# | |||||
def test_fee_p2pkh_multi_out(self): | |||||
# | # | ||||
# compare fee of a standard pubkeyhash transaction with multiple | # compare fee of a standard pubkeyhash transaction with multiple | ||||
# outputs | # outputs | ||||
# | |||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[1].getnewaddress(): 1.1, self.nodes[1].getnewaddress(): 1.2, self.nodes[1].getnewaddress(): 0.1, self.nodes[ | outputs = {self.nodes[1].getnewaddress(): 1.1, self.nodes[1].getnewaddress(): 1.2, self.nodes[1].getnewaddress(): 0.1, self.nodes[ | ||||
1].getnewaddress(): 1.3, self.nodes[1].getnewaddress(): 0.2, self.nodes[1].getnewaddress(): 0.3} | 1].getnewaddress(): 1.3, self.nodes[1].getnewaddress(): 0.2, self.nodes[1].getnewaddress(): 0.3} | ||||
rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[0].fundrawtransaction(rawTx) | fundedTx = self.nodes[0].fundrawtransaction(rawTx) | ||||
# create same transaction over sendtoaddress | # create same transaction over sendtoaddress | ||||
txId = self.nodes[0].sendmany("", outputs) | txId = self.nodes[0].sendmany("", outputs) | ||||
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | ||||
# compare fee | # compare fee | ||||
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | ||||
assert feeDelta >= 0 and feeDelta <= feeTolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
# | |||||
def test_fee_p2sh(self): | |||||
# | # | ||||
# compare fee of a 2of2 multisig p2sh transaction | # compare fee of a 2of2 multisig p2sh transaction | ||||
# | |||||
# create 2of2 addr | # create 2of2 addr | ||||
addr1 = self.nodes[1].getnewaddress() | addr1 = self.nodes[1].getnewaddress() | ||||
addr2 = self.nodes[1].getnewaddress() | addr2 = self.nodes[1].getnewaddress() | ||||
addr1Obj = self.nodes[1].getaddressinfo(addr1) | addr1Obj = self.nodes[1].getaddressinfo(addr1) | ||||
addr2Obj = self.nodes[1].getaddressinfo(addr2) | addr2Obj = self.nodes[1].getaddressinfo(addr2) | ||||
mSigObj = self.nodes[1].addmultisigaddress( | mSigObj = self.nodes[1].addmultisigaddress( | ||||
2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] | 2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] | ||||
inputs = [] | inputs = [] | ||||
outputs = {mSigObj: 1.1} | outputs = {mSigObj: 1.1} | ||||
rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | rawTx = self.nodes[0].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[0].fundrawtransaction(rawTx) | fundedTx = self.nodes[0].fundrawtransaction(rawTx) | ||||
# create same transaction over sendtoaddress | # create same transaction over sendtoaddress | ||||
txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) | txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) | ||||
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | ||||
# compare fee | # compare fee | ||||
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | ||||
assert feeDelta >= 0 and feeDelta <= feeTolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
# | |||||
def test_fee_4of5(self): | |||||
# | # | ||||
# compare fee of a standard pubkeyhash transaction | # compare fee of a standard pubkeyhash transaction | ||||
# | |||||
# create 4of5 addr | # create 4of5 addr | ||||
addr1 = self.nodes[1].getnewaddress() | addr1 = self.nodes[1].getnewaddress() | ||||
addr2 = self.nodes[1].getnewaddress() | addr2 = self.nodes[1].getnewaddress() | ||||
addr3 = self.nodes[1].getnewaddress() | addr3 = self.nodes[1].getnewaddress() | ||||
addr4 = self.nodes[1].getnewaddress() | addr4 = self.nodes[1].getnewaddress() | ||||
addr5 = self.nodes[1].getnewaddress() | addr5 = self.nodes[1].getnewaddress() | ||||
Show All 12 Lines | def test_fee_4of5(self): | ||||
fundedTx = self.nodes[0].fundrawtransaction(rawTx) | fundedTx = self.nodes[0].fundrawtransaction(rawTx) | ||||
# create same transaction over sendtoaddress | # create same transaction over sendtoaddress | ||||
txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) | txId = self.nodes[0].sendtoaddress(mSigObj, 1.1) | ||||
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | signedFee = self.nodes[0].getrawmempool(True)[txId]['fee'] | ||||
# compare fee | # compare fee | ||||
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | ||||
assert feeDelta >= 0 and feeDelta <= feeTolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
# | |||||
def test_spend_2of2(self): | |||||
# | # | ||||
# spend a 2of2 multisig transaction over fundraw | # spend a 2of2 multisig transaction over fundraw | ||||
# | |||||
# create 2of2 addr | # create 2of2 addr | ||||
addr1 = self.nodes[2].getnewaddress() | addr1 = self.nodes[2].getnewaddress() | ||||
addr2 = self.nodes[2].getnewaddress() | addr2 = self.nodes[2].getnewaddress() | ||||
addr1Obj = self.nodes[2].getaddressinfo(addr1) | addr1Obj = self.nodes[2].getaddressinfo(addr1) | ||||
addr2Obj = self.nodes[2].getaddressinfo(addr2) | addr2Obj = self.nodes[2].getaddressinfo(addr2) | ||||
mSigObj = self.nodes[2].addmultisigaddress( | mSigObj = self.nodes[2].addmultisigaddress( | ||||
2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] | 2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address'] | ||||
# send 1.2 BCH to msig addr | # send 1.2 BCH to msig addr | ||||
txId = self.nodes[0].sendtoaddress(mSigObj, 1.2) | self.nodes[0].sendtoaddress(mSigObj, 1.2) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
oldBalance = self.nodes[1].getbalance() | oldBalance = self.nodes[1].getbalance() | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[1].getnewaddress(): 1.1} | outputs = {self.nodes[1].getnewaddress(): 1.1} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[2].fundrawtransaction(rawTx) | fundedTx = self.nodes[2].fundrawtransaction(rawTx) | ||||
signedTx = self.nodes[2].signrawtransactionwithwallet(fundedTx['hex']) | signedTx = self.nodes[2].signrawtransactionwithwallet(fundedTx['hex']) | ||||
txId = self.nodes[2].sendrawtransaction(signedTx['hex']) | self.nodes[2].sendrawtransaction(signedTx['hex']) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# make sure funds are received at node1 | # make sure funds are received at node1 | ||||
assert_equal( | assert_equal( | ||||
oldBalance + Decimal('1.10000000'), self.nodes[1].getbalance()) | oldBalance + Decimal('1.10000000'), self.nodes[1].getbalance()) | ||||
def test_locked_wallet(self): | |||||
# | # | ||||
# locked wallet test | # locked wallet test | ||||
# | |||||
self.nodes[1].encryptwallet("test") | self.nodes[1].encryptwallet("test") | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.start_nodes() | self.start_nodes() | ||||
# This test is not meant to test fee estimation and we'd like | # This test is not meant to test fee estimation and we'd like | ||||
# to be sure all txs are sent at a consistent desired feerate | # to be sure all txs are sent at a consistent desired feerate | ||||
for node in self.nodes: | for node in self.nodes: | ||||
node.settxfee(min_relay_tx_fee) | node.settxfee(self.min_relay_tx_fee) | ||||
connect_nodes(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
connect_nodes(self.nodes[1], self.nodes[2]) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
connect_nodes(self.nodes[0], self.nodes[2]) | connect_nodes(self.nodes[0], self.nodes[2]) | ||||
connect_nodes(self.nodes[0], self.nodes[3]) | connect_nodes(self.nodes[0], self.nodes[3]) | ||||
# Again lock the watchonly UTXO or nodes[0] may spend it, because | # Again lock the watchonly UTXO or nodes[0] may spend it, because | ||||
# lockunspent is memory-only and thus lost on restart | # lockunspent is memory-only and thus lost on restart | ||||
self.nodes[0].lockunspent( | self.nodes[0].lockunspent( | ||||
False, [{"txid": watchonly_txid, "vout": watchonly_vout}]) | False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) | ||||
self.sync_all() | self.sync_all() | ||||
# drain the keypool | # drain the keypool | ||||
self.nodes[1].getnewaddress() | self.nodes[1].getnewaddress() | ||||
self.nodes[1].getrawchangeaddress() | self.nodes[1].getrawchangeaddress() | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.1} | outputs = {self.nodes[0].getnewaddress(): 1.1} | ||||
rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | ||||
Show All 16 Lines | def test_locked_wallet(self): | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.1} | outputs = {self.nodes[0].getnewaddress(): 1.1} | ||||
rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[1].fundrawtransaction(rawTx) | fundedTx = self.nodes[1].fundrawtransaction(rawTx) | ||||
# now we need to unlock | # now we need to unlock | ||||
self.nodes[1].walletpassphrase("test", 600) | self.nodes[1].walletpassphrase("test", 600) | ||||
signedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex']) | signedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex']) | ||||
txId = self.nodes[1].sendrawtransaction(signedTx['hex']) | self.nodes[1].sendrawtransaction(signedTx['hex']) | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# make sure funds are received at node1 | # make sure funds are received at node1 | ||||
assert_equal( | assert_equal( | ||||
oldBalance + Decimal('51.10000000'), self.nodes[0].getbalance()) | oldBalance + Decimal('51.10000000'), self.nodes[0].getbalance()) | ||||
def test_many_inputs_fee(self): | |||||
# | # | ||||
# multiple (~19) inputs tx test | Compare fee # | # multiple (~19) inputs tx test | Compare fee # | ||||
# | # | ||||
# empty node1, send some small coins from node0 to node1 | # empty node1, send some small coins from node0 to node1 | ||||
self.nodes[1].sendtoaddress( | self.nodes[1].sendtoaddress( | ||||
self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True) | self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True) | ||||
self.sync_all() | self.sync_all() | ||||
Show All 14 Lines | def test_many_inputs_fee(self): | ||||
# create same transaction over sendtoaddress | # create same transaction over sendtoaddress | ||||
txId = self.nodes[1].sendmany("", outputs) | txId = self.nodes[1].sendmany("", outputs) | ||||
signedFee = self.nodes[1].getrawmempool(True)[txId]['fee'] | signedFee = self.nodes[1].getrawmempool(True)[txId]['fee'] | ||||
# compare fee | # compare fee | ||||
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee) | ||||
# ~19 inputs | # ~19 inputs | ||||
assert feeDelta >= 0 and feeDelta <= feeTolerance * 19 | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 | ||||
def test_many_inputs_send(self): | |||||
# | # | ||||
# multiple (~19) inputs tx test | sign/send # | # multiple (~19) inputs tx test | sign/send # | ||||
# | # | ||||
# again, empty node1, send some small coins from node0 to node1 | # again, empty node1, send some small coins from node0 to node1 | ||||
self.nodes[1].sendtoaddress( | self.nodes[1].sendtoaddress( | ||||
self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True) | self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True) | ||||
self.sync_all() | self.sync_all() | ||||
Show All 10 Lines | def test_many_inputs_send(self): | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 0.15, self.nodes[0].getnewaddress(): 0.04} | self.nodes[0].getnewaddress(): 0.15, self.nodes[0].getnewaddress(): 0.04} | ||||
rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | rawTx = self.nodes[1].createrawtransaction(inputs, outputs) | ||||
fundedTx = self.nodes[1].fundrawtransaction(rawTx) | fundedTx = self.nodes[1].fundrawtransaction(rawTx) | ||||
fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet( | fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet( | ||||
fundedTx['hex']) | fundedTx['hex']) | ||||
txId = self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex']) | self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex']) | ||||
self.sync_all() | self.sync_all() | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
assert_equal(oldBalance + Decimal('50.19000000'), | assert_equal(oldBalance + Decimal('50.19000000'), | ||||
self.nodes[0].getbalance()) # 0.19+block reward | self.nodes[0].getbalance()) # 0.19+block reward | ||||
def test_op_return(self): | |||||
# | # | ||||
# test fundrawtransaction with OP_RETURN and no vin # | # test fundrawtransaction with OP_RETURN and no vin # | ||||
# | # | ||||
rawTx = "0100000000010000000000000000066a047465737400000000" | rawTx = "0100000000010000000000000000066a047465737400000000" | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
assert_equal(len(dec_tx['vin']), 0) | assert_equal(len(dec_tx['vin']), 0) | ||||
assert_equal(len(dec_tx['vout']), 1) | assert_equal(len(dec_tx['vout']), 1) | ||||
rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | rawtxfund = self.nodes[2].fundrawtransaction(rawTx) | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
assert_greater_than(len(dec_tx['vin']), 0) # at least one vin | # at least one vin | ||||
assert_equal(len(dec_tx['vout']), 2) # one change output added | assert_greater_than(len(dec_tx['vin']), 0) | ||||
# one change output added | |||||
assert_equal(len(dec_tx['vout']), 2) | |||||
def test_watchonly(self): | |||||
# | # | ||||
# test a fundrawtransaction using only watchonly # | # test a fundrawtransaction using only watchonly # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[2].getnewaddress(): watchonly_amount / 2} | outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
result = self.nodes[3].fundrawtransaction( | result = self.nodes[3].fundrawtransaction( | ||||
rawTx, {'includeWatching': True}) | rawTx, {'includeWatching': True}) | ||||
res_dec = self.nodes[0].decoderawtransaction(result["hex"]) | res_dec = self.nodes[0].decoderawtransaction(result["hex"]) | ||||
assert_equal(len(res_dec["vin"]), 1) | assert_equal(len(res_dec["vin"]), 1) | ||||
assert_equal(res_dec["vin"][0]["txid"], watchonly_txid) | assert_equal(res_dec["vin"][0]["txid"], self.watchonly_txid) | ||||
assert "fee" in result.keys() | assert "fee" in result.keys() | ||||
assert_greater_than(result["changepos"], -1) | assert_greater_than(result["changepos"], -1) | ||||
def test_all_watched_funds(self): | |||||
# | # | ||||
# test fundrawtransaction using the entirety of watched funds # | # test fundrawtransaction using the entirety of watched funds # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[2].getnewaddress(): watchonly_amount} | outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
# Backward compatibility test (2nd param is includeWatching) | # Backward compatibility test (2nd param is includeWatching) | ||||
result = self.nodes[3].fundrawtransaction(rawTx, True) | result = self.nodes[3].fundrawtransaction(rawTx, True) | ||||
res_dec = self.nodes[0].decoderawtransaction(result["hex"]) | res_dec = self.nodes[0].decoderawtransaction(result["hex"]) | ||||
assert_equal(len(res_dec["vin"]), 2) | assert_equal(len(res_dec["vin"]), 2) | ||||
assert res_dec["vin"][0]["txid"] == watchonly_txid or res_dec[ | assert res_dec["vin"][0]["txid"] == self.watchonly_txid or res_dec[ | ||||
"vin"][1]["txid"] == watchonly_txid | "vin"][1]["txid"] == self.watchonly_txid | ||||
assert_greater_than(result["fee"], 0) | assert_greater_than(result["fee"], 0) | ||||
assert_greater_than(result["changepos"], -1) | assert_greater_than(result["changepos"], -1) | ||||
assert_equal(result["fee"] + res_dec["vout"][ | assert_equal(result["fee"] + res_dec["vout"][ | ||||
result["changepos"]]["value"], watchonly_amount / 10) | result["changepos"]]["value"], self.watchonly_amount / 10) | ||||
signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"]) | signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"]) | ||||
assert not signedtx["complete"] | assert not signedtx["complete"] | ||||
signedtx = self.nodes[0].signrawtransactionwithwallet(signedtx["hex"]) | signedtx = self.nodes[0].signrawtransactionwithwallet(signedtx["hex"]) | ||||
assert signedtx["complete"] | assert signedtx["complete"] | ||||
self.nodes[0].sendrawtransaction(signedtx["hex"]) | self.nodes[0].sendrawtransaction(signedtx["hex"]) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
def test_option_feerate(self): | |||||
# | # | ||||
# Test feeRate option # | # Test feeRate option # | ||||
# | # | ||||
# Make sure there is exactly one input so coin selection can't skew the | # Make sure there is exactly one input so coin selection can't skew the | ||||
# result | # result | ||||
assert_equal(len(self.nodes[3].listunspent(1)), 1) | assert_equal(len(self.nodes[3].listunspent(1)), 1) | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[3].getnewaddress(): 1} | outputs = {self.nodes[3].getnewaddress(): 1} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
# uses min_relay_tx_fee (set by settxfee) | # uses self.min_relay_tx_fee (set by settxfee) | ||||
result = self.nodes[3].fundrawtransaction( | result = self.nodes[3].fundrawtransaction(rawTx) | ||||
rawTx) | |||||
result2 = self.nodes[3].fundrawtransaction( | result2 = self.nodes[3].fundrawtransaction( | ||||
rawTx, {"feeRate": 2 * min_relay_tx_fee}) | rawTx, {"feeRate": 2 * self.min_relay_tx_fee}) | ||||
result_fee_rate = result['fee'] * 1000 / \ | result_fee_rate = result['fee'] * 1000 / \ | ||||
FromHex(CTransaction(), result['hex']).billable_size() | FromHex(CTransaction(), result['hex']).billable_size() | ||||
assert_fee_amount( | assert_fee_amount( | ||||
result2['fee'], FromHex(CTransaction(), result2['hex']).billable_size(), 2 * result_fee_rate) | result2['fee'], FromHex(CTransaction(), result2['hex']).billable_size(), 2 * result_fee_rate) | ||||
result3 = self.nodes[3].fundrawtransaction( | result3 = self.nodes[3].fundrawtransaction( | ||||
rawTx, {"feeRate": 10 * min_relay_tx_fee}) | rawTx, {"feeRate": 10 * self.min_relay_tx_fee}) | ||||
assert_raises_rpc_error(-4, | assert_raises_rpc_error(-4, | ||||
"Fee exceeds maximum configured by -maxtxfee", | "Fee exceeds maximum configured by -maxtxfee", | ||||
self.nodes[3].fundrawtransaction, | self.nodes[3].fundrawtransaction, | ||||
rawTx, | rawTx, | ||||
{"feeRate": 1}) | {"feeRate": 1}) | ||||
# allow this transaction to be underfunded by 10 bytes. This is due | # allow this transaction to be underfunded by 10 bytes. This is due | ||||
# to the first transaction possibly being overfunded by up to .9 | # to the first transaction possibly being overfunded by up to .9 | ||||
# satoshi due to fee ceilings being used. | # satoshi due to fee ceilings being used. | ||||
assert_fee_amount( | assert_fee_amount( | ||||
result3['fee'], FromHex(CTransaction(), result3['hex']).billable_size(), 10 * result_fee_rate, 10) | result3['fee'], FromHex(CTransaction(), result3['hex']).billable_size(), 10 * result_fee_rate, 10) | ||||
def test_address_reuse(self): | |||||
# | # | ||||
# Test no address reuse occurs # | # Test no address reuse occurs # | ||||
# | # | ||||
rawTx = self.nodes[3].createrawtransaction( | |||||
inputs=[], outputs={self.nodes[3].getnewaddress(): 1}) | |||||
result3 = self.nodes[3].fundrawtransaction(rawTx) | result3 = self.nodes[3].fundrawtransaction(rawTx) | ||||
res_dec = self.nodes[0].decoderawtransaction(result3["hex"]) | res_dec = self.nodes[0].decoderawtransaction(result3["hex"]) | ||||
changeaddress = "" | changeaddress = "" | ||||
for out in res_dec['vout']: | for out in res_dec['vout']: | ||||
if out['value'] > 1.0: | if out['value'] > 1.0: | ||||
changeaddress += out['scriptPubKey']['addresses'][0] | changeaddress += out['scriptPubKey']['addresses'][0] | ||||
assert changeaddress != "" | assert changeaddress != "" | ||||
nextaddr = self.nodes[3].getnewaddress() | nextaddr = self.nodes[3].getnewaddress() | ||||
# Now the change address key should be removed from the keypool | # Now the change address key should be removed from the keypool | ||||
assert changeaddress != nextaddr | assert changeaddress != nextaddr | ||||
def test_option_subtract_fee_from_outputs(self): | |||||
# | # | ||||
# Test subtractFeeFromOutputs option # | # Test subtractFeeFromOutputs option # | ||||
# | # | ||||
# Make sure there is exactly one input so coin selection can't skew the | # Make sure there is exactly one input so coin selection can't skew the | ||||
# result | # result | ||||
assert_equal(len(self.nodes[3].listunspent(1)), 1) | assert_equal(len(self.nodes[3].listunspent(1)), 1) | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[2].getnewaddress(): 1} | outputs = {self.nodes[2].getnewaddress(): 1} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
result = [self.nodes[3].fundrawtransaction(rawTx), # uses min_relay_tx_fee (set by settxfee) | # uses self.min_relay_tx_fee (set by settxfee) | ||||
result = [self.nodes[3].fundrawtransaction(rawTx), | |||||
# empty subtraction list | |||||
self.nodes[3].fundrawtransaction( | |||||
rawTx, {"subtractFeeFromOutputs": []}), | |||||
# uses self.min_relay_tx_fee (set by settxfee) | |||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawTx, {"subtractFeeFromOutputs": []}), # empty subtraction list | rawTx, {"subtractFeeFromOutputs": [0]}), | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawTx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee) | rawTx, {"feeRate": 2 * self.min_relay_tx_fee}), | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawTx, {"feeRate": 2 * min_relay_tx_fee}), | rawTx, {"feeRate": 2 * self.min_relay_tx_fee, "subtractFeeFromOutputs": [0]}), | ||||
self.nodes[3].fundrawtransaction(rawTx, {"feeRate": 2 * min_relay_tx_fee, "subtractFeeFromOutputs": [0]})] | ] | ||||
dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) | dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) | ||||
for tx_ in result] | for tx_ in result] | ||||
output = [d['vout'][1 - r['changepos']]['value'] | output = [d['vout'][1 - r['changepos']]['value'] | ||||
for d, r in zip(dec_tx, result)] | for d, r in zip(dec_tx, result)] | ||||
change = [d['vout'][r['changepos']]['value'] | change = [d['vout'][r['changepos']]['value'] | ||||
for d, r in zip(dec_tx, result)] | for d, r in zip(dec_tx, result)] | ||||
▲ Show 20 Lines • Show All 54 Lines • Show Last 20 Lines |