Implmenting OP_VERIFY operation

This commit is contained in:
Chris Stewart 2016-01-06 19:26:37 -06:00
parent 9cbb809650
commit 17cfb5b6c2
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,22 @@
package org.scalacoin.script.control
import org.scalacoin.script.ScriptOperation
/**
* Created by chris on 1/6/16.
*/
trait ControlOperationsInterpreter {
/**
* Marks transaction as invalid if top stack value is not true.
* @param stack
* @param script
* @return
*/
def verify(stack : List[String], script : List[ScriptOperation]) : Boolean = {
require(stack.size > 0, "Stack must not be empty to verify it")
require(script.headOption.isDefined && script.head == OP_VERIFY, "Top of script stack must be OP_VERIFY")
if (stack.head == "1") true else false
}
}

View File

@ -0,0 +1,46 @@
package org.scalacoin.script.control
import org.scalatest.{MustMatchers, FlatSpec}
/**
* Created by chris on 1/6/16.
*/
class ControlOperationsInterpreterTest extends FlatSpec with MustMatchers with ControlOperationsInterpreter {
"ControlOperationsInterpreter" must "have OP_VERIFY evaluate to true with '1' on the stack" in {
val stack = List("1")
val script = List(OP_VERIFY)
val result = verify(stack,script)
result must be (true)
}
it must "have OP_VERIFY evaluate to false with '0' on the stack" in {
val stack = List("0")
val script = List(OP_VERIFY)
val result = verify(stack,script)
result must be (false)
}
it must "have OP_VERIFY evaluate to false with '2' on the stack" in {
val stack = List("2")
val script = List(OP_VERIFY)
val result = verify(stack,script)
result must be (false)
}
it must "fail for OP_VERIFY when there is nothing on the stack" in {
intercept[IllegalArgumentException] {
val stack = List()
val script = List(OP_VERIFY)
val result = verify(stack,script)
}
}
it must "fail for verify when there is nothing on the script stack" in {
intercept[IllegalArgumentException] {
val stack = List("1")
val script = List()
val result = verify(stack,script)
}
}
}