@@ -332,7 +332,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
332
332
case OP_NOP:
333
333
break ;
334
334
335
- case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
335
+ case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
336
336
case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
337
337
{
338
338
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
@@ -909,6 +909,48 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
909
909
}
910
910
break ;
911
911
912
+ case OP_CHECKLOCKTIMEVERIFY:
913
+ {
914
+ if (!(flags & SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
915
+ // not enabled; treat as a NOP1
916
+ if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
917
+ return set_error (serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
918
+ }
919
+ break ;
920
+ }
921
+
922
+ if (stack.size () < 1 )
923
+ return set_error (serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
924
+
925
+ // Note that elsewhere numeric opcodes are limited to
926
+ // operands in the range -2**31+1 to 2**31-1, however it is
927
+ // legal for opcodes to produce results exceeding that
928
+ // range. This limitation is implemented by CScriptNum's
929
+ // default 4-byte limit.
930
+ //
931
+ // If we kept to that limit we'd have a year 2038 problem,
932
+ // even though the nLockTime field in transactions
933
+ // themselves is uint32 which only becomes meaningless
934
+ // after the year 2106.
935
+ //
936
+ // Thus as a special case we tell CScriptNum to accept up
937
+ // to 5-byte bignums, which are good until 2**32-1, the
938
+ // same limit as the nLockTime field itself.
939
+ const CScriptNum nLockTime (stacktop (-1 ), 5 );
940
+
941
+ // In the rare event that the argument may be < 0 due to
942
+ // some arithmetic being done first, you can always use
943
+ // 0 MAX CHECKLOCKTIMEVERIFY.
944
+ if (nLockTime < 0 )
945
+ return set_error (serror, SCRIPT_ERR_NEGATIVE_LOCKTIME);
946
+
947
+ // Actually compare the specified lock time with the transaction.
948
+ if (!checker.CheckLockTime (nLockTime))
949
+ return set_error (serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME);
950
+
951
+ break ;
952
+ }
953
+
912
954
default :
913
955
return set_error (serror, SCRIPT_ERR_BAD_OPCODE);
914
956
}
@@ -1078,6 +1120,42 @@ bool SignatureChecker::CheckSig(const vector<unsigned char>& vchSigIn, const vec
1078
1120
return true ;
1079
1121
}
1080
1122
1123
+ bool SignatureChecker::CheckLockTime (const CScriptNum& nLockTime) const
1124
+ {
1125
+ // There are two times of nLockTime: lock-by-blockheight
1126
+ // and lock-by-blocktime, distinguished by whether
1127
+ // nLockTime < LOCKTIME_THRESHOLD.
1128
+ //
1129
+ // We want to compare apples to apples, so fail the script
1130
+ // unless the type of nLockTime being tested is the same as
1131
+ // the nLockTime in the transaction.
1132
+ if (!(
1133
+ (txTo.nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) ||
1134
+ (txTo.nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD)
1135
+ ))
1136
+ return false ;
1137
+
1138
+ // Now that we know we're comparing apples-to-apples, the
1139
+ // comparison is a simple numeric one.
1140
+ if (nLockTime > (int64_t )txTo.nLockTime )
1141
+ return false ;
1142
+
1143
+ // Finally the nLockTime feature can be disabled and thus
1144
+ // CHECKLOCKTIMEVERIFY bypassed if every txin has been
1145
+ // finalized by setting nSequence to maxint. The
1146
+ // transaction would be allowed into the blockchain, making
1147
+ // the opcode ineffective.
1148
+ //
1149
+ // Testing if this vin is not final is sufficient to
1150
+ // prevent this condition. Alternatively we could test all
1151
+ // inputs, but testing just this input minimizes the data
1152
+ // required to prove correct CHECKLOCKTIMEVERIFY execution.
1153
+ if (txTo.vin [nIn].IsFinal ())
1154
+ return false ;
1155
+
1156
+ return true ;
1157
+ }
1158
+
1081
1159
bool VerifyScript (const CScript& scriptSig, const CScript& scriptPubKey, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror)
1082
1160
{
1083
1161
set_error (serror, SCRIPT_ERR_UNKNOWN_ERROR);
0 commit comments