2018-01-24 06:59:08 +01:00
|
|
|
#!/usr/bin/env python3
|
2020-04-16 19:14:08 +02:00
|
|
|
# Copyright (c) 2018-2020 The Bitcoin Core developers
|
2018-01-24 06:59:08 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
"""Test wallet group functionality."""
|
|
|
|
|
2021-05-17 16:38:19 +02:00
|
|
|
from test_framework.blocktools import COINBASE_MATURITY
|
2018-01-24 06:59:08 +01:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2021-06-15 23:02:28 +02:00
|
|
|
from test_framework.messages import (
|
|
|
|
tx_from_hex,
|
|
|
|
)
|
2018-01-24 06:59:08 +01:00
|
|
|
from test_framework.util import (
|
2019-09-19 14:53:40 +02:00
|
|
|
assert_approx,
|
2018-01-24 06:59:08 +01:00
|
|
|
assert_equal,
|
|
|
|
)
|
|
|
|
|
2020-03-13 16:10:36 +01:00
|
|
|
|
2018-01-24 06:59:08 +01:00
|
|
|
class WalletGroupTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
|
|
self.setup_clean_chain = True
|
2020-08-17 07:38:21 +02:00
|
|
|
self.num_nodes = 5
|
|
|
|
self.extra_args = [
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
["-avoidpartialspends"],
|
|
|
|
["-maxapsfee=0.00002719"],
|
|
|
|
["-maxapsfee=0.00002720"],
|
|
|
|
]
|
2020-03-13 16:10:36 +01:00
|
|
|
self.rpc_timeout = 480
|
2018-01-24 06:59:08 +01:00
|
|
|
|
2018-09-09 19:32:37 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
|
|
|
def run_test(self):
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Setting up")
|
2018-01-24 06:59:08 +01:00
|
|
|
# Mine some coins
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
2018-01-24 06:59:08 +01:00
|
|
|
|
|
|
|
# Get some addresses from the two nodes
|
2020-08-03 01:10:56 +02:00
|
|
|
addr1 = [self.nodes[1].getnewaddress() for _ in range(3)]
|
|
|
|
addr2 = [self.nodes[2].getnewaddress() for _ in range(3)]
|
2018-01-24 06:59:08 +01:00
|
|
|
addrs = addr1 + addr2
|
|
|
|
|
|
|
|
# Send 1 + 0.5 coin to each address
|
|
|
|
[self.nodes[0].sendtoaddress(addr, 1.0) for addr in addrs]
|
|
|
|
[self.nodes[0].sendtoaddress(addr, 0.5) for addr in addrs]
|
|
|
|
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2018-01-24 06:59:08 +01:00
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# For each node, send 0.2 coins back to 0;
|
|
|
|
# - 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
|
|
|
|
# given address, and leave the rest
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Test sending transactions picks one UTXO group and leaves the rest")
|
2018-01-24 06:59:08 +01:00
|
|
|
txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
|
|
|
|
tx1 = self.nodes[1].getrawtransaction(txid1, True)
|
|
|
|
# txid1 should have 1 input and 2 outputs
|
|
|
|
assert_equal(1, len(tx1["vin"]))
|
|
|
|
assert_equal(2, len(tx1["vout"]))
|
|
|
|
# one output should be 0.2, the other should be ~0.3
|
|
|
|
v = [vout["value"] for vout in tx1["vout"]]
|
|
|
|
v.sort()
|
2020-08-17 07:38:21 +02:00
|
|
|
assert_approx(v[0], vexp=0.2, vspan=0.0001)
|
|
|
|
assert_approx(v[1], vexp=0.3, vspan=0.0001)
|
2018-01-24 06:59:08 +01:00
|
|
|
|
|
|
|
txid2 = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
|
|
|
|
tx2 = self.nodes[2].getrawtransaction(txid2, True)
|
|
|
|
# txid2 should have 2 inputs and 2 outputs
|
|
|
|
assert_equal(2, len(tx2["vin"]))
|
|
|
|
assert_equal(2, len(tx2["vout"]))
|
|
|
|
# one output should be 0.2, the other should be ~1.3
|
|
|
|
v = [vout["value"] for vout in tx2["vout"]]
|
|
|
|
v.sort()
|
2020-08-17 07:38:21 +02:00
|
|
|
assert_approx(v[0], vexp=0.2, vspan=0.0001)
|
|
|
|
assert_approx(v[1], vexp=1.3, vspan=0.0001)
|
2018-01-24 06:59:08 +01:00
|
|
|
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Test avoiding partial spends if warranted, even if avoidpartialspends is disabled")
|
2018-10-26 08:41:00 +02:00
|
|
|
self.sync_all()
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2018-10-26 08:41:00 +02:00
|
|
|
# Nodes 1-2 now have confirmed UTXOs (letters denote destinations):
|
|
|
|
# Node #1: Node #2:
|
|
|
|
# - A 1.0 - D0 1.0
|
|
|
|
# - B0 1.0 - D1 0.5
|
|
|
|
# - B1 0.5 - E0 1.0
|
|
|
|
# - C0 1.0 - E1 0.5
|
|
|
|
# - C1 0.5 - F ~1.3
|
|
|
|
# - D ~0.3
|
2020-08-17 07:38:21 +02:00
|
|
|
assert_approx(self.nodes[1].getbalance(), vexp=4.3, vspan=0.0001)
|
|
|
|
assert_approx(self.nodes[2].getbalance(), vexp=4.3, vspan=0.0001)
|
2018-10-26 08:41:00 +02:00
|
|
|
# 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
|
|
|
|
# B0 + B1 or C0 + C1, because this avoids partial spends while not being
|
|
|
|
# detrimental to transaction cost
|
|
|
|
txid3 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.4)
|
|
|
|
tx3 = self.nodes[1].getrawtransaction(txid3, True)
|
|
|
|
# tx3 should have 2 inputs and 2 outputs
|
|
|
|
assert_equal(2, len(tx3["vin"]))
|
|
|
|
assert_equal(2, len(tx3["vout"]))
|
|
|
|
# the accumulated value should be 1.5, so the outputs should be
|
|
|
|
# ~0.1 and 1.4 and should come from the same destination
|
|
|
|
values = [vout["value"] for vout in tx3["vout"]]
|
|
|
|
values.sort()
|
2020-08-17 07:38:21 +02:00
|
|
|
assert_approx(values[0], vexp=0.1, vspan=0.0001)
|
|
|
|
assert_approx(values[1], vexp=1.4, vspan=0.0001)
|
2018-10-26 08:41:00 +02:00
|
|
|
|
|
|
|
input_txids = [vin["txid"] for vin in tx3["vin"]]
|
|
|
|
input_addrs = [self.nodes[1].gettransaction(txid)['details'][0]['address'] for txid in input_txids]
|
|
|
|
assert_equal(input_addrs[0], input_addrs[1])
|
|
|
|
# Node 2 enforces avoidpartialspends so needs no checking here
|
|
|
|
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Test wallet option maxapsfee")
|
2018-10-26 08:41:00 +02:00
|
|
|
addr_aps = self.nodes[3].getnewaddress()
|
|
|
|
self.nodes[0].sendtoaddress(addr_aps, 1.0)
|
|
|
|
self.nodes[0].sendtoaddress(addr_aps, 1.0)
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2020-08-18 03:08:49 +02:00
|
|
|
self.sync_all()
|
2020-08-17 07:38:21 +02:00
|
|
|
with self.nodes[3].assert_debug_log(['Fee non-grouped = 2820, grouped = 4160, using grouped']):
|
|
|
|
txid4 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), 0.1)
|
2018-10-26 08:41:00 +02:00
|
|
|
tx4 = self.nodes[3].getrawtransaction(txid4, True)
|
|
|
|
# tx4 should have 2 inputs and 2 outputs although one output would
|
|
|
|
# have been enough and the transaction caused higher fees
|
|
|
|
assert_equal(2, len(tx4["vin"]))
|
|
|
|
assert_equal(2, len(tx4["vout"]))
|
|
|
|
|
2020-08-17 07:38:21 +02:00
|
|
|
addr_aps2 = self.nodes[3].getnewaddress()
|
|
|
|
[self.nodes[0].sendtoaddress(addr_aps2, 1.0) for _ in range(5)]
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2020-08-17 07:38:21 +02:00
|
|
|
self.sync_all()
|
|
|
|
with self.nodes[3].assert_debug_log(['Fee non-grouped = 5520, grouped = 8240, using non-grouped']):
|
|
|
|
txid5 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), 2.95)
|
|
|
|
tx5 = self.nodes[3].getrawtransaction(txid5, True)
|
|
|
|
# tx5 should have 3 inputs (1.0, 1.0, 1.0) and 2 outputs
|
|
|
|
assert_equal(3, len(tx5["vin"]))
|
|
|
|
assert_equal(2, len(tx5["vout"]))
|
|
|
|
|
|
|
|
# Test wallet option maxapsfee with node 4, which sets maxapsfee
|
|
|
|
# 1 sat higher, crossing the threshold from non-grouped to grouped.
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Test wallet option maxapsfee threshold from non-grouped to grouped")
|
2020-08-17 07:38:21 +02:00
|
|
|
addr_aps3 = self.nodes[4].getnewaddress()
|
|
|
|
[self.nodes[0].sendtoaddress(addr_aps3, 1.0) for _ in range(5)]
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2020-08-17 07:38:21 +02:00
|
|
|
self.sync_all()
|
|
|
|
with self.nodes[4].assert_debug_log(['Fee non-grouped = 5520, grouped = 8240, using grouped']):
|
|
|
|
txid6 = self.nodes[4].sendtoaddress(self.nodes[0].getnewaddress(), 2.95)
|
|
|
|
tx6 = self.nodes[4].getrawtransaction(txid6, True)
|
|
|
|
# tx6 should have 5 inputs and 2 outputs
|
|
|
|
assert_equal(5, len(tx6["vin"]))
|
|
|
|
assert_equal(2, len(tx6["vout"]))
|
|
|
|
|
2018-07-30 16:43:59 +02:00
|
|
|
# Empty out node2's wallet
|
|
|
|
self.nodes[2].sendtoaddress(address=self.nodes[0].getnewaddress(), amount=self.nodes[2].getbalance(), subtractfeefromamount=True)
|
|
|
|
self.sync_all()
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2018-07-30 16:43:59 +02:00
|
|
|
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Fill a wallet with 10,000 outputs corresponding to the same scriptPubKey")
|
2020-08-03 01:10:56 +02:00
|
|
|
for _ in range(5):
|
2018-07-30 16:43:59 +02:00
|
|
|
raw_tx = self.nodes[0].createrawtransaction([{"txid":"0"*64, "vout":0}], [{addr2[0]: 0.05}])
|
2021-06-15 23:02:28 +02:00
|
|
|
tx = tx_from_hex(raw_tx)
|
2018-07-30 16:43:59 +02:00
|
|
|
tx.vin = []
|
|
|
|
tx.vout = [tx.vout[0]] * 2000
|
2021-06-16 00:32:18 +02:00
|
|
|
funded_tx = self.nodes[0].fundrawtransaction(tx.serialize().hex())
|
2018-07-30 16:43:59 +02:00
|
|
|
signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx['hex'])
|
|
|
|
self.nodes[0].sendrawtransaction(signed_tx['hex'])
|
2021-08-19 17:10:24 +02:00
|
|
|
self.generate(self.nodes[0], 1)
|
2021-03-07 20:23:22 +01:00
|
|
|
self.sync_all()
|
2018-07-30 16:43:59 +02:00
|
|
|
|
|
|
|
# Check that we can create a transaction that only requires ~100 of our
|
|
|
|
# utxos, without pulling in all outputs and creating a transaction that
|
|
|
|
# is way too big.
|
2021-03-07 20:21:34 +01:00
|
|
|
self.log.info("Test creating txn that only requires ~100 of our UTXOs without pulling in all outputs")
|
2018-07-30 16:43:59 +02:00
|
|
|
assert self.nodes[2].sendtoaddress(address=addr2[0], amount=5)
|
|
|
|
|
2020-03-13 16:10:36 +01:00
|
|
|
|
2018-01-24 06:59:08 +01:00
|
|
|
if __name__ == '__main__':
|
2020-03-13 16:10:36 +01:00
|
|
|
WalletGroupTest().main()
|