2017-12-21 20:33:47 +01:00
|
|
|
#!/usr/bin/env python3
|
2016-05-12 22:16:44 +02:00
|
|
|
# Copyright (c) 2014-2015 The Bitcoin Core developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test txindex generation and fetching
|
|
|
|
#
|
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
from test_framework.util import *
|
|
|
|
from test_framework.script import *
|
|
|
|
from test_framework.mininode import *
|
|
|
|
import binascii
|
|
|
|
|
|
|
|
class TxIndexTest(BitcoinTestFramework):
|
|
|
|
|
2018-04-18 13:48:59 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.setup_clean_chain = True
|
|
|
|
self.num_nodes = 4
|
2016-05-12 22:16:44 +02:00
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
self.nodes = []
|
|
|
|
# Nodes 0/1 are "wallet" nodes
|
2019-03-08 09:05:00 +01:00
|
|
|
self.nodes.append(start_node(0, self.options.tmpdir))
|
|
|
|
self.nodes.append(start_node(1, self.options.tmpdir, ["-txindex"]))
|
2016-05-12 22:16:44 +02:00
|
|
|
# Nodes 2/3 are used for testing
|
2019-03-08 09:05:00 +01:00
|
|
|
self.nodes.append(start_node(2, self.options.tmpdir, ["-txindex"]))
|
|
|
|
self.nodes.append(start_node(3, self.options.tmpdir, ["-txindex"]))
|
2016-05-12 22:16:44 +02:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
connect_nodes(self.nodes[0], 3)
|
|
|
|
|
|
|
|
self.is_network_split = False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def run_test(self):
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Mining blocks...")
|
2016-05-12 22:16:44 +02:00
|
|
|
self.nodes[0].generate(105)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
|
|
assert_equal(chain_height, 105)
|
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Testing transaction index...")
|
2016-05-12 22:16:44 +02:00
|
|
|
|
2016-07-19 08:36:56 +02:00
|
|
|
privkey = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
|
|
|
|
address = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
|
2019-05-21 10:03:15 +02:00
|
|
|
addressHash = binascii.unhexlify("C5E4FB9171C22409809A3E8047A29C83886E325D")
|
2016-05-12 22:16:44 +02:00
|
|
|
scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
|
|
|
|
unspent = self.nodes[0].listunspent()
|
|
|
|
tx = CTransaction()
|
2019-05-21 10:03:15 +02:00
|
|
|
tx_fee_sat = 1000
|
|
|
|
amount = int(unspent[0]["amount"] * 100000000) - tx_fee_sat
|
2016-05-12 22:16:44 +02:00
|
|
|
tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
|
|
|
|
tx.vout = [CTxOut(amount, scriptPubKey)]
|
|
|
|
tx.rehash()
|
|
|
|
|
|
|
|
signed_tx = self.nodes[0].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8"))
|
|
|
|
txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], True)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Check verbose raw transaction results
|
2019-05-21 10:03:15 +02:00
|
|
|
verbose = self.nodes[3].getrawtransaction(txid, 1)
|
|
|
|
assert_equal(verbose["vout"][0]["valueSat"], 50000000000 - tx_fee_sat);
|
|
|
|
assert_equal(verbose["vout"][0]["value"] * 100000000, 50000000000 - tx_fee_sat);
|
2016-05-12 22:16:44 +02:00
|
|
|
|
2019-03-08 09:05:00 +01:00
|
|
|
self.log.info("Passed")
|
2016-05-12 22:16:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
TxIndexTest().main()
|