2016-05-06 11:23:48 +02:00
#!/usr/bin/env python3
2019-12-31 18:35:41 +01:00
# Copyright (c) 2014-2019 The Bitcoin Core developers
2015-02-04 02:59:41 +01:00
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
2019-01-07 10:55:35 +01:00
""" Test gettxoutproof and verifytxoutproof RPCs. """
2015-02-04 02:59:41 +01:00
2021-05-31 11:26:16 +02:00
from test_framework . blocktools import COINBASE_MATURITY
2021-06-24 12:47:04 +02:00
from test_framework . messages import (
CMerkleBlock ,
from_hex ,
)
2015-05-02 12:53:35 +02:00
from test_framework . test_framework import BitcoinTestFramework
2020-09-11 16:16:10 +02:00
from test_framework . wallet import MiniWallet
2021-06-24 12:47:04 +02:00
from test_framework . util import (
assert_equal ,
assert_raises_rpc_error ,
)
2015-02-04 02:59:41 +01:00
class MerkleBlockTest ( BitcoinTestFramework ) :
2017-09-01 18:47:13 +02:00
def set_test_params ( self ) :
2020-09-11 16:16:10 +02:00
self . num_nodes = 2
2017-09-01 18:47:13 +02:00
self . setup_clean_chain = True
2017-05-02 20:02:55 +02:00
# Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
2020-09-11 16:16:10 +02:00
self . extra_args = [
[ ] ,
[ " -txindex " ] ,
]
2018-09-13 12:33:15 +02:00
2015-02-04 02:59:41 +01:00
def run_test ( self ) :
2020-09-11 16:16:10 +02:00
miniwallet = MiniWallet ( self . nodes [ 0 ] )
# Add enough mature utxos to the wallet, so that all txs spend confirmed coins
miniwallet . generate ( 5 )
self . nodes [ 0 ] . generate ( COINBASE_MATURITY )
2015-02-04 02:59:41 +01:00
self . sync_all ( )
chain_height = self . nodes [ 1 ] . getblockcount ( )
2020-09-11 16:16:10 +02:00
assert_equal ( chain_height , 5 + COINBASE_MATURITY )
txid1 = miniwallet . send_self_transfer ( from_node = self . nodes [ 0 ] ) [ ' txid ' ]
txid2 = miniwallet . send_self_transfer ( from_node = self . nodes [ 0 ] ) [ ' txid ' ]
2017-06-14 15:36:56 +02:00
# This will raise an exception because the transaction is not yet in a block
2019-09-25 11:34:51 +02:00
assert_raises_rpc_error ( - 5 , " Transaction not yet in block " , self . nodes [ 0 ] . gettxoutproof , [ txid1 ] )
2015-02-04 02:59:41 +01:00
self . nodes [ 0 ] . generate ( 1 )
blockhash = self . nodes [ 0 ] . getblockhash ( chain_height + 1 )
self . sync_all ( )
txlist = [ ]
blocktxn = self . nodes [ 0 ] . getblock ( blockhash , True ) [ " tx " ]
txlist . append ( blocktxn [ 1 ] )
txlist . append ( blocktxn [ 2 ] )
2020-09-11 16:16:10 +02:00
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid1 ] ) ) , [ txid1 ] )
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid1 , txid2 ] ) ) , txlist )
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid1 , txid2 ] , blockhash ) ) , txlist )
2015-02-04 02:59:41 +01:00
2020-09-11 16:16:10 +02:00
txin_spent = miniwallet . get_utxo ( ) # Get the change from txid2
tx3 = miniwallet . send_self_transfer ( from_node = self . nodes [ 0 ] , utxo_to_spend = txin_spent )
txid3 = tx3 [ ' txid ' ]
2015-02-04 02:59:41 +01:00
self . nodes [ 0 ] . generate ( 1 )
self . sync_all ( )
txid_spent = txin_spent [ " txid " ]
2020-09-11 16:16:10 +02:00
txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent
2015-02-04 02:59:41 +01:00
2018-09-24 20:54:10 +02:00
# Invalid txids
2020-09-11 16:16:10 +02:00
assert_raises_rpc_error ( - 8 , " txid must be of length 64 (not 32, for ' 00000000000000000000000000000000 ' ) " , self . nodes [ 0 ] . gettxoutproof , [ " 00000000000000000000000000000000 " ] , blockhash )
assert_raises_rpc_error ( - 8 , " txid must be hexadecimal string (not ' ZZZ0000000000000000000000000000000000000000000000000000000000000 ' ) " , self . nodes [ 0 ] . gettxoutproof , [ " ZZZ0000000000000000000000000000000000000000000000000000000000000 " ] , blockhash )
2018-09-24 20:54:10 +02:00
# Invalid blockhashes
2020-09-11 16:16:10 +02:00
assert_raises_rpc_error ( - 8 , " blockhash must be of length 64 (not 32, for ' 00000000000000000000000000000000 ' ) " , self . nodes [ 0 ] . gettxoutproof , [ txid_spent ] , " 00000000000000000000000000000000 " )
assert_raises_rpc_error ( - 8 , " blockhash must be hexadecimal string (not ' ZZZ0000000000000000000000000000000000000000000000000000000000000 ' ) " , self . nodes [ 0 ] . gettxoutproof , [ txid_spent ] , " ZZZ0000000000000000000000000000000000000000000000000000000000000 " )
2017-09-07 17:59:00 +02:00
# We can't find the block from a fully-spent tx
2016-07-29 07:30:19 +02:00
# Doesn't apply to Dash Core - we have txindex always on
2019-09-25 11:34:51 +02:00
# assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[2].gettxoutproof, [txid_spent])
2017-06-14 15:36:56 +02:00
# We can get the proof if we specify the block
2020-09-11 16:16:10 +02:00
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid_spent ] , blockhash ) ) , [ txid_spent ] )
2017-06-14 15:36:56 +02:00
# We can't get the proof if we specify a non-existent block
2020-09-11 16:16:10 +02:00
assert_raises_rpc_error ( - 5 , " Block not found " , self . nodes [ 0 ] . gettxoutproof , [ txid_spent ] , " 0000000000000000000000000000000000000000000000000000000000000000 " )
2017-06-14 15:36:56 +02:00
# We can get the proof if the transaction is unspent
2020-09-11 16:16:10 +02:00
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid_unspent ] ) ) , [ txid_unspent ] )
2017-06-14 15:36:56 +02:00
# We can get the proof if we provide a list of transactions and one of them is unspent. The ordering of the list should not matter.
2020-09-11 16:16:10 +02:00
assert_equal ( sorted ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid1 , txid2 ] ) ) ) , sorted ( txlist ) )
assert_equal ( sorted ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 0 ] . gettxoutproof ( [ txid2 , txid1 ] ) ) ) , sorted ( txlist ) )
2017-06-14 15:36:56 +02:00
# We can always get a proof if we have a -txindex
2020-09-11 16:16:10 +02:00
assert_equal ( self . nodes [ 0 ] . verifytxoutproof ( self . nodes [ 1 ] . gettxoutproof ( [ txid_spent ] ) ) , [ txid_spent ] )
2017-06-14 15:36:56 +02:00
# We can't get a proof if we specify transactions from different blocks
2020-09-11 16:16:10 +02:00
assert_raises_rpc_error ( - 5 , " Not all transactions found in specified or retrieved block " , self . nodes [ 0 ] . gettxoutproof , [ txid1 , txid3 ] )
# Test empty list
2020-12-07 09:17:02 +01:00
assert_raises_rpc_error ( - 8 , " Parameter ' txids ' cannot be empty " , self . nodes [ 0 ] . gettxoutproof , [ ] )
2020-09-11 16:16:10 +02:00
# Test duplicate txid
assert_raises_rpc_error ( - 8 , ' Invalid parameter, duplicated txid ' , self . nodes [ 0 ] . gettxoutproof , [ txid1 , txid1 ] )
2017-06-14 15:36:56 +02:00
2018-07-09 20:23:46 +02:00
# Now we'll try tweaking a proof.
2020-09-11 16:16:10 +02:00
proof = self . nodes [ 1 ] . gettxoutproof ( [ txid1 , txid2 ] )
2018-07-09 20:23:46 +02:00
assert txid1 in self . nodes [ 0 ] . verifytxoutproof ( proof )
assert txid2 in self . nodes [ 1 ] . verifytxoutproof ( proof )
2021-06-24 12:47:04 +02:00
tweaked_proof = from_hex ( CMerkleBlock ( ) , proof )
2018-07-09 20:23:46 +02:00
# Make sure that our serialization/deserialization is working
2021-06-24 12:47:04 +02:00
assert txid1 in self . nodes [ 0 ] . verifytxoutproof ( tweaked_proof . serialize ( ) . hex ( ) )
2018-07-09 20:23:46 +02:00
# Check to see if we can go up the merkle tree and pass this off as a
# single-transaction block
tweaked_proof . txn . nTransactions = 1
tweaked_proof . txn . vHash = [ tweaked_proof . header . hashMerkleRoot ]
tweaked_proof . txn . vBits = [ True ] + [ False ] * 7
for n in self . nodes :
2021-06-24 12:47:04 +02:00
assert not n . verifytxoutproof ( tweaked_proof . serialize ( ) . hex ( ) )
2018-07-09 20:23:46 +02:00
# TODO: try more variants, eg transactions at different depths, and
# verify that the proofs are invalid
2015-02-04 02:59:41 +01:00
if __name__ == ' __main__ ' :
MerkleBlockTest ( ) . main ( )