script: refactor execution.

This commit is contained in:
Christopher Jeffrey 2017-06-30 14:47:13 -07:00
parent 7f1b23223c
commit e89f285e9a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -486,16 +486,10 @@ Script.prototype.removeSeparators = function removeSeparators() {
Script.prototype.execute = function execute(stack, flags, tx, index, value, version) {
let lastSep = 0;
let opCount = 0;
let alt = [];
let state = [];
let negate = 0;
let minimal = false;
let val, v1, v2, v3;
let num, n1, n2, n3;
let op, ip, m, n, key, sig;
let type, subscript, hash;
let ikey, isig, ikey2;
let i, j, res, locktime;
let state = [];
let alt = [];
if (flags == null)
flags = Script.flags.STANDARD_VERIFY_FLAGS;
@ -509,8 +503,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
if (this.getSize() > consensus.MAX_SCRIPT_SIZE)
throw new ScriptError('SCRIPT_SIZE');
for (ip = 0; ip < this.code.length; ip++) {
op = this.code[ip];
for (let ip = 0; ip < this.code.length; ip++) {
let op = this.code[ip];
if (op.value === -1)
throw new ScriptError('BAD_OPCODE', op, ip);
@ -572,6 +566,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_CHECKLOCKTIMEVERIFY: {
let locktime;
// OP_CHECKLOCKTIMEVERIFY = OP_NOP2
if (!(flags & Script.flags.VERIFY_CHECKLOCKTIMEVERIFY)) {
if (flags & Script.flags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
@ -598,6 +594,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_CHECKSEQUENCEVERIFY: {
let locktime;
// OP_CHECKSEQUENCEVERIFY = OP_NOP3
if (!(flags & Script.flags.VERIFY_CHECKSEQUENCEVERIFY)) {
if (flags & Script.flags.VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
@ -637,7 +635,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
case opcodes.OP_IF:
case opcodes.OP_NOTIF: {
val = false;
let val = false;
if (negate === 0) {
if (stack.length < 1)
@ -727,6 +725,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_2DUP: {
let v1, v2;
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -738,6 +738,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_3DUP: {
let v1, v2, v3;
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -751,6 +753,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_2OVER: {
let v1, v2;
if (stack.length < 4)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -762,6 +766,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_2ROT: {
let v1, v2;
if (stack.length < 6)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -782,6 +788,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_IFDUP: {
let val;
if (stack.length === 0)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -825,6 +833,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
case opcodes.OP_PICK:
case opcodes.OP_ROLL: {
let num, val;
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -873,6 +883,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
case opcodes.OP_EQUAL:
case opcodes.OP_EQUALVERIFY: {
let v1, v2, res;
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -900,6 +912,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
case opcodes.OP_ABS:
case opcodes.OP_NOT:
case opcodes.OP_0NOTEQUAL: {
let num;
if (stack.length < 1)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -949,6 +963,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
case opcodes.OP_GREATERTHANOREQUAL:
case opcodes.OP_MIN:
case opcodes.OP_MAX: {
let n1, n2, num;
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -1022,6 +1038,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
break;
}
case opcodes.OP_WITHIN: {
let val, n1, n2, n3;
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
@ -1079,6 +1097,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
case opcodes.OP_CHECKSIG:
case opcodes.OP_CHECKSIGVERIFY: {
let sig, key, res, subscript;
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
@ -1098,8 +1118,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
validateKey(key, flags, version);
if (sig.length > 0) {
type = sig[sig.length - 1];
hash = tx.signatureHash(index, subscript, value, type, version);
let type = sig[sig.length - 1];
let hash = tx.signatureHash(index, subscript, value, type, version);
res = checksig(hash, sig, key);
}
@ -1123,6 +1143,8 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
case opcodes.OP_CHECKMULTISIG:
case opcodes.OP_CHECKMULTISIGVERIFY: {
let i, m, n, isig, ikey, ikey2, subscript, res;
if (!tx)
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
@ -1162,23 +1184,23 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
subscript = this.getSubscript(lastSep);
for (j = 0; j < m; j++) {
sig = stack.top(-isig - j);
for (let j = 0; j < m; j++) {
let sig = stack.top(-isig - j);
if (version === 0)
subscript.removeData(sig);
}
res = true;
while (res && m > 0) {
sig = stack.top(-isig);
key = stack.top(-ikey);
let sig = stack.top(-isig);
let key = stack.top(-ikey);
validateSignature(sig, flags);
validateKey(key, flags, version);
if (sig.length > 0) {
type = sig[sig.length - 1];
hash = tx.signatureHash(index, subscript, value, type, version);
let type = sig[sig.length - 1];
let hash = tx.signatureHash(index, subscript, value, type, version);
if (checksig(hash, sig, key)) {
isig++;
@ -1230,10 +1252,10 @@ Script.prototype.execute = function execute(stack, flags, tx, index, value, vers
}
if (stack.length + alt.length > consensus.MAX_SCRIPT_STACK)
throw new ScriptError('STACK_SIZE', op, ip);
throw new ScriptError('STACK_SIZE');
if (state.length !== 0)
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
throw new ScriptError('UNBALANCED_CONDITIONAL');
return true;
};