wallet_groups.py raw

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