mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 18:47:38 +01:00
* Script Program apply method refactor part 3 (#760) * Remove unsused method * Documentation * Fix test case
This commit is contained in:
parent
76f5810078
commit
7641c2b73f
4 changed files with 31 additions and 49 deletions
|
@ -514,18 +514,14 @@ class ArithmeticInterpreterTest extends FlatSpec with MustMatchers {
|
||||||
it must "evaluate an OP_WITHIN correctly" in {
|
it must "evaluate an OP_WITHIN correctly" in {
|
||||||
val stack = List(ScriptNumber(2), ScriptNumber.one, ScriptNumber.zero)
|
val stack = List(ScriptNumber(2), ScriptNumber.one, ScriptNumber.zero)
|
||||||
val script = List(OP_WITHIN)
|
val script = List(OP_WITHIN)
|
||||||
val program = ScriptProgram(
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram = AI.opWithin(program)
|
val newProgram = AI.opWithin(program)
|
||||||
newProgram.stack must be(List(OP_FALSE))
|
newProgram.stack must be(List(OP_FALSE))
|
||||||
newProgram.script.isEmpty must be(true)
|
newProgram.script.isEmpty must be(true)
|
||||||
|
|
||||||
val stack1 = List(ScriptNumber.one, OP_0, ScriptNumber.zero)
|
val stack1 = List(ScriptNumber.one, OP_0, ScriptNumber.zero)
|
||||||
val script1 = List(OP_WITHIN)
|
val script1 = List(OP_WITHIN)
|
||||||
val program1 = ScriptProgram(
|
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram1 = AI.opWithin(program1)
|
val newProgram1 = AI.opWithin(program1)
|
||||||
newProgram1.stack must be(List(OP_TRUE))
|
newProgram1.stack must be(List(OP_TRUE))
|
||||||
newProgram1.script.isEmpty must be(true)
|
newProgram1.script.isEmpty must be(true)
|
||||||
|
@ -546,9 +542,7 @@ class ArithmeticInterpreterTest extends FlatSpec with MustMatchers {
|
||||||
val stack =
|
val stack =
|
||||||
List(ScriptNumber("0000000000000000"), ScriptNumber.one, ScriptNumber.one)
|
List(ScriptNumber("0000000000000000"), ScriptNumber.one, ScriptNumber.one)
|
||||||
val script = List(OP_WITHIN)
|
val script = List(OP_WITHIN)
|
||||||
val program = ScriptProgram(
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram = AI.opWithin(program)
|
val newProgram = AI.opWithin(program)
|
||||||
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
|
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
|
||||||
newProgram.asInstanceOf[ExecutedScriptProgram].error must be(
|
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 {
|
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 stack = List(ScriptNumber("0000000000000000"), ScriptNumber.one)
|
||||||
val script = List(OP_WITHIN)
|
val script = List(OP_WITHIN)
|
||||||
val program = ScriptProgram(
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram = AI.opWithin(program)
|
val newProgram = AI.opWithin(program)
|
||||||
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
|
newProgram.isInstanceOf[ExecutedScriptProgram] must be(true)
|
||||||
newProgram.asInstanceOf[ExecutedScriptProgram].error must be(
|
newProgram.asInstanceOf[ExecutedScriptProgram].error must be(
|
||||||
|
|
|
@ -78,29 +78,20 @@ class ConstantInterpreterTest extends FlatSpec with MustMatchers {
|
||||||
it must "push 0 bytes onto the stack which is OP_0" in {
|
it must "push 0 bytes onto the stack which is OP_0" in {
|
||||||
val stack = List()
|
val stack = List()
|
||||||
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
|
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
|
||||||
val program =
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram = CI.opPushData1(program)
|
val newProgram = CI.opPushData1(program)
|
||||||
newProgram.stackTopIsFalse must be(true)
|
newProgram.stackTopIsFalse must be(true)
|
||||||
newProgram.stack must be(List(ScriptNumber.zero))
|
newProgram.stack must be(List(ScriptNumber.zero))
|
||||||
|
|
||||||
val stack1 = List()
|
val stack1 = List()
|
||||||
val script1 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
|
val script1 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
|
||||||
val program1 =
|
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram1 = CI.opPushData2(program1)
|
val newProgram1 = CI.opPushData2(program1)
|
||||||
newProgram1.stack must be(List(ScriptNumber.zero))
|
newProgram1.stack must be(List(ScriptNumber.zero))
|
||||||
|
|
||||||
val stack2 = List()
|
val stack2 = List()
|
||||||
val script2 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
|
val script2 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
|
||||||
val program2 =
|
val program2 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
val newProgram2 = CI.opPushData4(program2)
|
val newProgram2 = CI.opPushData4(program2)
|
||||||
newProgram2.stack must be(List(ScriptNumber.zero))
|
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 {
|
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 stack = List()
|
||||||
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(1))
|
val script = List(OP_PUSHDATA1, BytesToPushOntoStack(1))
|
||||||
val program = ScriptProgram(
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).removeFlags()
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
|
|
||||||
val newProgram =
|
val newProgram =
|
||||||
ScriptProgramTestUtil.toExecutedScriptProgram(CI.opPushData1(program))
|
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 {
|
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 stack1 = List()
|
||||||
val script1 = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
|
val script1 = List(OP_PUSHDATA1, BytesToPushOntoStack(0))
|
||||||
val program1 =
|
val program1 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack1, script1),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
|
|
||||||
val stack2 = List()
|
val stack2 = List()
|
||||||
val script2 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
|
val script2 = List(OP_PUSHDATA2, BytesToPushOntoStack(0))
|
||||||
val program2 =
|
val program2 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack2, script2),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
|
|
||||||
val stack4 = List()
|
val stack4 = List()
|
||||||
val script4 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
|
val script4 = List(OP_PUSHDATA4, BytesToPushOntoStack(0))
|
||||||
val program4 =
|
val program4 = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack4, script4).removeFlags()
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack4, script4),
|
|
||||||
Seq[ScriptFlag]())
|
|
||||||
|
|
||||||
//purposely call incorrect functions to mismatch opCodes
|
//purposely call incorrect functions to mismatch opCodes
|
||||||
intercept[IllegalArgumentException] {
|
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 {
|
it must "return ScriptErrorMinimalData if program contains ScriptVerifyMinimalData flag and 2nd item in script is zero" in {
|
||||||
val stack = List()
|
val stack = List()
|
||||||
val script = List(OP_PUSHDATA4, ScriptNumber.zero)
|
val script = List(OP_PUSHDATA4, ScriptNumber.zero)
|
||||||
val program =
|
val program = ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script).replaceFlags(Seq[ScriptFlag](ScriptVerifyMinimalData))
|
||||||
ScriptProgram(
|
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script),
|
|
||||||
Seq[ScriptFlag](ScriptVerifyMinimalData))
|
|
||||||
val newProgram =
|
val newProgram =
|
||||||
ScriptProgramTestUtil.toExecutedScriptProgram(CI.opPushData4(program))
|
ScriptProgramTestUtil.toExecutedScriptProgram(CI.opPushData4(program))
|
||||||
newProgram.error must be(Some(ScriptErrorMinimalData))
|
newProgram.error must be(Some(ScriptErrorMinimalData))
|
||||||
|
|
|
@ -117,7 +117,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
|
||||||
val script = List(OP_CHECKMULTISIG)
|
val script = List(OP_CHECKMULTISIG)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
|
val programNoFlags = program.removeFlags()
|
||||||
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
||||||
newProgram.stack must be(List(OP_TRUE))
|
newProgram.stack must be(List(OP_TRUE))
|
||||||
newProgram.script.isEmpty must be(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 script = List(OP_CHECKMULTISIG, OP_16, OP_16, OP_16, OP_16)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
|
val programNoFlags = program.removeFlags()
|
||||||
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
||||||
newProgram.stack must be(List(OP_TRUE, OP_16, OP_16, OP_16))
|
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))
|
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 script = List(OP_CHECKMULTISIGVERIFY)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
|
val programNoFlags = program.removeFlags()
|
||||||
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
|
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
|
||||||
newProgram.script.isEmpty must be(true)
|
newProgram.script.isEmpty must be(true)
|
||||||
newProgram.stack.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 script = List(OP_CHECKMULTISIGVERIFY, OP_16, OP_16, OP_16, OP_16)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
|
val programNoFlags = program.removeFlags()
|
||||||
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
|
val newProgram = CI.opCheckMultiSigVerify(programNoFlags)
|
||||||
newProgram.stack must be(List(OP_16, OP_16, OP_16))
|
newProgram.stack must be(List(OP_16, OP_16, OP_16))
|
||||||
newProgram.script must be(List(OP_16, 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 script = List(OP_CHECKMULTISIG)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programNoFlags = ScriptProgram(program, ScriptFlagFactory.empty)
|
val programNoFlags = program.removeFlags()
|
||||||
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
val newProgram = CI.opCheckMultiSig(programNoFlags)
|
||||||
newProgram.stack must be(List(OP_TRUE))
|
newProgram.stack must be(List(OP_TRUE))
|
||||||
newProgram.script.isEmpty must be(true)
|
newProgram.script.isEmpty must be(true)
|
||||||
|
@ -207,7 +207,7 @@ class CryptoInterpreterTest extends FlatSpec with MustMatchers {
|
||||||
val script = Seq(OP_CHECKSIG)
|
val script = Seq(OP_CHECKSIG)
|
||||||
val program =
|
val program =
|
||||||
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
ScriptProgram(TestUtil.testProgramExecutionInProgress, stack, script)
|
||||||
val programWithFlags = ScriptProgram(program, flags)
|
val programWithFlags = program.replaceFlags(flags)
|
||||||
val newProgram = ScriptProgramTestUtil.toExecutedScriptProgram(
|
val newProgram = ScriptProgramTestUtil.toExecutedScriptProgram(
|
||||||
CI.opCheckSig(programWithFlags))
|
CI.opCheckSig(programWithFlags))
|
||||||
newProgram.error must be(Some(ScriptErrorSigDer))
|
newProgram.error must be(Some(ScriptErrorSigDer))
|
||||||
|
|
|
@ -108,6 +108,19 @@ case class ExecutionInProgressScriptProgram(
|
||||||
override def failExecution(error: ScriptError): ExecutedScriptProgram = {
|
override def failExecution(error: ScriptError): ExecutedScriptProgram = {
|
||||||
ScriptProgram.toExecutedProgram(this).failExecution(error)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue