|
| 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 | +"""Tests that fundrawtransaction correctly handles the case of sending many |
| 6 | +inputs of an issued asset, with no policy asset recipient. |
| 7 | +
|
| 8 | +See: https://github.com/ElementsProject/elements/issues/1259 |
| 9 | +
|
| 10 | +This test issues an asset and creates many utxos, which are then used as inputs in |
| 11 | +a consolidation transaction created with the various raw transaction calls. |
| 12 | +""" |
| 13 | +from decimal import Decimal |
| 14 | + |
| 15 | +from test_framework.blocktools import COINBASE_MATURITY |
| 16 | +from test_framework.test_framework import BitcoinTestFramework |
| 17 | +from test_framework.util import ( |
| 18 | + assert_equal, |
| 19 | +) |
| 20 | + |
| 21 | +class WalletTest(BitcoinTestFramework): |
| 22 | + def set_test_params(self): |
| 23 | + self.setup_clean_chain = True |
| 24 | + self.num_nodes = 3 |
| 25 | + self.extra_args = [[ |
| 26 | + "-blindedaddresses=1", |
| 27 | + "-initialfreecoins=2100000000000000", |
| 28 | + "-con_blocksubsidy=0", |
| 29 | + "-con_connect_genesis_outputs=1", |
| 30 | + "-txindex=1", |
| 31 | + ]] * self.num_nodes |
| 32 | + self.extra_args[0].append("-anyonecanspendaremine=1") |
| 33 | + |
| 34 | + def skip_test_if_missing_module(self): |
| 35 | + self.skip_if_no_wallet() |
| 36 | + |
| 37 | + def run_test(self): |
| 38 | + self.generate(self.nodes[0], COINBASE_MATURITY + 1) |
| 39 | + self.sync_all() |
| 40 | + |
| 41 | + self.log.info(f"Send some policy asset to node 1") |
| 42 | + self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 10) |
| 43 | + self.generate(self.nodes[0], 1) |
| 44 | + |
| 45 | + self.log.info(f"Issuing an asset from node 0") |
| 46 | + issuance = self.nodes[0].issueasset(1000, 1, True) |
| 47 | + self.generate(self.nodes[0], 1) |
| 48 | + asset = issuance["asset"] |
| 49 | + self.log.info(f"Asset ID is {asset}") |
| 50 | + |
| 51 | + # create many outputs of the new asset on node 1 |
| 52 | + num_utxos = 45 |
| 53 | + value = 10 |
| 54 | + fee_rate = 10 |
| 55 | + self.log.info(f"Sending {num_utxos} utxos of asset to node 1") |
| 56 | + for i in range(num_utxos): |
| 57 | + self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), value, "", "", False, False, None, None, None, asset, False, fee_rate, True) |
| 58 | + self.generate(self.nodes[0], 1) |
| 59 | + |
| 60 | + # create a raw tx on node 1 consolidating the asset utxos |
| 61 | + self.log.info(f"Create the raw consolidation transaction") |
| 62 | + hex = self.nodes[1].createrawtransaction([], [{ 'asset': asset, self.nodes[2].getnewaddress(): num_utxos * value }]) |
| 63 | + |
| 64 | + # fund the raw tx |
| 65 | + self.log.info(f"Fund the raw transaction") |
| 66 | + raw_tx = self.nodes[1].fundrawtransaction(hex, True) |
| 67 | + |
| 68 | + # blind and sign the tx |
| 69 | + self.log.info(f"Blind and sign the raw transaction") |
| 70 | + hex = self.nodes[1].blindrawtransaction(raw_tx['hex']) |
| 71 | + signed_tx = self.nodes[1].signrawtransactionwithwallet(hex) |
| 72 | + assert_equal(signed_tx['complete'], True) |
| 73 | + |
| 74 | + # decode tx |
| 75 | + tx = self.nodes[1].decoderawtransaction(signed_tx['hex']) |
| 76 | + |
| 77 | + assert_equal(len(tx['vin']), num_utxos + 1) |
| 78 | + assert_equal(len(tx['vout']), 3) |
| 79 | + assert_equal(tx['fee'], {'b2e15d0d7a0c94e4e2ce0fe6e8691b9e451377f6e46e8045a86f7c4b5d4f0f23': Decimal('0.00112380')}) # fee output |
| 80 | + |
| 81 | + # send and mine the tx |
| 82 | + self.log.info(f"Send the raw transaction") |
| 83 | + self.nodes[1].sendrawtransaction(signed_tx['hex']) |
| 84 | + self.generate(self.nodes[1], 1) |
| 85 | + self.sync_all() |
| 86 | + balance = self.nodes[2].getbalance() |
| 87 | + assert_equal(balance[asset], Decimal(num_utxos * value)) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + WalletTest().main() |
0 commit comments