Skip to content

Commit

Permalink
Port python tests part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Aug 12, 2024
1 parent ece590d commit 2aea7e8
Show file tree
Hide file tree
Showing 42 changed files with 429 additions and 341 deletions.
12 changes: 6 additions & 6 deletions test/functional/interface_bitcoin_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from decimal import Decimal
import re

from test_framework.blocktools import COINBASE_MATURITY
from test_framework.qtumconfig import INITIAL_BLOCK_REWARD, COINBASE_MATURITY
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
Expand All @@ -22,7 +22,7 @@
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
# node 0 to have a balance of (BLOCKS - COINBASE_MATURITY) * 50 BTC/block.
BLOCKS = COINBASE_MATURITY + 1
BALANCE = (BLOCKS - 100) * 50
BALANCE = (BLOCKS - COINBASE_MATURITY) * INITIAL_BLOCK_REWARD

JSON_PARSING_ERROR = 'error: Error parsing JSON: foo'
BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
Expand Down Expand Up @@ -139,7 +139,7 @@ def run_test(self):
expected_network_info = f"in {network_info['connections_in']}, out {network_info['connections_out']}, total {network_info['connections']}"
assert_equal(cli_get_info["Network"], expected_network_info)
assert_equal(cli_get_info['Proxies'], network_info['networks'][0]['proxy'])
assert_equal(Decimal(cli_get_info['Difficulty']), blockchain_info['difficulty'])
# assert_equal(Decimal(cli_get_info['Difficulty']), blockchain_info['difficulty'])
assert_equal(cli_get_info['Chain'], blockchain_info['chain'])

self.log.info("Test -getinfo and bitcoin-cli return all proxies")
Expand All @@ -159,13 +159,13 @@ def run_test(self):
wallet_info = self.nodes[0].getwalletinfo()
assert_equal(int(cli_get_info['Keypool size']), wallet_info['keypoolsize'])
assert_equal(int(cli_get_info['Unlocked until']), wallet_info['unlocked_until'])
assert_equal(Decimal(cli_get_info['Transaction fee rate (-paytxfee) (BTC/kvB)']), wallet_info['paytxfee'])
assert_equal(Decimal(cli_get_info['Min tx relay fee rate (BTC/kvB)']), network_info['relayfee'])
assert_equal(Decimal(cli_get_info['Transaction fee rate (-paytxfee) (QTUM/kvB)']), wallet_info['paytxfee'])
assert_equal(Decimal(cli_get_info['Min tx relay fee rate (QTUM/kvB)']), network_info['relayfee'])
assert_equal(self.nodes[0].cli.getwalletinfo(), wallet_info)

# Setup to test -getinfo, -generate, and -rpcwallet= with multiple wallets.
wallets = [self.default_wallet_name, 'Encrypted', 'secret']
amounts = [BALANCE + Decimal('9.999928'), Decimal(9), Decimal(31)]
amounts = [2*INITIAL_BLOCK_REWARD-50 + Decimal('9.99856000'), Decimal(9), Decimal(31)]
self.nodes[0].createwallet(wallet_name=wallets[1])
self.nodes[0].createwallet(wallet_name=wallets[2])
w1 = self.nodes[0].get_wallet_rpc(wallets[0])
Expand Down
14 changes: 10 additions & 4 deletions test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


from test_framework.messages import (
BLOCK_HEADER_SIZE,
# BLOCK_HEADER_SIZE,
COIN,
)
from test_framework.test_framework import BitcoinTestFramework
Expand All @@ -27,6 +27,12 @@
getnewdestination,
)
from typing import Optional
from test_framework.messages import CBlockHeader

from test_framework.qtumconfig import COINBASE_MATURITY, INITIAL_BLOCK_REWARD
from test_framework.qtum import convert_btc_address_to_qtum, generatesynchronized

BLOCK_HEADER_SIZE = len(CBlockHeader().serialize())


INVALID_PARAM = "abc"
Expand Down Expand Up @@ -166,15 +172,15 @@ def run_test(self):
response_hash = bin_response[4:36][::-1].hex()

assert_equal(bb_hash, response_hash) # check if getutxo's chaintip during calculation was fine
assert_equal(chain_height, 201) # chain height must be 201 (pre-mined chain [200] + generated block [1])
assert_equal(chain_height, COINBASE_MATURITY+101) # chain height must be 2101 (pre-mined chain [200] + generated block [1])

self.log.info("Test the /getutxos URI with and without /checkmempool")
# Create a transaction, check that it's found with /checkmempool, but
# not found without. Then confirm the transaction and check that it's
# found with or without /checkmempool.

# do a tx and don't sync
txid = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=int(0.1 * COIN))["txid"]
txid = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=getnewdestination()[1], amount=int(0.1 * COIN), sort_by_height=True)["txid"]
json_obj = self.test_rest_request(f"/tx/{txid}")
# get the spent output to later check for utxo (should be spent by then)
spent = (json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout'])
Expand Down Expand Up @@ -291,7 +297,7 @@ def run_test(self):
# See if we can get 5 headers in one response
self.generate(self.nodes[1], 5)
expected_filter = {
'basic block filter index': {'synced': True, 'best_block_height': 208},
'basic block filter index': {'synced': True, 'best_block_height': 2108},
}
self.wait_until(lambda: self.nodes[0].getindexinfo() == expected_filter)
json_obj = self.test_rest_request(f"/headers/{bb_hash}", query_params={"count": 5})
Expand Down
16 changes: 11 additions & 5 deletions test/functional/interface_zmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from test_framework.messages import (
hash256,
tx_from_hex,
CBlockHeader,
)
from test_framework.util import (
assert_equal,
Expand All @@ -29,7 +30,8 @@
MiniWallet,
)
from test_framework.netutil import test_ipv6_local

from io import BytesIO
from test_framework.qtum import convert_btc_bech32_address_to_qtum

# Test may be skipped and not have zmq installed
try:
Expand Down Expand Up @@ -204,7 +206,11 @@ def test_basic(self):

# Should receive the generated raw block.
block = rawblock.receive()
assert_equal(genhashes[x], hash256_reversed(block[:80]).hex())
f = BytesIO(block)
header = CBlockHeader()
header.deserialize(f)
header.rehash()
assert_equal(genhashes[x], header.hash)

# Should receive the generated block hash.
hash = hashblock.receive().hex()
Expand Down Expand Up @@ -335,7 +341,7 @@ def test_sequence(self):

self.log.info("Testing RBF notification")
# Replace it to test eviction/addition notification
payment_tx['tx'].vout[0].nValue -= 1000
payment_tx['tx'].vout[0].nValue -= 10000
rbf_txid = self.nodes[1].sendrawtransaction(payment_tx['tx'].serialize().hex())
self.sync_all()
assert_equal((payment_txid, "R", seq_num), seq.receive_sequence())
Expand Down Expand Up @@ -397,7 +403,7 @@ def test_sequence(self):
for _ in range(5):
more_tx.append(self.wallet.send_self_transfer(from_node=self.nodes[0]))

orig_tx['tx'].vout[0].nValue -= 1000
orig_tx['tx'].vout[0].nValue -= 10000
bump_txid = self.nodes[0].sendrawtransaction(orig_tx['tx'].serialize().hex())
# Mine the pre-bump tx
txs_to_add = [orig_tx['hex']] + [tx['hex'] for tx in more_tx]
Expand Down Expand Up @@ -481,7 +487,7 @@ def test_mempool_sync(self):
# We have node 0 do all these to avoid p2p races with RBF announcements
for _ in range(num_txs):
txs.append(self.wallet.send_self_transfer(from_node=self.nodes[0]))
txs[-1]['tx'].vout[0].nValue -= 1000
txs[-1]['tx'].vout[0].nValue -= 10000
self.nodes[0].sendrawtransaction(txs[-1]['tx'].serialize().hex())
self.sync_all()
self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_UNSPENDABLE)
Expand Down
11 changes: 6 additions & 5 deletions test/functional/mempool_accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@
)
from test_framework.wallet import MiniWallet
from test_framework.wallet_util import generate_keypair
from test_framework.qtumconfig import COINBASE_MATURITY


class MempoolAcceptanceTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.extra_args = [[
'-txindex','-permitbaremultisig=0',
'-txindex','-permitbaremultisig=0','-minrelaytxfee=0.0000001'
]] * self.num_nodes
self.supports_cli = False

Expand All @@ -72,7 +73,7 @@ def run_test(self):

self.log.info('Start with empty mempool, and 200 blocks')
self.mempool_size = 0
assert_equal(node.getblockcount(), 200)
assert_equal(node.getblockcount(), COINBASE_MATURITY+100)
assert_equal(node.getmempoolinfo()['size'], self.mempool_size)

self.log.info('Should not accept garbage to testmempoolaccept')
Expand Down Expand Up @@ -104,7 +105,7 @@ def run_test(self):
)

self.log.info('A transaction not in the mempool')
fee = Decimal('0.000007')
fee = Decimal('0.003')
utxo_to_spend = self.wallet.get_utxo(txid=txid_in_block) # use 0.3 BTC UTXO
tx = self.wallet.create_self_transfer(utxo_to_spend=utxo_to_spend, sequence=MAX_BIP125_RBF_SEQUENCE)['tx']
tx.vout[0].nValue = int((Decimal('0.3') - fee) * COIN)
Expand All @@ -124,7 +125,7 @@ def run_test(self):
tx.vout[0].nValue = int(output_amount * COIN)
raw_tx_final = tx.serialize().hex()
tx = tx_from_hex(raw_tx_final)
fee_expected = Decimal('50.0') - output_amount
fee_expected = Decimal('20000') - output_amount
self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': True, 'vsize': tx.get_vsize(), 'fees': {'base': fee_expected}}],
rawtxs=[tx.serialize().hex()],
Expand Down Expand Up @@ -220,7 +221,7 @@ def run_test(self):

self.log.info('A really large transaction')
tx = tx_from_hex(raw_tx_reference)
tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_WEIGHT // 4 / len(tx.vin[0].serialize()))
tx.vin = [tx.vin[0]] * math.ceil(MAX_BLOCK_WEIGHT / len(tx.vin[0].serialize()))
self.check_mempool_result(
result_expected=[{'txid': tx.rehash(), 'allowed': False, 'reject-reason': 'bad-txns-oversize'}],
rawtxs=[tx.serialize().hex()],
Expand Down
6 changes: 4 additions & 2 deletions test/functional/mempool_accept_wtxid.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
assert_equal,
)

from test_framework.qtumconfig import COINBASE_MATURITY

class MempoolWtxidTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
Expand All @@ -44,7 +46,7 @@ def run_test(self):

self.log.info('Start with empty mempool and 101 blocks')
# The last 100 coinbase transactions are premature
blockhash = self.generate(node, 101)[0]
blockhash = self.generate(node, COINBASE_MATURITY+1)[0]
txid = node.getblock(blockhash=blockhash, verbosity=2)["tx"][0]["txid"]
assert_equal(node.getmempoolinfo()['size'], 0)

Expand Down Expand Up @@ -73,7 +75,7 @@ def run_test(self):

child_one = CTransaction()
child_one.vin.append(CTxIn(COutPoint(int(parent_txid, 16), 0), b""))
child_one.vout.append(CTxOut(int(9.99996 * COIN), child_script_pubkey))
child_one.vout.append(CTxOut(int(9.999 * COIN), child_script_pubkey))
child_one.wit.vtxinwit.append(CTxInWitness())
child_one.wit.vtxinwit[0].scriptWitness.stack = [b'Preimage', b'\x01', witness_script]
child_one_wtxid = child_one.getwtxid()
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mempool_datacarrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_test_params(self):
]

def test_null_data_transaction(self, node: TestNode, data, success: bool) -> None:
tx = self.wallet.create_self_transfer(fee_rate=0)["tx"]
tx = self.wallet.create_self_transfer(fee_rate=78400)["tx"]
data = [] if data is None else [data]
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN] + data)))
tx.vout[0].nValue -= tx.get_vsize() # simply pay 1sat/vbyte fee
Expand Down
4 changes: 2 additions & 2 deletions test/functional/mempool_dust.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from test_framework.wallet_util import generate_keypair


DUST_RELAY_TX_FEE = 3000 # default setting [sat/kvB]
DUST_RELAY_TX_FEE = 400000 # default setting [sat/kvB]


class DustRelayFeeTest(BitcoinTestFramework):
Expand Down Expand Up @@ -95,7 +95,7 @@ def run_test(self):
)

# test default (no parameter), disabled (=0) and a bunch of arbitrary dust fee rates [sat/kvB]
for dustfee_sat_kvb in (DUST_RELAY_TX_FEE, 0, 1, 66, 500, 1337, 12345, 21212, 333333):
for dustfee_sat_kvb in (DUST_RELAY_TX_FEE, 0, 133, 8800, 66667, 178267, 1646000, 2828267, 44444400):
dustfee_btc_kvb = dustfee_sat_kvb / Decimal(COIN)
if dustfee_sat_kvb == DUST_RELAY_TX_FEE:
self.log.info(f"Test default dust limit setting ({dustfee_sat_kvb} sat/kvB)...")
Expand Down
11 changes: 6 additions & 5 deletions test/functional/mempool_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(<n> is the timeout in hours) are tested.
"""

from decimal import Decimal
from datetime import timedelta

from test_framework.messages import (
Expand All @@ -36,7 +37,7 @@ def test_transaction_expiry(self, timeout):
node = self.nodes[0]

# Send a parent transaction that will expire.
parent = self.wallet.send_self_transfer(from_node=node)
parent = self.wallet.send_self_transfer(from_node=node, fee_rate=Decimal("0.03"))
parent_txid = parent["txid"]
parent_utxo = self.wallet.get_utxo(txid=parent_txid)
independent_utxo = self.wallet.get_utxo()
Expand All @@ -57,21 +58,21 @@ def test_transaction_expiry(self, timeout):
# Let half of the timeout elapse and broadcast the child transaction spending the parent transaction.
half_expiry_time = entry_time + int(60 * 60 * timeout/2)
node.setmocktime(half_expiry_time)
child_txid = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=parent_utxo)['txid']
child_txid = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=parent_utxo, fee_rate=Decimal("0.03"))['txid']
assert_equal(parent_txid, node.getmempoolentry(child_txid)['depends'][0])
self.log.info('Broadcast child transaction after {} hours.'.format(
timedelta(seconds=(half_expiry_time-entry_time))))

# Broadcast another (independent) transaction.
independent_txid = self.wallet.send_self_transfer(from_node=node, utxo_to_spend=independent_utxo)['txid']
independent_txid = self.wallet.send_self_transfer(from_node=node, fee_rate=Decimal("0.03"), utxo_to_spend=independent_utxo)['txid']

# Let most of the timeout elapse and check that the parent tx is still
# in the mempool.
nearly_expiry_time = entry_time + 60 * 60 * timeout - 5
node.setmocktime(nearly_expiry_time)
# Broadcast a transaction as the expiry of transactions in the mempool is only checked
# when a new transaction is added to the mempool.
self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo1)
self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo1, fee_rate=Decimal("0.03"))
self.log.info('Test parent tx not expired after {} hours.'.format(
timedelta(seconds=(nearly_expiry_time-entry_time))))
assert_equal(entry_time, node.getmempoolentry(parent_txid)['time'])
Expand All @@ -81,7 +82,7 @@ def test_transaction_expiry(self, timeout):
expiry_time = entry_time + 60 * 60 * timeout + 5
node.setmocktime(expiry_time)
# Again, broadcast a transaction so the expiry of transactions in the mempool is checked.
self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo2)
self.wallet.send_self_transfer(from_node=node, utxo_to_spend=trigger_utxo2, fee_rate=Decimal("0.03"))
self.log.info('Test parent tx expiry after {} hours.'.format(
timedelta(seconds=(expiry_time-entry_time))))
assert_raises_rpc_error(-5, 'Transaction not in mempool',
Expand Down
16 changes: 8 additions & 8 deletions test/functional/mempool_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def fill_mempool(self):
assert tx_to_be_evicted_id not in node.getrawmempool()

self.log.debug("Check that mempoolminfee is larger than minrelaytxfee")
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
assert_greater_than(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
assert_greater_than(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))

def test_rbf_carveout_disallowed(self):
node = self.nodes[0]
Expand Down Expand Up @@ -136,8 +136,8 @@ def test_mid_package_eviction(self):
self.restart_node(0, extra_args=self.extra_args[0])

# Restarting the node resets mempool minimum feerate
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))

self.fill_mempool()
current_info = node.getmempoolinfo()
Expand Down Expand Up @@ -226,8 +226,8 @@ def test_mid_package_replacement(self):
self.restart_node(0, extra_args=self.extra_args[0])

# Restarting the node resets mempool minimum feerate
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))

self.fill_mempool()
current_info = node.getmempoolinfo()
Expand Down Expand Up @@ -300,8 +300,8 @@ def run_test(self):

relayfee = node.getnetworkinfo()['relayfee']
self.log.info('Check that mempoolminfee is minrelaytxfee')
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00001000'))
assert_equal(node.getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
assert_equal(node.getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))

self.fill_mempool()

Expand Down
Loading

0 comments on commit 2aea7e8

Please sign in to comment.