|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014-2015 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 | +# |
| 7 | +# Test txindex generation and fetching |
| 8 | +# |
| 9 | + |
| 10 | +import time |
| 11 | +from test_framework.test_framework import BitcoinTestFramework |
| 12 | +from test_framework.util import * |
| 13 | +from test_framework.script import * |
| 14 | +from test_framework.mininode import * |
| 15 | +import binascii |
| 16 | + |
| 17 | +class TxIndexTest(BitcoinTestFramework): |
| 18 | + |
| 19 | + def setup_chain(self): |
| 20 | + print("Initializing test directory "+self.options.tmpdir) |
| 21 | + initialize_chain_clean(self.options.tmpdir, 4) |
| 22 | + |
| 23 | + def setup_network(self): |
| 24 | + self.nodes = [] |
| 25 | + # Nodes 0/1 are "wallet" nodes |
| 26 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"])) |
| 27 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-debug", "-txindex"])) |
| 28 | + # Nodes 2/3 are used for testing |
| 29 | + self.nodes.append(start_node(2, self.options.tmpdir, ["-debug", "-txindex"])) |
| 30 | + self.nodes.append(start_node(3, self.options.tmpdir, ["-debug", "-txindex"])) |
| 31 | + connect_nodes(self.nodes[0], 1) |
| 32 | + connect_nodes(self.nodes[0], 2) |
| 33 | + connect_nodes(self.nodes[0], 3) |
| 34 | + |
| 35 | + self.is_network_split = False |
| 36 | + self.sync_all() |
| 37 | + |
| 38 | + def run_test(self): |
| 39 | + print "Mining blocks..." |
| 40 | + self.nodes[0].generate(105) |
| 41 | + self.sync_all() |
| 42 | + |
| 43 | + chain_height = self.nodes[1].getblockcount() |
| 44 | + assert_equal(chain_height, 105) |
| 45 | + |
| 46 | + print "Testing transaction index..." |
| 47 | + |
| 48 | + privkey = "cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG" |
| 49 | + address = "mgY65WSfEmsyYaYPQaXhmXMeBhwp4EcsQW" |
| 50 | + addressHash = "0b2f0a0c31bfe0406b0ccc1381fdbe311946dadc".decode("hex") |
| 51 | + scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG]) |
| 52 | + unspent = self.nodes[0].listunspent() |
| 53 | + tx = CTransaction() |
| 54 | + amount = unspent[0]["amount"] * 100000000 |
| 55 | + tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))] |
| 56 | + tx.vout = [CTxOut(amount, scriptPubKey)] |
| 57 | + tx.rehash() |
| 58 | + |
| 59 | + signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8")) |
| 60 | + txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], True) |
| 61 | + self.nodes[0].generate(1) |
| 62 | + self.sync_all() |
| 63 | + |
| 64 | + # Check verbose raw transaction results |
| 65 | + verbose = self.nodes[3].getrawtransaction(unspent[0]["txid"], 1) |
| 66 | + assert_equal(verbose["vout"][0]["valueSat"], 5000000000); |
| 67 | + assert_equal(verbose["vout"][0]["value"], 50); |
| 68 | + |
| 69 | + print "Passed\n" |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + TxIndexTest().main() |
0 commit comments