Script Program apply method refactor part 3 (#760) (#795)

* Script Program apply method refactor part 3 (#760)

* Remove unsused method

* Documentation

* Fix test case
This commit is contained in:
Ben Carman 2019-10-12 11:09:37 -05:00 committed by Chris Stewart
parent 76f5810078
commit 7641c2b73f
4 changed files with 31 additions and 49 deletions

View file

@ -514,18 +514,14 @@ class ArithmeticInterpreterTest extends FlatSpec with MustMatchers {
it must "evaluate an OP_WITHIN correctly" in {
val stack = List(ScriptNumber(2), ScriptNumber.one, ScriptNumber.zero)
val script = List(OP_WITHIN)
val program = ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag]())
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
val newProgram = AI.opWithin(program)
newProgram.stack must be(List(OP_FALSE))
newProgram.script.isEmpty must be(true)
val stack1 = List(ScriptNumber.one, OP_0, ScriptNumber.zero)
val script1 = List(OP_WITHIN)
val program1 = ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
Seq[ScriptFlag]())
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
val newProgram1 = AI.opWithin(program1)
newProgram1.stack must be(List(OP_TRUE))
newProgram1.script.isEmpty must be(true)
@ -546,9 +542,7 @@ class ArithmeticInterpreterTest extends FlatSpec with MustMatchers {
val stack =
List(ScriptNumber("0000000000000000"), ScriptNumber.one, ScriptNumber.one)
val script = List(OP_WITHIN)
val program = ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag]())
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
val newProgram = AI.opWithin(program)
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
newProgram.asInstanceOf[ExecutedScriptProgram].error must be(
@ -558,9 +552,7 @@ class ArithmeticInterpreterTest extends FlatSpec with MustMatchers {
it must "mark the script as invalid for OP_WITHIN if we do not have 3 stack elements" in {
val stack = List(ScriptNumber("0000000000000000"), ScriptNumber.one)
val script = List(OP_WITHIN)
val program = ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag]())
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
val newProgram = AI.opWithin(program)
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
newProgram.asInstanceOf[ExecutedScriptProgram].error must be(

View file

@ -78,29 +78,20 @@ class ConstantInterpreterTest extends FlatSpec with MustMatchers {
it must "push 0 bytes onto the stack which is OP_0" in {
val stack = List()
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
val program =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag]())
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
val newProgram = CI.opPushData1(program)
newProgram.stackTopIsFalse must be(true)
newProgram.stack must be(List(ScriptNumber.zero))
val stack1 = List()
val script1 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
val program1 =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
Seq[ScriptFlag]())
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
val newProgram1 = CI.opPushData2(program1)
newProgram1.stack must be(List(ScriptNumber.zero))
val stack2 = List()
val script2 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
val program2 =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2),
Seq[ScriptFlag]())
val program2 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2).removeFlags()
val newProgram2 = CI.opPushData4(program2)
newProgram2.stack must be(List(ScriptNumber.zero))
}
@ -108,9 +99,7 @@ class ConstantInterpreterTest extends FlatSpec with MustMatchers {
it must "mark a program as invalid if we have do not have enough bytes to be pushed onto the stack by the push operation" in {
val stack = List()
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(1))
val program = ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag]())
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
val newProgram =
ScriptProgramTestUtil.toExecutedScriptProgram(CI.opPushData1(program))
@ -120,24 +109,15 @@ class ConstantInterpreterTest extends FlatSpec with MustMatchers {
it must "fail the require statement if the first op_code in the program's script doesn't match the OP_PUSHDATA we're looking for" in {
val stack1 = List()
val script1 = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
val program1 =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
Seq[ScriptFlag]())
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
val stack2 = List()
val script2 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
val program2 =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2),
Seq[ScriptFlag]())
val program2 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2).removeFlags()
val stack4 = List()
val script4 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
val program4 =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack4, script4),
Seq[ScriptFlag]())
val program4 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack4, script4).removeFlags()
//purposely call incorrect functions to mismatch opCodes
intercept[IllegalArgumentException] {
@ -168,10 +148,7 @@ class ConstantInterpreterTest extends FlatSpec with MustMatchers {
it must "return ScriptErrorMinimalData if program contains ScriptVerifyMinimalData flag and 2nd item in script is zero" in {
val stack = List()
val script = List(OP_PUSHDATA4, ScriptNumber.zero)
val program =
ScriptProgram(
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
Seq[ScriptFlag](ScriptVerifyMinimalData))
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).replaceFlags(Seq[ScriptFlag](ScriptVerifyMinimalData))
val newProgram =
ScriptProgramTestUtil.toExecutedScriptProgram(CI.opPushData4(program))
newProgram.error must be(Some(ScriptErrorMinimalData))

View file

@ -117,7 +117,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = List(OP_CHECKMULTISIG)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
val programNoFlags = program.removeFlags()
val newProgram = CI.opCheckMultiSig(programNoFlags)
newProgram.stack must be(List(OP_TRUE))
newProgram.script.isEmpty must be(true)
@ -128,7 +128,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = List(OP_CHECKMULTISIG, OP_16, OP_16, OP_16, OP_16)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
val programNoFlags = program.removeFlags()
val newProgram = CI.opCheckMultiSig(programNoFlags)
newProgram.stack must be(List(OP_TRUE, OP_16, OP_16, OP_16))
newProgram.script must be(List(OP_16, OP_16, OP_16, OP_16))
@ -139,7 +139,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = List(OP_CHECKMULTISIGVERIFY)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
val programNoFlags = program.removeFlags()
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
newProgram.script.isEmpty must be(true)
newProgram.stack.isEmpty must be(true)
@ -151,7 +151,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = List(OP_CHECKMULTISIGVERIFY, OP_16, OP_16, OP_16, OP_16)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
val programNoFlags = program.removeFlags()
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
newProgram.stack must be(List(OP_16, OP_16, OP_16))
newProgram.script must be(List(OP_16, OP_16, OP_16, OP_16))
@ -164,7 +164,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = List(OP_CHECKMULTISIG)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
val programNoFlags = program.removeFlags()
val newProgram = CI.opCheckMultiSig(programNoFlags)
newProgram.stack must be(List(OP_TRUE))
newProgram.script.isEmpty must be(true)
@ -207,7 +207,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
val script = Seq(OP_CHECKSIG)
val program =
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
val programWithFlags = ScriptProgram(program, flags)
val programWithFlags = program.replaceFlags(flags)
val newProgram = ScriptProgramTestUtil.toExecutedScriptProgram(
CI.opCheckSig(programWithFlags))
newProgram.error must be(Some(ScriptErrorSigDer))

View file

@ -108,6 +108,19 @@ case class ExecutionInProgressScriptProgram(
override def failExecution(error: ScriptError): ExecutedScriptProgram = {
ScriptProgram.toExecutedProgram(this).failExecution(error)
}
def replaceFlags(
newFlags: Seq[ScriptFlag]): ExecutionInProgressScriptProgram = {
this.copy(flags = newFlags)
}
/**
* Removes the flags on the given [[org.bitcoins.core.script.ScriptProgram ScriptProgram]]
* @return
*/
def removeFlags(): ExecutionInProgressScriptProgram = {
this.replaceFlags(Seq.empty)
}
}
/**