/ test / functional / wallet_groups.py
wallet_groups.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2018-2022 The Bitcoin Core developers
  3  # Distributed under the MIT software license, see the accompanying
  4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5  """Test wallet group functionality."""
  6  
  7  from test_framework.blocktools import COINBASE_MATURITY
  8  from test_framework.test_framework import BitcoinTestFramework
  9  from test_framework.messages import (
 10      tx_from_hex,
 11  )
 12  from test_framework.util import (
 13      assert_approx,
 14      assert_equal,
 15  )
 16  
 17  
 18  class WalletGroupTest(BitcoinTestFramework):
 19      def add_options(self, parser):
 20          self.add_wallet_options(parser)
 21  
 22      def set_test_params(self):
 23          self.setup_clean_chain = True
 24          self.num_nodes = 5
 25          # whitelist peers to speed up tx relay / mempool sync
 26          self.noban_tx_relay = True
 27          self.extra_args = [
 28              [],
 29              [],
 30              ["-avoidpartialspends"],
 31              ["-maxapsfee=0.00002719"],
 32              ["-maxapsfee=0.00002720"],
 33          ]
 34  
 35          for args in self.extra_args:
 36              args.append(f"-paytxfee={20 * 1e3 / 1e8}")  # apply feerate of 20 sats/vB across all nodes
 37  
 38          self.rpc_timeout = 480
 39  
 40      def skip_test_if_missing_module(self):
 41          self.skip_if_no_wallet()
 42  
 43      def run_test(self):
 44          self.log.info("Setting up")
 45          # To take full use of immediate tx relay, all nodes need to be reachable
 46          # via inbound peers, i.e. connect first to last to close the circle
 47          # (the default test network topology looks like this:
 48          #  node0 <-- node1 <-- node2 <-- node3 <-- node4 <-- node5)
 49          self.connect_nodes(0, self.num_nodes - 1)
 50          # Mine some coins
 51          self.generate(self.nodes[0], COINBASE_MATURITY + 1)
 52  
 53          # Get some addresses from the two nodes
 54          addr1 = [self.nodes[1].getnewaddress() for _ in range(3)]
 55          addr2 = [self.nodes[2].getnewaddress() for _ in range(3)]
 56          addrs = addr1 + addr2
 57  
 58          # Send 1 + 0.5 coin to each address
 59          [self.nodes[0].sendtoaddress(addr, 1.0) for addr in addrs]
 60          [self.nodes[0].sendtoaddress(addr, 0.5) for addr in addrs]
 61  
 62          self.generate(self.nodes[0], 1)
 63  
 64          # For each node, send 0.2 coins back to 0;
 65          # - node[1] should pick one 0.5 UTXO and leave the rest
 66          # - node[2] should pick one (1.0 + 0.5) UTXO group corresponding to a
 67          #   given address, and leave the rest
 68          self.log.info("Test sending transactions picks one UTXO group and leaves the rest")
 69          txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
 70          tx1 = self.nodes[1].getrawtransaction(txid1, True)
 71          # txid1 should have 1 input and 2 outputs
 72          assert_equal(1, len(tx1["vin"]))
 73          assert_equal(2, len(tx1["vout"]))
 74          # one output should be 0.2, the other should be ~0.3
 75          v = [vout["value"] for vout in tx1["vout"]]
 76          v.sort()
 77          assert_approx(v[0], vexp=0.2, vspan=0.0001)
 78          assert_approx(v[1], vexp=0.3, vspan=0.0001)
 79  
 80          txid2 = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 0.2)
 81          tx2 = self.nodes[2].getrawtransaction(txid2, True)
 82          # txid2 should have 2 inputs and 2 outputs
 83          assert_equal(2, len(tx2["vin"]))
 84          assert_equal(2, len(tx2["vout"]))
 85          # one output should be 0.2, the other should be ~1.3
 86          v = [vout["value"] for vout in tx2["vout"]]
 87          v.sort()
 88          assert_approx(v[0], vexp=0.2, vspan=0.0001)
 89          assert_approx(v[1], vexp=1.3, vspan=0.0001)
 90  
 91          self.log.info("Test avoiding partial spends if warranted, even if avoidpartialspends is disabled")
 92          self.sync_all()
 93          self.generate(self.nodes[0], 1)
 94          # Nodes 1-2 now have confirmed UTXOs (letters denote destinations):
 95          # Node #1:      Node #2:
 96          # - A  1.0      - D0 1.0
 97          # - B0 1.0      - D1 0.5
 98          # - B1 0.5      - E0 1.0
 99          # - C0 1.0      - E1 0.5
100          # - C1 0.5      - F  ~1.3
101          # - D ~0.3
102          assert_approx(self.nodes[1].getbalance(), vexp=4.3, vspan=0.0001)
103          assert_approx(self.nodes[2].getbalance(), vexp=4.3, vspan=0.0001)
104          # Sending 1.4 btc should pick one 1.0 + one more. For node #1,
105          # this could be (A / B0 / C0) + (B1 / C1 / D). We ensure that it is
106          # B0 + B1 or C0 + C1, because this avoids partial spends while not being
107          # detrimental to transaction cost
108          txid3 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1.4)
109          tx3 = self.nodes[1].getrawtransaction(txid3, True)
110          # tx3 should have 2 inputs and 2 outputs
111          assert_equal(2, len(tx3["vin"]))
112          assert_equal(2, len(tx3["vout"]))
113          # the accumulated value should be 1.5, so the outputs should be
114          # ~0.1 and 1.4 and should come from the same destination
115          values = [vout["value"] for vout in tx3["vout"]]
116          values.sort()
117          assert_approx(values[0], vexp=0.1, vspan=0.0001)
118          assert_approx(values[1], vexp=1.4, vspan=0.0001)
119  
120          input_txids = [vin["txid"] for vin in tx3["vin"]]
121          input_addrs = [self.nodes[1].gettransaction(txid)['details'][0]['address'] for txid in input_txids]
122          assert_equal(input_addrs[0], input_addrs[1])
123          # Node 2 enforces avoidpartialspends so needs no checking here
124  
125          tx4_ungrouped_fee = 2820
126          tx4_grouped_fee = 4160
127          tx5_6_ungrouped_fee = 5520
128          tx5_6_grouped_fee = 8240
129  
130          self.log.info("Test wallet option maxapsfee")
131          addr_aps = self.nodes[3].getnewaddress()
132          self.nodes[0].sendtoaddress(addr_aps, 1.0)
133          self.nodes[0].sendtoaddress(addr_aps, 1.0)
134          self.generate(self.nodes[0], 1)
135          with self.nodes[3].assert_debug_log([f'Fee non-grouped = {tx4_ungrouped_fee}, grouped = {tx4_grouped_fee}, using grouped']):
136              txid4 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), 0.1)
137          tx4 = self.nodes[3].getrawtransaction(txid4, True)
138          # tx4 should have 2 inputs and 2 outputs although one output would
139          # have been enough and the transaction caused higher fees
140          assert_equal(2, len(tx4["vin"]))
141          assert_equal(2, len(tx4["vout"]))
142  
143          addr_aps2 = self.nodes[3].getnewaddress()
144          [self.nodes[0].sendtoaddress(addr_aps2, 1.0) for _ in range(5)]
145          self.generate(self.nodes[0], 1)
146          with self.nodes[3].assert_debug_log([f'Fee non-grouped = {tx5_6_ungrouped_fee}, grouped = {tx5_6_grouped_fee}, using non-grouped']):
147              txid5 = self.nodes[3].sendtoaddress(self.nodes[0].getnewaddress(), 2.95)
148          tx5 = self.nodes[3].getrawtransaction(txid5, True)
149          # tx5 should have 3 inputs (1.0, 1.0, 1.0) and 2 outputs
150          assert_equal(3, len(tx5["vin"]))
151          assert_equal(2, len(tx5["vout"]))
152  
153          # Test wallet option maxapsfee with node 4, which sets maxapsfee
154          # 1 sat higher, crossing the threshold from non-grouped to grouped.
155          self.log.info("Test wallet option maxapsfee threshold from non-grouped to grouped")
156          addr_aps3 = self.nodes[4].getnewaddress()
157          [self.nodes[0].sendtoaddress(addr_aps3, 1.0) for _ in range(5)]
158          self.generate(self.nodes[0], 1)
159          with self.nodes[4].assert_debug_log([f'Fee non-grouped = {tx5_6_ungrouped_fee}, grouped = {tx5_6_grouped_fee}, using grouped']):
160              txid6 = self.nodes[4].sendtoaddress(self.nodes[0].getnewaddress(), 2.95)
161          tx6 = self.nodes[4].getrawtransaction(txid6, True)
162          # tx6 should have 5 inputs and 2 outputs
163          assert_equal(5, len(tx6["vin"]))
164          assert_equal(2, len(tx6["vout"]))
165  
166          # Empty out node2's wallet
167          self.nodes[2].sendall(recipients=[self.nodes[0].getnewaddress()])
168          self.sync_all()
169          self.generate(self.nodes[0], 1)
170  
171          self.log.info("Fill a wallet with 10,000 outputs corresponding to the same scriptPubKey")
172          for _ in range(5):
173              raw_tx = self.nodes[0].createrawtransaction([{"txid":"0"*64, "vout":0}], [{addr2[0]: 0.05}])
174              tx = tx_from_hex(raw_tx)
175              tx.vin = []
176              tx.vout = [tx.vout[0]] * 2000
177              funded_tx = self.nodes[0].fundrawtransaction(tx.serialize().hex())
178              signed_tx = self.nodes[0].signrawtransactionwithwallet(funded_tx['hex'])
179              self.nodes[0].sendrawtransaction(signed_tx['hex'])
180              self.generate(self.nodes[0], 1)
181  
182          # Check that we can create a transaction that only requires ~100 of our
183          # utxos, without pulling in all outputs and creating a transaction that
184          # is way too big.
185          self.log.info("Test creating txn that only requires ~100 of our UTXOs without pulling in all outputs")
186          assert self.nodes[2].sendtoaddress(address=addr2[0], amount=5)
187  
188  
189  if __name__ == '__main__':
190      WalletGroupTest().main()