mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
cbc4c63f58
5a1bef60a03b57de708a1a751bd90b8245fd8b83 test: refactor: remove binascii from test_framework (Zero-1729) Pull request description: This PR continues the work started in PR #22593, regarding using the `bytes` built-in module. In this PR specifically, instances of `binascii`'s methods `hexlify`, `unhexlify`, and `a2b_hex` have been replaced with the build-in `bytes` module's `hex` and `fromhex` methods where appropriate to make bytes <-> hex-string conversions consistent across the functional test files and test_framework. Additionally, certain changes made are based on the following assumption: ``` bytes.hex(data) == binascii.hexlify(data).decode() bytes.hex(data).encode() == binascii.hexlify(data) ``` Ran the functional tests to ensure behaviour is still consistent and changes didn't break existing tests. closes #22605 ACKs for top commit: theStack: Code-review ACK 5a1bef60a03b57de708a1a751bd90b8245fd8b83 🔢 Tree-SHA512: 8f28076cf0580a0d02a156f3e1e94c9badd3d41c3fbdfb2b87cd8a761dde2c94faa5f4c448d6747b1ccc9111c3ef1a1d7b42a11c806b241fa0410b7529e2445f Signed-off-by: Vijay <vijaydas.mp@gmail.com>
136 lines
5.6 KiB
Python
Executable File
136 lines
5.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# 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 decimal import Decimal
|
|
|
|
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut
|
|
from test_framework.script import CScript, OP_CHECKSIG, OP_DUP, OP_EQUALVERIFY, OP_HASH160
|
|
from test_framework.test_node import ErrorMatch
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import assert_equal
|
|
|
|
|
|
class SpentIndexTest(BitcoinTestFramework):
|
|
|
|
def set_test_params(self):
|
|
self.setup_clean_chain = True
|
|
self.num_nodes = 4
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
|
|
def setup_network(self):
|
|
self.add_nodes(self.num_nodes)
|
|
# Nodes 0/1 are "wallet" nodes
|
|
self.start_node(0)
|
|
self.start_node(1, ["-spentindex"])
|
|
# Nodes 2/3 are used for testing
|
|
self.start_node(2, ["-spentindex"])
|
|
self.start_node(3, ["-spentindex", "-txindex"])
|
|
self.connect_nodes(0, 1)
|
|
self.connect_nodes(0, 2)
|
|
self.connect_nodes(0, 3)
|
|
self.sync_all()
|
|
self.import_deterministic_coinbase_privkeys()
|
|
|
|
def run_test(self):
|
|
self.log.info("Test that settings can't be changed without -reindex...")
|
|
self.stop_node(1)
|
|
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)
|
|
self.start_node(1, ["-spentindex=0", "-reindex"])
|
|
self.connect_nodes(0, 1)
|
|
self.sync_all()
|
|
self.stop_node(1)
|
|
self.nodes[1].assert_start_raises_init_error(["-spentindex"], "You need to rebuild the database using -reindex to change -spentindex", match=ErrorMatch.PARTIAL_REGEX)
|
|
self.start_node(1, ["-spentindex", "-reindex"])
|
|
self.connect_nodes(0, 1)
|
|
self.sync_all()
|
|
|
|
self.log.info("Mining blocks...")
|
|
self.nodes[0].generate(105)
|
|
self.sync_all()
|
|
|
|
chain_height = self.nodes[1].getblockcount()
|
|
assert_equal(chain_height, 105)
|
|
|
|
# Check that
|
|
self.log.info("Testing spent index...")
|
|
|
|
privkey = "cU4zhap7nPJAWeMFu4j6jLrfPmqakDAzy8zn8Fhb3oEevdm4e5Lc"
|
|
addressHash = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
|
|
scriptPubKey = CScript([OP_DUP, OP_HASH160, addressHash, OP_EQUALVERIFY, OP_CHECKSIG])
|
|
unspent = self.nodes[0].listunspent()
|
|
tx = CTransaction()
|
|
tx_fee = Decimal('0.00001')
|
|
tx_fee_sat = int(tx_fee * COIN)
|
|
amount = int(unspent[0]["amount"] * COIN) - tx_fee_sat
|
|
tx.vin = [CTxIn(COutPoint(int(unspent[0]["txid"], 16), unspent[0]["vout"]))]
|
|
tx.vout = [CTxOut(amount, scriptPubKey)]
|
|
tx.rehash()
|
|
|
|
signed_tx = self.nodes[0].signrawtransactionwithwallet(tx.serialize().hex())
|
|
txid = self.nodes[0].sendrawtransaction(signed_tx["hex"], 0)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
self.log.info("Testing getspentinfo method...")
|
|
|
|
# Check that the spentinfo works standalone
|
|
info = self.nodes[1].getspentinfo({"txid": unspent[0]["txid"], "index": unspent[0]["vout"]})
|
|
assert_equal(info["txid"], txid)
|
|
assert_equal(info["index"], 0)
|
|
assert_equal(info["height"], 106)
|
|
|
|
self.log.info("Testing getrawtransaction method...")
|
|
|
|
# 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)
|
|
assert_equal(txVerbose["vout"][unspent[0]["vout"]]["spentHeight"], 106)
|
|
|
|
# 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"]))
|
|
assert_equal(txVerbose2["vin"][0]["valueSat"] - tx_fee_sat, amount)
|
|
|
|
# Check that verbose raw transaction includes address values and input values
|
|
address2 = "yeMpGzMj3rhtnz48XsfpB8itPHhHtgxLc3"
|
|
addressHash2 = bytes.fromhex("C5E4FB9171C22409809A3E8047A29C83886E325D")
|
|
scriptPubKey2 = CScript([OP_DUP, OP_HASH160, addressHash2, OP_EQUALVERIFY, OP_CHECKSIG])
|
|
tx2 = CTransaction()
|
|
tx2.vin = [CTxIn(COutPoint(int(txid, 16), 0))]
|
|
tx2.vout = [CTxOut(amount - int(COIN / 10), scriptPubKey2)]
|
|
tx2.rehash()
|
|
self.nodes[0].importprivkey(privkey)
|
|
signed_tx2 = self.nodes[0].signrawtransactionwithwallet(tx2.serialize().hex())
|
|
txid2 = self.nodes[0].sendrawtransaction(signed_tx2["hex"], 0)
|
|
|
|
# Check the mempool index
|
|
self.sync_all()
|
|
txVerbose3 = self.nodes[1].getrawtransaction(txid2, 1)
|
|
assert_equal(txVerbose3["vin"][0]["address"], address2)
|
|
assert_equal(txVerbose3["vin"][0]["value"], Decimal(unspent[0]["amount"]) - tx_fee)
|
|
assert_equal(txVerbose3["vin"][0]["valueSat"], amount)
|
|
|
|
# Check the database index
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
txVerbose4 = self.nodes[3].getrawtransaction(txid2, 1)
|
|
assert_equal(txVerbose4["vin"][0]["address"], address2)
|
|
assert_equal(txVerbose4["vin"][0]["value"], Decimal(unspent[0]["amount"]) - tx_fee)
|
|
assert_equal(txVerbose4["vin"][0]["valueSat"], amount)
|
|
|
|
self.log.info("Passed")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
SpentIndexTest().main()
|