dash/test/functional/rpc_verifyislock.py

95 lines
4.1 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
# Copyright (c) 2020-2024 The Dash Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
Merge bitcoin/bitcoin#22257: test: refactor: various (de)serialization helpers cleanups/improvements 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
2021-06-24 12:47:04 +02:00
from test_framework.messages import CTransaction, from_hex, hash256, ser_compact_size, ser_string
from test_framework.test_framework import DashTestFramework
from test_framework.util import assert_raises_rpc_error, satoshi_round
'''
rpc_verifyislock.py
Test verifyislock rpc
'''
class RPCVerifyISLockTest(DashTestFramework):
def set_test_params(self):
# -whitelist is needed to avoid the trickling logic on node0
self.set_dash_test_params(6, 5, [["-whitelist=127.0.0.1"], [], [], [], [], []])
def get_request_id(self, tx_hex):
Merge bitcoin/bitcoin#22257: test: refactor: various (de)serialization helpers cleanups/improvements 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
2021-06-24 12:47:04 +02:00
tx = from_hex(CTransaction(), tx_hex)
request_id_buf = ser_string(b"islock") + ser_compact_size(len(tx.vin))
for txin in tx.vin:
request_id_buf += txin.prevout.serialize()
return hash256(request_id_buf)[::-1].hex()
def run_test(self):
node = self.nodes[0]
node.sporkupdate("SPORK_17_QUORUM_DKG_ENABLED", 0)
self.wait_for_sporks_same()
self.activate_v19(expected_activation_height=900)
self.log.info("Activated v19 at height:" + str(self.nodes[0].getblockcount()))
self.mine_cycle_quorum()
self.bump_mocktime(1)
self.generate(self.nodes[0], 8, sync_fun=self.sync_blocks())
txid = node.sendtoaddress(node.getnewaddress(), 1)
self.wait_for_instantlock(txid, node)
request_id = self.get_request_id(self.nodes[0].getrawtransaction(txid))
self.wait_until(lambda: node.quorum("hasrecsig", 103, request_id, txid))
rec_sig = node.quorum("getrecsig", 103, request_id, txid)['sig']
2021-08-27 21:03:02 +02:00
assert node.verifyislock(request_id, txid, rec_sig)
# Not mined, should use maxHeight
assert not node.verifyislock(request_id, txid, rec_sig, 1)
self.generate(node, 1, sync_fun=self.no_op)
2021-08-27 21:03:02 +02:00
assert txid not in node.getrawmempool()
# Mined but at higher height, should use maxHeight
assert not node.verifyislock(request_id, txid, rec_sig, 1)
# Mined, should ignore higher maxHeight
2021-08-27 21:03:02 +02:00
assert node.verifyislock(request_id, txid, rec_sig, node.getblockcount() + 100)
# Mine one more cycle of rotated quorums
self.mine_cycle_quorum(is_first=False)
# Create an ISLOCK using an active quorum which will be replaced when a new cycle happens
request_id = None
utxos = node.listunspent()
fee = 0.001
amount = 1
# Try all available utxo's until we have one valid in_amount
for utxo in utxos:
in_amount = float(utxo['amount'])
if in_amount < amount + fee:
continue
outputs = dict()
outputs[node.getnewaddress()] = satoshi_round(amount)
change = in_amount - amount - fee
if change > 0:
outputs[node.getnewaddress()] = satoshi_round(change)
rawtx = node.createrawtransaction([utxo], outputs)
rawtx = node.signrawtransactionwithwallet(rawtx)["hex"]
request_id = self.get_request_id(rawtx)
break
# Create the ISDLOCK, then mine a cycle quorum to move renew active set
isdlock = self.create_isdlock(rawtx)
# Mine one block to trigger the "signHeight + dkgInterval" verification for the ISDLOCK
self.mine_cycle_quorum(is_first=False)
# Verify the ISLOCK for a transaction that is not yet known by the node
rawtx_txid = node.decoderawtransaction(rawtx)["txid"]
assert_raises_rpc_error(-5, "No such mempool or blockchain transaction", node.getrawtransaction, rawtx_txid)
assert node.verifyislock(request_id, rawtx_txid, isdlock.sig.hex(), node.getblockcount())
# Send the tx and verify the ISDLOCK for a now known transaction
assert rawtx_txid == node.sendrawtransaction(rawtx)
assert node.verifyislock(request_id, rawtx_txid, isdlock.sig.hex(), node.getblockcount())
if __name__ == '__main__':
RPCVerifyISLockTest().main()