Changeset View
Changeset View
Standalone View
Standalone View
test/functional/rpc_fundrawtransaction.py
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all() | self.sync_all() | ||||
# | # | ||||
# 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'] | fee = rawtxfund['fee'] | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
assert(len(dec_tx['vin']) > 0) # test that we have enough inputs | assert(len(dec_tx['vin']) > 0) # test that we have enough inputs | ||||
# | # | ||||
# 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'] | fee = rawtxfund['fee'] | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
assert(len(dec_tx['vin']) > 0) # test if we have enough inputs | assert(len(dec_tx['vin']) > 0) # test if we have enough inputs | ||||
# | # | ||||
# simple test with two coins # | # simple test with two coins # | ||||
# | # | ||||
inputs = [] | inputs = [] | ||||
outputs = {self.nodes[0].getnewaddress(): 2.6} | outputs = {self.nodes[0].getnewaddress(): 2.6} | ||||
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'] | fee = rawtxfund['fee'] | ||||
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex']) | ||||
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'], '') | ||||
# | # | ||||
# 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'] | 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'], '') | ||||
# | # | ||||
# 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'] | ||||
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']) | ||||
# | # | ||||
# 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) - fee - feeTolerance} | ||||
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']) | assert_equal(fee + totalOut, utx['amount']) | ||||
# compare vin total and totalout+fee | # compare vin total and totalout+fee | ||||
# | # | ||||
# 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'}) | ||||
# | # | ||||
# 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'}) | ||||
# | # | ||||
# 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]) | ||||
# | # | ||||
# 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'] | 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 | ||||
Show All 10 Lines | def run_test(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'] | 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 | ||||
Show All 15 Lines | def run_test(self): | ||||
# | # | ||||
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'] | 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) | ||||
# | # | ||||
# test a fundrawtransaction with invalid vin # | # test a fundrawtransaction with invalid vin # | ||||
# | # | ||||
inputs = [ | inputs = [ | ||||
{'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | {'txid': "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout': 0}] | ||||
# invalid vin! | # 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) | 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) | ||||
# | # | ||||
# 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) | ||||
▲ Show 20 Lines • Show All 237 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
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 | ||||
# | # | ||||
# 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 | assert_greater_than(len(dec_tx['vin']), 0) # at least one vin | ||||
assert_equal(len(dec_tx['vout']), 2) # one change output added | assert_equal(len(dec_tx['vout']), 2) # one change output added | ||||
# | # | ||||
# 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(): 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"], watchonly_txid) | ||||
assert("fee" in result.keys()) | assert("fee" in result.keys()) | ||||
assert_greater_than(result["changepos"], -1) | assert_greater_than(result["changepos"], -1) | ||||
# | # | ||||
# 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(): 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"] == watchonly_txid or res_dec[ | ||||
"vin"][1]["txid"] == watchonly_txid) | "vin"][1]["txid"] == 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"][ | ||||
Show All 12 Lines | def run_test(self): | ||||
# | # | ||||
# 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) | ||||
result = self.nodes[3].fundrawtransaction( | result = self.nodes[3].fundrawtransaction( | ||||
rawtx) # uses min_relay_tx_fee (set by settxfee) | rawTx) # uses min_relay_tx_fee (set by settxfee) | ||||
result2 = self.nodes[3].fundrawtransaction( | result2 = self.nodes[3].fundrawtransaction( | ||||
rawtx, {"feeRate": 2 * min_relay_tx_fee}) | rawTx, {"feeRate": 2 * 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 * min_relay_tx_fee}) | ||||
# 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) | ||||
# | # | ||||
# Test address reuse option # | # Test address reuse option # | ||||
# | # | ||||
result3 = self.nodes[3].fundrawtransaction( | result3 = self.nodes[3].fundrawtransaction( | ||||
rawtx, {"reserveChangeKey": False}) | rawTx, {"reserveChangeKey": False}) | ||||
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].getrawchangeaddress() | nextaddr = self.nodes[3].getrawchangeaddress() | ||||
# frt should not have removed the key from the keypool | # frt should not have removed the key from the keypool | ||||
assert(changeaddress == nextaddr) | assert(changeaddress == nextaddr) | ||||
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) | ||||
# | # | ||||
# 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) | result = [self.nodes[3].fundrawtransaction(rawTx), # uses min_relay_tx_fee (set by settxfee) | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list | rawTx, {"subtractFeeFromOutputs": []}), # empty subtraction list | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawtx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee) | rawTx, {"subtractFeeFromOutputs": [0]}), # uses min_relay_tx_fee (set by settxfee) | ||||
self.nodes[3].fundrawtransaction( | self.nodes[3].fundrawtransaction( | ||||
rawtx, {"feeRate": 2 * min_relay_tx_fee}), | rawTx, {"feeRate": 2 * min_relay_tx_fee}), | ||||
self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * 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)] | ||||
assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee']) | assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee']) | ||||
assert_equal(result[3]['fee'], result[4]['fee']) | assert_equal(result[3]['fee'], result[4]['fee']) | ||||
assert_equal(change[0], change[1]) | assert_equal(change[0], change[1]) | ||||
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 (1.0, 1.1, 1.2, 1.3)} | ||||
keys = list(outputs.keys()) | keys = list(outputs.keys()) | ||||
rawtx = self.nodes[3].createrawtransaction(inputs, outputs) | rawTx = self.nodes[3].createrawtransaction(inputs, outputs) | ||||
result = [self.nodes[3].fundrawtransaction(rawtx), | result = [self.nodes[3].fundrawtransaction(rawTx), | ||||
# 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 | ||||
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'])] | ||||
# Nested list of non-change output amounts for each transaction | # Nested list of non-change output amounts for each transaction | ||||
output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']] | output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']] | ||||
for d, r in zip(dec_tx, result)] | for d, r in zip(dec_tx, result)] | ||||
Show All 29 Lines |