Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_basic.py
Show All 32 Lines | class WalletTest(BitcoinTestFramework): | ||||
def setup_network(self): | def setup_network(self): | ||||
self.setup_nodes() | self.setup_nodes() | ||||
# Only need nodes 0-2 running at start of test | # Only need nodes 0-2 running at start of test | ||||
self.stop_node(3) | self.stop_node(3) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[1]) | connect_nodes_bi(self.nodes[0], self.nodes[1]) | ||||
connect_nodes_bi(self.nodes[1], self.nodes[2]) | connect_nodes_bi(self.nodes[1], self.nodes[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes_bi(self.nodes[0], self.nodes[2]) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
def check_fee_amount(self, curr_balance, | def check_fee_amount(self, curr_balance, | ||||
balance_with_fee, fee_per_byte, tx_size): | balance_with_fee, fee_per_byte, tx_size): | ||||
"""Return curr_balance after asserting the fee was in range""" | """Return curr_balance after asserting the fee was in range""" | ||||
fee = balance_with_fee - curr_balance | fee = balance_with_fee - curr_balance | ||||
assert_fee_amount(fee, tx_size, fee_per_byte * 1000) | assert_fee_amount(fee, tx_size, fee_per_byte * 1000) | ||||
return curr_balance | return curr_balance | ||||
def run_test(self): | def run_test(self): | ||||
# Check that there's no UTXO on none of the nodes | # Check that there's no UTXO on none of the nodes | ||||
assert_equal(len(self.nodes[0].listunspent()), 0) | assert_equal(len(self.nodes[0].listunspent()), 0) | ||||
assert_equal(len(self.nodes[1].listunspent()), 0) | assert_equal(len(self.nodes[1].listunspent()), 0) | ||||
assert_equal(len(self.nodes[2].listunspent()), 0) | assert_equal(len(self.nodes[2].listunspent()), 0) | ||||
self.log.info("Mining blocks...") | self.log.info("Mining blocks...") | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
walletinfo = self.nodes[0].getwalletinfo() | walletinfo = self.nodes[0].getwalletinfo() | ||||
assert_equal(walletinfo['immature_balance'], 50) | assert_equal(walletinfo['immature_balance'], 50) | ||||
assert_equal(walletinfo['balance'], 0) | assert_equal(walletinfo['balance'], 0) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
self.nodes[1].generate(101) | self.nodes[1].generate(101) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
assert_equal(self.nodes[0].getbalance(), 50) | assert_equal(self.nodes[0].getbalance(), 50) | ||||
assert_equal(self.nodes[1].getbalance(), 50) | assert_equal(self.nodes[1].getbalance(), 50) | ||||
assert_equal(self.nodes[2].getbalance(), 0) | assert_equal(self.nodes[2].getbalance(), 0) | ||||
# Check that only first and second nodes have UTXOs | # Check that only first and second nodes have UTXOs | ||||
utxos = self.nodes[0].listunspent() | utxos = self.nodes[0].listunspent() | ||||
assert_equal(len(utxos), 1) | assert_equal(len(utxos), 1) | ||||
Show All 34 Lines | def run_test(self): | ||||
balance = self.nodes[0].getbalance() | balance = self.nodes[0].getbalance() | ||||
assert_equal(set([txout1['value'], txout2['value']]), | assert_equal(set([txout1['value'], txout2['value']]), | ||||
set([10, balance])) | set([10, balance])) | ||||
walletinfo = self.nodes[0].getwalletinfo() | walletinfo = self.nodes[0].getwalletinfo() | ||||
assert_equal(walletinfo['immature_balance'], 0) | assert_equal(walletinfo['immature_balance'], 0) | ||||
# Have node0 mine a block, thus it will collect its own fee. | # Have node0 mine a block, thus it will collect its own fee. | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
# Exercise locking of unspent outputs | # Exercise locking of unspent outputs | ||||
unspent_0 = self.nodes[2].listunspent()[0] | unspent_0 = self.nodes[2].listunspent()[0] | ||||
unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]} | unspent_0 = {"txid": unspent_0["txid"], "vout": unspent_0["vout"]} | ||||
assert_raises_rpc_error(-8, "Invalid parameter, expected locked output", | assert_raises_rpc_error(-8, "Invalid parameter, expected locked output", | ||||
self.nodes[2].lockunspent, True, [unspent_0]) | self.nodes[2].lockunspent, True, [unspent_0]) | ||||
self.nodes[2].lockunspent(False, [unspent_0]) | self.nodes[2].lockunspent(False, [unspent_0]) | ||||
assert_raises_rpc_error(-8, "Invalid parameter, output already locked", | assert_raises_rpc_error(-8, "Invalid parameter, output already locked", | ||||
Show All 12 Lines | def run_test(self): | ||||
assert_raises_rpc_error(-8, "Invalid parameter, unknown transaction", | assert_raises_rpc_error(-8, "Invalid parameter, unknown transaction", | ||||
self.nodes[2].lockunspent, False, | self.nodes[2].lockunspent, False, | ||||
[{"txid": "0000000000000000000000000000000000000000000000000000000000000000", "vout": 0}]) | [{"txid": "0000000000000000000000000000000000000000000000000000000000000000", "vout": 0}]) | ||||
assert_raises_rpc_error(-8, "Invalid parameter, vout index out of bounds", | assert_raises_rpc_error(-8, "Invalid parameter, vout index out of bounds", | ||||
self.nodes[2].lockunspent, False, [{"txid": unspent_0["txid"], "vout": 999}]) | self.nodes[2].lockunspent, False, [{"txid": unspent_0["txid"], "vout": 999}]) | ||||
# Have node1 generate 100 blocks (so node0 can recover the fee) | # Have node1 generate 100 blocks (so node0 can recover the fee) | ||||
self.nodes[1].generate(100) | self.nodes[1].generate(100) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
# node0 should end up with 100 btc in block rewards plus fees, but | # node0 should end up with 100 btc in block rewards plus fees, but | ||||
# minus the 21 plus fees sent to node2 | # minus the 21 plus fees sent to node2 | ||||
assert_equal(self.nodes[0].getbalance(), 100 - 21) | assert_equal(self.nodes[0].getbalance(), 100 - 21) | ||||
assert_equal(self.nodes[2].getbalance(), 21) | assert_equal(self.nodes[2].getbalance(), 21) | ||||
# Node0 should have two unspent outputs. | # Node0 should have two unspent outputs. | ||||
# Create a couple of transactions to send them to node2, submit them through | # Create a couple of transactions to send them to node2, submit them through | ||||
Show All 13 Lines | def run_test(self): | ||||
self.nodes[0].signrawtransactionwithwallet(raw_tx)) | self.nodes[0].signrawtransactionwithwallet(raw_tx)) | ||||
# Have node 1 (miner) send the transactions | # Have node 1 (miner) send the transactions | ||||
self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True) | self.nodes[1].sendrawtransaction(txns_to_send[0]["hex"], True) | ||||
self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True) | self.nodes[1].sendrawtransaction(txns_to_send[1]["hex"], True) | ||||
# Have node1 mine a block to confirm transactions: | # Have node1 mine a block to confirm transactions: | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
assert_equal(self.nodes[0].getbalance(), 0) | assert_equal(self.nodes[0].getbalance(), 0) | ||||
assert_equal(self.nodes[2].getbalance(), 94) | assert_equal(self.nodes[2].getbalance(), 94) | ||||
# Verify that a spent output cannot be locked anymore | # Verify that a spent output cannot be locked anymore | ||||
spent_0 = {"txid": node0utxos[0]["txid"], | spent_0 = {"txid": node0utxos[0]["txid"], | ||||
"vout": node0utxos[0]["vout"]} | "vout": node0utxos[0]["vout"]} | ||||
assert_raises_rpc_error(-8, "Invalid parameter, expected unspent output", | assert_raises_rpc_error(-8, "Invalid parameter, expected unspent output", | ||||
self.nodes[0].lockunspent, False, [spent_0]) | self.nodes[0].lockunspent, False, [spent_0]) | ||||
# Send 10 BCH normal | # Send 10 BCH normal | ||||
old_balance = self.nodes[2].getbalance() | old_balance = self.nodes[2].getbalance() | ||||
address = self.nodes[0].getnewaddress("test") | address = self.nodes[0].getnewaddress("test") | ||||
fee_per_byte = Decimal('0.001') / 1000 | fee_per_byte = Decimal('0.001') / 1000 | ||||
self.nodes[2].settxfee(fee_per_byte * 1000) | self.nodes[2].settxfee(fee_per_byte * 1000) | ||||
txid = self.nodes[2].sendtoaddress(address, 10, "", "", False) | txid = self.nodes[2].sendtoaddress(address, 10, "", "", False) | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
ctx = FromHex(CTransaction(), | ctx = FromHex(CTransaction(), | ||||
self.nodes[2].gettransaction(txid)['hex']) | self.nodes[2].gettransaction(txid)['hex']) | ||||
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), old_balance - Decimal('10'), | node_2_bal = self.check_fee_amount(self.nodes[2].getbalance(), old_balance - Decimal('10'), | ||||
fee_per_byte, ctx.billable_size()) | fee_per_byte, ctx.billable_size()) | ||||
assert_equal(self.nodes[0].getbalance(), Decimal('10')) | assert_equal(self.nodes[0].getbalance(), Decimal('10')) | ||||
# Send 10 BCH with subtract fee from amount | # Send 10 BCH with subtract fee from amount | ||||
txid = self.nodes[2].sendtoaddress(address, 10, "", "", True) | txid = self.nodes[2].sendtoaddress(address, 10, "", "", True) | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
node_2_bal -= Decimal('10') | node_2_bal -= Decimal('10') | ||||
assert_equal(self.nodes[2].getbalance(), node_2_bal) | assert_equal(self.nodes[2].getbalance(), node_2_bal) | ||||
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal( | node_0_bal = self.check_fee_amount(self.nodes[0].getbalance(), Decimal( | ||||
'20'), fee_per_byte, count_bytes(self.nodes[2].gettransaction(txid)['hex'])) | '20'), fee_per_byte, count_bytes(self.nodes[2].gettransaction(txid)['hex'])) | ||||
# Sendmany 10 BCH | # Sendmany 10 BCH | ||||
txid = self.nodes[2].sendmany('', {address: 10}, 0, "", []) | txid = self.nodes[2].sendmany('', {address: 10}, 0, "", []) | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
node_0_bal += Decimal('10') | node_0_bal += Decimal('10') | ||||
ctx = FromHex(CTransaction(), | ctx = FromHex(CTransaction(), | ||||
self.nodes[2].gettransaction(txid)['hex']) | self.nodes[2].gettransaction(txid)['hex']) | ||||
node_2_bal = self.check_fee_amount(self.nodes[2].getbalance( | node_2_bal = self.check_fee_amount(self.nodes[2].getbalance( | ||||
), node_2_bal - Decimal('10'), fee_per_byte, ctx.billable_size()) | ), node_2_bal - Decimal('10'), fee_per_byte, ctx.billable_size()) | ||||
assert_equal(self.nodes[0].getbalance(), node_0_bal) | assert_equal(self.nodes[0].getbalance(), node_0_bal) | ||||
# Sendmany 10 BCH with subtract fee from amount | # Sendmany 10 BCH with subtract fee from amount | ||||
txid = self.nodes[2].sendmany('', {address: 10}, 0, "", [address]) | txid = self.nodes[2].sendmany('', {address: 10}, 0, "", [address]) | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
node_2_bal -= Decimal('10') | node_2_bal -= Decimal('10') | ||||
assert_equal(self.nodes[2].getbalance(), node_2_bal) | assert_equal(self.nodes[2].getbalance(), node_2_bal) | ||||
ctx = FromHex(CTransaction(), | ctx = FromHex(CTransaction(), | ||||
self.nodes[2].gettransaction(txid)['hex']) | self.nodes[2].gettransaction(txid)['hex']) | ||||
node_0_bal = self.check_fee_amount(self.nodes[0].getbalance( | node_0_bal = self.check_fee_amount(self.nodes[0].getbalance( | ||||
), node_0_bal + Decimal('10'), fee_per_byte, ctx.billable_size()) | ), node_0_bal + Decimal('10'), fee_per_byte, ctx.billable_size()) | ||||
self.start_node(3, self.extra_args[3]) | self.start_node(3, self.extra_args[3]) | ||||
Show All 34 Lines | def run_test(self): | ||||
# do some -walletbroadcast tests | # do some -walletbroadcast tests | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.start_node(0, self.extra_args[0] + ["-walletbroadcast=0"]) | self.start_node(0, self.extra_args[0] + ["-walletbroadcast=0"]) | ||||
self.start_node(1, self.extra_args[1] + ["-walletbroadcast=0"]) | self.start_node(1, self.extra_args[1] + ["-walletbroadcast=0"]) | ||||
self.start_node(2, self.extra_args[2] + ["-walletbroadcast=0"]) | self.start_node(2, self.extra_args[2] + ["-walletbroadcast=0"]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[1]) | connect_nodes_bi(self.nodes[0], self.nodes[1]) | ||||
connect_nodes_bi(self.nodes[1], self.nodes[2]) | connect_nodes_bi(self.nodes[1], self.nodes[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes_bi(self.nodes[0], self.nodes[2]) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
txIdNotBroadcasted = self.nodes[0].sendtoaddress( | txIdNotBroadcasted = self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 2) | self.nodes[2].getnewaddress(), 2) | ||||
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) | txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) | ||||
self.nodes[1].generate(1) # mine a block, tx should not be in there | self.nodes[1].generate(1) # mine a block, tx should not be in there | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
# should not be changed because tx was not broadcasted | # should not be changed because tx was not broadcasted | ||||
assert_equal(self.nodes[2].getbalance(), node_2_bal) | assert_equal(self.nodes[2].getbalance(), node_2_bal) | ||||
# now broadcast from another node, mine a block, sync, and check the | # now broadcast from another node, mine a block, sync, and check the | ||||
# balance | # balance | ||||
self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex']) | self.nodes[1].sendrawtransaction(txObjNotBroadcasted['hex']) | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
node_2_bal += 2 | node_2_bal += 2 | ||||
txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) | txObjNotBroadcasted = self.nodes[0].gettransaction(txIdNotBroadcasted) | ||||
assert_equal(self.nodes[2].getbalance(), node_2_bal) | assert_equal(self.nodes[2].getbalance(), node_2_bal) | ||||
# create another tx | # create another tx | ||||
txIdNotBroadcasted = self.nodes[0].sendtoaddress( | txIdNotBroadcasted = self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 2) | self.nodes[2].getnewaddress(), 2) | ||||
Show All 39 Lines | def run_test(self): | ||||
assert_raises_rpc_error(-1, "not an integer", | assert_raises_rpc_error(-1, "not an integer", | ||||
self.nodes[0].generate, "2") | self.nodes[0].generate, "2") | ||||
# Import address and private key to check correct behavior of spendable unspents | # Import address and private key to check correct behavior of spendable unspents | ||||
# 1. Send some coins to generate new UTXO | # 1. Send some coins to generate new UTXO | ||||
address_to_import = self.nodes[2].getnewaddress() | address_to_import = self.nodes[2].getnewaddress() | ||||
txid = self.nodes[0].sendtoaddress(address_to_import, 1) | txid = self.nodes[0].sendtoaddress(address_to_import, 1) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
# 2. Import address from node2 to node1 | # 2. Import address from node2 to node1 | ||||
self.nodes[1].importaddress(address_to_import) | self.nodes[1].importaddress(address_to_import) | ||||
# 3. Validate that the imported address is watch-only on node1 | # 3. Validate that the imported address is watch-only on node1 | ||||
assert self.nodes[1].getaddressinfo(address_to_import)["iswatchonly"] | assert self.nodes[1].getaddressinfo(address_to_import)["iswatchonly"] | ||||
# 4. Check that the unspents after import are not spendable | # 4. Check that the unspents after import are not spendable | ||||
Show All 9 Lines | def run_test(self): | ||||
assert_array_result(self.nodes[1].listunspent(), | assert_array_result(self.nodes[1].listunspent(), | ||||
{"address": address_to_import}, | {"address": address_to_import}, | ||||
{"spendable": True}) | {"spendable": True}) | ||||
# Mine a block from node0 to an address from node1 | # Mine a block from node0 to an address from node1 | ||||
cbAddr = self.nodes[1].getnewaddress() | cbAddr = self.nodes[1].getnewaddress() | ||||
blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0] | blkHash = self.nodes[0].generatetoaddress(1, cbAddr)[0] | ||||
cbTxId = self.nodes[0].getblock(blkHash)['tx'][0] | cbTxId = self.nodes[0].getblock(blkHash)['tx'][0] | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
# Check that the txid and balance is found by node1 | # Check that the txid and balance is found by node1 | ||||
self.nodes[1].gettransaction(cbTxId) | self.nodes[1].gettransaction(cbTxId) | ||||
# check if wallet or blockchain maintenance changes the balance | # check if wallet or blockchain maintenance changes the balance | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
blocks = self.nodes[0].generate(2) | blocks = self.nodes[0].generate(2) | ||||
self.sync_all([self.nodes[0:3]]) | self.sync_all(self.nodes[0:3]) | ||||
balance_nodes = [self.nodes[i].getbalance() for i in range(3)] | balance_nodes = [self.nodes[i].getbalance() for i in range(3)] | ||||
block_count = self.nodes[0].getblockcount() | block_count = self.nodes[0].getblockcount() | ||||
# Check modes: | # Check modes: | ||||
# - True: unicode escaped as \u.... | # - True: unicode escaped as \u.... | ||||
# - False: unicode directly as UTF-8 | # - False: unicode directly as UTF-8 | ||||
for mode in [True, False]: | for mode in [True, False]: | ||||
self.nodes[0].rpc.ensure_ascii = mode | self.nodes[0].rpc.ensure_ascii = mode | ||||
▲ Show 20 Lines • Show All 146 Lines • Show Last 20 Lines |