2016-05-06 11:23:48 +02:00
#!/usr/bin/env python3
# Copyright (c) 2014-2016 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
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
from decimal import Decimal
2021-05-31 11:26:16 +02:00
from test_framework . blocktools import COINBASE_MATURITY
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
from test_framework . messages import CMerkleBlock , FromHex , ToHex
2015-05-02 12:53:35 +02:00
from test_framework . test_framework import BitcoinTestFramework
2022-09-24 14:36:35 +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 ) :
2016-05-20 15:16:51 +02:00
self . num_nodes = 4
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
self . extra_args = [ [ ] , [ ] , [ ] , [ " -txindex " ] ]
2015-02-04 02:59:41 +01:00
2018-09-13 12:33:15 +02:00
def skip_test_if_missing_module ( self ) :
self . skip_if_no_wallet ( )
2015-02-04 02:59:41 +01:00
def setup_network ( self ) :
2017-05-02 20:02:55 +02:00
self . setup_nodes ( )
2022-09-24 14:36:35 +02:00
self . connect_nodes ( 0 , 1 )
self . connect_nodes ( 0 , 2 )
self . connect_nodes ( 0 , 3 )
2015-02-04 02:59:41 +01:00
self . sync_all ( )
def run_test ( self ) :
2017-03-09 21:16:20 +01:00
self . log . info ( " Mining blocks... " )
2021-05-31 11:26:16 +02:00
self . nodes [ 0 ] . generate ( COINBASE_MATURITY + 5 )
2015-02-04 02:59:41 +01:00
self . sync_all ( )
chain_height = self . nodes [ 1 ] . getblockcount ( )
assert_equal ( chain_height , 105 )
assert_equal ( self . nodes [ 1 ] . getbalance ( ) , 0 )
assert_equal ( self . nodes [ 2 ] . getbalance ( ) , 0 )
2019-03-15 07:18:16 +01:00
tx_fee = Decimal ( ' 0.00001 ' )
2015-02-04 02:59:41 +01:00
node0utxos = self . nodes [ 0 ] . listunspent ( 1 )
2019-03-15 07:18:16 +01:00
tx1 = self . nodes [ 0 ] . createrawtransaction ( [ node0utxos . pop ( ) ] , { self . nodes [ 1 ] . getnewaddress ( ) : 500 - tx_fee } )
2018-02-20 03:29:22 +01:00
txid1 = self . nodes [ 0 ] . sendrawtransaction ( self . nodes [ 0 ] . signrawtransactionwithwallet ( tx1 ) [ " hex " ] )
2019-03-15 07:18:16 +01:00
tx2 = self . nodes [ 0 ] . createrawtransaction ( [ node0utxos . pop ( ) ] , { self . nodes [ 1 ] . getnewaddress ( ) : 500 - tx_fee } )
2018-02-20 03:29:22 +01:00
txid2 = self . nodes [ 0 ] . sendrawtransaction ( self . nodes [ 0 ] . signrawtransactionwithwallet ( tx2 ) [ " hex " ] )
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 ] )
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . gettxoutproof ( [ txid1 ] ) ) , [ txid1 ] )
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . gettxoutproof ( [ txid1 , txid2 ] ) ) , txlist )
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . gettxoutproof ( [ txid1 , txid2 ] , blockhash ) ) , txlist )
txin_spent = self . nodes [ 1 ] . listunspent ( 1 ) . pop ( )
2019-03-15 07:18:16 +01:00
tx3 = self . nodes [ 1 ] . createrawtransaction ( [ txin_spent ] , { self . nodes [ 0 ] . getnewaddress ( ) : 500 - tx_fee * 2 } )
2018-02-20 03:29:22 +01:00
txid3 = self . nodes [ 0 ] . sendrawtransaction ( self . nodes [ 1 ] . signrawtransactionwithwallet ( tx3 ) [ " hex " ] )
2015-02-04 02:59:41 +01:00
self . nodes [ 0 ] . generate ( 1 )
self . sync_all ( )
txid_spent = txin_spent [ " txid " ]
txid_unspent = txid1 if txin_spent [ " txid " ] != txid1 else txid2
2018-09-24 20:54:10 +02:00
# Invalid txids
assert_raises_rpc_error ( - 8 , " txid must be of length 64 (not 32, for ' 00000000000000000000000000000000 ' ) " , self . nodes [ 2 ] . gettxoutproof , [ " 00000000000000000000000000000000 " ] , blockhash )
assert_raises_rpc_error ( - 8 , " txid must be hexadecimal string (not ' ZZZ0000000000000000000000000000000000000000000000000000000000000 ' ) " , self . nodes [ 2 ] . gettxoutproof , [ " ZZZ0000000000000000000000000000000000000000000000000000000000000 " ] , blockhash )
# Invalid blockhashes
assert_raises_rpc_error ( - 8 , " blockhash must be of length 64 (not 32, for ' 00000000000000000000000000000000 ' ) " , self . nodes [ 2 ] . gettxoutproof , [ txid_spent ] , " 00000000000000000000000000000000 " )
assert_raises_rpc_error ( - 8 , " blockhash must be hexadecimal string (not ' ZZZ0000000000000000000000000000000000000000000000000000000000000 ' ) " , self . nodes [ 2 ] . 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
2015-02-04 02:59:41 +01:00
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . 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
2018-09-24 20:54:10 +02:00
assert_raises_rpc_error ( - 5 , " Block not found " , self . nodes [ 2 ] . gettxoutproof , [ txid_spent ] , " 0000000000000000000000000000000000000000000000000000000000000000 " )
2017-06-14 15:36:56 +02:00
# We can get the proof if the transaction is unspent
2015-02-04 02:59:41 +01:00
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . 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.
assert_equal ( sorted ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . gettxoutproof ( [ txid1 , txid2 ] ) ) ) , sorted ( txlist ) )
assert_equal ( sorted ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 2 ] . gettxoutproof ( [ txid2 , txid1 ] ) ) ) , sorted ( txlist ) )
# We can always get a proof if we have a -txindex
2015-02-04 02:59:41 +01:00
assert_equal ( self . nodes [ 2 ] . verifytxoutproof ( self . nodes [ 3 ] . 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
2019-09-25 11:34:51 +02:00
assert_raises_rpc_error ( - 5 , " Not all transactions found in specified or retrieved block " , self . nodes [ 2 ] . gettxoutproof , [ txid1 , txid3 ] )
2017-06-14 15:36:56 +02:00
2018-07-09 20:23:46 +02:00
# Now we'll try tweaking a proof.
proof = self . nodes [ 3 ] . gettxoutproof ( [ txid1 , txid2 ] )
assert txid1 in self . nodes [ 0 ] . verifytxoutproof ( proof )
assert txid2 in self . nodes [ 1 ] . verifytxoutproof ( proof )
tweaked_proof = FromHex ( CMerkleBlock ( ) , proof )
# Make sure that our serialization/deserialization is working
assert txid1 in self . nodes [ 2 ] . verifytxoutproof ( ToHex ( tweaked_proof ) )
# 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 :
assert not n . verifytxoutproof ( ToHex ( tweaked_proof ) )
# 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 ( )