The functional test for BIP65 / OP_CHECKLOCKTIMEVERIFY (feature_cltv.py) currently only tests one out of five conditions that lead to failure of the op-code -- by prepending the script OP_1NEGATE OP_CHECKLOCKTIMEVERIFY OP_DROP to a tx's first input's scriptSig, the case of "the top item on the stack is less than 0" is checked:
This PR adds the other cases (5 in total) by taking an integer argument to the function cltv_invalidate that is called in a loop instead of only once per testing scenario. Here is the full list of failure conditions and how they are tested (note that the scriptSig should still be valid before activation of BIP65, when OP_CLTV is simply a no-op):
- the stack is empty
➡️ prepending
OP_CHECKLOCKTIMEVERIFYto scriptSig - the top item on the stack is less than 0
➡️ prepending
OP_1NEGATE OP_CHECKLOCKTIMEVERIFY OP_DROPto scriptSig - the lock-time type (height vs. timestamp) of the top stack item and the nLockTime field are not the same
➡️ prepending
OPNum(1000) OP_CHECKLOCKTIMEVERIFY OP_DROPto scriptSig ➡️ setting tx.vin[0].nSequence=0 and tx.nCheckTimeLock=1296688602 (genesis block timestamp) - the top stack item is greater than the transaction's nLockTime field
➡️ prepending
OPNum(1000) OP_CHECKLOCKTIMEVERIFY OP_DROPto scriptSig ➡️ setting tx.vin[0].nSequence=0 and tx.nCheckTimeLock=500 - the nSequence field of the txin is 0xffffffff
➡️ prepending
OPNum(500) OP_CHECKLOCKTIMEVERIFY OP_DROPto scriptSig ➡️ setting tx.vin[0].nSequence=0xffffffff and tx.nCheckTimeLock=500
The first commit creates a helper function for the tx modification and also includes some tidying up like turning single-line to multi-line Python imports where necessary and cleaning up some PEP8 warnings. The second commit prepares the invalidation function cltv_invalidate and the third and the fourth use it and check for the expected reject reason strings ("Operation not valid with the current stack size", "Negative locktime" and "Locktime requirement not satisfied").