Skip to content

Commit 8d0ce50

Browse files
committed
test: prepare cltv_invalidate to test all failure reasons in feature_cltv.py
only the "top item on the stack is less than 0" is used in the test right now
1 parent ce994e1 commit 8d0ce50

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

test/functional/feature_cltv.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,35 @@ def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None):
5656
return new_tx
5757

5858

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])
6881

6982

7083
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])
7688

7789

7890
class BIP65Test(BitcoinTestFramework):
@@ -110,7 +122,7 @@ def run_test(self):
110122

111123
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[0],
112124
self.nodeaddress, amount=1.0)
113-
cltv_invalidate(spendtx)
125+
spendtx = cltv_invalidate(self.nodes[0], spendtx, 1)
114126
spendtx.rehash()
115127

116128
tip = self.nodes[0].getbestblockhash()
@@ -143,7 +155,7 @@ def run_test(self):
143155

144156
spendtx = create_transaction(self.nodes[0], self.coinbase_txids[1],
145157
self.nodeaddress, amount=1.0)
146-
cltv_invalidate(spendtx)
158+
spendtx = cltv_invalidate(self.nodes[0], spendtx, 1)
147159
spendtx.rehash()
148160

149161
# First we show that this tx is valid except for CLTV by getting it

0 commit comments

Comments
 (0)