mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
57b6fe7327
bdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb test: doc: improve doc for `from_hex` helper (mention `to_hex` alternative) (Sebastian Falbesoner) 191405420815d49ab50184513717a303fc2744d6 scripted-diff: test: rename `FromHex` to `from_hex` (Sebastian Falbesoner) a79396fe5f8f81c78cf84117a87074c6ff6c9d95 test: remove `ToHex` helper, use .serialize().hex() instead (Sebastian Falbesoner) 2ce7b47958c4a10ba20dc86c011d71cda4b070a5 test: introduce `tx_from_hex` helper for tx deserialization (Sebastian Falbesoner) Pull request description: There are still many functional tests that perform conversions from a hex-string to a message object (deserialization) manually. This PR identifies all those instances and replaces them with a newly introduced helper `tx_from_hex`. Instances were found via * `git grep "deserialize.*BytesIO"` and some of them manually, when it were not one-liners. Further, the helper `ToHex` was removed and simply replaced by `.serialize().hex()`, since now both variants are in use (sometimes even within the same test) and using the helper doesn't really have an advantage in readability. (see discussion https://github.com/bitcoin/bitcoin/pull/22257#discussion_r652404782) ACKs for top commit: MarcoFalke: review re-ACK bdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb 😁 Tree-SHA512: e25d7dc85918de1d6755a5cea65471b07a743204c20ad1c2f71ff07ef48cc1b9ad3fe5f515c1efaba2b2e3d89384e7980380c5d81895f9826e2046808cd3266e
119 lines
6.3 KiB
Python
Executable File
119 lines
6.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2019 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 test_framework.blocktools import COINBASE_MATURITY
|
|
from test_framework.messages import (
|
|
CMerkleBlock,
|
|
from_hex,
|
|
)
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.wallet import MiniWallet
|
|
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
|
|
class MerkleBlockTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 2
|
|
self.setup_clean_chain = True
|
|
# Nodes 0/1 are "wallet" nodes, Nodes 2/3 are used for testing
|
|
self.extra_args = [
|
|
[],
|
|
["-txindex"],
|
|
]
|
|
|
|
|
|
def run_test(self):
|
|
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)
|
|
self.sync_all()
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
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']
|
|
# 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[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)
|
|
|
|
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']
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
txid_spent = txin_spent["txid"]
|
|
txid_unspent = txid1 # Input was change from txid2, so txid1 should be unspent
|
|
|
|
# Invalid txids
|
|
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)
|
|
# Invalid blockhashes
|
|
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")
|
|
# 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[0].verifytxoutproof(self.nodes[0].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[0].gettxoutproof, [txid_spent], "0000000000000000000000000000000000000000000000000000000000000000")
|
|
# We can get the proof if the transaction is unspent
|
|
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[0].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[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))
|
|
# We can always get a proof if we have a -txindex
|
|
assert_equal(self.nodes[0].verifytxoutproof(self.nodes[1].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[0].gettxoutproof, [txid1, txid3])
|
|
# Test empty list
|
|
assert_raises_rpc_error(-8, "Parameter 'txids' cannot be empty", self.nodes[0].gettxoutproof, [])
|
|
# Test duplicate txid
|
|
assert_raises_rpc_error(-8, 'Invalid parameter, duplicated txid', self.nodes[0].gettxoutproof, [txid1, txid1])
|
|
|
|
# Now we'll try tweaking a proof.
|
|
proof = self.nodes[1].gettxoutproof([txid1, txid2])
|
|
assert txid1 in self.nodes[0].verifytxoutproof(proof)
|
|
assert txid2 in self.nodes[1].verifytxoutproof(proof)
|
|
|
|
tweaked_proof = from_hex(CMerkleBlock(), proof)
|
|
|
|
# Make sure that our serialization/deserialization is working
|
|
assert txid1 in self.nodes[0].verifytxoutproof(tweaked_proof.serialize().hex())
|
|
|
|
# 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(tweaked_proof.serialize().hex())
|
|
|
|
# TODO: try more variants, eg transactions at different depths, and
|
|
# verify that the proofs are invalid
|
|
|
|
if __name__ == '__main__':
|
|
MerkleBlockTest().main()
|