|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2018 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.test_framework import BitcoinTestFramework |
| 8 | +from test_framework.util import ( |
| 9 | + assert_equal, |
| 10 | +) |
| 11 | + |
| 12 | +def assert_approx(v, vexp, vspan=0.00001): |
| 13 | + if v < vexp - vspan: |
| 14 | + raise AssertionError("%s < [%s..%s]" % (str(v), str(vexp - vspan), str(vexp + vspan))) |
| 15 | + if v > vexp + vspan: |
| 16 | + raise AssertionError("%s > [%s..%s]" % (str(v), str(vexp - vspan), str(vexp + vspan))) |
| 17 | + |
| 18 | +class WalletGroupTest(BitcoinTestFramework): |
| 19 | + def set_test_params(self): |
| 20 | + self.setup_clean_chain = True |
| 21 | + self.num_nodes = 3 |
| 22 | + self.extra_args = [[], [], ['-avoidpartialspends']] |
| 23 | + |
| 24 | + def run_test (self): |
| 25 | + # Mine some coins |
| 26 | + self.nodes[0].generate(110) |
| 27 | + |
| 28 | + # Get some addresses from the two nodes |
| 29 | + addr1 = [self.nodes[1].getnewaddress() for i in range(3)] |
| 30 | + addr2 = [self.nodes[2].getnewaddress() for i in range(3)] |
| 31 | + addrs = addr1 + addr2 |
| 32 | + |
| 33 | + # Send 1 + 0.5 coin to each address |
| 34 | + [self.nodes[0].sendtoaddress(addr, 1.0) for addr in addrs] |
| 35 | + [self.nodes[0].sendtoaddress(addr, 0.5) for addr in addrs] |
| 36 | + |
| 37 | + self.nodes[0].generate(1) |
| 38 | + self.sync_all() |
| 39 | + |
| 40 | + # For each node, send 0.2 coins back to 0; |
| 41 | + # - node[1] should pick one 0.5 UTXO and leave the rest |
| 42 | + # - node[2] should pick one (1.0 + 0.5) UTXO group corresponding to a |
| 43 | + # given address, and leave the rest |
| 44 | + txid1 = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 0.2) |
| 45 | + tx1 = self.nodes[1].getrawtransaction(txid1, True) |
| 46 | + # txid1 should have 1 input and 2 outputs |
| 47 | + assert_equal(1, len(tx1["vin"])) |
| 48 | + assert_equal(2, len(tx1["vout"])) |
| 49 | + # one output should be 0.2, the other should be ~0.3 |
| 50 | + v = [vout["value"] for vout in tx1["vout"]] |
| 51 | + v.sort() |
| 52 | + assert_approx(v[0], 0.2) |
| 53 | + assert_approx(v[1], 0.3, 0.0001) |
| 54 | + |
| 55 | + txid2 = self.nodes[2].sendtoaddress(self.nodes[0].getnewaddress(), 0.2) |
| 56 | + tx2 = self.nodes[2].getrawtransaction(txid2, True) |
| 57 | + # txid2 should have 2 inputs and 2 outputs |
| 58 | + assert_equal(2, len(tx2["vin"])) |
| 59 | + assert_equal(2, len(tx2["vout"])) |
| 60 | + # one output should be 0.2, the other should be ~1.3 |
| 61 | + v = [vout["value"] for vout in tx2["vout"]] |
| 62 | + v.sort() |
| 63 | + assert_approx(v[0], 0.2) |
| 64 | + assert_approx(v[1], 1.3, 0.0001) |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + WalletGroupTest().main () |
0 commit comments