Skip to content

Commit ce994e1

Browse files
committed
test: add tx modfication helper function in feature_cltv.py
+ reformat python imports + fix PEP8 warnings (all except E501 line too long)
1 parent b144620 commit ce994e1

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

test/functional/feature_cltv.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,24 @@
88
1351.
99
"""
1010

11-
from test_framework.blocktools import create_coinbase, create_block, create_transaction
12-
from test_framework.messages import CTransaction, msg_block, ToHex
11+
from test_framework.blocktools import (
12+
create_block,
13+
create_coinbase,
14+
create_transaction,
15+
)
16+
from test_framework.messages import (
17+
CTransaction,
18+
ToHex,
19+
msg_block,
20+
)
1321
from test_framework.p2p import P2PInterface
14-
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
22+
from test_framework.script import (
23+
CScript,
24+
CScriptNum,
25+
OP_1NEGATE,
26+
OP_CHECKLOCKTIMEVERIFY,
27+
OP_DROP,
28+
)
1529
from test_framework.test_framework import BitcoinTestFramework
1630
from test_framework.util import (
1731
assert_equal,
@@ -23,6 +37,25 @@
2337
CLTV_HEIGHT = 1351
2438

2539

40+
# Helper function to modify a transaction by
41+
# 1) prepending a given script to the scriptSig of vin 0 and
42+
# 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime
43+
def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None):
44+
if nsequence is not None:
45+
tx.vin[0].nSequence = nsequence
46+
tx.nLockTime = nlocktime
47+
48+
# Need to re-sign, since nSequence and nLockTime changed
49+
signed_result = node.signrawtransactionwithwallet(ToHex(tx))
50+
new_tx = CTransaction()
51+
new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
52+
else:
53+
new_tx = tx
54+
55+
new_tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(new_tx.vin[0].scriptSig)))
56+
return new_tx
57+
58+
2659
def cltv_invalidate(tx):
2760
'''Modify the signature in vin 0 of the tx to fail CLTV
2861
@@ -31,24 +64,15 @@ def cltv_invalidate(tx):
3164
TODO: test more ways that transactions using CLTV could be invalid (eg
3265
locktime requirements fail, sequence time requirements fail, etc).
3366
'''
34-
tx.vin[0].scriptSig = CScript([OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
35-
list(CScript(tx.vin[0].scriptSig)))
67+
cltv_modify_tx(None, tx, [OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP])
68+
3669

3770
def cltv_validate(node, tx, height):
3871
'''Modify the signature in vin 0 of the tx to pass CLTV
3972
Prepends <height> CLTV DROP in the scriptSig, and sets
4073
the locktime to height'''
41-
tx.vin[0].nSequence = 0
42-
tx.nLockTime = height
43-
44-
# Need to re-sign, since nSequence and nLockTime changed
45-
signed_result = node.signrawtransactionwithwallet(ToHex(tx))
46-
new_tx = CTransaction()
47-
new_tx.deserialize(BytesIO(hex_str_to_bytes(signed_result['hex'])))
48-
49-
new_tx.vin[0].scriptSig = CScript([CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP] +
50-
list(CScript(new_tx.vin[0].scriptSig)))
51-
return new_tx
74+
return cltv_modify_tx(node, tx, [CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP],
75+
nsequence=0, nlocktime=height)
5276

5377

5478
class BIP65Test(BitcoinTestFramework):
@@ -66,8 +90,7 @@ def skip_test_if_missing_module(self):
6690
self.skip_if_no_wallet()
6791

6892
def test_cltv_info(self, *, is_active):
69-
assert_equal(self.nodes[0].getblockchaininfo()['softforks']['bip65'],
70-
{
93+
assert_equal(self.nodes[0].getblockchaininfo()['softforks']['bip65'], {
7194
"active": is_active,
7295
"height": CLTV_HEIGHT,
7396
"type": "buried",
@@ -86,7 +109,7 @@ def run_test(self):
86109
self.log.info("Test that an invalid-according-to-CLTV transaction can still appear in a block")
87110

88111
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[0],
89-
self.nodeaddress, amount=1.0)
112+
self.nodeaddress, amount=1.0)
90113
cltv_invalidate(spendtx)
91114
spendtx.rehash()
92115

@@ -119,7 +142,7 @@ def run_test(self):
119142
block.nVersion = 4
120143

121144
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[1],
122-
self.nodeaddress, amount=1.0)
145+
self.nodeaddress, amount=1.0)
123146
cltv_invalidate(spendtx)
124147
spendtx.rehash()
125148

0 commit comments

Comments
 (0)