Adding control flow operations

This commit is contained in:
Chris Stewart 2016-01-06 10:56:33 -06:00
parent d23aa244f9
commit a0f4bb4365
3 changed files with 101 additions and 3 deletions

View file

@ -4,13 +4,13 @@ package org.scalacoin.script
* Created by chris on 1/6/16.
*/
trait ScriptOperation
sealed trait Constant extends ScriptOperation {
trait ScriptOperation {
def opCode : Int
def hex : String = Integer.toHexString(opCode)
}
sealed trait Constant extends ScriptOperation
/**
* An empty array of bytes is pushed onto the stack. (This is not a no-op: an item is added to the stack.)
*/

View file

@ -0,0 +1,65 @@
package org.scalacoin.script.control
import org.scalacoin.script.ScriptOperation
/**
* Created by chris on 1/6/16.
*/
sealed trait ControlOperations extends ScriptOperation
/**
* Does nothing
*/
case object OP_NOP extends ControlOperations {
override def opCode = 97
}
/**
* If the top stack value is not 0, the statements are executed. The top stack value is removed.
*/
case object OP_IF extends ControlOperations {
override def opCode = 99
}
/**
* If the top stack value is 0, the statements are executed. The top stack value is removed.
*/
case object OP_NOTIF extends ControlOperations {
override def opCode = 100
}
/**
* If the preceding OP_IF or OP_NOTIF or OP_ELSE was not executed then these statements are and
* if the preceding OP_IF or OP_NOTIF or OP_ELSE was executed then these statements are not.
*/
case object OP_ELSE extends ControlOperations {
override def opCode = 103
}
/**
* Ends an if/else block. All blocks must end, or the transaction is invalid.
* An OP_ENDIF without OP_IF earlier is also invalid.
*/
case object OP_ENDIF extends ControlOperations {
override def opCode = 104
}
/**
* Marks transaction as invalid if top stack value is not true.
*/
case object OP_VERIFY extends ControlOperations {
override def opCode = 105
}
/**
* Marks transaction as invalid. A standard way of attaching extra data to transactions is to add a zero-value
* output with a scriptPubKey consisting of OP_RETURN followed by exactly one pushdata op.
* Such outputs are provably unspendable, reducing their cost to the network.
* Currently it is usually considered non-standard
* (though valid) for a transaction to have more than one OP_RETURN output or an OP_RETURN output
* with more than one pushdata op.
*/
case object OP_RETURN extends ControlOperations {
override def opCode = 106
}

View file

@ -0,0 +1,33 @@
package org.scalacoin.script.control
import org.scalatest.{MustMatchers, FlatSpec}
/**
* Created by chris on 1/6/16.
*/
class ControlOperationsTest extends FlatSpec with MustMatchers {
"ControlOperations" must "define an OP_IF" in {
OP_IF.opCode must be (99)
}
it must "define an OP_NOTIF" in {
OP_NOTIF.opCode must be (100)
}
it must "define an OP_ELSE" in {
OP_ELSE.opCode must be (103)
}
it must "define an OP_ENDIF" in {
OP_ENDIF.opCode must be (104)
}
it must "define an OP_VERIFY" in {
OP_VERIFY.opCode must be (105)
}
it must "define an OP_RETURN" in {
OP_RETURN.opCode must be (106)
}
}