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