|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2017-2020 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 blinding logic when change is dropped and we have only one other blinded input |
| 6 | +
|
| 7 | +Constructs a transaction with a sufficiently small change output that it |
| 8 | +gets dropped, in which there is only one other blinded input. In the case |
| 9 | +that we have no blinded inputs, we would need to add an OP_RETURN output |
| 10 | +to the transaction, neccessitating special logic. |
| 11 | +
|
| 12 | +Check that this special logic still results in a correct transaction that |
| 13 | +sends the money to the desired recipient (and that the recipient is able |
| 14 | +to receive/spend the money). |
| 15 | +""" |
| 16 | + |
| 17 | +from decimal import Decimal |
| 18 | + |
| 19 | +from test_framework.blocktools import COINBASE_MATURITY |
| 20 | +from test_framework.test_framework import BitcoinTestFramework |
| 21 | +from test_framework.util import ( |
| 22 | + assert_equal, |
| 23 | + satoshi_round, |
| 24 | +) |
| 25 | + |
| 26 | +class WalletCtTest(BitcoinTestFramework): |
| 27 | + def set_test_params(self): |
| 28 | + self.setup_clean_chain = True |
| 29 | + self.num_nodes = 3 |
| 30 | + self.extra_args = [[ |
| 31 | + "-blindedaddresses=1", |
| 32 | + "-initialfreecoins=2100000000000000", |
| 33 | + "-con_blocksubsidy=0", |
| 34 | + "-con_connect_genesis_outputs=1", |
| 35 | + "-txindex=1", |
| 36 | + ]] * self.num_nodes |
| 37 | + self.extra_args[0].append("-anyonecanspendaremine=1") # first node gets the coins |
| 38 | + |
| 39 | + def skip_test_if_missing_module(self): |
| 40 | + self.skip_if_no_wallet() |
| 41 | + |
| 42 | + def test_send(self, amt, from_idx, to_idx, confidential): |
| 43 | + # Try to send those coins to yet another wallet, sending a large enough amount |
| 44 | + # that the change output is dropped. |
| 45 | + address = self.nodes[to_idx].getnewaddress() |
| 46 | + if not confidential: |
| 47 | + address = self.nodes[to_idx].getaddressinfo(address)['unconfidential'] |
| 48 | + txid = self.nodes[from_idx].sendtoaddress(address, amt) |
| 49 | + self.log.info(f"Sent {amt} LBTC to node {to_idx} in {txid}") |
| 50 | + self.nodes[from_idx].generate(2) |
| 51 | + self.sync_all() |
| 52 | + |
| 53 | + for i in range(self.num_nodes): |
| 54 | + self.log.info(f"Finished with node {i} balance: {self.nodes[i].getbalance()}") |
| 55 | + assert_equal(self.nodes[from_idx].getbalance(), { "bitcoin": Decimal(0) }) |
| 56 | + assert_equal(self.nodes[to_idx].getbalance(), { "bitcoin": amt }) |
| 57 | + |
| 58 | + def run_test(self): |
| 59 | + # Mine 101 blocks to get the initial coins out of IBD |
| 60 | + self.nodes[0].generate(COINBASE_MATURITY + 1) |
| 61 | + self.nodes[0].syncwithvalidationinterfacequeue() |
| 62 | + self.sync_all() |
| 63 | + |
| 64 | + for i in range(self.num_nodes): |
| 65 | + self.log.info(f"Starting with node {i} balance: {self.nodes[i].getbalance()}") |
| 66 | + |
| 67 | + # Send 1 coin to a new wallet |
| 68 | + txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 1) |
| 69 | + self.log.info(f"Sent one coin to node 1 in {txid}") |
| 70 | + self.nodes[0].generate(2) |
| 71 | + self.sync_all() |
| 72 | + |
| 73 | + # Try to send those coins to yet another wallet, sending a large enough amount |
| 74 | + # that the change output is dropped. |
| 75 | + amt = satoshi_round(Decimal(0.9995)) |
| 76 | + self.test_send(amt, 1, 2, True) |
| 77 | + |
| 78 | + # Repeat, sending to a non-confidential output |
| 79 | + amt = satoshi_round(Decimal(amt - Decimal(0.00035))) |
| 80 | + self.test_send(amt, 2, 1, False) |
| 81 | + |
| 82 | + # Again, sending from non-confidential to non-confidential |
| 83 | + amt = satoshi_round(Decimal(amt - Decimal(0.00033))) |
| 84 | + self.test_send(amt, 1, 2, False) |
| 85 | + |
| 86 | + # Finally sending from non-confidential to confidential |
| 87 | + amt = satoshi_round(Decimal(amt - Decimal(0.0005))) |
| 88 | + self.test_send(amt, 2, 1, True) |
| 89 | + |
| 90 | + # Then send the coins again to make sure they're spendable |
| 91 | + amt = satoshi_round(Decimal(amt - Decimal(0.0005))) |
| 92 | + self.test_send(amt, 1, 2, True) |
| 93 | + |
| 94 | + addresses = [ self.nodes[1].getnewaddress() for i in range(15) ] \ |
| 95 | + + [ self.nodes[2].getnewaddress() for i in range(15) ] |
| 96 | + txid = self.nodes[2].sendmany(amounts={address: satoshi_round(Decimal(0.00025)) for address in addresses}) |
| 97 | + self.log.info(f"Sent many small UTXOs to nodes 1 and 2 in {txid}") |
| 98 | + self.nodes[2].generate(2) |
| 99 | + self.sync_all() |
| 100 | + |
| 101 | + self.log.info(f"Issuing some assets from node 1") |
| 102 | + # Try issuing assets |
| 103 | + amt = satoshi_round(Decimal(1)) |
| 104 | + res1 = self.nodes[1].issueasset(amt, amt, True) |
| 105 | + res2 = self.nodes[1].issueasset(amt, amt, False) |
| 106 | + |
| 107 | + assets = [ res1["asset"], res1["token"], res2["asset"], res2["token"] ] |
| 108 | + addresses = [ self.nodes[2].getnewaddress() for i in range(len(assets)) ] |
| 109 | + txid = self.nodes[1].sendmany( |
| 110 | + amounts={address: amt for address in addresses}, |
| 111 | + output_assets={addresses[i]: assets[i] for i in range(len(assets))}, |
| 112 | + ) |
| 113 | + self.log.info(f"Sent them to node 2 in {txid}") |
| 114 | + self.nodes[1].generate(2) |
| 115 | + self.sync_all() |
| 116 | + # Send them back |
| 117 | + addresses = [ self.nodes[1].getnewaddress() for i in range(len(assets)) ] |
| 118 | + txid = self.nodes[2].sendmany( |
| 119 | + amounts={address: amt for address in addresses}, |
| 120 | + output_assets={addresses[i]: assets[i] for i in range(len(assets))}, |
| 121 | + ) |
| 122 | + self.log.info(f"Sent them back to node 1 in {txid}") |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + WalletCtTest().main() |
| 126 | + |
0 commit comments