Skip to content

Commit a5bab55

Browse files
knstPastaPastaPasta
authored andcommitted
test: remove wallet dependency from p2p_blockfilters.py and refactor duplicated code
1 parent a1ecf5d commit a5bab55

File tree

1 file changed

+29
-75
lines changed

1 file changed

+29
-75
lines changed

test/functional/p2p_blockfilters.py

Lines changed: 29 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,11 @@ def run_test(self):
7474
peer_0 = self.nodes[0].add_p2p_connection(FiltersClient())
7575
peer_1 = self.nodes[1].add_p2p_connection(FiltersClient())
7676

77-
# Ensure node 0 has a wallet so block rewards are saved for later use
78-
if not self.nodes[0].listwallets():
79-
self.nodes[0].createwallet(wallet_name="")
80-
81-
# Get an address to mine to so we receive the rewards
82-
mining_addr = self.nodes[0].getnewaddress()
77+
self.privkeys = [self.nodes[0].get_deterministic_priv_key().key]
78+
self.change_addr = self.nodes[0].get_deterministic_priv_key().address
8379

8480
# Nodes 0 & 1 share the same first 999 blocks in the chain.
85-
self.generatetoaddress(self.nodes[0], 999, mining_addr)
81+
self.generate(self.nodes[0], 999)
8682

8783
# Stale blocks by disconnecting nodes 0 & 1, mining, then reconnecting
8884
self.disconnect_nodes(0, 1)
@@ -308,52 +304,52 @@ def run_test(self):
308304

309305
self.test_special_transactions_in_filters()
310306

311-
def create_simple_assetlock(self, amount_locked):
307+
def create_simple_assetlock(self, base_tx, base_tx_value, amount_locked_1, amount_locked_2=0):
312308
"""Create a simple AssetLockTx for testing"""
313309
node = self.nodes[0]
314310

315-
# Get a coin to lock
316-
unspent = node.listunspent()[0]
317-
318-
# Create a key for the credit outputs
319-
key = ECKey()
320-
key.generate()
321-
pubkey = key.get_pubkey().get_bytes()
322-
323311
# Create the AssetLockTx
324-
inputs = [CTxIn(COutPoint(int(unspent["txid"], 16), unspent["vout"]))]
312+
inputs = [CTxIn(outpoint=COutPoint(int(base_tx, 16), 0))]
325313

326314
# Credit outputs (what will be credited on Platform)
327-
credit_outputs = [CTxOut(int(amount_locked * COIN), key_to_p2pkh_script(pubkey))]
315+
credit_outputs = []
316+
for amount in [amount_locked_1, amount_locked_2]:
317+
if amount == 0:
318+
continue
319+
320+
# Create a key for the credit outputs
321+
key = ECKey()
322+
key.generate()
323+
pubkey = key.get_pubkey().get_bytes()
324+
credit_outputs.append(CTxOut(int(amount * COIN), key_to_p2pkh_script(pubkey)))
328325

329326
# AssetLock payload
330327
lock_tx_payload = CAssetLockTx(1, credit_outputs)
331328

332329
# Calculate remaining amount (minus small fee)
333330
fee = Decimal("0.0001")
334-
remaining = Decimal(str(unspent['amount'])) - amount_locked - fee
331+
remaining = base_tx_value - amount_locked_1 - amount_locked_2 - fee
335332

336333
# Create the transaction
337334
lock_tx = CTransaction()
338335
lock_tx.vin = inputs
339336

340337
# Add change output if there's remaining amount
341338
if remaining > 0:
342-
change_addr = node.getnewaddress()
343-
change_script = bytes.fromhex(node.validateaddress(change_addr)['scriptPubKey'])
339+
change_script = bytes.fromhex(node.validateaddress(self.change_addr)['scriptPubKey'])
344340
lock_tx.vout = [CTxOut(int(remaining * COIN), change_script)]
345341

346342
# Add OP_RETURN output with the locked amount
347-
lock_tx.vout.append(CTxOut(int(amount_locked * COIN), CScript([OP_RETURN, b""])))
343+
lock_tx.vout.append(CTxOut(int((amount_locked_1 + amount_locked_2) * COIN), CScript([OP_RETURN, b""])))
348344

349345
lock_tx.nVersion = 3
350346
lock_tx.nType = 8 # asset lock type
351347
lock_tx.vExtraPayload = lock_tx_payload.serialize()
352348

353349
# Sign the transaction
354350
tx_hex = lock_tx.serialize().hex()
355-
signed_tx = node.signrawtransactionwithwallet(tx_hex)
356-
return tx_from_hex(signed_tx["hex"]), pubkey
351+
signed_tx = node.signrawtransactionwithkey(tx_hex, self.privkeys)
352+
return tx_from_hex(signed_tx["hex"]).serialize().hex()
357353

358354
def test_special_transactions_in_filters(self):
359355
"""Test that special transactions are included in block filters.
@@ -387,10 +383,11 @@ def test_assetlock_basic(self):
387383

388384
# Create an AssetLockTx
389385
amount_to_lock = Decimal("0.1")
390-
lock_tx, pubkey = self.create_simple_assetlock(amount_to_lock)
391-
392-
# Send the transaction
393-
txid = node.sendrawtransaction(lock_tx.serialize().hex())
386+
block = node.getblock(node.getblockhash(300), 2)
387+
base_tx = block['tx'][0]['txid']
388+
base_tx_value = block['tx'][0]['vout'][0]['value']
389+
signed_hex = self.create_simple_assetlock(base_tx, base_tx_value, amount_to_lock)
390+
txid = node.sendrawtransaction(signed_hex)
394391

395392
# Verify it's recognized as an AssetLockTx
396393
tx_details = node.getrawtransaction(txid, True)
@@ -444,56 +441,13 @@ def test_assetlock_multiple_outputs(self):
444441
# Create an AssetLockTx with multiple credit outputs
445442
# This tests that all credit outputs are properly included in the filter
446443

447-
# Create keys for multiple credit outputs
448-
key1 = ECKey()
449-
key1.generate()
450-
pubkey1 = key1.get_pubkey().get_bytes()
451-
452-
key2 = ECKey()
453-
key2.generate()
454-
pubkey2 = key2.get_pubkey().get_bytes()
455-
456-
# Get a coin to lock
457-
unspent = node.listunspent()[0]
458-
459-
# Create the AssetLockTx with multiple credit outputs
460-
inputs = [CTxIn(COutPoint(int(unspent["txid"], 16), unspent["vout"]))]
461-
462-
# Multiple credit outputs
463444
amount1 = Decimal("0.03")
464445
amount2 = Decimal("0.04")
465-
credit_outputs = [
466-
CTxOut(int(amount1 * COIN), key_to_p2pkh_script(pubkey1)),
467-
CTxOut(int(amount2 * COIN), key_to_p2pkh_script(pubkey2))
468-
]
469-
470-
# AssetLock payload
471-
lock_tx_payload = CAssetLockTx(1, credit_outputs)
472-
473-
# Calculate remaining amount
474-
fee = Decimal("0.0001")
475-
total_locked = amount1 + amount2
476-
remaining = Decimal(str(unspent['amount'])) - total_locked - fee
477-
478-
# Create the transaction
479-
lock_tx = CTransaction()
480-
lock_tx.vin = inputs
481-
482-
# Add change output
483-
if remaining > 0:
484-
change_addr = node.getnewaddress()
485-
change_script = bytes.fromhex(node.validateaddress(change_addr)['scriptPubKey'])
486-
lock_tx.vout = [CTxOut(int(remaining * COIN), change_script)]
487-
488-
# Add OP_RETURN output
489-
lock_tx.vout.append(CTxOut(int(total_locked * COIN), CScript([OP_RETURN, b""])))
490-
491-
lock_tx.nVersion = 3
492-
lock_tx.nType = 8 # asset lock type
493-
lock_tx.vExtraPayload = lock_tx_payload.serialize()
494446

495-
# Sign and send the transaction
496-
signed_hex = node.signrawtransactionwithwallet(lock_tx.serialize().hex())["hex"]
447+
block = node.getblock(node.getblockhash(301), 2)
448+
base_tx = block['tx'][0]['txid']
449+
base_tx_value = block['tx'][0]['vout'][0]['value']
450+
signed_hex = self.create_simple_assetlock(base_tx, base_tx_value, amount1, amount2)
497451
txid = node.sendrawtransaction(signed_hex)
498452

499453
# Mine it into a block

0 commit comments

Comments
 (0)