mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 21:12:48 +01:00
8db2aabee0
bfa9309ad606102f24c9bd3c33dfe78949f09418 Use COINBASE_MATURITY constant in functional tests. (Kiminuo) 525448df9dc2ab6b7e960ff138956ae3e2efdf60 Move COINBASE_MATURITY from `feature_nulldummy` test to `blocktools`. (Kiminuo) Pull request description: `COINBASE_MATURITY` constant was added to `feature_nulldummy` test in #21373. This PR moves the constant to `blocktools.py` file and uses the constant in more tests as suggested [here](https://github.com/bitcoin/bitcoin/pull/21373#discussion_r605418462). Edit: Goal of this PR is to replace integer constants with `COINBASE_MATURITY` but not necessarily in *all* cases because that would mean to read and fully understand all tests. That's out of my time constraints. Any reports where `COINBASE_MATURITY` should be used are welcome though! ACKs for top commit: theStack: ACK bfa9309ad606102f24c9bd3c33dfe78949f09418 🌇 Tree-SHA512: 01f04645f05a39028681f355cf3d42dd63ea3303f76d93c430e0fdce441934358a2d847a54e6068d61932f1b75e1d406f51859b057b3e4b569f7083915cb317f
120 lines
6.6 KiB
Python
Executable File
120 lines
6.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2016 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 gettxoutproof and verifytxoutproof RPCs."""
|
|
|
|
from decimal import Decimal
|
|
|
|
from test_framework.blocktools import COINBASE_MATURITY
|
|
from test_framework.messages import CMerkleBlock, FromHex, ToHex
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import assert_equal, assert_raises_rpc_error
|
|
|
|
class MerkleBlockTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 4
|
|
self.setup_clean_chain = True
|
|
# Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
|
|
self.extra_args = [[], [], [], ["-txindex"]]
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
|
|
def setup_network(self):
|
|
self.setup_nodes()
|
|
self.connect_nodes(0, 1)
|
|
self.connect_nodes(0, 2)
|
|
self.connect_nodes(0, 3)
|
|
|
|
self.sync_all()
|
|
|
|
def run_test(self):
|
|
self.log.info("Mining blocks...")
|
|
self.nodes[0].generate(COINBASE_MATURITY + 5)
|
|
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)
|
|
|
|
tx_fee = Decimal('0.00001')
|
|
node0utxos = self.nodes[0].listunspent(1)
|
|
tx1 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 500 - tx_fee})
|
|
txid1 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx1)["hex"])
|
|
tx2 = self.nodes[0].createrawtransaction([node0utxos.pop()], {self.nodes[1].getnewaddress(): 500 - tx_fee})
|
|
txid2 = self.nodes[0].sendrawtransaction(self.nodes[0].signrawtransactionwithwallet(tx2)["hex"])
|
|
# This will raise an exception because the transaction is not yet in a block
|
|
assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[0].gettxoutproof, [txid1])
|
|
|
|
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()
|
|
tx3 = self.nodes[1].createrawtransaction([txin_spent], {self.nodes[0].getnewaddress(): 500 - tx_fee*2})
|
|
txid3 = self.nodes[0].sendrawtransaction(self.nodes[1].signrawtransactionwithwallet(tx3)["hex"])
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
txid_spent = txin_spent["txid"]
|
|
txid_unspent = txid1 if txin_spent["txid"] != txid1 else txid2
|
|
|
|
# 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")
|
|
# We can't find the block from a fully-spent tx
|
|
# Doesn't apply to Dash Core - we have txindex always on
|
|
# assert_raises_rpc_error(-5, "Transaction not yet in block", self.nodes[2].gettxoutproof, [txid_spent])
|
|
# We can get the proof if we specify the block
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_spent], blockhash)), [txid_spent])
|
|
# We can't get the proof if we specify a non-existent block
|
|
assert_raises_rpc_error(-5, "Block not found", self.nodes[2].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000")
|
|
# We can get the proof if the transaction is unspent
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[2].gettxoutproof([txid_unspent])), [txid_unspent])
|
|
# 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
|
|
assert_equal(self.nodes[2].verifytxoutproof(self.nodes[3].gettxoutproof([txid_spent])), [txid_spent])
|
|
# We can't get a proof if we specify transactions from different blocks
|
|
assert_raises_rpc_error(-5, "Not all transactions found in specified or retrieved block", self.nodes[2].gettxoutproof, [txid1, txid3])
|
|
|
|
# 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
|
|
|
|
if __name__ == '__main__':
|
|
MerkleBlockTest().main()
|