|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2016 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +from decimal import Decimal |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | +from test_framework.util import ( |
| 9 | + assert_equal, |
| 10 | +) |
| 11 | + |
| 12 | +class CTTest(BitcoinTestFramework): |
| 13 | + def set_test_params(self): |
| 14 | + self.num_nodes = 3 |
| 15 | + self.setup_clean_chain = True |
| 16 | + args = [ |
| 17 | + "-anyonecanspendaremine=1", |
| 18 | + "-con_blocksubsidy=0", |
| 19 | + "-con_connect_genesis_outputs=1", |
| 20 | + "-initialfreecoins=2100000000000000", |
| 21 | + "-txindex=1", |
| 22 | + ] |
| 23 | + self.extra_args = [ |
| 24 | + # node 0 does not accept nor create discounted CTs |
| 25 | + args + ["-acceptdiscountct=0", "-creatediscountct=0"], |
| 26 | + # node 1 accepts but does not create discounted CTs |
| 27 | + args + ["-acceptdiscountct=1", "-creatediscountct=0"], |
| 28 | + # node 2 both accepts and creates discounted CTs |
| 29 | + args + ["-acceptdiscountct=1", "-creatediscountct=1"], |
| 30 | + ] |
| 31 | + |
| 32 | + def skip_test_if_missing_module(self): |
| 33 | + self.skip_if_no_wallet() |
| 34 | + |
| 35 | + def run_test(self): |
| 36 | + feerate = 1.0 |
| 37 | + |
| 38 | + node0 = self.nodes[0] |
| 39 | + node1 = self.nodes[1] |
| 40 | + node2 = self.nodes[2] |
| 41 | + |
| 42 | + self.generate(node0, 101) |
| 43 | + balance = node0.getbalance() |
| 44 | + assert_equal(balance['bitcoin'], 21000000) |
| 45 | + |
| 46 | + self.log.info("Create UTXOs") |
| 47 | + many = {} |
| 48 | + num = 25 |
| 49 | + for i in range(num): |
| 50 | + addr = node0.getnewaddress() |
| 51 | + info = node0.getaddressinfo(addr) |
| 52 | + many[info['unconfidential']] = 1 |
| 53 | + for i in range(10): |
| 54 | + addr = node1.getnewaddress() |
| 55 | + info = node1.getaddressinfo(addr) |
| 56 | + many[info['unconfidential']] = 1 |
| 57 | + for i in range(10): |
| 58 | + addr = node2.getnewaddress() |
| 59 | + info = node2.getaddressinfo(addr) |
| 60 | + many[info['unconfidential']] = 1 |
| 61 | + |
| 62 | + txid = node0.sendmany("", many) |
| 63 | + self.generate(node0, 1) |
| 64 | + |
| 65 | + self.log.info("Send explicit tx to node 0") |
| 66 | + addr = node0.getnewaddress() |
| 67 | + info = node0.getaddressinfo(addr) |
| 68 | + txid = node0.sendtoaddress(info['unconfidential'], 1.0, "", "", False, None, None, None, None, None, None, feerate) |
| 69 | + tx = node0.gettransaction(txid, True, True) |
| 70 | + decoded = tx['decoded'] |
| 71 | + vin = decoded['vin'] |
| 72 | + vout = decoded['vout'] |
| 73 | + assert_equal(len(vin), 2) |
| 74 | + assert_equal(len(vout), 3) |
| 75 | + assert_equal(tx['fee']['bitcoin'], Decimal('-0.00000326')) |
| 76 | + assert_equal(decoded['vsize'], 326) |
| 77 | + self.generate(node0, 1) |
| 78 | + tx = node1.getrawtransaction(txid, True) |
| 79 | + assert_equal(tx['discountvsize'], 326) |
| 80 | + |
| 81 | + self.log.info("Send confidential tx to node 0") |
| 82 | + addr = node0.getnewaddress() |
| 83 | + info = node0.getaddressinfo(addr) |
| 84 | + txid = node0.sendtoaddress(info['confidential'], 1.0, "", "", False, None, None, None, None, None, None, feerate) |
| 85 | + tx = node0.gettransaction(txid, True, True) |
| 86 | + decoded = tx['decoded'] |
| 87 | + vin = decoded['vin'] |
| 88 | + vout = decoded['vout'] |
| 89 | + assert_equal(len(vin), 2) |
| 90 | + assert_equal(len(vout), 3) |
| 91 | + assert_equal(tx['fee']['bitcoin'], Decimal('-0.00002575')) |
| 92 | + assert_equal(decoded['vsize'], 2575) |
| 93 | + self.generate(node0, 1) |
| 94 | + tx = node1.getrawtransaction(txid, True) |
| 95 | + assert_equal(tx['discountvsize'], 410) # node1 has discountvsize |
| 96 | + |
| 97 | + self.log.info("Send explicit tx to node 1") |
| 98 | + addr = node1.getnewaddress() |
| 99 | + info = node1.getaddressinfo(addr) |
| 100 | + txid = node0.sendtoaddress(info['unconfidential'], 1.0, "", "", False, None, None, None, None, None, None, feerate) |
| 101 | + tx = node0.gettransaction(txid, True, True) |
| 102 | + decoded = tx['decoded'] |
| 103 | + vin = decoded['vin'] |
| 104 | + vout = decoded['vout'] |
| 105 | + assert_equal(len(vin), 2) |
| 106 | + assert_equal(len(vout), 3) |
| 107 | + assert_equal(tx['fee']['bitcoin'], Decimal('-0.00000326')) |
| 108 | + assert_equal(decoded['vsize'], 326) |
| 109 | + self.generate(node0, 1) |
| 110 | + tx = node1.getrawtransaction(txid, True) |
| 111 | + assert_equal(tx['discountvsize'], 326) |
| 112 | + |
| 113 | + self.log.info("Send confidential (undiscounted) tx to node 1") |
| 114 | + addr = node1.getnewaddress() |
| 115 | + info = node1.getaddressinfo(addr) |
| 116 | + txid = node0.sendtoaddress(info['confidential'], 1.0, "", "", False, None, None, None, None, None, None, feerate) |
| 117 | + tx = node0.gettransaction(txid, True, True) |
| 118 | + decoded = tx['decoded'] |
| 119 | + vin = decoded['vin'] |
| 120 | + vout = decoded['vout'] |
| 121 | + assert_equal(len(vin), 2) |
| 122 | + assert_equal(len(vout), 3) |
| 123 | + assert_equal(tx['fee']['bitcoin'], Decimal('-0.00002575')) |
| 124 | + assert_equal(decoded['vsize'], 2575) |
| 125 | + self.generate(node0, 1) |
| 126 | + tx = node1.getrawtransaction(txid, True) |
| 127 | + assert_equal(tx['discountvsize'], 410) # node1 has discountvsize |
| 128 | + |
| 129 | + self.log.info("Send confidential (discounted) tx to node 1") |
| 130 | + bitcoin = 'b2e15d0d7a0c94e4e2ce0fe6e8691b9e451377f6e46e8045a86f7c4b5d4f0f23' |
| 131 | + addr = node1.getnewaddress() |
| 132 | + info = node1.getaddressinfo(addr) |
| 133 | + txid = node2.sendtoaddress(info['confidential'], 1.0, "", "", False, None, None, None, None, None, None, feerate) |
| 134 | + # node0 won't accept or relay the tx |
| 135 | + self.sync_mempools([node1, node2]) |
| 136 | + assert_equal(node0.getrawmempool(), []) |
| 137 | + self.generate(node2, 1, sync_fun=self.sync_blocks) |
| 138 | + for node in [node2, node1]: |
| 139 | + tx = node.gettransaction(txid, True, True) |
| 140 | + decoded = tx['decoded'] |
| 141 | + vin = decoded['vin'] |
| 142 | + vout = decoded['vout'] |
| 143 | + assert_equal(len(vin), 2) |
| 144 | + assert_equal(len(vout), 3) |
| 145 | + if 'bitcoin' in decoded['fee']: |
| 146 | + assert_equal(decoded['fee']['bitcoin'], Decimal('-0.00000410')) |
| 147 | + else: |
| 148 | + assert_equal(decoded['fee'][bitcoin], Decimal('0.00000410')) |
| 149 | + assert_equal(decoded['vsize'], 2575) |
| 150 | + assert_equal(decoded['discountvsize'], 410) |
| 151 | + |
| 152 | + # node0 only has vsize |
| 153 | + tx = node0.getrawtransaction(txid, True) |
| 154 | + assert_equal(tx['vsize'], 2575) |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + CTTest().main() |
0 commit comments