8
8
1351.
9
9
"""
10
10
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
+ )
13
21
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
+ )
15
29
from test_framework .test_framework import BitcoinTestFramework
16
30
from test_framework .util import (
17
31
assert_equal ,
23
37
CLTV_HEIGHT = 1351
24
38
25
39
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
+
26
59
def cltv_invalidate (tx ):
27
60
'''Modify the signature in vin 0 of the tx to fail CLTV
28
61
@@ -31,24 +64,15 @@ def cltv_invalidate(tx):
31
64
TODO: test more ways that transactions using CLTV could be invalid (eg
32
65
locktime requirements fail, sequence time requirements fail, etc).
33
66
'''
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
+
36
69
37
70
def cltv_validate (node , tx , height ):
38
71
'''Modify the signature in vin 0 of the tx to pass CLTV
39
72
Prepends <height> CLTV DROP in the scriptSig, and sets
40
73
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 )
52
76
53
77
54
78
class BIP65Test (BitcoinTestFramework ):
@@ -66,8 +90,7 @@ def skip_test_if_missing_module(self):
66
90
self .skip_if_no_wallet ()
67
91
68
92
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' ], {
71
94
"active" : is_active ,
72
95
"height" : CLTV_HEIGHT ,
73
96
"type" : "buried" ,
@@ -86,7 +109,7 @@ def run_test(self):
86
109
self .log .info ("Test that an invalid-according-to-CLTV transaction can still appear in a block" )
87
110
88
111
spendtx = create_transaction (self .nodes [0 ], self .coinbase_txids [0 ],
89
- self .nodeaddress , amount = 1.0 )
112
+ self .nodeaddress , amount = 1.0 )
90
113
cltv_invalidate (spendtx )
91
114
spendtx .rehash ()
92
115
@@ -119,7 +142,7 @@ def run_test(self):
119
142
block .nVersion = 4
120
143
121
144
spendtx = create_transaction (self .nodes [0 ], self .coinbase_txids [1 ],
122
- self .nodeaddress , amount = 1.0 )
145
+ self .nodeaddress , amount = 1.0 )
123
146
cltv_invalidate (spendtx )
124
147
spendtx .rehash ()
125
148
0 commit comments