Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_basic.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2019 The Bitcoin Core developers | # Copyright (c) 2014-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test the wallet.""" | """Test the wallet.""" | ||||
from decimal import Decimal | from decimal import Decimal | ||||
import time | import time | ||||
from test_framework.messages import FromHex, CTransaction | from test_framework.messages import FromHex, CTransaction | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_array_result, | assert_array_result, | ||||
assert_equal, | assert_equal, | ||||
assert_fee_amount, | assert_fee_amount, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes_bi, | connect_nodes, | ||||
count_bytes, | count_bytes, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
class WalletTest(BitcoinTestFramework): | class WalletTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 4 | self.num_nodes = 4 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.extra_args = [ | self.extra_args = [ | ||||
["-acceptnonstdtxn=1"], | ["-acceptnonstdtxn=1"], | ||||
] * self.num_nodes | ] * self.num_nodes | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
self.skip_if_no_wallet() | self.skip_if_no_wallet() | ||||
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(self.nodes[0], self.nodes[1]) | ||||
connect_nodes_bi(self.nodes[1], self.nodes[2]) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes(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 | ||||
▲ Show 20 Lines • Show All 187 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
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]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[3]) | connect_nodes(self.nodes[0], self.nodes[3]) | ||||
self.sync_all() | self.sync_all() | ||||
# check if we can list zero value tx as available coins | # check if we can list zero value tx as available coins | ||||
# 1. create rawtx | # 1. create rawtx | ||||
# 2. hex-changed one output to 0.0 | # 2. hex-changed one output to 0.0 | ||||
# 3. sign and send | # 3. sign and send | ||||
# 4. check if recipient (node0) can list the zero value tx | # 4. check if recipient (node0) can list the zero value tx | ||||
usp = self.nodes[1].listunspent( | usp = self.nodes[1].listunspent( | ||||
Show All 23 Lines | def run_test(self): | ||||
assert_equal(uTx['amount'], Decimal('0')) | assert_equal(uTx['amount'], Decimal('0')) | ||||
assert found | assert found | ||||
# 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(self.nodes[0], self.nodes[1]) | ||||
connect_nodes_bi(self.nodes[1], self.nodes[2]) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes(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 | ||||
Show All 12 Lines | def run_test(self): | ||||
txIdNotBroadcasted = self.nodes[0].sendtoaddress( | txIdNotBroadcasted = self.nodes[0].sendtoaddress( | ||||
self.nodes[2].getnewaddress(), 2) | self.nodes[2].getnewaddress(), 2) | ||||
# restart the nodes with -walletbroadcast=1 | # restart the nodes with -walletbroadcast=1 | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.start_node(0, self.extra_args[0]) | self.start_node(0, self.extra_args[0]) | ||||
self.start_node(1, self.extra_args[1]) | self.start_node(1, self.extra_args[1]) | ||||
self.start_node(2, self.extra_args[2]) | self.start_node(2, self.extra_args[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[1]) | connect_nodes(self.nodes[0], self.nodes[1]) | ||||
connect_nodes_bi(self.nodes[1], self.nodes[2]) | connect_nodes(self.nodes[1], self.nodes[2]) | ||||
connect_nodes_bi(self.nodes[0], self.nodes[2]) | connect_nodes(self.nodes[0], self.nodes[2]) | ||||
self.sync_blocks(self.nodes[0:3]) | self.sync_blocks(self.nodes[0:3]) | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
self.sync_blocks(self.nodes[0:3]) | self.sync_blocks(self.nodes[0:3]) | ||||
node_2_bal += 2 | node_2_bal += 2 | ||||
# tx should be added to balance because after restarting the nodes tx | # tx should be added to balance because after restarting the nodes tx | ||||
# should be broadcasted | # should be broadcasted | ||||
▲ Show 20 Lines • Show All 220 Lines • Show Last 20 Lines |