script: implement minimalif.

This commit is contained in:
Christopher Jeffrey 2016-09-30 11:20:58 -07:00
parent 144beb343a
commit 63dd30393c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 21 additions and 1 deletions

View File

@ -775,8 +775,9 @@ exports.flags = {
VERIFY_CHECKSEQUENCEVERIFY: (1 << 10), VERIFY_CHECKSEQUENCEVERIFY: (1 << 10),
VERIFY_WITNESS: (1 << 11), VERIFY_WITNESS: (1 << 11),
VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: (1 << 12), VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: (1 << 12),
VERIFY_MAST: (1 << 13), VERIFY_MINIMALIF: (1 << 13),
VERIFY_NULLFAIL: (1 << 14), VERIFY_NULLFAIL: (1 << 14),
VERIFY_MAST: (1 << 15), // should be 1 << 13
VERIFY_SEQUENCE: (1 << 0), VERIFY_SEQUENCE: (1 << 0),
MEDIAN_TIME_PAST: (1 << 1) MEDIAN_TIME_PAST: (1 << 1)
}; };

View File

@ -362,33 +362,52 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
case opcodes.OP_IF: case opcodes.OP_IF:
case opcodes.OP_NOTIF: { case opcodes.OP_NOTIF: {
val = false; val = false;
if (negate === 0) { if (negate === 0) {
if (stack.length < 1) if (stack.length < 1)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
val = Script.bool(stack.pop()); val = Script.bool(stack.pop());
if (version == 1 && (flags & constants.flags.VERIFY_MINIMALIF)) {
if (val.length > 1)
throw new ScriptError('MINIMALIF');
if (val.length === 1 && val[0] !== 1)
throw new ScriptError('MINIMALIF');
}
if (op === opcodes.OP_NOTIF) if (op === opcodes.OP_NOTIF)
val = !val; val = !val;
} }
state.push(val); state.push(val);
if (!val) if (!val)
negate++; negate++;
break; break;
} }
case opcodes.OP_ELSE: { case opcodes.OP_ELSE: {
if (state.length === 0) if (state.length === 0)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
state[state.length - 1] = !state[state.length - 1]; state[state.length - 1] = !state[state.length - 1];
if (!state[state.length - 1]) if (!state[state.length - 1])
negate++; negate++;
else else
negate--; negate--;
break; break;
} }
case opcodes.OP_ENDIF: { case opcodes.OP_ENDIF: {
if (state.length === 0) if (state.length === 0)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
if (!state.pop()) if (!state.pop())
negate--; negate--;
break; break;
} }
case opcodes.OP_VERIF: case opcodes.OP_VERIF: