Skip to content

Commit

Permalink
Refactoring of functional test feature_asset_locks: better function t…
Browse files Browse the repository at this point in the history
…o check mempool usage
  • Loading branch information
knst committed Dec 8, 2022
1 parent 235cb56 commit 879f60f
Showing 1 changed file with 20 additions and 40 deletions.
60 changes: 20 additions & 40 deletions test/functional/feature_asset_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ def set_test_params(self):
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

# TODO remove duplicated function `check_mempool_result` with mempool_accept.py
def check_mempool_result(self, result_expected, *args, **kwargs):
def check_mempool_result(self, result_expected, tx):
"""Wrapper to check result of testmempoolaccept on node_0's mempool"""
result_test = self.nodes[0].testmempoolaccept(*args, **kwargs)
assert_equal(result_expected, result_test)
result_expected['txid'] = tx.rehash()

result_test = self.nodes[0].testmempoolaccept([tx.serialize().hex()])

assert_equal([result_expected], result_test)
assert_equal(self.nodes[0].getmempoolinfo()['size'], self.mempool_size) # Must not change mempool state

def set_sporks(self):
Expand Down Expand Up @@ -166,10 +168,7 @@ def run_test(self):
locked_2 = 10 * COIN + 314159
asset_lock_tx = create_assetlock(node, coin, locked_1, pubkey)

self.check_mempool_result(
result_expected=[{'txid': asset_lock_tx.rehash(), 'allowed': True }],
rawtxs=[asset_lock_tx.serialize().hex()],
)
self.check_mempool_result(tx=asset_lock_tx, result_expected={'allowed': True})
assert_equal(get_credit_pool_amount(node), 0)
txid_in_block = node.sendrawtransaction(hexstring=asset_lock_tx.serialize().hex(), maxfeerate=0)

Expand Down Expand Up @@ -228,14 +227,9 @@ def run_test(self):
asset_unlock_tx_duplicate_index = copy.deepcopy(asset_unlock_tx)
asset_unlock_tx_duplicate_index.vout[0].nValue += COIN

self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx.rehash(), 'allowed': True }],
rawtxs=[asset_unlock_tx.serialize().hex()],
)
self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx_duplicate_index.rehash(), 'allowed': False, 'reject-reason' : '16: bad-assetunlock-not-verified'}],
rawtxs=[asset_unlock_tx_duplicate_index.serialize().hex()],
)
self.check_mempool_result(tx=asset_unlock_tx, result_expected={'allowed': True})
self.check_mempool_result(tx=asset_unlock_tx_duplicate_index,
result_expected={'allowed': False, 'reject-reason' : '16: bad-assetunlock-not-verified'})

# validate that we calculate payload hash correctly: ask quorum forcely by message hash
asset_unlock_tx_payload = CAssetUnlockTx()
Expand All @@ -256,10 +250,8 @@ def run_test(self):
except JSONRPCException as e:
assert "Transaction already in block chain" in e.error['message']

self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx_duplicate_index.rehash(), 'allowed': False, 'reject-reason' : '16: bad-assetunlock-duplicated-index'}],
rawtxs=[asset_unlock_tx_duplicate_index.serialize().hex()],
)
self.check_mempool_result(tx=asset_unlock_tx_duplicate_index,
result_expected={'allowed': False, 'reject-reason' : '16: bad-assetunlock-duplicated-index'})
try:
node.sendrawtransaction(hexstring=asset_unlock_tx_duplicate_index.serialize().hex(), maxfeerate=0)
raise AssertionError("Transaction should not be mined: double index")
Expand All @@ -273,10 +265,7 @@ def run_test(self):
self.mine_quorum()
# should stay same
assert_equal(get_credit_pool_amount(node), locked_1 - COIN)
self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx_late.rehash(), 'allowed': True }],
rawtxs=[asset_unlock_tx_late.serialize().hex()],
)
self.check_mempool_result(tx=asset_unlock_tx_late, result_expected={'allowed': True})
# should still stay same
assert_equal(get_credit_pool_amount(node), locked_1 - COIN)
node.sendrawtransaction(hexstring=asset_unlock_tx_late.serialize().hex(), maxfeerate=0)
Expand All @@ -288,17 +277,14 @@ def run_test(self):
node.generate(100)
self.sync_all()

self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx_too_late.rehash(), 'allowed': False, 'reject-reason' : '16: bad-assetunlock-too-late'}],
rawtxs=[asset_unlock_tx_too_late.serialize().hex()],
)
self.check_mempool_result(tx=asset_unlock_tx_too_late,
result_expected={'allowed': False, 'reject-reason' : '16: bad-assetunlock-too-late'})

# two quorums later is too late
self.mine_quorum()
self.check_mempool_result(
result_expected=[{'txid': asset_unlock_tx_inactive_quorum.rehash(), 'allowed': False, 'reject-reason' : '16: bad-assetunlock-not-active-quorum'}],
rawtxs=[asset_unlock_tx_inactive_quorum.serialize().hex()],
)
self.check_mempool_result(tx=asset_unlock_tx_inactive_quorum,
result_expected={'allowed': False, 'reject-reason' : '16: bad-assetunlock-not-active-quorum'})

block_to_reconsider = node.getbestblockhash()
self.log.info("Test block invalidation with asset unlock tx...")
for inode in self.nodes:
Expand Down Expand Up @@ -344,10 +330,7 @@ def run_test(self):

# Mempool doesn't know about the size of the credit pool, so the transaction will be accepted to mempool,
# but won't be mined
self.check_mempool_result(
result_expected=[{'txid': aset_unlock_tx_full.rehash(), 'allowed': True }],
rawtxs=[aset_unlock_tx_full.serialize().hex()],
)
self.check_mempool_result(tx=aset_unlock_tx_full, result_expected={'allowed': True })

txid_in_block = node.sendrawtransaction(hexstring=aset_unlock_tx_full.serialize().hex(), maxfeerate=0)
node.generate(13)
Expand All @@ -361,10 +344,7 @@ def run_test(self):

self.mempool_size += 1
aset_unlock_tx_full = create_assetunlock(node, self.mninfo, 301, get_credit_pool_amount(node), pubkey)
self.check_mempool_result(
result_expected=[{'txid': aset_unlock_tx_full.rehash(), 'allowed': True }],
rawtxs=[aset_unlock_tx_full.serialize().hex()],
)
self.check_mempool_result(tx=aset_unlock_tx_full, result_expected={'allowed': True })

txid_in_block = node.sendrawtransaction(hexstring=aset_unlock_tx_full.serialize().hex(), maxfeerate=0)
node.generate(1)
Expand Down

0 comments on commit 879f60f

Please sign in to comment.