Skip to content

Commit 233c4d1

Browse files
committed
feat: improved functional tests for asset locks feature
- reconsider/invalide blocks - manually created block with invalid transaction
1 parent 77ae8fb commit 233c4d1

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

test/functional/feature_asset_locks.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from decimal import Decimal
99
from io import BytesIO
1010

11+
from test_framework.blocktools import (
12+
create_block,
13+
create_coinbase,
14+
)
1115
from test_framework.authproxy import JSONRPCException
1216
from test_framework.key import ECKey
1317
from test_framework.messages import (
@@ -158,6 +162,7 @@ def run_test(self):
158162
coins = node.listunspent()
159163
coin = coins.pop()
160164
locked_1 = 10 * COIN + 141421
165+
locked_2 = 10 * COIN + 314159
161166
asset_lock_tx = create_assetlock(node, coin, locked_1, pubkey)
162167

163168
self.check_mempool_result(
@@ -189,15 +194,18 @@ def run_test(self):
189194
self.sync_all()
190195
assert_equal(get_credit_pool_amount(node), 0)
191196
self.log.info("Resubmit asset lock tx to new chain...")
192-
txid_in_block = node.sendrawtransaction(hexstring=asset_lock_tx.serialize().hex(), maxfeerate=0)
193-
node.generate(3)
197+
# NEW tx appears
198+
asset_lock_tx_2 = create_assetlock(node, coin, locked_2, pubkey)
199+
200+
txid_in_block = node.sendrawtransaction(hexstring=asset_lock_tx_2.serialize().hex(), maxfeerate=0)
201+
node.generate(1)
194202
self.sync_all()
195203

196-
assert_equal(get_credit_pool_amount(node), locked_1)
204+
assert_equal(get_credit_pool_amount(node), locked_2)
197205

198206
node.generate(3)
199207
self.sync_all()
200-
assert_equal(get_credit_pool_amount(node), locked_1)
208+
assert_equal(get_credit_pool_amount(node), locked_2)
201209
self.log.info("Reconsider old blocks...")
202210
for inode in self.nodes:
203211
inode.reconsiderblock(block_hash_1)
@@ -237,17 +245,7 @@ def run_test(self):
237245
except JSONRPCException as e:
238246
assert "Transaction already in block chain" in e.error['message']
239247

240-
self.log.info("Invalidate block with asset unlock tx...")
241248
block_asset_unlock = node.getbestblockhash()
242-
for inode in self.nodes:
243-
inode.invalidateblock(block_asset_unlock)
244-
assert_equal(get_credit_pool_amount(node), locked_1)
245-
# TODO: strange, fails if generate there new blocks
246-
#node.generate(3)
247-
#self.sync_all()
248-
for inode in self.nodes:
249-
inode.reconsiderblock(block_asset_unlock)
250-
assert_equal(get_credit_pool_amount(node), locked_1 - COIN)
251249

252250
# mine next quorum, tx should be still accepted
253251
self.mine_quorum()
@@ -279,6 +277,39 @@ def run_test(self):
279277
result_expected=[{'txid': asset_unlock_tx_inactive_quorum.rehash(), 'allowed': False, 'reject-reason' : '16: bad-assetunlock-not-active-quorum'}],
280278
rawtxs=[asset_unlock_tx_inactive_quorum.serialize().hex()],
281279
)
280+
block_to_reconsider = node.getbestblockhash()
281+
self.log.info("Test block invalidation with asset unlock tx...")
282+
for inode in self.nodes:
283+
inode.invalidateblock(block_asset_unlock)
284+
assert_equal(get_credit_pool_amount(node), locked_1)
285+
# generate some new blocks
286+
node.generate(90)
287+
self.sync_all()
288+
assert_equal(get_credit_pool_amount(node), locked_1)
289+
for inode in self.nodes:
290+
inode.reconsiderblock(block_to_reconsider)
291+
assert_equal(get_credit_pool_amount(node), locked_1 - 2 * COIN)
292+
293+
# Forcibly mine asset_unlock_tx_too_late and ensure block is invalid
294+
hh = node.getbestblockhash()
295+
best_block = node.getblock(hh)
296+
tip = int(node.getbestblockhash(), 16)
297+
height = best_block["height"] + 1
298+
block_time = best_block["time"] + 1
299+
300+
301+
cbb = create_coinbase(height, dip4_activated=True, dip27_activated=True)
302+
block = create_block(tip, cbb, block_time, version=3)
303+
block.vtx.append(asset_unlock_tx_too_late)
304+
block.hashMerkleRoot = block.calc_merkle_root()
305+
block.solve()
306+
result = node.submitblock(block.serialize().hex())
307+
# Expect an error here
308+
expected_error = "bad-assetunlock-not-active-quorum"
309+
if result != expected_error:
310+
raise AssertionError('mining the block should have failed with error %s, but submitblock returned %s' % (expected_error, result))
311+
312+
# ----
282313

283314
node.generate(13)
284315
self.sync_all()

0 commit comments

Comments
 (0)