Skip to content

Commit

Permalink
Small optimization for ScriptOperationFactory.operations (#1450)
Browse files Browse the repository at this point in the history
* Small optimization for ScriptOperationFactory.operations

* Make operations a val
  • Loading branch information
benthecarman committed May 20, 2020
1 parent 606d2fe commit 7dd1084
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
@@ -1,6 +1,5 @@
package org.bitcoins.core.script

import org.bitcoins.core.script.ScriptOperation.operations
import org.bitcoins.core.script.arithmetic.ArithmeticOperation
import org.bitcoins.core.script.bitwise.BitwiseOperation
import org.bitcoins.core.script.constant._
Expand Down Expand Up @@ -66,7 +65,7 @@ trait ScriptOperationFactory[T <: ScriptOperation] extends BitcoinSLogger {
}
}
private lazy val scriptOpMap: Map[Byte, ScriptOperation] = {
operations.map(o => (o.toByte,o)).toMap
operations.map(o => (o.toByte, o)).toMap
}
def apply(byte: Byte): T = fromByte(byte)

Expand All @@ -76,12 +75,12 @@ trait ScriptOperationFactory[T <: ScriptOperation] extends BitcoinSLogger {
object ScriptOperation extends ScriptOperationFactory[ScriptOperation] {

/** This contains duplicate operations
* There is an optimization here by moving popular opcodes
* to the front of the vector so when we iterate through it,
* we are more likely to find the op code we are looking for
* sooner */
* There is an optimization here by moving popular opcodes
* to the front of the vector so when we iterate through it,
* we are more likely to find the op code we are looking for
* sooner */
final override val operations: Vector[ScriptOperation] = {
Vector(OP_FALSE, OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4, OP_TRUE) ++
StackPushOperationFactory.pushDataOperations ++
StackOperation.operations ++
LocktimeOperation.operations ++
CryptoOperation.operations ++
Expand All @@ -91,7 +90,7 @@ object ScriptOperation extends ScriptOperationFactory[ScriptOperation] {
BytesToPushOntoStack.operations ++
SpliceOperation.operations ++
ReservedOperation.operations ++
ScriptNumberOperation.operations
ScriptNumberOperation.operations
}

}
Expand Up @@ -13,15 +13,18 @@ trait StackPushOperationFactory {
*/
def isPushOperation(token: ScriptToken): Boolean = operations.contains(token)

val pushDataOperations: Vector[ScriptOperation] =
Vector(OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4)

/**
* Gives back all of the script operations that can push data onto the stack
* The operations are determined according to BIP62
* https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#push-operators
*
* @return
*/
private def operations =
Seq(OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4) ++ BytesToPushOntoStack.operations ++
private val operations =
pushDataOperations ++ BytesToPushOntoStack.operations ++
Seq(OP_0,
OP_1,
OP_1NEGATE,
Expand All @@ -42,7 +45,6 @@ trait StackPushOperationFactory {
OP_16,
OP_FALSE,
OP_TRUE)

}

object StackPushOperationFactory extends StackPushOperationFactory

0 comments on commit 7dd1084

Please sign in to comment.