2017-12-21 20:33:47 +01:00
#!/usr/bin/env python3
2016-04-05 21:53:38 +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 addressindex generation and fetching
#
from test_framework . test_framework import BitcoinTestFramework
2021-04-08 22:29:01 +02:00
from test_framework . test_node import ErrorMatch
2016-04-05 21:53:38 +02:00
from test_framework . util import *
from test_framework . script import *
from test_framework . mininode import *
import binascii
class SpentIndexTest ( BitcoinTestFramework ) :
2019-09-24 00:56:31 +02:00
def set_test_params ( self ) :
2018-04-18 13:48:59 +02:00
self . setup_clean_chain = True
self . num_nodes = 4
2016-04-05 21:53:38 +02:00
def setup_network ( self ) :
2019-09-24 00:56:31 +02:00
self . add_nodes ( self . num_nodes )
2016-04-05 21:53:38 +02:00
# Nodes 0/1 are "wallet" nodes
2019-09-24 00:56:31 +02:00
self . start_node ( 0 )
self . start_node ( 1 , [ " -spentindex " ] )
2016-04-05 21:53:38 +02:00
# Nodes 2/3 are used for testing
2019-09-24 00:56:31 +02:00
self . start_node ( 2 , [ " -spentindex " ] )
self . start_node ( 3 , [ " -spentindex " , " -txindex " ] )
2016-04-05 21:53:38 +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-10-31 18:30:42 +01:00
self . log . info ( " Test that settings can ' t be changed without -reindex... " )
self . stop_node ( 1 )
2021-04-08 22:29:01 +02:00
self . nodes [ 1 ] . assert_start_raises_init_error ( [ " -spentindex=0 " ] , " You need to rebuild the database using -reindex to change -spentindex " , match = ErrorMatch . PARTIAL_REGEX )
2019-10-31 18:30:42 +01:00
self . start_node ( 1 , [ " -spentindex=0 " , " -reindex " ] )
connect_nodes ( self . nodes [ 0 ] , 1 )
self . sync_all ( )
self . stop_node ( 1 )
2021-04-08 22:29:01 +02:00
self . nodes [ 1 ] . assert_start_raises_init_error ( [ " -spentindex " ] , " You need to rebuild the database using -reindex to change -spentindex " , match = ErrorMatch . PARTIAL_REGEX )
2019-10-31 18:30:42 +01:00
self . start_node ( 1 , [ " -spentindex " , " -reindex " ] )
connect_nodes ( self . nodes [ 0 ] , 1 )
self . sync_all ( )
2019-03-08 09:05:00 +01:00
self . log . info ( " Mining blocks... " )
2016-04-05 21:53:38 +02:00
self . nodes [ 0 ] . generate ( 105 )
self . sync_all ( )
chain_height = self . nodes [ 1 ] . getblockcount ( )
assert_equal ( chain_height , 105 )
# Check that
2019-03-08 09:05:00 +01:00
self . log . info ( " Testing spent index... " )
2016-04-05 21:53:38 +02:00
2016-07-19 08:36:56 +02:00
privkey = " cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc "
2017-12-21 20:33:47 +01:00
addressHash = binascii . unhexlify ( " C5E4FB9171C22409809A3E8047A29C83886E325D " )
2016-04-05 21:53:38 +02:00
scriptPubKey = CScript ( [ OP_DUP , OP_HASH160 , addressHash , OP_EQUALVERIFY , OP_CHECKSIG ] )
unspent = self . nodes [ 0 ] . listunspent ( )
tx = CTransaction ( )
2019-03-15 07:18:16 +01:00
tx_fee = Decimal ( ' 0.00001 ' )
tx_fee_sat = int ( tx_fee * COIN )
amount = int ( unspent [ 0 ] [ " amount " ] * COIN ) - tx_fee_sat
2016-04-05 21:53:38 +02:00
tx . vin = [ CTxIn ( COutPoint ( int ( unspent [ 0 ] [ " txid " ] , 16 ) , unspent [ 0 ] [ " vout " ] ) ) ]
tx . vout = [ CTxOut ( amount , scriptPubKey ) ]
tx . rehash ( )
2020-12-11 03:25:55 +01:00
signed_tx = self . nodes [ 0 ] . signrawtransactionwithwallet ( binascii . hexlify ( tx . serialize ( ) ) . decode ( " utf-8 " ) )
2019-03-15 07:18:16 +01:00
txid = self . nodes [ 0 ] . sendrawtransaction ( signed_tx [ " hex " ] , True )
2016-04-05 21:53:38 +02:00
self . nodes [ 0 ] . generate ( 1 )
self . sync_all ( )
2019-03-08 09:05:00 +01:00
self . log . info ( " Testing getspentinfo method... " )
2016-05-13 17:43:29 +02:00
2016-04-12 18:31:21 +02:00
# Check that the spentinfo works standalone
2016-04-05 21:53:38 +02:00
info = self . nodes [ 1 ] . getspentinfo ( { " txid " : unspent [ 0 ] [ " txid " ] , " index " : unspent [ 0 ] [ " vout " ] } )
assert_equal ( info [ " txid " ] , txid )
assert_equal ( info [ " index " ] , 0 )
2016-04-12 21:31:19 +02:00
assert_equal ( info [ " height " ] , 106 )
2016-04-05 21:53:38 +02:00
2019-03-08 09:05:00 +01:00
self . log . info ( " Testing getrawtransaction method... " )
2016-05-13 17:43:29 +02:00
2016-04-12 18:31:21 +02:00
# Check that verbose raw transaction includes spent info
txVerbose = self . nodes [ 3 ] . getrawtransaction ( unspent [ 0 ] [ " txid " ] , 1 )
assert_equal ( txVerbose [ " vout " ] [ unspent [ 0 ] [ " vout " ] ] [ " spentTxId " ] , txid )
assert_equal ( txVerbose [ " vout " ] [ unspent [ 0 ] [ " vout " ] ] [ " spentIndex " ] , 0 )
2016-04-12 21:31:19 +02:00
assert_equal ( txVerbose [ " vout " ] [ unspent [ 0 ] [ " vout " ] ] [ " spentHeight " ] , 106 )
2016-04-12 18:31:21 +02:00
2016-05-13 17:43:29 +02:00
# Check that verbose raw transaction includes input values
txVerbose2 = self . nodes [ 3 ] . getrawtransaction ( txid , 1 )
assert_equal ( txVerbose2 [ " vin " ] [ 0 ] [ " value " ] , Decimal ( unspent [ 0 ] [ " amount " ] ) )
2019-03-15 07:18:16 +01:00
assert_equal ( txVerbose2 [ " vin " ] [ 0 ] [ " valueSat " ] - tx_fee_sat , amount )
2016-05-13 17:43:29 +02:00
# Check that verbose raw transaction includes address values and input values
2016-07-19 08:36:56 +02:00
address2 = " yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3 "
2017-12-21 20:33:47 +01:00
addressHash2 = binascii . unhexlify ( " C5E4FB9171C22409809A3E8047A29C83886E325D " )
2016-05-13 17:43:29 +02:00
scriptPubKey2 = CScript ( [ OP_DUP , OP_HASH160 , addressHash2 , OP_EQUALVERIFY , OP_CHECKSIG ] )
tx2 = CTransaction ( )
tx2 . vin = [ CTxIn ( COutPoint ( int ( txid , 16 ) , 0 ) ) ]
2018-01-15 08:26:15 +01:00
tx2 . vout = [ CTxOut ( amount - int ( COIN / 10 ) , scriptPubKey2 ) ]
tx2 . rehash ( )
2016-05-13 17:43:29 +02:00
self . nodes [ 0 ] . importprivkey ( privkey )
2020-12-11 03:25:55 +01:00
signed_tx2 = self . nodes [ 0 ] . signrawtransactionwithwallet ( binascii . hexlify ( tx2 . serialize ( ) ) . decode ( " utf-8 " ) )
2019-03-15 07:18:16 +01:00
txid2 = self . nodes [ 0 ] . sendrawtransaction ( signed_tx2 [ " hex " ] , True )
2016-05-16 20:23:01 +02:00
# Check the mempool index
self . sync_all ( )
txVerbose3 = self . nodes [ 1 ] . getrawtransaction ( txid2 , 1 )
assert_equal ( txVerbose3 [ " vin " ] [ 0 ] [ " address " ] , address2 )
2019-03-15 07:18:16 +01:00
assert_equal ( txVerbose3 [ " vin " ] [ 0 ] [ " value " ] , Decimal ( unspent [ 0 ] [ " amount " ] ) - tx_fee )
2016-05-16 20:23:01 +02:00
assert_equal ( txVerbose3 [ " vin " ] [ 0 ] [ " valueSat " ] , amount )
# Check the database index
2016-05-13 17:43:29 +02:00
self . nodes [ 0 ] . generate ( 1 )
self . sync_all ( )
2016-05-16 20:23:01 +02:00
txVerbose4 = self . nodes [ 3 ] . getrawtransaction ( txid2 , 1 )
assert_equal ( txVerbose4 [ " vin " ] [ 0 ] [ " address " ] , address2 )
2019-03-15 07:18:16 +01:00
assert_equal ( txVerbose4 [ " vin " ] [ 0 ] [ " value " ] , Decimal ( unspent [ 0 ] [ " amount " ] ) - tx_fee )
2016-05-16 20:23:01 +02:00
assert_equal ( txVerbose4 [ " vin " ] [ 0 ] [ " valueSat " ] , amount )
2016-05-13 17:43:29 +02:00
2019-03-08 09:05:00 +01:00
self . log . info ( " Passed " )
2016-04-05 21:53:38 +02:00
if __name__ == ' __main__ ' :
SpentIndexTest ( ) . main ( )