From 17cfb5b6c2ddb4faa5b37f5cbdc7440e9ba8822a Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Wed, 6 Jan 2016 19:26:37 -0600 Subject: [PATCH] Implmenting OP_VERIFY operation --- .../ControlOperationsInterpreter.scala | 22 +++++++++ .../ControlOperationsInterpreterTest.scala | 46 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/scala/org/scalacoin/script/control/ControlOperationsInterpreter.scala create mode 100644 src/test/scala/org/scalacoin/script/control/ControlOperationsInterpreterTest.scala diff --git a/src/main/scala/org/scalacoin/script/control/ControlOperationsInterpreter.scala b/src/main/scala/org/scalacoin/script/control/ControlOperationsInterpreter.scala new file mode 100644 index 0000000000..1131e662d0 --- /dev/null +++ b/src/main/scala/org/scalacoin/script/control/ControlOperationsInterpreter.scala @@ -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 + } +} diff --git a/src/test/scala/org/scalacoin/script/control/ControlOperationsInterpreterTest.scala b/src/test/scala/org/scalacoin/script/control/ControlOperationsInterpreterTest.scala new file mode 100644 index 0000000000..a1f5b30401 --- /dev/null +++ b/src/test/scala/org/scalacoin/script/control/ControlOperationsInterpreterTest.scala @@ -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) + } + } +}