Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_groups.py
Show All 10 Lines | |||||
class WalletGroupTest(BitcoinTestFramework): | class WalletGroupTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 5 | self.num_nodes = 5 | ||||
self.extra_args = [ | self.extra_args = [ | ||||
[], | [], | ||||
[], | [], | ||||
['-avoidpartialspends'], | ["-avoidpartialspends"], | ||||
# 2.93 XEC is the threshold that causes a node to prefer a | # 2.93 XEC is the threshold that causes a node to prefer a | ||||
# non-grouped tx (3 inputs, 2 outputs) to a grouped tx (5 inputs, | # non-grouped tx (3 inputs, 2 outputs) to a grouped tx (5 inputs, | ||||
# 2 outputs). The fee for the grouped tx is 294 sats higher than | # 2 outputs). The fee for the grouped tx is 294 sats higher than | ||||
# the fee for the non-grouped tx. See tx5 below. | # the fee for the non-grouped tx. See tx5 below. | ||||
["-maxapsfee=2.93"], | ["-maxapsfee=2.93"], | ||||
["-maxapsfee=2.94"]] | ["-maxapsfee=2.94"], | ||||
] | |||||
# whitelist peers to speed up tx relay / mempool sync | # whitelist peers to speed up tx relay / mempool sync | ||||
for args in self.extra_args: | for args in self.extra_args: | ||||
args.append("-whitelist=noban@127.0.0.1") | args.append("-whitelist=noban@127.0.0.1") | ||||
self.rpc_timeout = 480 | self.rpc_timeout = 480 | ||||
self.supports_cli = False | self.supports_cli = False | ||||
def skip_test_if_missing_module(self): | def skip_test_if_missing_module(self): | ||||
Show All 13 Lines | def run_test(self): | ||||
[self.nodes[0].sendtoaddress(addr, 500000) for addr in addrs] | [self.nodes[0].sendtoaddress(addr, 500000) for addr in addrs] | ||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
# For each node, send 0.2 coins back to 0; | # For each node, send 0.2 coins back to 0; | ||||
# - node[1] should pick one 0.5 UTXO and leave the rest | # - node[1] should pick one 0.5 UTXO and leave the rest | ||||
# - node[2] should pick one (1.0 + 0.5) UTXO group corresponding to a | # - node[2] should pick one (1.0 + 0.5) UTXO group corresponding to a | ||||
# given address, and leave the rest | # given address, and leave the rest | ||||
txid1 = self.nodes[1].sendtoaddress( | txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 200000) | ||||
self.nodes[0].getnewaddress(), 200000) | |||||
tx1 = self.nodes[1].getrawtransaction(txid1, True) | tx1 = self.nodes[1].getrawtransaction(txid1, True) | ||||
# txid1 should have 1 input and 2 outputs | # txid1 should have 1 input and 2 outputs | ||||
assert_equal(1, len(tx1["vin"])) | assert_equal(1, len(tx1["vin"])) | ||||
assert_equal(2, len(tx1["vout"])) | assert_equal(2, len(tx1["vout"])) | ||||
# one output should be 0.2, the other should be ~0.3 | # one output should be 0.2, the other should be ~0.3 | ||||
v = sorted([vout["value"] for vout in tx1["vout"]]) | v = sorted([vout["value"] for vout in tx1["vout"]]) | ||||
assert_approx(v[0], vexp=200_000, vspan=100) | assert_approx(v[0], vexp=200_000, vspan=100) | ||||
assert_approx(v[1], vexp=300_000, vspan=100) | assert_approx(v[1], vexp=300_000, vspan=100) | ||||
txid2 = self.nodes[2].sendtoaddress( | txid2 = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 200000) | ||||
self.nodes[0].getnewaddress(), 200000) | |||||
tx2 = self.nodes[2].getrawtransaction(txid2, True) | tx2 = self.nodes[2].getrawtransaction(txid2, True) | ||||
# txid2 should have 2 inputs and 2 outputs | # txid2 should have 2 inputs and 2 outputs | ||||
assert_equal(2, len(tx2["vin"])) | assert_equal(2, len(tx2["vin"])) | ||||
assert_equal(2, len(tx2["vout"])) | assert_equal(2, len(tx2["vout"])) | ||||
# one output should be 0.2, the other should be ~1.3 | # one output should be 0.2, the other should be ~1.3 | ||||
v = sorted([vout["value"] for vout in tx2["vout"]]) | v = sorted([vout["value"] for vout in tx2["vout"]]) | ||||
assert_approx(v[0], vexp=200_000, vspan=100) | assert_approx(v[0], vexp=200_000, vspan=100) | ||||
assert_approx(v[1], vexp=1_300_000, vspan=100) | assert_approx(v[1], vexp=1_300_000, vspan=100) | ||||
Show All 10 Lines | def run_test(self): | ||||
# - C1 0.5 - F ~1.3 | # - C1 0.5 - F ~1.3 | ||||
# - D ~0.3 | # - D ~0.3 | ||||
assert_approx(self.nodes[1].getbalance(), vexp=4_300_000, vspan=100) | assert_approx(self.nodes[1].getbalance(), vexp=4_300_000, vspan=100) | ||||
assert_approx(self.nodes[2].getbalance(), vexp=4_300_000, vspan=100) | assert_approx(self.nodes[2].getbalance(), vexp=4_300_000, vspan=100) | ||||
# Sending 1.4 btc should pick one 1.0 + one more. For node #1, | # Sending 1.4 btc should pick one 1.0 + one more. For node #1, | ||||
# this could be (A / B0 / C0) + (B1 / C1 / D). We ensure that it is | # this could be (A / B0 / C0) + (B1 / C1 / D). We ensure that it is | ||||
# B0 + B1 or C0 + C1, because this avoids partial spends while not being | # B0 + B1 or C0 + C1, because this avoids partial spends while not being | ||||
# detrimental to transaction cost | # detrimental to transaction cost | ||||
txid3 = self.nodes[1].sendtoaddress( | txid3 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1400000) | ||||
self.nodes[0].getnewaddress(), 1400000) | |||||
tx3 = self.nodes[1].getrawtransaction(txid3, True) | tx3 = self.nodes[1].getrawtransaction(txid3, True) | ||||
# tx3 should have 2 inputs and 2 outputs | # tx3 should have 2 inputs and 2 outputs | ||||
assert_equal(2, len(tx3["vin"])) | assert_equal(2, len(tx3["vin"])) | ||||
assert_equal(2, len(tx3["vout"])) | assert_equal(2, len(tx3["vout"])) | ||||
# the accumulated value should be 1.5, so the outputs should be | # the accumulated value should be 1.5, so the outputs should be | ||||
# ~0.1 and 1.4 and should come from the same destination | # ~0.1 and 1.4 and should come from the same destination | ||||
values = sorted([vout["value"] for vout in tx3["vout"]]) | values = sorted([vout["value"] for vout in tx3["vout"]]) | ||||
assert_approx(values[0], vexp=100_000, vspan=100) | assert_approx(values[0], vexp=100_000, vspan=100) | ||||
assert_approx(values[1], vexp=1_400_000, vspan=100) | assert_approx(values[1], vexp=1_400_000, vspan=100) | ||||
input_txids = [vin["txid"] for vin in tx3["vin"]] | input_txids = [vin["txid"] for vin in tx3["vin"]] | ||||
input_addrs = [self.nodes[1].gettransaction( | input_addrs = [ | ||||
txid)['details'][0]['address'] for txid in input_txids] | self.nodes[1].gettransaction(txid)["details"][0]["address"] | ||||
for txid in input_txids | |||||
] | |||||
assert_equal(input_addrs[0], input_addrs[1]) | assert_equal(input_addrs[0], input_addrs[1]) | ||||
# Node 2 enforces avoidpartialspends so needs no checking here | # Node 2 enforces avoidpartialspends so needs no checking here | ||||
# Test wallet option maxapsfee with Node 3 | # Test wallet option maxapsfee with Node 3 | ||||
addr_aps = self.nodes[3].getnewaddress() | addr_aps = self.nodes[3].getnewaddress() | ||||
self.nodes[0].sendtoaddress(addr_aps, 1000000) | self.nodes[0].sendtoaddress(addr_aps, 1000000) | ||||
self.nodes[0].sendtoaddress(addr_aps, 1000000) | self.nodes[0].sendtoaddress(addr_aps, 1000000) | ||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
with self.nodes[3].assert_debug_log([ | with self.nodes[3].assert_debug_log( | ||||
'Fee non-grouped = 225, grouped = 372, using grouped']): | ["Fee non-grouped = 225, grouped = 372, using grouped"] | ||||
txid4 = self.nodes[3].sendtoaddress( | ): | ||||
self.nodes[0].getnewaddress(), 100_000) | txid4 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), 100_000) | ||||
tx4 = self.nodes[3].getrawtransaction(txid4, True) | tx4 = self.nodes[3].getrawtransaction(txid4, True) | ||||
# tx4 should have 2 inputs and 2 outputs although one output would | # tx4 should have 2 inputs and 2 outputs although one output would | ||||
# have been enough and the transaction caused higher fees | # have been enough and the transaction caused higher fees | ||||
assert_equal(2, len(tx4["vin"])) | assert_equal(2, len(tx4["vin"])) | ||||
assert_equal(2, len(tx4["vout"])) | assert_equal(2, len(tx4["vout"])) | ||||
addr_aps2 = self.nodes[3].getnewaddress() | addr_aps2 = self.nodes[3].getnewaddress() | ||||
[self.nodes[0].sendtoaddress(addr_aps2, 1_000_000) for _ in range(5)] | [self.nodes[0].sendtoaddress(addr_aps2, 1_000_000) for _ in range(5)] | ||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
with self.nodes[3].assert_debug_log([ | with self.nodes[3].assert_debug_log( | ||||
'Fee non-grouped = 519, grouped = 813, using non-grouped']): | ["Fee non-grouped = 519, grouped = 813, using non-grouped"] | ||||
txid5 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), | ): | ||||
2_950_000) | txid5 = self.nodes[3].sendtoaddress( | ||||
self.nodes[0].getnewaddress(), 2_950_000 | |||||
) | |||||
tx5 = self.nodes[3].getrawtransaction(txid5, True) | tx5 = self.nodes[3].getrawtransaction(txid5, True) | ||||
# tx5 should have 3 inputs (1.0, 1.0, 1.0) and 2 outputs | # tx5 should have 3 inputs (1.0, 1.0, 1.0) and 2 outputs | ||||
assert_equal(3, len(tx5["vin"])) | assert_equal(3, len(tx5["vin"])) | ||||
assert_equal(2, len(tx5["vout"])) | assert_equal(2, len(tx5["vout"])) | ||||
# Test wallet option maxapsfee with node 4, which sets maxapsfee | # Test wallet option maxapsfee with node 4, which sets maxapsfee | ||||
# 1 sat higher, crossing the threshold from non-grouped to grouped. | # 1 sat higher, crossing the threshold from non-grouped to grouped. | ||||
addr_aps3 = self.nodes[4].getnewaddress() | addr_aps3 = self.nodes[4].getnewaddress() | ||||
[self.nodes[0].sendtoaddress(addr_aps3, 1_000_000) for _ in range(5)] | [self.nodes[0].sendtoaddress(addr_aps3, 1_000_000) for _ in range(5)] | ||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
with self.nodes[4].assert_debug_log([ | with self.nodes[4].assert_debug_log( | ||||
'Fee non-grouped = 519, grouped = 813, using grouped']): | ["Fee non-grouped = 519, grouped = 813, using grouped"] | ||||
txid6 = self.nodes[4].sendtoaddress(self.nodes[0].getnewaddress(), | ): | ||||
2_950_000) | txid6 = self.nodes[4].sendtoaddress( | ||||
self.nodes[0].getnewaddress(), 2_950_000 | |||||
) | |||||
tx6 = self.nodes[4].getrawtransaction(txid6, True) | tx6 = self.nodes[4].getrawtransaction(txid6, True) | ||||
# tx6 should have 5 inputs and 2 outputs | # tx6 should have 5 inputs and 2 outputs | ||||
assert_equal(5, len(tx6["vin"])) | assert_equal(5, len(tx6["vin"])) | ||||
assert_equal(2, len(tx6["vout"])) | assert_equal(2, len(tx6["vout"])) | ||||
# Empty out node2's wallet | # Empty out node2's wallet | ||||
self.nodes[2].sendtoaddress(address=self.nodes[0].getnewaddress( | self.nodes[2].sendtoaddress( | ||||
), amount=self.nodes[2].getbalance(), subtractfeefromamount=True) | address=self.nodes[0].getnewaddress(), | ||||
amount=self.nodes[2].getbalance(), | |||||
subtractfeefromamount=True, | |||||
) | |||||
self.sync_all() | self.sync_all() | ||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
# Fill node2's wallet with 10000 outputs corresponding to the same | # Fill node2's wallet with 10000 outputs corresponding to the same | ||||
# scriptPubKey | # scriptPubKey | ||||
for _ in range(5): | for _ in range(5): | ||||
raw_tx = self.nodes[0].createrawtransaction( | raw_tx = self.nodes[0].createrawtransaction( | ||||
[{"txid": "0" * 64, "vout": 0}], [{addr2[0]: 50_000}]) | [{"txid": "0" * 64, "vout": 0}], [{addr2[0]: 50_000}] | ||||
) | |||||
tx = FromHex(CTransaction(), raw_tx) | tx = FromHex(CTransaction(), raw_tx) | ||||
tx.vin = [] | tx.vin = [] | ||||
tx.vout = [tx.vout[0]] * 2000 | tx.vout = [tx.vout[0]] * 2000 | ||||
funded_tx = self.nodes[0].fundrawtransaction(ToHex(tx)) | funded_tx = self.nodes[0].fundrawtransaction(ToHex(tx)) | ||||
signed_tx = self.nodes[0].signrawtransactionwithwallet( | signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx["hex"]) | ||||
funded_tx['hex']) | self.nodes[0].sendrawtransaction(signed_tx["hex"]) | ||||
self.nodes[0].sendrawtransaction(signed_tx['hex']) | |||||
self.generate(self.nodes[0], 1) | self.generate(self.nodes[0], 1) | ||||
# Check that we can create a transaction that only requires ~100 of our | # Check that we can create a transaction that only requires ~100 of our | ||||
# utxos, without pulling in all outputs and creating a transaction that | # utxos, without pulling in all outputs and creating a transaction that | ||||
# is way too big. | # is way too big. | ||||
assert self.nodes[2].sendtoaddress(address=addr2[0], amount=5000000) | assert self.nodes[2].sendtoaddress(address=addr2[0], amount=5000000) | ||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
WalletGroupTest().main() | WalletGroupTest().main() |