@@ -56,23 +56,35 @@ def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None):
56
56
return new_tx
57
57
58
58
59
- def cltv_invalidate (tx ):
60
- '''Modify the signature in vin 0 of the tx to fail CLTV
61
-
62
- Prepends -1 CLTV DROP in the scriptSig itself.
63
-
64
- TODO: test more ways that transactions using CLTV could be invalid (eg
65
- locktime requirements fail, sequence time requirements fail, etc).
66
- '''
67
- cltv_modify_tx (None , tx , [OP_1NEGATE , OP_CHECKLOCKTIMEVERIFY , OP_DROP ])
59
+ def cltv_invalidate (node , tx , failure_reason ):
60
+ # Modify the signature in vin 0 and nSequence/nLockTime of the tx to fail CLTV
61
+ #
62
+ # According to BIP65, OP_CHECKLOCKTIMEVERIFY can fail due the following reasons:
63
+ # 1) the stack is empty
64
+ # 2) the top item on the stack is less than 0
65
+ # 3) the lock-time type (height vs. timestamp) of the top stack item and the
66
+ # nLockTime field are not the same
67
+ # 4) the top stack item is greater than the transaction's nLockTime field
68
+ # 5) the nSequence field of the txin is 0xffffffff
69
+ assert failure_reason in range (5 )
70
+ scheme = [
71
+ # | Script to prepend to scriptSig | nSequence | nLockTime |
72
+ # +-------------------------------------------------+------------+--------------+
73
+ [[OP_CHECKLOCKTIMEVERIFY ], None , None ],
74
+ [[OP_1NEGATE , OP_CHECKLOCKTIMEVERIFY , OP_DROP ], None , None ],
75
+ [[CScriptNum (1000 ), OP_CHECKLOCKTIMEVERIFY , OP_DROP ], 0 , 1296688602 ], # timestamp of genesis block
76
+ [[CScriptNum (1000 ), OP_CHECKLOCKTIMEVERIFY , OP_DROP ], 0 , 500 ],
77
+ [[CScriptNum (500 ), OP_CHECKLOCKTIMEVERIFY , OP_DROP ], 0xffffffff , 500 ],
78
+ ][failure_reason ]
79
+
80
+ return cltv_modify_tx (node , tx , prepend_scriptsig = scheme [0 ], nsequence = scheme [1 ], nlocktime = scheme [2 ])
68
81
69
82
70
83
def cltv_validate (node , tx , height ):
71
- '''Modify the signature in vin 0 of the tx to pass CLTV
72
- Prepends <height> CLTV DROP in the scriptSig, and sets
73
- the locktime to height'''
74
- return cltv_modify_tx (node , tx , [CScriptNum (height ), OP_CHECKLOCKTIMEVERIFY , OP_DROP ],
75
- nsequence = 0 , nlocktime = height )
84
+ # Modify the signature in vin 0 and nSequence/nLockTime of the tx to pass CLTV
85
+ scheme = [[CScriptNum (height ), OP_CHECKLOCKTIMEVERIFY , OP_DROP ], 0 , height ]
86
+
87
+ return cltv_modify_tx (node , tx , prepend_scriptsig = scheme [0 ], nsequence = scheme [1 ], nlocktime = scheme [2 ])
76
88
77
89
78
90
class BIP65Test (BitcoinTestFramework ):
@@ -110,7 +122,7 @@ def run_test(self):
110
122
111
123
spendtx = create_transaction (self .nodes [0 ], self .coinbase_txids [0 ],
112
124
self .nodeaddress , amount = 1.0 )
113
- cltv_invalidate (spendtx )
125
+ spendtx = cltv_invalidate (self . nodes [ 0 ], spendtx , 1 )
114
126
spendtx .rehash ()
115
127
116
128
tip = self .nodes [0 ].getbestblockhash ()
@@ -143,7 +155,7 @@ def run_test(self):
143
155
144
156
spendtx = create_transaction (self .nodes [0 ], self .coinbase_txids [1 ],
145
157
self .nodeaddress , amount = 1.0 )
146
- cltv_invalidate (spendtx )
158
+ spendtx = cltv_invalidate (self . nodes [ 0 ], spendtx , 1 )
147
159
spendtx .rehash ()
148
160
149
161
# First we show that this tx is valid except for CLTV by getting it
0 commit comments