clean up script.js.
This commit is contained in:
parent
dd57bba151
commit
896f1f989c
@ -301,17 +301,14 @@ Witness.isWitness = function isWitness(obj) {
|
||||
* @constructor
|
||||
* @param {Buffer[]?} items - Stack items.
|
||||
* @property {Buffer[]} items - Stack items.
|
||||
* @property {Buffer[]} alt - Alt stack items.
|
||||
* @property {Number[]} state - State of if statements.
|
||||
* @property {Boolean} negate - State of if negations.
|
||||
* @property {Number} length - Size of stack.
|
||||
*/
|
||||
|
||||
function Stack(items) {
|
||||
if (!(this instanceof Stack))
|
||||
return new Stack(items);
|
||||
|
||||
this.items = items || [];
|
||||
this.alt = [];
|
||||
this.state = [];
|
||||
this.negate = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -372,18 +369,17 @@ Stack.prototype.getRedeem = function getRedeem(pop) {
|
||||
*/
|
||||
|
||||
Stack.prototype.clone = function clone() {
|
||||
var stack = new Stack(this.items.slice());
|
||||
stack.alt = this.alt.slice();
|
||||
return stack;
|
||||
return new Stack(this.items.slice());
|
||||
};
|
||||
|
||||
/**
|
||||
* Get total size of the stack, including the alt stack.
|
||||
* @param {Array} alt - Alt stack.
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
Stack.prototype.getSize = function getSize() {
|
||||
return this.items.length + this.alt.length;
|
||||
Stack.prototype.getSize = function getSize(alt) {
|
||||
return this.items.length + alt.length;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -504,26 +500,29 @@ Stack.prototype._swap = function _swap(i1, i2) {
|
||||
|
||||
/**
|
||||
* Perform the OP_TOALTSTACK operation.
|
||||
* @param {Array} alt - Alt stack.
|
||||
* @throws {ScriptError}
|
||||
*/
|
||||
|
||||
Stack.prototype.toalt = function toalt() {
|
||||
|
||||
Stack.prototype.toalt = function toalt(alt) {
|
||||
if (this.length === 0)
|
||||
throw new ScriptError('INVALID_STACK_OPERATION', opcodes.OP_TOALTSTACK);
|
||||
|
||||
this.alt.push(this.pop());
|
||||
alt.push(this.pop());
|
||||
};
|
||||
|
||||
/**
|
||||
* Perform the OP_FROMALTSTACK operation.
|
||||
* @param {Array} alt - Alt stack.
|
||||
* @throws {ScriptError}
|
||||
*/
|
||||
|
||||
Stack.prototype.fromalt = function fromalt() {
|
||||
if (this.alt.length === 0)
|
||||
Stack.prototype.fromalt = function fromalt(alt) {
|
||||
if (alt.length === 0)
|
||||
throw new ScriptError('INVALID_ALTSTACK_OPERATION', opcodes.OP_FROMALTSTACK);
|
||||
|
||||
this.push(this.alt.pop());
|
||||
this.push(alt.pop());
|
||||
};
|
||||
|
||||
/**
|
||||
@ -793,7 +792,7 @@ Stack.prototype.size = function size() {
|
||||
*/
|
||||
|
||||
Stack.isStack = function isStack(obj) {
|
||||
return obj && Array.isArray(obj.alt) && typeof obj.swap2 === 'function';
|
||||
return obj && Array.isArray(obj.items) && typeof obj.swap2 === 'function';
|
||||
};
|
||||
|
||||
/**
|
||||
@ -988,6 +987,9 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
var ip = 0;
|
||||
var lastSep = 0;
|
||||
var opCount = 0;
|
||||
var alt = [];
|
||||
var state = [];
|
||||
var negate = 0;
|
||||
var op, val, v1, v2, v3;
|
||||
var n, n1, n2, n3;
|
||||
var res, key, sig, type, subscript, hash;
|
||||
@ -997,12 +999,6 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
if (flags == null)
|
||||
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
||||
|
||||
// The alt stack and execution
|
||||
// stack are local to the script.
|
||||
stack.alt.length = 0;
|
||||
stack.state.length = 0;
|
||||
stack.negate = 0;
|
||||
|
||||
if (this.getSize() > constants.script.MAX_SIZE)
|
||||
throw new ScriptError('SCRIPT_SIZE');
|
||||
|
||||
@ -1017,7 +1013,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
throw new ScriptError('PUSH_SIZE', op, ip);
|
||||
// Note that minimaldata is not checked
|
||||
// on unexecuted branches of code.
|
||||
if (stack.negate === 0) {
|
||||
if (negate === 0) {
|
||||
if (!Script.checkMinimal(op, flags))
|
||||
throw new ScriptError('MINIMALDATA', op, ip);
|
||||
stack.push(op);
|
||||
@ -1055,36 +1051,36 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
switch (op) {
|
||||
case opcodes.OP_IF:
|
||||
case opcodes.OP_NOTIF: {
|
||||
if (stack.negate === 0) {
|
||||
if (negate === 0) {
|
||||
if (stack.length < 1)
|
||||
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
|
||||
val = Script.bool(stack.pop());
|
||||
if (op === opcodes.OP_NOTIF)
|
||||
val = !val;
|
||||
stack.state.push(val === true ? 1 : 0);
|
||||
state.push(val === true ? 1 : 0);
|
||||
if (!val)
|
||||
stack.negate++;
|
||||
negate++;
|
||||
} else {
|
||||
stack.state.push(0);
|
||||
stack.negate++;
|
||||
state.push(0);
|
||||
negate++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_ELSE: {
|
||||
if (stack.state.length === 0)
|
||||
if (state.length === 0)
|
||||
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
|
||||
stack.state[stack.state.length - 1] ^= 1;
|
||||
if (stack.state[stack.state.length - 1] === 0)
|
||||
stack.negate++;
|
||||
state[state.length - 1] ^= 1;
|
||||
if (state[state.length - 1] === 0)
|
||||
negate++;
|
||||
else
|
||||
stack.negate--;
|
||||
negate--;
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_ENDIF: {
|
||||
if (stack.state.length === 0)
|
||||
if (state.length === 0)
|
||||
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
|
||||
if (stack.state.pop() === 0)
|
||||
stack.negate--;
|
||||
if (state.pop() === 0)
|
||||
negate--;
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_VERIF:
|
||||
@ -1098,7 +1094,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stack.negate !== 0)
|
||||
if (negate !== 0)
|
||||
continue;
|
||||
|
||||
switch (op) {
|
||||
@ -1210,11 +1206,11 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
throw new ScriptError('OP_RETURN', op, ip);
|
||||
}
|
||||
case opcodes.OP_TOALTSTACK: {
|
||||
stack.toalt();
|
||||
stack.toalt(alt);
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_FROMALTSTACK: {
|
||||
stack.fromalt();
|
||||
stack.fromalt(alt);
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_2DROP: {
|
||||
@ -1718,10 +1714,10 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
}
|
||||
}
|
||||
|
||||
if (stack.getSize() > constants.script.MAX_STACK)
|
||||
if (stack.getSize(alt) > constants.script.MAX_STACK)
|
||||
throw new ScriptError('STACK_SIZE', op, ip);
|
||||
|
||||
if (stack.state.length !== 0)
|
||||
if (state.length !== 0)
|
||||
throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip);
|
||||
|
||||
return true;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user