2016-03-19 20:58:06 +01:00
|
|
|
#!/usr/bin/env python3
|
2019-02-21 02:03:13 +01:00
|
|
|
# Copyright (c) 2014-2019 The Bitcoin Core developers
|
2015-04-25 03:26:30 +02:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-18 00:34:40 +01:00
|
|
|
"""Test the fundrawtransaction RPC."""
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2018-01-25 11:03:05 +01:00
|
|
|
from decimal import Decimal
|
2017-08-16 17:52:24 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2018-01-25 11:03:05 +01:00
|
|
|
from test_framework.util import (
|
|
|
|
assert_equal,
|
|
|
|
assert_fee_amount,
|
|
|
|
assert_greater_than,
|
|
|
|
assert_greater_than_or_equal,
|
|
|
|
assert_raises_rpc_error,
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes,
|
2018-01-25 11:03:05 +01:00
|
|
|
count_bytes,
|
|
|
|
find_vout_for_address,
|
|
|
|
)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2016-04-16 18:36:39 +02:00
|
|
|
|
|
|
|
def get_unspent(listunspent, amount):
|
|
|
|
for utx in listunspent:
|
|
|
|
if utx['amount'] == amount:
|
|
|
|
return utx
|
|
|
|
raise AssertionError('Could not find unspent with amount={}'.format(amount))
|
|
|
|
|
2015-04-25 03:26:30 +02:00
|
|
|
class RawTransactionsTest(BitcoinTestFramework):
|
2017-06-10 00:21:21 +02:00
|
|
|
def set_test_params(self):
|
2016-05-14 13:01:31 +02:00
|
|
|
self.num_nodes = 4
|
|
|
|
self.setup_clean_chain = True
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2018-12-22 17:19:50 +01:00
|
|
|
def setup_network(self):
|
2017-04-03 15:34:04 +02:00
|
|
|
self.setup_nodes()
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[1], 2)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
connect_nodes(self.nodes[0], 3)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
def run_test(self):
|
2019-06-26 18:42:11 +02:00
|
|
|
self.min_relay_tx_fee = self.nodes[0].getnetworkinfo()['relayfee']
|
2016-01-05 23:47:04 +01:00
|
|
|
# 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
|
|
|
|
for node in self.nodes:
|
2019-06-26 18:42:11 +02:00
|
|
|
node.settxfee(self.min_relay_tx_fee)
|
2016-01-05 23:47:04 +01:00
|
|
|
|
2015-10-14 14:27:03 +02:00
|
|
|
# if the fee's positive delta is higher than this value tests will fail,
|
|
|
|
# neg. delta always fail the tests.
|
|
|
|
# The size of the signature of every input may be at most 2 bytes larger
|
|
|
|
# than a minimum sized signature.
|
|
|
|
|
|
|
|
# = 2 bytes * minRelayTxFeePerByte
|
2019-06-26 18:42:11 +02:00
|
|
|
self.fee_tolerance = 2 * self.min_relay_tx_fee / 1000
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
self.nodes[2].generate(1)
|
2015-07-10 20:11:44 +02:00
|
|
|
self.sync_all()
|
2015-04-24 06:42:49 +02:00
|
|
|
self.nodes[0].generate(121)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
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_change_type()
|
|
|
|
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):
|
2017-04-28 23:22:37 +02:00
|
|
|
# ensure that setting changePosition in fundraw with an exact match is handled properly
|
2019-09-20 17:33:17 +02:00
|
|
|
rawmatch = self.nodes[2].createrawtransaction([], {self.nodes[2].getnewaddress():1})
|
2017-04-28 23:22:37 +02:00
|
|
|
rawmatch = self.nodes[2].fundrawtransaction(rawmatch, {"changePosition":1, "subtractFeeFromOutputs":[0]})
|
|
|
|
assert_equal(rawmatch["changepos"], -1)
|
|
|
|
|
2015-04-24 06:42:49 +02:00
|
|
|
watchonly_address = self.nodes[0].getnewaddress()
|
2018-02-09 17:12:27 +01:00
|
|
|
watchonly_pubkey = self.nodes[0].getaddressinfo(watchonly_address)["pubkey"]
|
2019-09-20 17:33:17 +02:00
|
|
|
self.watchonly_amount = Decimal(4)
|
2015-04-24 06:42:49 +02:00
|
|
|
self.nodes[3].importpubkey(watchonly_pubkey, "", True)
|
2019-06-26 18:42:11 +02:00
|
|
|
self.watchonly_txid = self.nodes[0].sendtoaddress(watchonly_address, self.watchonly_amount)
|
2018-01-25 11:03:05 +01:00
|
|
|
|
|
|
|
# Lock UTXO so nodes[0] doesn't accidentally spend it
|
2019-06-26 18:42:11 +02:00
|
|
|
self.watchonly_vout = find_vout_for_address(self.nodes[0], self.watchonly_txid, watchonly_address)
|
|
|
|
self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}])
|
2018-01-25 11:03:05 +01:00
|
|
|
|
2019-06-26 18:42:11 +02:00
|
|
|
self.nodes[0].sendtoaddress(self.nodes[3].getnewaddress(), self.watchonly_amount / 10)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2019-09-20 17:33:17 +02:00
|
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 0.03)
|
|
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 0.02)
|
|
|
|
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 0.1)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2015-04-25 03:26:30 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_simple(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
###############
|
|
|
|
# simple test #
|
|
|
|
###############
|
|
|
|
inputs = [ ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.02 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(dec_tx['vin']) > 0 #test that we have enough inputs
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_simple_two_coins(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
##############################
|
|
|
|
# simple test with two coins #
|
|
|
|
##############################
|
|
|
|
inputs = [ ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.044 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(dec_tx['vin']) > 0 #test if we have enough inputs
|
2015-04-25 03:26:30 +02:00
|
|
|
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_simple_two_outputs(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
################################
|
|
|
|
# simple test with two outputs #
|
|
|
|
################################
|
|
|
|
inputs = [ ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.052, self.nodes[1].getnewaddress() : 0.05 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
for out in dec_tx['vout']:
|
|
|
|
totalOut += out['value']
|
|
|
|
|
2019-02-19 23:43:44 +01:00
|
|
|
assert len(dec_tx['vin']) > 0
|
2015-04-25 03:26:30 +02:00
|
|
|
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_change(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
#########################################################################
|
|
|
|
# test a fundrawtransaction with a VIN greater than the required amount #
|
|
|
|
#########################################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.02 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
fee = rawtxfund['fee']
|
2019-06-26 18:42:11 +02:00
|
|
|
self.test_no_change_fee = fee # Use the same fee for the next tx
|
2015-04-25 03:26:30 +02:00
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
for out in dec_tx['vout']:
|
|
|
|
totalOut += out['value']
|
|
|
|
|
|
|
|
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_no_change(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
#####################################################################
|
|
|
|
# test a fundrawtransaction with which will not get a change output #
|
|
|
|
#####################################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[0].getnewaddress(): Decimal("0.1") - self.test_no_change_fee - self.fee_tolerance}
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
fee = rawtxfund['fee']
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
for out in dec_tx['vout']:
|
|
|
|
totalOut += out['value']
|
|
|
|
|
|
|
|
assert_equal(rawtxfund['changepos'], -1)
|
|
|
|
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_invalid_option(self):
|
2016-03-30 03:04:22 +02:00
|
|
|
####################################################
|
|
|
|
# test a fundrawtransaction with an invalid option #
|
|
|
|
####################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2016-03-30 03:04:22 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : Decimal("0.08") }
|
2016-03-30 03:04:22 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-3, "Unexpected key foo", self.nodes[2].fundrawtransaction, rawtx, {'foo':'bar'})
|
2016-03-30 03:04:22 +02:00
|
|
|
|
2018-02-11 04:12:43 +01:00
|
|
|
# reserveChangeKey was deprecated and is now removed
|
|
|
|
assert_raises_rpc_error(-3, "Unexpected key reserveChangeKey", lambda: self.nodes[2].fundrawtransaction(hexstring=rawtx, options={'reserveChangeKey': True}))
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_invalid_change_address(self):
|
2016-03-30 03:04:22 +02:00
|
|
|
############################################################
|
|
|
|
# test a fundrawtransaction with an invalid change address #
|
|
|
|
############################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2016-03-30 03:04:22 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : Decimal("0.08") }
|
2016-03-30 03:04:22 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-5, "changeAddress must be a valid bitcoin address", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':'foobar'})
|
2016-03-30 03:04:22 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_valid_change_address(self):
|
2016-03-30 03:04:22 +02:00
|
|
|
############################################################
|
|
|
|
# test a fundrawtransaction with a provided change address #
|
|
|
|
############################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2016-03-30 03:04:22 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : Decimal("0.08") }
|
2016-03-30 03:04:22 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
|
|
|
change = self.nodes[2].getnewaddress()
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-8, "changePosition out of bounds", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':change, 'changePosition':2})
|
2016-03-30 03:04:22 +02:00
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx, {'changeAddress': change, 'changePosition': 0})
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
2017-01-20 22:55:26 +01:00
|
|
|
out = dec_tx['vout'][0]
|
2016-03-30 03:04:22 +02:00
|
|
|
assert_equal(change, out['scriptPubKey']['addresses'][0])
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_change_type(self):
|
2018-01-24 16:28:15 +01:00
|
|
|
#########################################################
|
|
|
|
# test a fundrawtransaction with a provided change type #
|
|
|
|
#########################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2018-01-24 16:28:15 +01:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : Decimal("0.08") }
|
2018-01-24 16:28:15 +01:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
|
2018-03-19 20:57:11 +01:00
|
|
|
assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, {'change_type': ''})
|
2018-01-24 16:28:15 +01:00
|
|
|
rawtx = self.nodes[2].fundrawtransaction(rawtx, {'change_type': 'bech32'})
|
2018-01-29 20:25:40 +01:00
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
|
|
|
|
assert_equal('witness_v0_keyhash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type'])
|
2016-03-30 03:04:22 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_coin_selection(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
#########################################################################
|
|
|
|
# test a fundrawtransaction with a VIN smaller than the required amount #
|
|
|
|
#########################################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.02"))
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.02 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
|
|
|
|
# 4-byte version + 1-byte vin count + 36-byte prevout then script_len
|
|
|
|
rawtx = rawtx[:82] + "0100" + rawtx[84:]
|
|
|
|
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
matchingOuts = 0
|
|
|
|
for i, out in enumerate(dec_tx['vout']):
|
|
|
|
totalOut += out['value']
|
2016-03-19 21:36:32 +01:00
|
|
|
if out['scriptPubKey']['addresses'][0] in outputs:
|
2015-04-25 03:26:30 +02:00
|
|
|
matchingOuts+=1
|
|
|
|
else:
|
|
|
|
assert_equal(i, rawtxfund['changepos'])
|
|
|
|
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
|
|
|
|
|
|
|
|
assert_equal(matchingOuts, 1)
|
|
|
|
assert_equal(len(dec_tx['vout']), 2)
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_two_vin(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
###########################################
|
|
|
|
# test a fundrawtransaction with two VINs #
|
|
|
|
###########################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.02"))
|
|
|
|
utx2 = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.12 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
matchingOuts = 0
|
|
|
|
for out in dec_tx['vout']:
|
|
|
|
totalOut += out['value']
|
2016-03-19 21:36:32 +01:00
|
|
|
if out['scriptPubKey']['addresses'][0] in outputs:
|
2015-04-25 03:26:30 +02:00
|
|
|
matchingOuts+=1
|
|
|
|
|
|
|
|
assert_equal(matchingOuts, 1)
|
|
|
|
assert_equal(len(dec_tx['vout']), 2)
|
|
|
|
|
|
|
|
matchingIns = 0
|
|
|
|
for vinOut in dec_tx['vin']:
|
|
|
|
for vinIn in inputs:
|
|
|
|
if vinIn['txid'] == vinOut['txid']:
|
|
|
|
matchingIns+=1
|
|
|
|
|
|
|
|
assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_two_vin_two_vout(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
#########################################################
|
|
|
|
# test a fundrawtransaction with two VINs and two vOUTs #
|
|
|
|
#########################################################
|
2019-09-20 17:33:17 +02:00
|
|
|
utx = get_unspent(self.nodes[2].listunspent(), Decimal("0.02"))
|
|
|
|
utx2 = get_unspent(self.nodes[2].listunspent(), Decimal("0.1"))
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.12, self.nodes[0].getnewaddress() : 0.02 }
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
totalOut = 0
|
|
|
|
matchingOuts = 0
|
|
|
|
for out in dec_tx['vout']:
|
|
|
|
totalOut += out['value']
|
2016-03-19 21:36:32 +01:00
|
|
|
if out['scriptPubKey']['addresses'][0] in outputs:
|
2015-04-25 03:26:30 +02:00
|
|
|
matchingOuts+=1
|
|
|
|
|
|
|
|
assert_equal(matchingOuts, 2)
|
|
|
|
assert_equal(len(dec_tx['vout']), 3)
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_invalid_input(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
##############################################
|
|
|
|
# test a fundrawtransaction with invalid vin #
|
|
|
|
##############################################
|
|
|
|
inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = { self.nodes[0].getnewaddress() : 0.02}
|
2015-04-25 03:26:30 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_fee_p2pkh(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
#compare fee of a standard pubkeyhash transaction
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[1].getnewaddress():0.022}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[0].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
#create same transaction over sendtoaddress
|
2019-09-20 17:33:17 +02:00
|
|
|
txId = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.022)
|
2015-04-25 03:26:30 +02:00
|
|
|
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
|
|
|
|
|
|
|
|
#compare fee
|
2015-12-02 18:12:23 +01:00
|
|
|
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_fee_p2pkh_multi_out(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
#compare fee of a standard pubkeyhash transaction with multiple outputs
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[1].getnewaddress():0.022,self.nodes[1].getnewaddress():0.024,self.nodes[1].getnewaddress():0.002,self.nodes[1].getnewaddress():0.026,self.nodes[1].getnewaddress():0.004,self.nodes[1].getnewaddress():0.006}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[0].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
#create same transaction over sendtoaddress
|
2015-12-02 18:12:23 +01:00
|
|
|
txId = self.nodes[0].sendmany("", outputs)
|
2015-04-25 03:26:30 +02:00
|
|
|
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
|
|
|
|
|
|
|
|
#compare fee
|
2015-12-02 18:12:23 +01:00
|
|
|
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_fee_p2sh(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
#compare fee of a 2of2 multisig p2sh transaction
|
|
|
|
|
|
|
|
# create 2of2 addr
|
|
|
|
addr1 = self.nodes[1].getnewaddress()
|
|
|
|
addr2 = self.nodes[1].getnewaddress()
|
|
|
|
|
2018-02-09 17:12:27 +01:00
|
|
|
addr1Obj = self.nodes[1].getaddressinfo(addr1)
|
|
|
|
addr2Obj = self.nodes[1].getaddressinfo(addr2)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2017-09-29 06:21:28 +02:00
|
|
|
mSigObj = self.nodes[1].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address']
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = []
|
|
|
|
outputs = {mSigObj:1.1}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[0].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
#create same transaction over sendtoaddress
|
2015-12-02 18:12:23 +01:00
|
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 1.1)
|
2015-04-25 03:26:30 +02:00
|
|
|
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
|
|
|
|
|
|
|
|
#compare fee
|
2015-12-02 18:12:23 +01:00
|
|
|
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_fee_4of5(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
#compare fee of a standard pubkeyhash transaction
|
|
|
|
|
|
|
|
# create 4of5 addr
|
|
|
|
addr1 = self.nodes[1].getnewaddress()
|
|
|
|
addr2 = self.nodes[1].getnewaddress()
|
|
|
|
addr3 = self.nodes[1].getnewaddress()
|
|
|
|
addr4 = self.nodes[1].getnewaddress()
|
|
|
|
addr5 = self.nodes[1].getnewaddress()
|
|
|
|
|
2018-02-09 17:12:27 +01:00
|
|
|
addr1Obj = self.nodes[1].getaddressinfo(addr1)
|
|
|
|
addr2Obj = self.nodes[1].getaddressinfo(addr2)
|
|
|
|
addr3Obj = self.nodes[1].getaddressinfo(addr3)
|
|
|
|
addr4Obj = self.nodes[1].getaddressinfo(addr4)
|
|
|
|
addr5Obj = self.nodes[1].getaddressinfo(addr5)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2017-09-29 06:21:28 +02:00
|
|
|
mSigObj = self.nodes[1].addmultisigaddress(4, [addr1Obj['pubkey'], addr2Obj['pubkey'], addr3Obj['pubkey'], addr4Obj['pubkey'], addr5Obj['pubkey']])['address']
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {mSigObj:0.022}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[0].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
#create same transaction over sendtoaddress
|
2019-09-20 17:33:17 +02:00
|
|
|
txId = self.nodes[0].sendtoaddress(mSigObj, 0.022)
|
2015-04-25 03:26:30 +02:00
|
|
|
signedFee = self.nodes[0].getrawmempool(True)[txId]['fee']
|
|
|
|
|
|
|
|
#compare fee
|
2015-12-02 18:12:23 +01:00
|
|
|
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_spend_2of2(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
# spend a 2of2 multisig transaction over fundraw
|
|
|
|
|
|
|
|
# create 2of2 addr
|
|
|
|
addr1 = self.nodes[2].getnewaddress()
|
|
|
|
addr2 = self.nodes[2].getnewaddress()
|
|
|
|
|
2018-02-09 17:12:27 +01:00
|
|
|
addr1Obj = self.nodes[2].getaddressinfo(addr1)
|
|
|
|
addr2Obj = self.nodes[2].getaddressinfo(addr2)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2017-09-29 06:21:28 +02:00
|
|
|
mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])['address']
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
# send 1.2 BTC to msig addr
|
2019-09-20 17:33:17 +02:00
|
|
|
self.nodes[0].sendtoaddress(mSigObj, 0.024)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[1].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
oldBalance = self.nodes[1].getbalance()
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[1].getnewaddress():0.022}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[2].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2017-09-06 01:49:18 +02:00
|
|
|
signedTx = self.nodes[2].signrawtransactionwithwallet(fundedTx['hex'])
|
2019-06-26 19:49:11 +02:00
|
|
|
self.nodes[2].sendrawtransaction(signedTx['hex'])
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[1].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# make sure funds are received at node1
|
2019-09-20 17:33:17 +02:00
|
|
|
assert_equal(oldBalance+Decimal('0.022'), self.nodes[1].getbalance())
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_locked_wallet(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
############################################################
|
|
|
|
# locked wallet test
|
2018-02-20 22:09:51 +01:00
|
|
|
self.nodes[1].encryptwallet("test")
|
|
|
|
self.stop_nodes()
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2017-06-09 22:35:17 +02:00
|
|
|
self.start_nodes()
|
2016-01-05 23:47:04 +01:00
|
|
|
# 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
|
|
|
|
for node in self.nodes:
|
2019-06-26 18:42:11 +02:00
|
|
|
node.settxfee(self.min_relay_tx_fee)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-08-27 21:13:56 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[1], 2)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
connect_nodes(self.nodes[0], 3)
|
2018-01-25 11:03:05 +01:00
|
|
|
# Again lock the watchonly UTXO or nodes[0] may spend it, because
|
|
|
|
# lockunspent is memory-only and thus lost on restart
|
2019-06-26 18:42:11 +02:00
|
|
|
self.nodes[0].lockunspent(False, [{"txid": self.watchonly_txid, "vout": self.watchonly_vout}])
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
|
2016-12-07 15:20:40 +01:00
|
|
|
# drain the keypool
|
|
|
|
self.nodes[1].getnewaddress()
|
2017-01-10 16:45:30 +01:00
|
|
|
self.nodes[1].getrawchangeaddress()
|
2016-12-07 15:20:40 +01:00
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[0].getnewaddress():0.022}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
|
2016-12-07 15:20:40 +01:00
|
|
|
# fund a transaction that requires a new key for the change output
|
|
|
|
# creating the key must be impossible because the wallet is locked
|
2017-07-12 16:33:46 +02:00
|
|
|
assert_raises_rpc_error(-4, "Keypool ran out, please call keypoolrefill first", self.nodes[1].fundrawtransaction, rawtx)
|
2016-12-07 15:20:40 +01:00
|
|
|
|
|
|
|
#refill the keypool
|
|
|
|
self.nodes[1].walletpassphrase("test", 100)
|
2017-01-10 16:45:30 +01:00
|
|
|
self.nodes[1].keypoolrefill(8) #need to refill the keypool to get an internal change address
|
2016-12-07 15:20:40 +01:00
|
|
|
self.nodes[1].walletlock()
|
|
|
|
|
2019-09-20 17:33:17 +02:00
|
|
|
assert_raises_rpc_error(-13, "walletpassphrase", self.nodes[1].sendtoaddress, self.nodes[0].getnewaddress(), 0.024)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
oldBalance = self.nodes[0].getbalance()
|
|
|
|
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[0].getnewaddress():0.022}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[1].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
#now we need to unlock
|
2016-12-09 17:42:23 +01:00
|
|
|
self.nodes[1].walletpassphrase("test", 600)
|
2017-09-06 01:49:18 +02:00
|
|
|
signedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex'])
|
2019-06-26 19:49:11 +02:00
|
|
|
self.nodes[1].sendrawtransaction(signedTx['hex'])
|
2015-04-25 03:26:30 +02:00
|
|
|
self.nodes[1].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# make sure funds are received at node1
|
2019-09-20 17:33:17 +02:00
|
|
|
assert_equal(oldBalance+Decimal('1.022'), self.nodes[0].getbalance())
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_many_inputs_fee(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
###############################################
|
|
|
|
# multiple (~19) inputs tx test | Compare fee #
|
|
|
|
###############################################
|
|
|
|
|
|
|
|
#empty node1, send some small coins from node0 to node1
|
2015-12-02 18:12:23 +01:00
|
|
|
self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
for i in range(0,20):
|
2019-09-20 17:33:17 +02:00
|
|
|
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.0002)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
#fund a tx with ~20 small inputs
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[0].getnewaddress():0.003,self.nodes[0].getnewaddress():0.0008}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[1].fundrawtransaction(rawtx)
|
2015-04-25 03:26:30 +02:00
|
|
|
|
|
|
|
#create same transaction over sendtoaddress
|
2015-12-02 18:12:23 +01:00
|
|
|
txId = self.nodes[1].sendmany("", outputs)
|
2015-04-25 03:26:30 +02:00
|
|
|
signedFee = self.nodes[1].getrawmempool(True)[txId]['fee']
|
|
|
|
|
|
|
|
#compare fee
|
2015-12-02 18:12:23 +01:00
|
|
|
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 #~19 inputs
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_many_inputs_send(self):
|
2015-04-25 03:26:30 +02:00
|
|
|
#############################################
|
|
|
|
# multiple (~19) inputs tx test | sign/send #
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
#again, empty node1, send some small coins from node0 to node1
|
2015-12-02 18:12:23 +01:00
|
|
|
self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), self.nodes[1].getbalance(), "", "", True)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
for i in range(0,20):
|
2019-09-20 17:33:17 +02:00
|
|
|
self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.0002)
|
2015-04-25 03:26:30 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
#fund a tx with ~20 small inputs
|
|
|
|
oldBalance = self.nodes[0].getbalance()
|
|
|
|
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[0].getnewaddress():0.003,self.nodes[0].getnewaddress():0.0008}
|
2017-03-26 07:47:27 +02:00
|
|
|
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
|
|
|
|
fundedTx = self.nodes[1].fundrawtransaction(rawtx)
|
2017-09-06 01:49:18 +02:00
|
|
|
fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex'])
|
2019-06-26 19:49:11 +02:00
|
|
|
self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
|
2015-04-25 03:26:30 +02:00
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
2019-09-20 17:33:17 +02:00
|
|
|
assert_equal(oldBalance+Decimal('1.0038'), self.nodes[0].getbalance()) #0.19+block reward
|
2015-04-25 03:26:30 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_op_return(self):
|
2015-03-25 10:04:02 +01:00
|
|
|
#####################################################
|
|
|
|
# test fundrawtransaction with OP_RETURN and no vin #
|
|
|
|
#####################################################
|
|
|
|
|
|
|
|
rawtx = "0100000000010000000000000000066a047465737400000000"
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
|
|
|
|
|
|
|
|
assert_equal(len(dec_tx['vin']), 0)
|
|
|
|
assert_equal(len(dec_tx['vout']), 1)
|
|
|
|
|
|
|
|
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
|
|
|
|
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
|
|
|
|
|
|
|
|
assert_greater_than(len(dec_tx['vin']), 0) # at least one vin
|
|
|
|
assert_equal(len(dec_tx['vout']), 2) # one change output added
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_watchonly(self):
|
2015-04-24 06:42:49 +02:00
|
|
|
##################################################
|
|
|
|
# test a fundrawtransaction using only watchonly #
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
inputs = []
|
2019-06-26 18:42:11 +02:00
|
|
|
outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount / 2}
|
2015-04-24 06:42:49 +02:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
|
|
|
|
|
2016-03-30 03:04:22 +02:00
|
|
|
result = self.nodes[3].fundrawtransaction(rawtx, {'includeWatching': True })
|
2015-04-24 06:42:49 +02:00
|
|
|
res_dec = self.nodes[0].decoderawtransaction(result["hex"])
|
|
|
|
assert_equal(len(res_dec["vin"]), 1)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert_equal(res_dec["vin"][0]["txid"], self.watchonly_txid)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2019-02-19 23:43:44 +01:00
|
|
|
assert "fee" in result.keys()
|
2015-04-24 06:42:49 +02:00
|
|
|
assert_greater_than(result["changepos"], -1)
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_all_watched_funds(self):
|
2015-04-24 06:42:49 +02:00
|
|
|
###############################################################
|
|
|
|
# test fundrawtransaction using the entirety of watched funds #
|
|
|
|
###############################################################
|
|
|
|
|
|
|
|
inputs = []
|
2019-06-26 18:42:11 +02:00
|
|
|
outputs = {self.nodes[2].getnewaddress(): self.watchonly_amount}
|
2015-04-24 06:42:49 +02:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
|
|
|
|
|
2016-03-30 03:04:22 +02:00
|
|
|
# Backward compatibility test (2nd param is includeWatching)
|
2015-04-24 06:42:49 +02:00
|
|
|
result = self.nodes[3].fundrawtransaction(rawtx, True)
|
|
|
|
res_dec = self.nodes[0].decoderawtransaction(result["hex"])
|
|
|
|
assert_equal(len(res_dec["vin"]), 2)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert res_dec["vin"][0]["txid"] == self.watchonly_txid or res_dec["vin"][1]["txid"] == self.watchonly_txid
|
2015-04-24 06:42:49 +02:00
|
|
|
|
|
|
|
assert_greater_than(result["fee"], 0)
|
|
|
|
assert_greater_than(result["changepos"], -1)
|
2019-06-26 18:42:11 +02:00
|
|
|
assert_equal(result["fee"] + res_dec["vout"][result["changepos"]]["value"], self.watchonly_amount / 10)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2017-09-06 01:49:18 +02:00
|
|
|
signedtx = self.nodes[3].signrawtransactionwithwallet(result["hex"])
|
2019-02-19 23:43:44 +01:00
|
|
|
assert not signedtx["complete"]
|
2017-09-06 01:49:18 +02:00
|
|
|
signedtx = self.nodes[0].signrawtransactionwithwallet(signedtx["hex"])
|
2019-02-19 23:43:44 +01:00
|
|
|
assert signedtx["complete"]
|
2015-04-24 06:42:49 +02:00
|
|
|
self.nodes[0].sendrawtransaction(signedtx["hex"])
|
2016-06-13 19:52:01 +02:00
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_option_feerate(self):
|
2016-06-13 19:52:01 +02:00
|
|
|
#######################
|
|
|
|
# Test feeRate option #
|
|
|
|
#######################
|
|
|
|
|
|
|
|
# Make sure there is exactly one input so coin selection can't skew the result
|
|
|
|
assert_equal(len(self.nodes[3].listunspent(1)), 1)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2016-04-28 22:04:07 +02:00
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[3].getnewaddress() : 0.02}
|
2016-04-28 22:04:07 +02:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
|
2019-06-26 18:42:11 +02:00
|
|
|
result = self.nodes[3].fundrawtransaction(rawtx) # uses self.min_relay_tx_fee (set by settxfee)
|
|
|
|
result2 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * self.min_relay_tx_fee})
|
|
|
|
result3 = self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 10 * self.min_relay_tx_fee})
|
2019-06-29 04:44:38 +02:00
|
|
|
assert_raises_rpc_error(-4, "Fee exceeds maximum configured by -maxtxfee", self.nodes[3].fundrawtransaction, rawtx, {"feeRate": 1})
|
2016-06-13 19:52:01 +02:00
|
|
|
result_fee_rate = result['fee'] * 1000 / count_bytes(result['hex'])
|
|
|
|
assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
|
|
|
|
assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
|
2015-04-24 06:42:49 +02:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_address_reuse(self):
|
2017-07-10 20:29:06 +02:00
|
|
|
################################
|
|
|
|
# Test no address reuse occurs #
|
|
|
|
################################
|
2016-12-19 09:10:33 +01:00
|
|
|
|
2019-06-26 18:42:11 +02:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs=[], outputs={self.nodes[3].getnewaddress(): 1})
|
2016-12-19 09:10:33 +01:00
|
|
|
result3 = self.nodes[3].fundrawtransaction(rawtx)
|
|
|
|
res_dec = self.nodes[0].decoderawtransaction(result3["hex"])
|
|
|
|
changeaddress = ""
|
|
|
|
for out in res_dec['vout']:
|
2019-09-20 17:33:17 +02:00
|
|
|
if out['value'] > Decimal("0.02"):
|
2016-12-19 09:10:33 +01:00
|
|
|
changeaddress += out['scriptPubKey']['addresses'][0]
|
2019-02-19 23:43:44 +01:00
|
|
|
assert changeaddress != ""
|
2016-12-19 09:10:33 +01:00
|
|
|
nextaddr = self.nodes[3].getnewaddress()
|
|
|
|
# Now the change address key should be removed from the keypool
|
2019-02-19 23:43:44 +01:00
|
|
|
assert changeaddress != nextaddr
|
2016-12-19 09:10:33 +01:00
|
|
|
|
2019-06-26 19:49:11 +02:00
|
|
|
def test_option_subtract_fee_from_outputs(self):
|
2016-12-13 22:36:23 +01:00
|
|
|
######################################
|
|
|
|
# Test subtractFeeFromOutputs option #
|
|
|
|
######################################
|
|
|
|
|
|
|
|
# Make sure there is exactly one input so coin selection can't skew the result
|
|
|
|
assert_equal(len(self.nodes[3].listunspent(1)), 1)
|
|
|
|
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[2].getnewaddress(): 0.02}
|
2016-12-13 22:36:23 +01:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
|
|
|
|
|
2019-06-26 18:42:11 +02:00
|
|
|
result = [self.nodes[3].fundrawtransaction(rawtx), # uses self.min_relay_tx_fee (set by settxfee)
|
|
|
|
self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": []}), # empty subtraction list
|
|
|
|
self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0]}), # uses self.min_relay_tx_fee (set by settxfee)
|
|
|
|
self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * self.min_relay_tx_fee}),
|
|
|
|
self.nodes[3].fundrawtransaction(rawtx, {"feeRate": 2 * self.min_relay_tx_fee, "subtractFeeFromOutputs": [0]}),]
|
2016-12-13 22:36:23 +01:00
|
|
|
|
2018-01-29 20:25:40 +01:00
|
|
|
dec_tx = [self.nodes[3].decoderawtransaction(tx_['hex']) for tx_ in result]
|
2016-12-13 22:36:23 +01:00
|
|
|
output = [d['vout'][1 - r['changepos']]['value'] for d, r in zip(dec_tx, result)]
|
|
|
|
change = [d['vout'][r['changepos']]['value'] for d, r in zip(dec_tx, result)]
|
|
|
|
|
|
|
|
assert_equal(result[0]['fee'], result[1]['fee'], result[2]['fee'])
|
|
|
|
assert_equal(result[3]['fee'], result[4]['fee'])
|
|
|
|
assert_equal(change[0], change[1])
|
|
|
|
assert_equal(output[0], output[1])
|
|
|
|
assert_equal(output[0], output[2] + result[2]['fee'])
|
|
|
|
assert_equal(change[0] + result[0]['fee'], change[2])
|
|
|
|
assert_equal(output[3], output[4] + result[4]['fee'])
|
|
|
|
assert_equal(change[3] + result[3]['fee'], change[4])
|
|
|
|
|
|
|
|
inputs = []
|
2019-09-20 17:33:17 +02:00
|
|
|
outputs = {self.nodes[2].getnewaddress(): value for value in (0.02, 0.022, 0.024, 0.026)}
|
2016-12-13 22:36:23 +01:00
|
|
|
rawtx = self.nodes[3].createrawtransaction(inputs, outputs)
|
|
|
|
|
|
|
|
result = [self.nodes[3].fundrawtransaction(rawtx),
|
|
|
|
# split the fee between outputs 0, 2, and 3, but not output 1
|
|
|
|
self.nodes[3].fundrawtransaction(rawtx, {"subtractFeeFromOutputs": [0, 2, 3]})]
|
|
|
|
|
|
|
|
dec_tx = [self.nodes[3].decoderawtransaction(result[0]['hex']),
|
|
|
|
self.nodes[3].decoderawtransaction(result[1]['hex'])]
|
|
|
|
|
|
|
|
# Nested list of non-change output amounts for each transaction
|
|
|
|
output = [[out['value'] for i, out in enumerate(d['vout']) if i != r['changepos']]
|
|
|
|
for d, r in zip(dec_tx, result)]
|
|
|
|
|
|
|
|
# List of differences in output amounts between normal and subtractFee transactions
|
|
|
|
share = [o0 - o1 for o0, o1 in zip(output[0], output[1])]
|
|
|
|
|
|
|
|
# output 1 is the same in both transactions
|
|
|
|
assert_equal(share[1], 0)
|
|
|
|
|
|
|
|
# the other 3 outputs are smaller as a result of subtractFeeFromOutputs
|
|
|
|
assert_greater_than(share[0], 0)
|
|
|
|
assert_greater_than(share[2], 0)
|
|
|
|
assert_greater_than(share[3], 0)
|
|
|
|
|
|
|
|
# outputs 2 and 3 take the same share of the fee
|
|
|
|
assert_equal(share[2], share[3])
|
|
|
|
|
|
|
|
# output 0 takes at least as much share of the fee, and no more than 2 satoshis more, than outputs 2 and 3
|
|
|
|
assert_greater_than_or_equal(share[0], share[2])
|
|
|
|
assert_greater_than_or_equal(share[2] + Decimal(2e-8), share[0])
|
|
|
|
|
|
|
|
# the fee is the same in both transactions
|
|
|
|
assert_equal(result[0]['fee'], result[1]['fee'])
|
|
|
|
|
|
|
|
# the total subtracted from the outputs is equal to the fee
|
|
|
|
assert_equal(share[0] + share[2] + share[3], result[0]['fee'])
|
|
|
|
|
2015-04-25 03:26:30 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
RawTransactionsTest().main()
|