Src files now compile on using ScriptProgram's new factory functions

This commit is contained in:
Chris Stewart 2016-04-19 19:30:06 -05:00
parent 87beb9ec8d
commit 8a45232d22
2 changed files with 190 additions and 153 deletions

View file

@ -435,5 +435,38 @@ object ScriptProgram {
def apply(txSignatureComponent : TransactionSignatureComponent, stack : Seq[ScriptToken], script : Seq[ScriptToken]) : ScriptProgram =
factory(txSignatureComponent, stack, script)
/**
* Changes a program that is being executed inside o
* @param executionInProgressScriptProgram
* @return
*/
def toExecutedProgram(executionInProgressScriptProgram: ExecutionInProgressScriptProgram) : ExecutedScriptProgram = {
ExecutedScriptProgramImpl(executionInProgressScriptProgram.txSignatureComponent, executionInProgressScriptProgram.stack,
executionInProgressScriptProgram.script,executionInProgressScriptProgram.originalScript,executionInProgressScriptProgram.altStack,
executionInProgressScriptProgram.flags,None)
}
/**
* Takes a script program that is pre execution and changes it to an execution in progress script program
* @param preExecutionScriptProgram
* @return
*/
def toExecutionInProgress(preExecutionScriptProgram: PreExecutionScriptProgram) : ExecutionInProgressScriptProgram = {
toExecutionInProgress(preExecutionScriptProgram,List())
}
/**
* Changes a pre execution script program to a execution in progress script program with the given stack state
* @param preExecutionScriptProgram
* @param stack
* @return
*/
def toExecutionInProgress(preExecutionScriptProgram: PreExecutionScriptProgram, stack : List[ScriptToken]) : ExecutionInProgressScriptProgram = {
ExecutionInProgressScriptProgramImpl(preExecutionScriptProgram.txSignatureComponent,stack,preExecutionScriptProgram.script,
preExecutionScriptProgram.originalScript,preExecutionScriptProgram.altStack,preExecutionScriptProgram.flags, 0)
}
}

View file

@ -2,10 +2,11 @@ package org.scalacoin.script.interpreter
import org.scalacoin.protocol.script._
import org.scalacoin.protocol.transaction.Transaction
import org.scalacoin.script.error._
import org.scalacoin.script.flag._
import org.scalacoin.script.locktime.{OP_CHECKLOCKTIMEVERIFY, LockTimeInterpreter}
import org.scalacoin.script.splice._
import org.scalacoin.script.{ScriptProgram}
import org.scalacoin.script.{ExecutionInProgressScriptProgram, PreExecutionScriptProgram, ExecutedScriptProgram, ScriptProgram}
import org.scalacoin.script.arithmetic._
import org.scalacoin.script.bitwise._
import org.scalacoin.script.constant._
@ -64,173 +65,176 @@ trait ScriptInterpreter extends CryptoInterpreter with StackInterpreter with Con
if (opCount > maxScriptOps) {
logger.error("We have reached the maximum amount of script operations allowed")
logger.error("Here are the remaining operations in the script: " + program.script)
(false,ScriptProgram(program,false))
(false,ScriptProgram(program,ScriptErrorOpCount))
} else if (program.script.flatMap(_.bytes).size > 10000) {
logger.error("We cannot run a script that is larger than 10,000 bytes")
(false,ScriptProgram(program,false))
} else {
program.script match {
//if at any time we see that the program is not valid
//cease script execution
case _ if !program.isValid =>
logger.error("Script program was marked as invalid: " + program)
(false, ScriptProgram(program, false))
case _ if !program.script.intersect(Seq(OP_VERIF, OP_VERNOTIF)).isEmpty =>
logger.error("Script is invalid even when a OP_VERIF or OP_VERNOTIF occurs in an unexecuted OP_IF branch")
(false, ScriptProgram(program, false))
//disabled splice operation
case _ if !program.script.intersect(Seq(OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT)).isEmpty =>
logger.error("Script is invalid because it contains a disabled splice operation")
(false, ScriptProgram(program, false))
//disabled bitwise operations
case _ if !program.script.intersect(Seq(OP_INVERT, OP_AND, OP_OR, OP_XOR)).isEmpty =>
logger.error("Script is invalid because it contains a disabled bitwise operation")
(false, ScriptProgram(program, false))
//disabled arithmetic operations
case _ if !program.script.intersect(Seq(OP_MUL, OP_2MUL, OP_DIV, OP_2DIV, OP_MOD, OP_LSHIFT, OP_RSHIFT)).isEmpty =>
logger.error("Script is invalid because it contains a disabled arithmetic operation")
(false, ScriptProgram(program, false))
//program cannot contain a push operation > 520 bytes
case _ if (program.script.exists(token => token.bytes.size > 520)) =>
logger.error("We have a script constant that is larger than 520 bytes, this is illegal: " + program.script)
loop(ScriptProgram(program, false))
//program stack size cannot be greater than 1000 elements
case _ if ((program.stack.size + program.altStack.size) > 1000) =>
logger.error("We cannot have a stack + alt stack size larger than 1000 elements")
loop(ScriptProgram(program, false))
(false, ScriptProgram(program, ScriptErrorScriptSize))
} else {
program match {
case p : PreExecutionScriptProgram => loop(ScriptProgram.toExecutionInProgress(p,p.stack))
case p : ExecutedScriptProgram => (p.error.isDefined, p)
case p : ExecutionInProgressScriptProgram =>
p.script match {
//if at any time we see that the program is not valid
//cease script execution
case _ if !p.script.intersect(Seq(OP_VERIF, OP_VERNOTIF)).isEmpty =>
logger.error("Script is invalid even when a OP_VERIF or OP_VERNOTIF occurs in an unexecuted OP_IF branch")
(false, ScriptProgram(p, ScriptErrorDisabledOpCode))
//disabled splice operation
case _ if !p.script.intersect(Seq(OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT)).isEmpty =>
logger.error("Script is invalid because it contains a disabled splice operation")
(false, ScriptProgram(p, ScriptErrorDisabledOpCode))
//disabled bitwise operations
case _ if !p.script.intersect(Seq(OP_INVERT, OP_AND, OP_OR, OP_XOR)).isEmpty =>
logger.error("Script is invalid because it contains a disabled bitwise operation")
(false, ScriptProgram(p, ScriptErrorDisabledOpCode))
//disabled arithmetic operations
case _ if !p.script.intersect(Seq(OP_MUL, OP_2MUL, OP_DIV, OP_2DIV, OP_MOD, OP_LSHIFT, OP_RSHIFT)).isEmpty =>
logger.error("Script is invalid because it contains a disabled arithmetic operation")
(false, ScriptProgram(p, ScriptErrorDisabledOpCode))
//program cannot contain a push operation > 520 bytes
case _ if (p.script.exists(token => token.bytes.size > 520)) =>
logger.error("We have a script constant that is larger than 520 bytes, this is illegal: " + p.script)
loop(ScriptProgram(p, ScriptErrorPushSize))
//program stack size cannot be greater than 1000 elements
case _ if ((p.stack.size + p.altStack.size) > 1000) =>
logger.error("We cannot have a stack + alt stack size larger than 1000 elements")
loop(ScriptProgram(p, ScriptErrorStackSize))
//stack operations
case OP_DUP :: t => loop(opDup(program))
case OP_DEPTH :: t => loop(opDepth(program))
case OP_TOALTSTACK :: t => loop(opToAltStack(program))
case OP_FROMALTSTACK :: t => loop(opFromAltStack(program))
case OP_DROP :: t => loop(opDrop(program))
case OP_IFDUP :: t => loop(opIfDup(program))
case OP_NIP :: t => loop(opNip(program))
case OP_OVER :: t => loop(opOver(program))
case OP_PICK :: t => loop(opPick(program))
case OP_ROLL :: t => loop(opRoll(program))
case OP_ROT :: t => loop(opRot(program))
case OP_2ROT :: t => loop(op2Rot(program))
case OP_2DROP :: t => loop(op2Drop(program))
case OP_SWAP :: t => loop(opSwap(program))
case OP_TUCK :: t => loop(opTuck(program))
case OP_2DUP :: t => loop(op2Dup(program))
case OP_3DUP :: t => loop(op3Dup(program))
case OP_2OVER :: t => loop(op2Over(program))
case OP_2SWAP :: t => loop(op2Swap(program))
//stack operations
case OP_DUP :: t => loop(opDup(p))
case OP_DEPTH :: t => loop(opDepth(p))
case OP_TOALTSTACK :: t => loop(opToAltStack(p))
case OP_FROMALTSTACK :: t => loop(opFromAltStack(p))
case OP_DROP :: t => loop(opDrop(p))
case OP_IFDUP :: t => loop(opIfDup(p))
case OP_NIP :: t => loop(opNip(p))
case OP_OVER :: t => loop(opOver(p))
case OP_PICK :: t => loop(opPick(p))
case OP_ROLL :: t => loop(opRoll(p))
case OP_ROT :: t => loop(opRot(p))
case OP_2ROT :: t => loop(op2Rot(p))
case OP_2DROP :: t => loop(op2Drop(p))
case OP_SWAP :: t => loop(opSwap(p))
case OP_TUCK :: t => loop(opTuck(p))
case OP_2DUP :: t => loop(op2Dup(p))
case OP_3DUP :: t => loop(op3Dup(p))
case OP_2OVER :: t => loop(op2Over(p))
case OP_2SWAP :: t => loop(op2Swap(p))
//arithmetic operations
case OP_ADD :: t => loop(opAdd(program))
case OP_1ADD :: t => loop(op1Add(program))
case OP_1SUB :: t => loop(op1Sub(program))
case OP_SUB :: t => loop(opSub(program))
case OP_ABS :: t => loop(opAbs(program))
case OP_NEGATE :: t => loop(opNegate(program))
case OP_NOT :: t => loop(opNot(program))
case OP_0NOTEQUAL :: t => loop(op0NotEqual(program))
case OP_BOOLAND :: t => loop(opBoolAnd(program))
case OP_BOOLOR :: t => loop(opBoolOr(program))
case OP_NUMEQUAL :: t => loop(opNumEqual(program))
case OP_NUMEQUALVERIFY :: t => loop(opNumEqualVerify(program))
case OP_NUMNOTEQUAL :: t => loop(opNumNotEqual(program))
case OP_LESSTHAN :: t => loop(opLessThan(program))
case OP_GREATERTHAN :: t => loop(opGreaterThan(program))
case OP_LESSTHANOREQUAL :: t => loop(opLessThanOrEqual(program))
case OP_GREATERTHANOREQUAL :: t => loop(opGreaterThanOrEqual(program))
case OP_MIN :: t => loop(opMin(program))
case OP_MAX :: t => loop(opMax(program))
case OP_WITHIN :: t => loop(opWithin(program))
//arithmetic operations
case OP_ADD :: t => loop(opAdd(p))
case OP_1ADD :: t => loop(op1Add(p))
case OP_1SUB :: t => loop(op1Sub(p))
case OP_SUB :: t => loop(opSub(p))
case OP_ABS :: t => loop(opAbs(p))
case OP_NEGATE :: t => loop(opNegate(p))
case OP_NOT :: t => loop(opNot(p))
case OP_0NOTEQUAL :: t => loop(op0NotEqual(p))
case OP_BOOLAND :: t => loop(opBoolAnd(p))
case OP_BOOLOR :: t => loop(opBoolOr(p))
case OP_NUMEQUAL :: t => loop(opNumEqual(p))
case OP_NUMEQUALVERIFY :: t => loop(opNumEqualVerify(p))
case OP_NUMNOTEQUAL :: t => loop(opNumNotEqual(p))
case OP_LESSTHAN :: t => loop(opLessThan(p))
case OP_GREATERTHAN :: t => loop(opGreaterThan(p))
case OP_LESSTHANOREQUAL :: t => loop(opLessThanOrEqual(p))
case OP_GREATERTHANOREQUAL :: t => loop(opGreaterThanOrEqual(p))
case OP_MIN :: t => loop(opMin(p))
case OP_MAX :: t => loop(opMax(p))
case OP_WITHIN :: t => loop(opWithin(p))
//bitwise operations
case OP_EQUAL :: t =>
val newProgram = opEqual(program)
loop(newProgram)
//bitwise operations
case OP_EQUAL :: t =>
val newProgram = opEqual(p)
loop(newProgram)
case OP_EQUALVERIFY :: t => loop(opEqualVerify(program))
case OP_EQUALVERIFY :: t => loop(opEqualVerify(p))
case (scriptNumberOp: ScriptNumberOperation) :: t =>
if (scriptNumberOp == OP_0) loop(ScriptProgram(program, ScriptNumberFactory.zero :: program.stack, t))
else loop(ScriptProgram(program, ScriptNumberFactory.fromNumber(scriptNumberOp.num) :: program.stack, t))
case (bytesToPushOntoStack: BytesToPushOntoStack) :: t => loop(pushScriptNumberBytesToStack(program))
case (scriptNumber: ScriptNumber) :: t =>
loop(ScriptProgram(program, scriptNumber :: program.stack, t))
case OP_PUSHDATA1 :: t => loop(opPushData1(program))
case OP_PUSHDATA2 :: t => loop(opPushData2(program))
case OP_PUSHDATA4 :: t => loop(opPushData4(program))
case (scriptNumberOp: ScriptNumberOperation) :: t =>
if (scriptNumberOp == OP_0) loop(ScriptProgram(p, ScriptNumberFactory.zero :: p.stack, t))
else loop(ScriptProgram(p, ScriptNumberFactory.fromNumber(scriptNumberOp.num) :: p.stack, t))
case (bytesToPushOntoStack: BytesToPushOntoStack) :: t => loop(pushScriptNumberBytesToStack(p))
case (scriptNumber: ScriptNumber) :: t =>
loop(ScriptProgram(p, scriptNumber :: p.stack, t))
case OP_PUSHDATA1 :: t => loop(opPushData1(p))
case OP_PUSHDATA2 :: t => loop(opPushData2(p))
case OP_PUSHDATA4 :: t => loop(opPushData4(p))
case (x : ScriptConstant) :: t => loop(ScriptProgram(program, x :: program.stack, t))
case (x : ScriptConstant) :: t => loop(ScriptProgram(p, x :: p.stack, t))
//control operations
case OP_IF :: t => loop(opIf(program))
case OP_NOTIF :: t => loop(opNotIf(program))
case OP_ELSE :: t => loop(opElse(program))
case OP_ENDIF :: t => loop(opEndIf(program))
case OP_RETURN :: t =>
val newProgram = opReturn(program)
(newProgram.isValid, newProgram)
case OP_VERIFY :: t => loop(opVerify(program))
//control operations
case OP_IF :: t => loop(opIf(p))
case OP_NOTIF :: t => loop(opNotIf(p))
case OP_ELSE :: t => loop(opElse(p))
case OP_ENDIF :: t => loop(opEndIf(p))
case OP_RETURN :: t => loop(opReturn(p))
//crypto operations
case OP_HASH160 :: t => loop(opHash160(program))
case OP_CHECKSIG :: t => loop(opCheckSig(program))
case OP_SHA1 :: t => loop(opSha1(program))
case OP_RIPEMD160 :: t => loop(opRipeMd160(program))
case OP_SHA256 :: t => loop(opSha256(program))
case OP_HASH256 :: t => loop(opHash256(program))
case OP_CODESEPARATOR :: t => loop(opCodeSeparator(program))
case OP_CHECKMULTISIG :: t => loop(opCheckMultiSig(program))
case OP_CHECKMULTISIGVERIFY :: t => loop(opCheckMultiSigVerify(program))
//reserved operations
case OP_NOP :: t =>
//script discourage upgradeable flag does not apply to a OP_NOP
loop(ScriptProgram(program, program.stack, t))
case OP_VERIFY :: t => loop(opVerify(p))
//if we see an OP_NOP and the DISCOURAGE_UPGRADABLE_OP_NOPS flag is set we must fail our program
case (nop: NOP) :: t if ScriptFlagUtil.discourageUpgradableNOPs(program.flags) =>
logger.error("We cannot execute a NOP when the ScriptVerifyDiscourageUpgradableNOPs is set")
(false, ScriptProgram(program, false))
case (nop: NOP) :: t => loop(ScriptProgram(program, program.stack, t))
case OP_RESERVED :: t =>
logger.error("OP_RESERVED automatically marks transaction invalid")
(false, program)
case OP_VER :: t =>
logger.error("Transaction is invalid when executing OP_VER")
(false, program)
case OP_RESERVED1 :: t =>
logger.error("Transaction is invalid when executing OP_RESERVED1")
(false, program)
case OP_RESERVED2 :: t =>
logger.error("Transaction is invalid when executing OP_RESERVED2")
(false, program)
//crypto operations
case OP_HASH160 :: t => loop(opHash160(p))
case OP_CHECKSIG :: t => loop(opCheckSig(p))
case OP_SHA1 :: t => loop(opSha1(p))
case OP_RIPEMD160 :: t => loop(opRipeMd160(p))
case OP_SHA256 :: t => loop(opSha256(p))
case OP_HASH256 :: t => loop(opHash256(p))
case OP_CODESEPARATOR :: t => loop(opCodeSeparator(p))
case OP_CHECKMULTISIG :: t => loop(opCheckMultiSig(p))
case OP_CHECKMULTISIGVERIFY :: t => loop(opCheckMultiSigVerify(p))
//reserved operations
case OP_NOP :: t =>
//script discourage upgradeable flag does not apply to a OP_NOP
loop(ScriptProgram(p, p.stack, t))
case (reservedOperation : ReservedOperation) :: t =>
logger.error("Undefined operation found which automatically fails the script: " + reservedOperation)
loop(ScriptProgram(program,false))
//splice operations
case OP_SIZE :: t => loop(opSize(program))
//if we see an OP_NOP and the DISCOURAGE_UPGRADABLE_OP_NOPS flag is set we must fail our program
case (nop: NOP) :: t if ScriptFlagUtil.discourageUpgradableNOPs(p.flags) =>
logger.error("We cannot execute a NOP when the ScriptVerifyDiscourageUpgradableNOPs is set")
loop(ScriptProgram(p, ScriptErrorDiscourageUpgradableNOPs))
case (nop: NOP) :: t => loop(ScriptProgram(p, p.stack, t))
case OP_RESERVED :: t =>
logger.error("OP_RESERVED automatically marks transaction invalid")
(false, p)
case OP_VER :: t =>
logger.error("Transaction is invalid when executing OP_VER")
(false, p)
case OP_RESERVED1 :: t =>
logger.error("Transaction is invalid when executing OP_RESERVED1")
(false, p)
case OP_RESERVED2 :: t =>
logger.error("Transaction is invalid when executing OP_RESERVED2")
(false, p)
//locktime operations
case OP_CHECKLOCKTIMEVERIFY :: t =>
//check if CLTV is enforced yet
if (ScriptFlagUtil.checkLockTimeVerifyEnabled(program.flags)) loop(opCheckLockTimeVerify(program))
//if not, check to see if we should discourage NOPs
else if (ScriptFlagUtil.discourageUpgradableNOPs(program.flags)) {
logger.error("We cannot execute a NOP when the ScriptVerifyDiscourageUpgradableNOPs is set")
(false, ScriptProgram(program, false))
case (reservedOperation : ReservedOperation) :: t =>
logger.error("Undefined operation found which automatically fails the script: " + reservedOperation)
loop(ScriptProgram(p,ScriptErrorBadOpCode))
//splice operations
case OP_SIZE :: t => loop(opSize(p))
//locktime operations
case OP_CHECKLOCKTIMEVERIFY :: t =>
//check if CLTV is enforced yet
if (ScriptFlagUtil.checkLockTimeVerifyEnabled(p.flags)) loop(opCheckLockTimeVerify(p))
//if not, check to see if we should discourage p
else if (ScriptFlagUtil.discourageUpgradableNOPs(p.flags)) {
logger.error("We cannot execute a NOP when the ScriptVerifyDiscourageUpgradableNOPs is set")
loop(ScriptProgram(p, ScriptErrorDiscourageUpgradableNOPs))
}
//in this case, just reat OP_CLTV just like a NOP and remove it from the stack
else loop(ScriptProgram(p, p.script.tail, ScriptProgram.Script))
//no more script operations to run, return whether the program is valid and the final state of the program
case Nil =>
//reset opCount variable to zero since we may need to count the ops
//in the scriptPubKey - we don't want the op count of the scriptSig
//to count towards the scriptPubKey op count
opCount = 0
loop(ScriptProgram.toExecutedProgram(p))
case h :: t => throw new RuntimeException(h + " was unmatched")
}
//in this case, just reat OP_CLTV just like a NOP and remove it from the stack
else loop(ScriptProgram(program, program.script.tail, ScriptProgram.Script))
//no more script operations to run, return whether the program is valid and the final state of the program
case Nil =>
//reset opCount variable to zero since we may need to count the ops
//in the scriptPubKey - we don't want the op count of the scriptSig
//to count towards the scriptPubKey op count
opCount = 0
(program.isValid, program)
case h :: t => throw new RuntimeException(h + " was unmatched")
}
}
}