Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_fundrawtransaction.py
Show First 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.test_option_subtract_fee_from_outputs() | self.test_option_subtract_fee_from_outputs() | ||||
self.test_subtract_fee_with_presets() | self.test_subtract_fee_with_presets() | ||||
def test_change_position(self): | def test_change_position(self): | ||||
"""Ensure setting changePosition in fundraw with an exact match is | """Ensure setting changePosition in fundraw with an exact match is | ||||
handled properly.""" | handled properly.""" | ||||
self.log.info("Test fundrawtxn changePosition option") | self.log.info("Test fundrawtxn changePosition option") | ||||
rawmatch = self.nodes[2].createrawtransaction( | rawmatch = self.nodes[2].createrawtransaction( | ||||
[], {self.nodes[2].getnewaddress(): 50}) | [], {self.nodes[2].getnewaddress(): 50000000}) | ||||
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[0].getaddressinfo(watchonly_address)[ | watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)[ | ||||
"pubkey"] | "pubkey"] | ||||
self.watchonly_amount = Decimal(200) | self.watchonly_amount = Decimal(200000000) | ||||
self.nodes[3].importpubkey(watchonly_pubkey, "", True) | self.nodes[3].importpubkey(watchonly_pubkey, "", True) | ||||
self.watchonly_txid = self.nodes[0].sendtoaddress( | self.watchonly_txid = self.nodes[0].sendtoaddress( | ||||
watchonly_address, self.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 | ||||
self.watchonly_vout = find_vout_for_address( | self.watchonly_vout = find_vout_for_address( | ||||
self.nodes[0], self.watchonly_txid, watchonly_address) | self.nodes[0], self.watchonly_txid, watchonly_address) | ||||
self.nodes[0].lockunspent( | self.nodes[0].lockunspent( | ||||
False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) | False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}]) | ||||
self.nodes[0].sendtoaddress( | self.nodes[0].sendtoaddress( | ||||
self.nodes[3].getnewaddress(), | self.nodes[3].getnewaddress(), | ||||
self.watchonly_amount / 10) | self.watchonly_amount / 10) | ||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.5) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1500000) | ||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1000000) | ||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5.0) | self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 5000000) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
def test_simple(self): | def test_simple(self): | ||||
self.log.info("Test fundrawtxn") | self.log.info("Test fundrawtxn") | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1000000} | ||||
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) | ||||
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): | def test_simple_two_coins(self): | ||||
self.log.info("Test fundrawtxn with 2 coins") | self.log.info("Test fundrawtxn with 2 coins") | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 2.2} | outputs = {self.nodes[0].getnewaddress(): 2200000} | ||||
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) | ||||
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 | ||||
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '') | ||||
def test_simple_two_outputs(self): | def test_simple_two_outputs(self): | ||||
self.log.info("Test fundrawtxn with 2 outputs") | self.log.info("Test fundrawtxn with 2 outputs") | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 2.6, self.nodes[1].getnewaddress(): 2.5} | self.nodes[0].getnewaddress(): 2600000, self.nodes[1].getnewaddress(): 2500000} | ||||
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) | ||||
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): | def test_change(self): | ||||
self.log.info("Test fundrawtxn with a vin > required amount") | self.log.info("Test fundrawtxn with a vin > required amount") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(): 1000000} | ||||
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 | # Use the same fee for the next tx | ||||
self.test_no_change_fee = fee | 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): | def test_no_change(self): | ||||
self.log.info("Test fundrawtxn not having a change output") | self.log.info("Test fundrawtxn not having a change output") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | inputs = [{'txid': utx['txid'], 'vout': utx['vout']}] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): Decimal(5.0) - | self.nodes[0].getnewaddress(): Decimal(5000000) - | ||||
self.test_no_change_fee - | self.test_no_change_fee - | ||||
self.fee_tolerance} | 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) | ||||
# 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_invalid_option(self): | def test_invalid_option(self): | ||||
self.log.info("Test fundrawtxn with an invalid option") | self.log.info("Test fundrawtxn with an invalid option") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(4000000)} | ||||
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'}) | ||||
# reserveChangeKey was deprecated and is now removed | # reserveChangeKey was deprecated and is now removed | ||||
assert_raises_rpc_error(-3, | assert_raises_rpc_error(-3, | ||||
"Unexpected key reserveChangeKey", | "Unexpected key reserveChangeKey", | ||||
lambda: self.nodes[2].fundrawtransaction(hexstring=rawTx, | lambda: self.nodes[2].fundrawtransaction(hexstring=rawTx, | ||||
options={'reserveChangeKey': True})) | options={'reserveChangeKey': True})) | ||||
def test_invalid_change_address(self): | def test_invalid_change_address(self): | ||||
self.log.info("Test fundrawtxn with an invalid change address") | self.log.info("Test fundrawtxn with an invalid change address") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(4000000)} | ||||
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): | def test_valid_change_address(self): | ||||
self.log.info("Test fundrawtxn with a provided change address") | self.log.info("Test fundrawtxn with a provided change address") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 5) | utx = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(4000000)} | ||||
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): | def test_coin_selection(self): | ||||
self.log.info("Test fundrawtxn with a vin < required amount") | self.log.info("Test fundrawtxn with a vin < required amount") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1000000) | ||||
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(): 1000000} | ||||
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']) | ||||
Show All 20 Lines | def test_coin_selection(self): | ||||
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): | def test_two_vin(self): | ||||
self.log.info("Test fundrawtxn with 2 vins") | self.log.info("Test fundrawtxn with 2 vins") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1000000) | ||||
utx2 = get_unspent(self.nodes[2].listunspent(), 5) | utx2 = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(): 6000000} | ||||
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']) | ||||
# Should fail without add_inputs: | # Should fail without add_inputs: | ||||
assert_raises_rpc_error(-4, | assert_raises_rpc_error(-4, | ||||
"Insufficient funds", | "Insufficient funds", | ||||
self.nodes[2].fundrawtransaction, | self.nodes[2].fundrawtransaction, | ||||
Show All 19 Lines | def test_two_vin(self): | ||||
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): | def test_two_vin_two_vout(self): | ||||
self.log.info("Test fundrawtxn with 2 vins and 2 vouts") | self.log.info("Test fundrawtxn with 2 vins and 2 vouts") | ||||
utx = get_unspent(self.nodes[2].listunspent(), 1) | utx = get_unspent(self.nodes[2].listunspent(), 1000000) | ||||
utx2 = get_unspent(self.nodes[2].listunspent(), 5) | utx2 = get_unspent(self.nodes[2].listunspent(), 5000000) | ||||
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(): 6000000, self.nodes[0].getnewaddress(): 1000000} | ||||
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']) | ||||
# Should fail without add_inputs: | # Should fail without add_inputs: | ||||
assert_raises_rpc_error(-4, | assert_raises_rpc_error(-4, | ||||
"Insufficient funds", | "Insufficient funds", | ||||
self.nodes[2].fundrawtransaction, | self.nodes[2].fundrawtransaction, | ||||
Show All 12 Lines | def test_two_vin_two_vout(self): | ||||
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): | def test_invalid_input(self): | ||||
self.log.info("Test fundrawtxn with an invalid vin") | self.log.info("Test fundrawtxn with an invalid vin") | ||||
inputs = [ | inputs = [ | ||||
{'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | {'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.0} | outputs = {self.nodes[0].getnewaddress(): 1000000} | ||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | rawTx = self.nodes[2].createrawtransaction(inputs, outputs) | ||||
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): | def test_fee_p2pkh(self): | ||||
"""Compare fee of a standard pubkeyhash transaction.""" | """Compare fee of a standard pubkeyhash transaction.""" | ||||
self.log.info("Test fundrawtxn p2pkh fee") | self.log.info("Test fundrawtxn p2pkh fee") | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[1].getnewaddress(): 1.1} | outputs = {self.nodes[1].getnewaddress(): 1100000} | ||||
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(), 1100000) | |||||
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 <= self.fee_tolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
def test_fee_p2pkh_multi_out(self): | 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.""" | ||||
self.log.info("Test fundrawtxn p2pkh fee with multiple outputs") | self.log.info("Test fundrawtxn p2pkh fee with multiple outputs") | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[1].getnewaddress(): 1.1, | self.nodes[1].getnewaddress(): 1100000, | ||||
self.nodes[1].getnewaddress(): 1.2, | self.nodes[1].getnewaddress(): 1200000, | ||||
self.nodes[1].getnewaddress(): 0.1, | self.nodes[1].getnewaddress(): 100000, | ||||
self.nodes[1].getnewaddress(): 1.3, | self.nodes[1].getnewaddress(): 1300000, | ||||
self.nodes[1].getnewaddress(): 0.2, | self.nodes[1].getnewaddress(): 200000, | ||||
self.nodes[1].getnewaddress(): 0.3, | self.nodes[1].getnewaddress(): 300000, | ||||
} | } | ||||
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 <= self.fee_tolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
def test_fee_p2sh(self): | def test_fee_p2sh(self): | ||||
"""Compare fee of a 2-of-2 multisig p2sh transaction.""" | """Compare fee of a 2-of-2 multisig p2sh transaction.""" | ||||
# Create 2-of-2 addr. | # Create 2-of-2 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: 1100000} | ||||
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, 1100000) | ||||
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 <= self.fee_tolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
def test_fee_4of5(self): | def test_fee_4of5(self): | ||||
"""Compare fee of a standard pubkeyhash transaction.""" | """Compare fee of a standard pubkeyhash transaction.""" | ||||
Show All 19 Lines | def test_fee_4of5(self): | ||||
addr2Obj['pubkey'], | addr2Obj['pubkey'], | ||||
addr3Obj['pubkey'], | addr3Obj['pubkey'], | ||||
addr4Obj['pubkey'], | addr4Obj['pubkey'], | ||||
addr5Obj['pubkey'], | addr5Obj['pubkey'], | ||||
] | ] | ||||
)['address'] | )['address'] | ||||
inputs = [] | inputs = [] | ||||
outputs = {mSigObj: 1.1} | outputs = {mSigObj: 1100000} | ||||
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, 1100000) | ||||
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 <= self.fee_tolerance | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance | ||||
def test_spend_2of2(self): | def test_spend_2of2(self): | ||||
"""Spend a 2-of-2 multisig transaction over fundraw.""" | """Spend a 2-of-2 multisig transaction over fundraw.""" | ||||
Show All 10 Lines | def test_spend_2of2(self): | ||||
2, | 2, | ||||
[ | [ | ||||
addr1Obj['pubkey'], | addr1Obj['pubkey'], | ||||
addr2Obj['pubkey'], | addr2Obj['pubkey'], | ||||
] | ] | ||||
)['address'] | )['address'] | ||||
# Send 1.2 BCH to msig addr. | # Send 1.2 BCH to msig addr. | ||||
self.nodes[0].sendtoaddress(mSigObj, 1.2) | self.nodes[0].sendtoaddress(mSigObj, 1200000) | ||||
self.nodes[0].generate(1) | self.nodes[0].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(): 1100000} | ||||
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']) | ||||
self.nodes[2].sendrawtransaction(signedTx['hex']) | self.nodes[2].sendrawtransaction(signedTx['hex']) | ||||
self.nodes[2].generate(1) | self.nodes[2].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('1100000.00'), self.nodes[1].getbalance()) | ||||
def test_locked_wallet(self): | def test_locked_wallet(self): | ||||
self.log.info("Test fundrawtxn with locked wallet") | self.log.info("Test fundrawtxn with locked wallet") | ||||
self.nodes[1].encryptwallet("test") | self.nodes[1].encryptwallet("test") | ||||
# 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.09999700} | outputs = {self.nodes[0].getnewaddress(): 1099997.00} | ||||
rawtx = self.nodes[1].createrawtransaction(inputs, outputs) | rawtx = self.nodes[1].createrawtransaction(inputs, outputs) | ||||
# fund a transaction that does not require a new key for the change | # fund a transaction that does not require a new key for the change | ||||
# output | # output | ||||
self.nodes[1].fundrawtransaction(rawtx) | self.nodes[1].fundrawtransaction(rawtx) | ||||
# fund a transaction that requires a new key for the change output | # fund a transaction that requires a new key for the change output | ||||
# creating the key must be impossible because the wallet is locked | # creating the key must be impossible because the wallet is locked | ||||
outputs = {self.nodes[0].getnewaddress(): 1.1} | outputs = {self.nodes[0].getnewaddress(): 1100000} | ||||
rawtx = self.nodes[1].createrawtransaction(inputs, outputs) | rawtx = self.nodes[1].createrawtransaction(inputs, outputs) | ||||
assert_raises_rpc_error( | assert_raises_rpc_error( | ||||
-4, | -4, | ||||
"Transaction needs a change address, but we can't generate it. Please call keypoolrefill first.", | "Transaction needs a change address, but we can't generate it. Please call keypoolrefill first.", | ||||
self.nodes[1].fundrawtransaction, | self.nodes[1].fundrawtransaction, | ||||
rawtx) | rawtx) | ||||
# Refill the keypool. | # Refill the keypool. | ||||
self.nodes[1].walletpassphrase("test", 100) | self.nodes[1].walletpassphrase("test", 100) | ||||
# need to refill the keypool to get an internal change address | # need to refill the keypool to get an internal change address | ||||
self.nodes[1].keypoolrefill(8) | self.nodes[1].keypoolrefill(8) | ||||
self.nodes[1].walletlock() | self.nodes[1].walletlock() | ||||
assert_raises_rpc_error(-13, "walletpassphrase", self.nodes[ | assert_raises_rpc_error(-13, "walletpassphrase", self.nodes[ | ||||
1].sendtoaddress, self.nodes[0].getnewaddress(), 1.2) | 1].sendtoaddress, self.nodes[0].getnewaddress(), 1200000) | ||||
oldBalance = self.nodes[0].getbalance() | oldBalance = self.nodes[0].getbalance() | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 1.1} | outputs = {self.nodes[0].getnewaddress(): 1100000} | ||||
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']) | ||||
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('51100000.00'), self.nodes[0].getbalance()) | ||||
def test_many_inputs_fee(self): | def test_many_inputs_fee(self): | ||||
"""Multiple (~19) inputs tx test | Compare fee.""" | """Multiple (~19) inputs tx test | Compare fee.""" | ||||
self.log.info("Test fundrawtxn fee with many inputs") | self.log.info("Test fundrawtxn fee with many inputs") | ||||
# 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.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
for _ in range(0, 20): | for _ in range(0, 20): | ||||
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) | self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 10000) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# Fund a tx with ~20 small inputs. | # Fund a tx with ~20 small inputs. | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 0.15, self.nodes[0].getnewaddress(): 0.04} | self.nodes[0].getnewaddress(): 150000, self.nodes[0].getnewaddress(): 40000} | ||||
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) | ||||
# 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 <= self.fee_tolerance * 19 | assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 | ||||
def test_many_inputs_send(self): | def test_many_inputs_send(self): | ||||
"""Multiple (~19) inputs tx test | sign/send.""" | """Multiple (~19) inputs tx test | sign/send.""" | ||||
self.log.info("Test fundrawtxn sign+send with many inputs") | self.log.info("Test fundrawtxn sign+send with many inputs") | ||||
# 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.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
for _ in range(0, 20): | for _ in range(0, 20): | ||||
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.01) | self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 10000) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# Fund a tx with ~20 small inputs. | # Fund a tx with ~20 small inputs. | ||||
oldBalance = self.nodes[0].getbalance() | oldBalance = self.nodes[0].getbalance() | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[0].getnewaddress(): 0.15, self.nodes[0].getnewaddress(): 0.04} | self.nodes[0].getnewaddress(): 150000, self.nodes[0].getnewaddress(): 40000} | ||||
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']) | ||||
self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex']) | self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex']) | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
assert_equal(oldBalance + Decimal('50.19000000'), | assert_equal(oldBalance + Decimal('50190000.00'), | ||||
self.nodes[0].getbalance()) # 0.19+block reward | self.nodes[0].getbalance()) # 0.19+block reward | ||||
def test_op_return(self): | def test_op_return(self): | ||||
self.log.info("Test fundrawtxn with OP_RETURN and no vin") | self.log.info("Test fundrawtxn with OP_RETURN and no vin") | ||||
rawTx = "0100000000010000000000000000066a047465737400000000" | rawTx = "0100000000010000000000000000066a047465737400000000" | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawTx) | dec_tx = self.nodes[2].decoderawtransaction(rawTx) | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | class RawTransactionsTest(BitcoinTestFramework): | ||||
def test_option_feerate(self): | def test_option_feerate(self): | ||||
self.log.info("Test fundrawtxn feeRate option") | self.log.info("Test fundrawtxn 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(): 1000000} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
# uses self.min_relay_tx_fee (set by settxfee) | # uses self.min_relay_tx_fee (set by settxfee) | ||||
result = self.nodes[3].fundrawtransaction(rawTx) | result = self.nodes[3].fundrawtransaction(rawTx) | ||||
result2 = self.nodes[3].fundrawtransaction( | result2 = self.nodes[3].fundrawtransaction( | ||||
rawTx, {"feeRate": 2 * self.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 * self.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": 1000000}) | ||||
# 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): | def test_address_reuse(self): | ||||
"""Test no address reuse occurs.""" | """Test no address reuse occurs.""" | ||||
self.log.info("Test fundrawtxn does not reuse addresses") | self.log.info("Test fundrawtxn does not reuse addresses") | ||||
rawTx = self.nodes[3].createrawtransaction( | rawTx = self.nodes[3].createrawtransaction( | ||||
inputs=[], outputs={self.nodes[3].getnewaddress(): 1}) | inputs=[], outputs={self.nodes[3].getnewaddress(): 1000000}) | ||||
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'] > 1000000.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): | def test_option_subtract_fee_from_outputs(self): | ||||
self.log.info("Test fundrawtxn subtractFeeFromOutputs option") | self.log.info("Test fundrawtxn 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(): 1000000} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
# uses self.min_relay_tx_fee (set by settxfee) | # uses self.min_relay_tx_fee (set by settxfee) | ||||
result = [self.nodes[3].fundrawtransaction(rawTx), | result = [self.nodes[3].fundrawtransaction(rawTx), | ||||
# empty subtraction list | # empty subtraction list | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawTx, {"subtractFeeFromOutputs": []}), | rawTx, {"subtractFeeFromOutputs": []}), | ||||
# uses self.min_relay_tx_fee (set by settxfee) | # uses self.min_relay_tx_fee (set by settxfee) | ||||
Show All 18 Lines | def test_option_subtract_fee_from_outputs(self): | ||||
assert_equal(output[0], output[1]) | assert_equal(output[0], output[1]) | ||||
assert_equal(output[0], output[2] + result[2]['fee']) | assert_equal(output[0], output[2] + result[2]['fee']) | ||||
assert_equal(change[0] + result[0]['fee'], change[2]) | assert_equal(change[0] + result[0]['fee'], change[2]) | ||||
assert_equal(output[3], output[4] + result[4]['fee']) | assert_equal(output[3], output[4] + result[4]['fee']) | ||||
assert_equal(change[3] + result[3]['fee'], change[4]) | assert_equal(change[3] + result[3]['fee'], change[4]) | ||||
inputs = [] | inputs = [] | ||||
outputs = { | outputs = { | ||||
self.nodes[2].getnewaddress(): value for value in (1.0, 1.1, 1.2, 1.3)} | self.nodes[2].getnewaddress(): value for value in (1000000.0, 1100000.0, 1200000.0, 1300000.0)} | ||||
rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
# Split the fee between outputs 0, 2, and 3, but not output 1 | # Split the fee between outputs 0, 2, and 3, but not output 1 | ||||
result = [self.nodes[3].fundrawtransaction(rawTx), | result = [self.nodes[3].fundrawtransaction(rawTx), | ||||
self.nodes[3].fundrawtransaction(rawTx, {"subtractFeeFromOutputs": [0, 2, 3]})] | self.nodes[3].fundrawtransaction(rawTx, {"subtractFeeFromOutputs": [0, 2, 3]})] | ||||
dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']), | dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']), | ||||
self.nodes[3].decoderawtransaction(result[1]['hex'])] | self.nodes[3].decoderawtransaction(result[1]['hex'])] | ||||
Show All 29 Lines | def test_option_subtract_fee_from_outputs(self): | ||||
# The total subtracted from the outputs is equal to the fee. | # The total subtracted from the outputs is equal to the fee. | ||||
assert_equal(share[0] + share[2] + share[3], result[0]['fee']) | assert_equal(share[0] + share[2] + share[3], result[0]['fee']) | ||||
def test_subtract_fee_with_presets(self): | def test_subtract_fee_with_presets(self): | ||||
self.log.info( | self.log.info( | ||||
"Test fundrawtxn subtract fee from outputs with preset inputs that are sufficient") | "Test fundrawtxn subtract fee from outputs with preset inputs that are sufficient") | ||||
addr = self.nodes[0].getnewaddress() | addr = self.nodes[0].getnewaddress() | ||||
txid = self.nodes[0].sendtoaddress(addr, 10) | txid = self.nodes[0].sendtoaddress(addr, 10000000) | ||||
vout = find_vout_for_address(self.nodes[0], txid, addr) | vout = find_vout_for_address(self.nodes[0], txid, addr) | ||||
rawtx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], [ | rawtx = self.nodes[0].createrawtransaction([{'txid': txid, 'vout': vout}], [ | ||||
{self.nodes[0].getnewaddress(): 5}]) | {self.nodes[0].getnewaddress(): 5000000}]) | ||||
fundedtx = self.nodes[0].fundrawtransaction( | fundedtx = self.nodes[0].fundrawtransaction( | ||||
rawtx, {'subtractFeeFromOutputs': [0]}) | rawtx, {'subtractFeeFromOutputs': [0]}) | ||||
signedtx = self.nodes[0].signrawtransactionwithwallet(fundedtx['hex']) | signedtx = self.nodes[0].signrawtransactionwithwallet(fundedtx['hex']) | ||||
self.nodes[0].sendrawtransaction(signedtx['hex']) | self.nodes[0].sendrawtransaction(signedtx['hex']) | ||||
if __name__ == '__main__': | if __name__ == '__main__': | ||||
RawTransactionsTest().main() | RawTransactionsTest().main() |