2019-12-04 07:06:05 -08:00
|
|
|
#!/usr/bin/env python3
|
2019-05-07 15:18:06 -07:00
|
|
|
# Copyright (c) 2019 The Zcash developers
|
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2019-05-07 15:18:06 -07:00
|
|
|
|
|
|
|
#
|
|
|
|
# Test the new fields added to the output of getrawtransaction
|
|
|
|
# RPC for the Insight Explorer by the new spentindex
|
|
|
|
#
|
|
|
|
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
|
|
|
|
|
|
from test_framework.util import assert_equal
|
|
|
|
from test_framework.util import start_nodes, stop_nodes, connect_nodes
|
|
|
|
from test_framework.util import wait_bitcoinds
|
|
|
|
|
|
|
|
from test_framework.mininode import COIN
|
|
|
|
|
|
|
|
|
|
|
|
class GetrawtransactionTest(BitcoinTestFramework):
|
|
|
|
|
2016-05-14 04:01:31 -07:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.num_nodes = 3
|
2022-06-30 22:17:58 -07:00
|
|
|
self.cache_behavior = 'clean'
|
2019-05-07 15:18:06 -07:00
|
|
|
|
|
|
|
def setup_network(self):
|
|
|
|
# -insightexplorer causes spentindex to be enabled (fSpentIndex = true)
|
|
|
|
|
2016-05-14 04:01:31 -07:00
|
|
|
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
|
|
|
|
[['-debug', '-txindex', '-experimentalfeatures', '-insightexplorer']] * self.num_nodes)
|
2019-05-07 15:18:06 -07:00
|
|
|
connect_nodes(self.nodes[0], 1)
|
|
|
|
connect_nodes(self.nodes[0], 2)
|
|
|
|
|
|
|
|
self.is_network_split = False
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
def run_test(self):
|
|
|
|
self.nodes[0].generate(105)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
|
|
assert_equal(chain_height, 105)
|
|
|
|
|
|
|
|
# Test getrawtransaction changes and the getspentinfo RPC
|
|
|
|
|
|
|
|
# send coinbase to address a
|
|
|
|
a = self.nodes[1].getnewaddress()
|
|
|
|
txid_a = self.nodes[0].sendtoaddress(a, 2)
|
|
|
|
self.sync_all()
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# send from a to b
|
|
|
|
# (the only utxo on node 1 is from address a)
|
|
|
|
b = self.nodes[2].getnewaddress()
|
|
|
|
txid_b = self.nodes[1].sendtoaddress(b, 1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# a to b transaction is not confirmed, so it has no height
|
|
|
|
tx_b = self.nodes[2].getrawtransaction(txid_b, 1)
|
|
|
|
assert('height' not in tx_b)
|
|
|
|
|
|
|
|
self.sync_all()
|
|
|
|
tx_a = self.nodes[2].getrawtransaction(txid_a, 1)
|
|
|
|
|
2019-08-07 13:23:42 -07:00
|
|
|
# txid_b is not yet confirmed, so height is invalid (-1)
|
2019-12-04 07:06:05 -08:00
|
|
|
vout = list(filter(lambda o: o['value'] == 2, tx_a['vout']))
|
2019-08-07 13:23:42 -07:00
|
|
|
assert_equal(vout[0]['spentTxId'], txid_b)
|
|
|
|
assert_equal(vout[0]['spentIndex'], 0)
|
|
|
|
assert_equal(vout[0]['spentHeight'], -1)
|
2019-05-07 15:18:06 -07:00
|
|
|
|
|
|
|
# confirm txid_b (a to b transaction)
|
|
|
|
self.nodes[0].generate(1)
|
|
|
|
self.sync_all()
|
|
|
|
|
|
|
|
# Restart all nodes to ensure index files are saved to disk and recovered
|
|
|
|
stop_nodes(self.nodes)
|
|
|
|
wait_bitcoinds()
|
|
|
|
self.setup_network()
|
|
|
|
|
|
|
|
# Check new fields added to getrawtransaction
|
|
|
|
tx_a = self.nodes[2].getrawtransaction(txid_a, 1)
|
|
|
|
assert_equal(tx_a['vin'][0]['value'], 10) # coinbase
|
|
|
|
assert_equal(tx_a['vin'][0]['valueSat'], 10*COIN)
|
|
|
|
# we want the non-change (payment) output
|
2019-12-04 07:06:05 -08:00
|
|
|
vout = list(filter(lambda o: o['value'] == 2, tx_a['vout']))
|
2019-05-07 15:18:06 -07:00
|
|
|
assert_equal(vout[0]['spentTxId'], txid_b)
|
|
|
|
assert_equal(vout[0]['spentIndex'], 0)
|
|
|
|
assert_equal(vout[0]['spentHeight'], 107)
|
|
|
|
assert_equal(tx_a['height'], 106)
|
|
|
|
|
|
|
|
tx_b = self.nodes[2].getrawtransaction(txid_b, 1)
|
|
|
|
assert_equal(tx_b['vin'][0]['address'], a)
|
|
|
|
assert_equal(tx_b['vin'][0]['value'], 2)
|
|
|
|
assert_equal(tx_b['vin'][0]['valueSat'], 2*COIN)
|
|
|
|
# since this transaction's outputs haven't yet been
|
|
|
|
# spent, these fields should not be present
|
|
|
|
assert('spentTxId' not in tx_b['vout'][0])
|
|
|
|
assert('spentIndex' not in tx_b['vout'][0])
|
|
|
|
assert('spentHeight' not in tx_b['vout'][0])
|
|
|
|
assert_equal(tx_b['height'], 107)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
GetrawtransactionTest().main()
|