Simplify toInt function with require statement

This commit is contained in:
Tom McCabe 2016-07-12 09:44:40 -05:00
parent ebdb29f832
commit 723156467a
5 changed files with 21 additions and 22 deletions

View File

@ -83,9 +83,8 @@ sealed trait UInt32 extends UnsignedNumber with NumberOperations[UInt32] {
override def hex = BitcoinSUtil.encodeHex(underlying).slice(8,16)
override def toInt = {
if (underlying > Int.MaxValue) throw new IllegalArgumentException("Overflow error when casting " + this +
" to an integer.")
else underlying.toInt
require(underlying <= Int.MaxValue, "Overflow error when casting " + this + " to an integer.")
underlying.toInt
}
/**
@ -134,9 +133,8 @@ sealed trait UInt64 extends UnsignedNumber with NumberOperations[UInt64] {
override def <= (num : UInt64): Boolean = underlying <= num.underlying
override def toInt = {
if (underlying > Int.MaxValue) throw new IllegalArgumentException("Overflow error when casting " + this +
" to an integer.")
else underlying.toInt
require(underlying <= Int.MaxValue, "Overflow error when casting " + this + " to an integer.")
underlying.toInt
}
/**
@ -249,11 +247,11 @@ sealed trait Int64 extends SignedNumber with NumberOperations[Int64] {
override def <= (num : Int64): Boolean = underlying <= num.underlying
override def toInt = {
if (underlying > Int.MaxValue) throw new IllegalArgumentException("Overflow error when casting " + this +
" to an integer.")
else underlying.toInt
require(underlying <= Int.MaxValue, "Overflow error when casting " + this + " to an integer.")
underlying.toInt
}
/**
* Checks the result of the arithmetic operation to see if an error occurred
* if an error does occur throw it, else return the [[Int64]]

View File

@ -98,6 +98,8 @@ sealed trait ScriptNumber extends ScriptConstant {
case 0 => 0L
case _ : Long => super.toLong
}
def toInt = underlying.toInt
}
object ScriptNumber extends Factory[ScriptNumber] {
@ -160,6 +162,7 @@ object ScriptNumber extends Factory[ScriptNumber] {
def apply(underlying : Long) : ScriptNumber = ScriptNumberImpl(underlying, ScriptNumberUtil.longToHex(underlying))
def apply(hex : String) : ScriptNumber = ScriptNumberImpl(ScriptNumberUtil.toLong(hex), hex)
def apply(bytes : Seq[Byte]) : ScriptNumber = ScriptNumberImpl(ScriptNumberUtil.toLong(bytes))
def apply(int64: Int64) : ScriptNumber = ScriptNumberImpl(int64.underlying)
}
}

View File

@ -226,8 +226,8 @@ trait CryptoInterpreter extends ControlOperationsInterpreter with BitcoinSLogger
}
logger.debug("nPossibleSignatures: " + nPossibleSignatures)
val (pubKeysScriptTokens, stackWithoutPubKeys) =
(program.stack.tail.slice(0, nPossibleSignatures.underlying.toInt),
program.stack.tail.slice(nPossibleSignatures.underlying.toInt, program.stack.tail.size))
(program.stack.tail.slice(0, nPossibleSignatures.toInt),
program.stack.tail.slice(nPossibleSignatures.toInt, program.stack.tail.size))
val pubKeys = pubKeysScriptTokens.map(key => ECPublicKey(key.bytes))
logger.debug("Public keys on the stack: " + pubKeys)
@ -235,13 +235,13 @@ trait CryptoInterpreter extends ControlOperationsInterpreter with BitcoinSLogger
logger.debug("mRequiredSignatures: " + mRequiredSignatures)
//+1 is for the fact that we have the # of sigs + the script token indicating the # of sigs
val signaturesScriptTokens = program.stack.tail.slice(nPossibleSignatures.underlying.toInt + 1,
nPossibleSignatures.underlying.toInt + mRequiredSignatures.underlying.toInt + 1)
val signaturesScriptTokens = program.stack.tail.slice(nPossibleSignatures.toInt + 1,
nPossibleSignatures.toInt + mRequiredSignatures.toInt + 1)
val signatures = signaturesScriptTokens.map(token => ECDigitalSignature(token.bytes))
logger.debug("Signatures on the stack: " + signatures)
//this contains the extra Script OP that is required for OP_CHECKMULTISIG
val stackWithoutPubKeysAndSignatures = stackWithoutPubKeys.tail.slice(mRequiredSignatures.underlying.toInt, stackWithoutPubKeys.tail.size)
val stackWithoutPubKeysAndSignatures = stackWithoutPubKeys.tail.slice(mRequiredSignatures.toInt, stackWithoutPubKeys.tail.size)
logger.debug("stackWithoutPubKeysAndSignatures: " + stackWithoutPubKeysAndSignatures)
if (pubKeys.size > ScriptSettings.maxPublicKeysPerMultiSig) {
logger.error("We have more public keys than the maximum amount of public keys allowed")

View File

@ -294,10 +294,10 @@ trait ScriptInterpreter extends CryptoInterpreter with StackInterpreter with Con
//script was marked invalid for other reasons, don't need to update the opcount
loop(newProgram)
case newProgram : ExecutionInProgressScriptProgram =>
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).underlying.toInt
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).toInt
loop(newProgram)
case newProgram : PreExecutionScriptProgram =>
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).underlying.toInt
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).toInt
loop(newProgram)
}
@ -307,10 +307,10 @@ trait ScriptInterpreter extends CryptoInterpreter with StackInterpreter with Con
//script was marked invalid for other reasons, don't need to update the opcount
loop(newProgram)
case newProgram : ExecutionInProgressScriptProgram =>
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).underlying.toInt
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).toInt
loop(newProgram)
case newProgram : PreExecutionScriptProgram =>
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).underlying.toInt
opCount = opCount + BitcoinScriptUtil.numPossibleSignaturesOnStack(program).toInt
loop(newProgram)
}
//reserved operations

View File

@ -1,14 +1,12 @@
package org.bitcoins.core.script.crypto
import org.bitcoins.core.crypto.TransactionSignatureSerializer
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.script.{ScriptPubKey, ScriptSignature}
import org.bitcoins.core.protocol.transaction._
import org.bitcoins.core.script.result._
import org.bitcoins.core.script._
import org.bitcoins.core.script.arithmetic.OP_NOT
import org.bitcoins.core.script.flag.{ScriptFlagFactory, ScriptVerifyDerSig, ScriptVerifyNullDummy}
import org.bitcoins.core.script.constant._
import org.bitcoins.core.script.flag.{ScriptFlagFactory, ScriptVerifyDerSig, ScriptVerifyNullDummy}
import org.bitcoins.core.script.result._
import org.bitcoins.core.util.{BitcoinSLogger, ScriptProgramTestUtil, TestUtil, TransactionTestUtil}
import org.scalatest.{FlatSpec, MustMatchers}