|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014-2016 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 | + |
| 6 | +from test_framework.test_framework import BitcoinTestFramework |
| 7 | +from test_framework.util import * |
| 8 | +import decimal |
| 9 | + |
| 10 | +class ImportPrunedFundsTest(BitcoinTestFramework): |
| 11 | + |
| 12 | + def setup_chain(self): |
| 13 | + print("Initializing test directory "+self.options.tmpdir) |
| 14 | + initialize_chain_clean(self.options.tmpdir, 4) |
| 15 | + |
| 16 | + def setup_network(self, split=False): |
| 17 | + self.nodes = start_nodes(2, self.options.tmpdir) |
| 18 | + connect_nodes_bi(self.nodes,0,1) |
| 19 | + self.is_network_split=False |
| 20 | + self.sync_all() |
| 21 | + |
| 22 | + def run_test (self): |
| 23 | + import time |
| 24 | + begintime = int(time.time()) |
| 25 | + |
| 26 | + print "Mining blocks..." |
| 27 | + self.nodes[0].generate(101) |
| 28 | + |
| 29 | + # sync |
| 30 | + self.sync_all() |
| 31 | + |
| 32 | + # address |
| 33 | + address1 = self.nodes[0].getnewaddress() |
| 34 | + # pubkey |
| 35 | + address2 = self.nodes[0].getnewaddress() |
| 36 | + address2_pubkey = self.nodes[0].validateaddress(address2)['pubkey'] # Using pubkey |
| 37 | + # privkey |
| 38 | + address3 = self.nodes[0].getnewaddress() |
| 39 | + address3_privkey = self.nodes[0].dumpprivkey(address3) # Using privkey |
| 40 | + |
| 41 | + #Check only one address |
| 42 | + address_info = self.nodes[0].validateaddress(address1) |
| 43 | + assert_equal(address_info['ismine'], True) |
| 44 | + |
| 45 | + self.sync_all() |
| 46 | + |
| 47 | + #Node 1 sync test |
| 48 | + assert_equal(self.nodes[1].getblockcount(),101) |
| 49 | + |
| 50 | + #Address Test - before import |
| 51 | + address_info = self.nodes[1].validateaddress(address1) |
| 52 | + assert_equal(address_info['iswatchonly'], False) |
| 53 | + assert_equal(address_info['ismine'], False) |
| 54 | + |
| 55 | + address_info = self.nodes[1].validateaddress(address2) |
| 56 | + assert_equal(address_info['iswatchonly'], False) |
| 57 | + assert_equal(address_info['ismine'], False) |
| 58 | + |
| 59 | + address_info = self.nodes[1].validateaddress(address3) |
| 60 | + assert_equal(address_info['iswatchonly'], False) |
| 61 | + assert_equal(address_info['ismine'], False) |
| 62 | + |
| 63 | + #Send funds to self |
| 64 | + txnid1 = self.nodes[0].sendtoaddress(address1, 0.1) |
| 65 | + self.nodes[0].generate(1) |
| 66 | + rawtxn1 = self.nodes[0].gettransaction(txnid1)['hex'] |
| 67 | + proof1 = self.nodes[0].gettxoutproof([txnid1]) |
| 68 | + |
| 69 | + txnid2 = self.nodes[0].sendtoaddress(address2, 0.05) |
| 70 | + self.nodes[0].generate(1) |
| 71 | + rawtxn2 = self.nodes[0].gettransaction(txnid2)['hex'] |
| 72 | + proof2 = self.nodes[0].gettxoutproof([txnid2]) |
| 73 | + |
| 74 | + |
| 75 | + txnid3 = self.nodes[0].sendtoaddress(address3, 0.025) |
| 76 | + self.nodes[0].generate(1) |
| 77 | + rawtxn3 = self.nodes[0].gettransaction(txnid3)['hex'] |
| 78 | + proof3 = self.nodes[0].gettxoutproof([txnid3]) |
| 79 | + |
| 80 | + self.sync_all() |
| 81 | + |
| 82 | + #Import with no affiliated address |
| 83 | + try: |
| 84 | + result1 = self.nodes[1].importprunedfunds(rawtxn1, proof1, "") |
| 85 | + except JSONRPCException,e: |
| 86 | + errorString = e.error['message'] |
| 87 | + |
| 88 | + assert('No addresses' in errorString) |
| 89 | + |
| 90 | + balance1 = self.nodes[1].getbalance("", 0, True) |
| 91 | + assert_equal(balance1, Decimal(0)) |
| 92 | + |
| 93 | + #Import with affiliated address with no rescan |
| 94 | + self.nodes[1].importaddress(address2, "", False) |
| 95 | + result2 = self.nodes[1].importprunedfunds(rawtxn2, proof2, "") |
| 96 | + balance2 = Decimal(self.nodes[1].getbalance("", 0, True)) |
| 97 | + assert_equal(balance2, Decimal('0.05')) |
| 98 | + |
| 99 | + #Import with private key with no rescan |
| 100 | + self.nodes[1].importprivkey(address3_privkey, "", False) |
| 101 | + result3 = self.nodes[1].importprunedfunds(rawtxn3, proof3, "") |
| 102 | + balance3 = Decimal(self.nodes[1].getbalance("", 0, False)) |
| 103 | + assert_equal(balance3, Decimal('0.025')) |
| 104 | + balance3 = Decimal(self.nodes[1].getbalance("", 0, True)) |
| 105 | + assert_equal(balance3, Decimal('0.075')) |
| 106 | + |
| 107 | + #Addresses Test - after import |
| 108 | + address_info = self.nodes[1].validateaddress(address1) |
| 109 | + assert_equal(address_info['iswatchonly'], False) |
| 110 | + assert_equal(address_info['ismine'], False) |
| 111 | + address_info = self.nodes[1].validateaddress(address2) |
| 112 | + assert_equal(address_info['iswatchonly'], True) |
| 113 | + assert_equal(address_info['ismine'], False) |
| 114 | + address_info = self.nodes[1].validateaddress(address3) |
| 115 | + assert_equal(address_info['iswatchonly'], False) |
| 116 | + assert_equal(address_info['ismine'], True) |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + ImportPrunedFundsTest ().main () |
0 commit comments