Skip to content

Commit

Permalink
Merge pull request #186 from Christewart/rm_scodec_prefix
Browse files Browse the repository at this point in the history
removing scoded.bits prefix from application code
  • Loading branch information
Christewart committed Aug 10, 2018
2 parents f718370 + 1799fa3 commit 1d74af0
Show file tree
Hide file tree
Showing 89 changed files with 442 additions and 394 deletions.
Expand Up @@ -2,6 +2,7 @@ package org.bitcoins.core.gen

import org.bitcoins.core.bloom._
import org.scalacheck.Gen
import scodec.bits.ByteVector

/**
* Created by chris on 8/7/16.
Expand All @@ -17,12 +18,12 @@ abstract class BloomFilterGenerator {
} yield BloomFilter(size, falsePositiveRate, tweak, flags)

/** Loads a generic bloom filter with the given byte vectors and returns it */
def bloomFilter(byteVectors: Seq[scodec.bits.ByteVector]): Gen[BloomFilter] = for {
def bloomFilter(byteVectors: Seq[ByteVector]): Gen[BloomFilter] = for {
filter <- bloomFilter
} yield filter.insertByteVectors(byteVectors)

/** Returns a bloom filter loaded with randomly generated byte vectors */
def loadedBloomFilter: Gen[(BloomFilter, Seq[scodec.bits.ByteVector])] = for {
def loadedBloomFilter: Gen[(BloomFilter, Seq[ByteVector])] = for {
filter <- bloomFilter
randomNum <- Gen.choose(0, filter.filterSize.num.toInt)
hashes <- CryptoGenerators.doubleSha256DigestSeq(randomNum)
Expand Down
Expand Up @@ -79,7 +79,7 @@ trait NumberGenerator {
} yield num == 1

/** Generates a bit vector */
def bitVector: Gen[scodec.bits.BitVector] = for {
def bitVector: Gen[BitVector] = for {
n <- Gen.choose(0, 100)
vector <- Gen.listOfN(n, bool)
} yield BitVector.bits(vector)
Expand Down
Expand Up @@ -2,6 +2,7 @@ package org.bitcoins.core.bloom

import org.bitcoins.core.gen.BloomFilterGenerator
import org.scalacheck.{ Prop, Properties }
import scodec.bits.ByteVector

/**
* Created by chris on 8/3/16.
Expand All @@ -10,7 +11,7 @@ class BloomFilterSpec extends Properties("BloomFilterSpec") {

property("No false negatives && serialization symmetry") =
Prop.forAll(BloomFilterGenerator.loadedBloomFilter) {
case (loadedFilter: BloomFilter, byteVectors: Seq[scodec.bits.ByteVector]) =>
case (loadedFilter: BloomFilter, byteVectors: Seq[ByteVector]) =>
val containsAllHashes = byteVectors.map(bytes => loadedFilter.contains(bytes))
!containsAllHashes.exists(_ == false) &&
BloomFilter(loadedFilter.hex) == loadedFilter
Expand Down
Expand Up @@ -45,7 +45,7 @@ class ECDigitalSignatureTest extends FlatSpec with MustMatchers {

it must "create an empty digital signature when given 0 in hex or byte format" in {
val hex = ECDigitalSignature("00")
val byte = ECDigitalSignature(scodec.bits.ByteVector.low(1))
val byte = ECDigitalSignature(ByteVector.low(1))
val emptySignature = ECDigitalSignature("")
byte must be(emptySignature)
hex must be(emptySignature)
Expand Down
Expand Up @@ -12,6 +12,7 @@ import org.bitcoins.core.script.result.ScriptOk
import org.bitcoins.core.util.{ BitcoinSLogger, TransactionTestUtil }
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ FlatSpec, MustMatchers }
import scodec.bits.ByteVector

import scala.concurrent.Future

Expand Down Expand Up @@ -211,8 +212,8 @@ class TransactionSignatureCreatorTest extends FlatSpec with MustMatchers with Sc
inputIndex = inputIndex,
output = TransactionOutput(CurrencyUnits.zero, redeemScript),
flags = Policy.standardScriptVerifyFlags)
val sign: scodec.bits.ByteVector => Future[ECDigitalSignature] = {
bytes: scodec.bits.ByteVector => Future(privateKey.sign(bytes))
val sign: ByteVector => Future[ECDigitalSignature] = {
bytes: ByteVector => Future(privateKey.sign(bytes))
}
val txSignature = TransactionSignatureCreator.createSig(txSignatureComponent, sign, HashType.sigHashAll)

Expand Down
Expand Up @@ -9,37 +9,37 @@ import scodec.bits.ByteVector
class Int32Test extends FlatSpec with MustMatchers {

"Int32" must "create the number zero" in {
val int32 = Int32(scodec.bits.ByteVector.low(1))
val int32 = Int32(ByteVector.low(1))
int32.toInt must be(0)
}

it must "represent the number -1" in {
val int32 = Int32(scodec.bits.ByteVector(0xff.toByte))
val int32 = Int32(ByteVector(0xff.toByte))
int32.toInt must be(-1)
}

it must "represent the number -1 with 4 bytes" in {
val int32 = Int32(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val int32 = Int32(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int32.toInt must be(-1)
}

it must "create the max number for a single byte" in {
val int32 = Int32(scodec.bits.ByteVector(0x7f.toByte))
val int32 = Int32(ByteVector(0x7f.toByte))
int32.toInt must be(127)
}

it must "create the min number for a single byte" in {
val int32 = Int32(scodec.bits.ByteVector(0x80.toByte))
val int32 = Int32(ByteVector(0x80.toByte))
int32.toInt must be(-128)
}

it must "create the max number for an Int32" in {
val int32 = Int32(scodec.bits.ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val int32 = Int32(ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int32.toInt must be(2147483647)
}

it must "create the minimum number for an Int32" in {
val int32 = Int32(scodec.bits.ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
val int32 = Int32(ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
int32.toInt must be(-2147483648)
}

Expand Down
Expand Up @@ -9,50 +9,50 @@ import scodec.bits.ByteVector
class Int64Test extends FlatSpec with MustMatchers {

"Int64" must "represent the nubmer zero" in {
val int64 = Int64(scodec.bits.ByteVector.low(1))
val int64 = Int64(ByteVector.low(1))
int64.toLong must be(0)
}
it must "represent the number 1" in {
val int64 = Int64(ByteVector(1.toByte))
int64.toLong must be(1)
}
it must "represent the number -1 with 1 byte" in {
val int64 = Int64(scodec.bits.ByteVector(0xff.toByte))
val int64 = Int64(ByteVector(0xff.toByte))
int64.toLong must be(-1)
}

it must "represent the number -1 with 8 bytes" in {
val int64 = Int64(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
val int64 = Int64(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int64.toLong must be(-1)
}
it must "represent the Int32 max value" in {
val int64 = Int64(scodec.bits.ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val int64 = Int64(ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int64.toLong must be(2147483647)
}

it must "represent the Int32 min value" in {
val int64 = Int64(scodec.bits.ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
val int64 = Int64(ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
int64.toLong must be(-2147483648L)
}

it must "represent the Int32 max value + 1" in {
val int64 = Int64(scodec.bits.ByteVector(0x0.toByte, 0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
val int64 = Int64(ByteVector(0x0.toByte, 0x80.toByte, 0.toByte, 0.toByte, 0.toByte))
int64.toLong must be(2147483648L)
}

it must "represent the Int32 min value - 1" in {
val int64 = Int64(scodec.bits.ByteVector(0xff.toByte, 0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val int64 = Int64(ByteVector(0xff.toByte, 0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int64.toLong must be(-2147483649L)
}

it must "represent the minimum value for int64" in {
val int64 = Int64(scodec.bits.ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte))
val int64 = Int64(ByteVector(0x80.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte, 0.toByte))
int64.toLong must be(-9223372036854775808L)
}

it must "represent the maximum value for a int64" in {
val int64 = Int64(scodec.bits.ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
val int64 = Int64(ByteVector(0x7f.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
int64.toLong must be(9223372036854775807L)
}
Expand Down
Expand Up @@ -9,32 +9,32 @@ import scodec.bits.ByteVector
class UInt32Test extends FlatSpec with MustMatchers {

"UInt32" must "create the number zero as an unsigned 32 bit integer" in {
val zero = UInt32(scodec.bits.ByteVector(0x0.toByte))
val zero = UInt32(ByteVector(0x0.toByte))
zero.toLong must be(0)
}

it must "create the max number for an unsigned byte" in {
val maxByteValue = UInt32(scodec.bits.ByteVector(0xff.toByte))
val maxByteValue = UInt32(ByteVector(0xff.toByte))
maxByteValue.toLong must be(255)
}

it must "create the number 256" in {
val uInt32 = UInt32(scodec.bits.ByteVector(0x01.toByte, 0x00.toByte))
val uInt32 = UInt32(ByteVector(0x01.toByte, 0x00.toByte))
uInt32.toLong must be(256)
}

it must "create the number 65535" in {
val uInt32 = UInt32(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte))
val uInt32 = UInt32(ByteVector(0xff.toByte, 0xff.toByte))
uInt32.toLong must be(65535)
}

it must "create the number 65536" in {
val uInt32 = UInt32(scodec.bits.ByteVector(0x01.toByte, 0x0.toByte, 0x0.toByte))
val uInt32 = UInt32(ByteVector(0x01.toByte, 0x0.toByte, 0x0.toByte))
uInt32.toLong must be(65536)
}

it must "create the number 16777215" in {
val uInt32 = UInt32(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte))
val uInt32 = UInt32(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte))
uInt32.toLong must be(16777215)
}

Expand All @@ -45,7 +45,7 @@ class UInt32Test extends FlatSpec with MustMatchers {

it must "create the number 4294967295" in {
//this is UInt32_t's max value
val uInt32 = UInt32(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val uInt32 = UInt32(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
uInt32.toLong must be(4294967295L)
uInt32.hex must be("ffffffff")
}
Expand Down
Expand Up @@ -9,7 +9,7 @@ import scodec.bits.ByteVector
class UInt64Test extends FlatSpec with MustMatchers {

"UInt64" must "hold the number 0" in {
val uInt64 = UInt64(scodec.bits.ByteVector.low(1))
val uInt64 = UInt64(ByteVector.low(1))
uInt64.hex must be("0000000000000000")
uInt64.toBigInt must be(0)
}
Expand All @@ -22,7 +22,7 @@ class UInt64Test extends FlatSpec with MustMatchers {

it must "hold the max for a uint32_t" in {
//this is UInt32_t's max value
val uInt64 = UInt64(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
val uInt64 = UInt64(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
uInt64.toBigInt must be(4294967295L)
}

Expand All @@ -32,7 +32,7 @@ class UInt64Test extends FlatSpec with MustMatchers {
}

it must "hold the max number for uint64_t" in {
val uInt64 = UInt64(scodec.bits.ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
val uInt64 = UInt64(ByteVector(0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte,
0xff.toByte, 0xff.toByte, 0xff.toByte, 0xff.toByte))
uInt64.toBigInt must be(BigInt("18446744073709551615"))
uInt64.hex must be("ffffffffffffffff")
Expand Down
Expand Up @@ -102,7 +102,7 @@ class CompactSizeUIntTest extends FlatSpec with MustMatchers {

it must "intercept a failed requirement when the byte array size is zero" in {
intercept[IllegalArgumentException] {
val emptyBytes: scodec.bits.ByteVector = ByteVector.empty
val emptyBytes: ByteVector = ByteVector.empty
CompactSizeUInt.parseCompactSizeUInt(emptyBytes)
}
}
Expand Down
@@ -1,9 +1,10 @@
package org.bitcoins.core.protocol.script

import org.bitcoins.core.crypto._
import org.bitcoins.core.script.constant.{ ScriptConstant }
import org.bitcoins.core.util.{ TestUtil, BitcoinSUtil }
import org.bitcoins.core.script.constant.ScriptConstant
import org.bitcoins.core.util.{ BitcoinSUtil, TestUtil }
import org.scalatest.{ FlatSpec, MustMatchers }
import scodec.bits.ByteVector

/**
* Created by chris on 2/17/16.
Expand All @@ -13,7 +14,7 @@ class ScriptSignatureFactoryTest extends FlatSpec with MustMatchers {
"ScriptSignatureFactory" must "give the exact same result whether parsing bytes or parsing hex" in {
val signatureHex = "30450221008949f0cb400094ad2b5eb399d59d01c14d73d8fe6e96df1a7150deb388ab8935022079656090d7" +
"f6bac4c9a94e0aad311a4268e082a725f8aeae0573fb12ff866a5f01"
val signatureBytes: scodec.bits.ByteVector = BitcoinSUtil.decodeHex(signatureHex)
val signatureBytes: ByteVector = BitcoinSUtil.decodeHex(signatureHex)

val scriptSigFromHex = ScriptSignature(signatureHex)
val scriptSigFromBytes = ScriptSignature(signatureBytes)
Expand Down
Expand Up @@ -70,7 +70,7 @@ class ScriptSignatureTest extends FlatSpec with MustMatchers {

it must "have an empty script signature" in {
EmptyScriptSignature.hex must be("00")
EmptyScriptSignature.bytes must be(scodec.bits.ByteVector.low(1))
EmptyScriptSignature.bytes must be(ByteVector.low(1))
EmptyScriptSignature.asm must be(Nil)
EmptyScriptSignature.signatures must be(Nil)
}
Expand Down
Expand Up @@ -2,6 +2,7 @@ package org.bitcoins.core.script.crypto

import org.bitcoins.core.number.Int32
import org.scalatest.{ FlatSpec, MustMatchers }
import scodec.bits.ByteVector

/**
* Created by chris on 2/27/16.
Expand Down Expand Up @@ -31,7 +32,7 @@ class HashTypeTest extends FlatSpec with MustMatchers {
}

it must "default to SIGHASH_ALL if the given string/byte is not known" in {
HashType(scodec.bits.ByteVector(0x124.toByte)) must be(SIGHASH_ALL(Int32(36)))
HashType(ByteVector(0x124.toByte)) must be(SIGHASH_ALL(Int32(36)))
}

it must "find hashType for number 1190874345" in {
Expand Down
Expand Up @@ -9,6 +9,7 @@ import org.bitcoins.core.script.constant.{ ScriptConstant, ScriptOperation, Scri
import org.bitcoins.core.script.result.ScriptResult
import org.bitcoins.core.util.{ BitcoinSLogger, BitcoinSUtil, BitcoinScriptUtil }
import org.slf4j.LoggerFactory
import scodec.bits.ByteVector
import spray.json._

/**
Expand All @@ -33,9 +34,9 @@ object CoreTestCaseProtocol extends DefaultJsonProtocol {
None
} else if (elements.size == 4) {
//means we are missing a comment
val scriptPubKeyBytes: scodec.bits.ByteVector = parseScriptPubKey(elements(1))
val scriptPubKeyBytes: ByteVector = parseScriptPubKey(elements(1))
val scriptPubKey = ScriptPubKey(scriptPubKeyBytes)
val scriptSignatureBytes: scodec.bits.ByteVector = parseScriptSignature(elements.head)
val scriptSignatureBytes: ByteVector = parseScriptSignature(elements.head)
val scriptSignature: ScriptSignature = ScriptSignature(scriptSignatureBytes)
val flags = elements(2).convertTo[String]
logger.info("Result: " + elements(3).convertTo[String])
Expand All @@ -48,19 +49,19 @@ object CoreTestCaseProtocol extends DefaultJsonProtocol {
val amount = Satoshis(Int64((witnessArray.elements.last.convertTo[Double] * 100000000L).toLong))
val stack = witnessArray.elements.slice(0, witnessArray.elements.size - 1).map(c => BitcoinSUtil.decodeHex(c.convertTo[String]))
val witness = ScriptWitness(stack.reverse)
val scriptPubKeyBytes: scodec.bits.ByteVector = parseScriptPubKey(elements(2))
val scriptPubKeyBytes: ByteVector = parseScriptPubKey(elements(2))
val scriptPubKey = ScriptPubKey(scriptPubKeyBytes)
val scriptSignatureBytes: scodec.bits.ByteVector = parseScriptSignature(elements(1))
val scriptSignatureBytes: ByteVector = parseScriptSignature(elements(1))
val scriptSignature: ScriptSignature = ScriptSignature(scriptSignatureBytes)
val flags = elements(3).convertTo[String]
logger.info("Result: " + elements(4).convertTo[String])
val expectedResult = ScriptResult(elements(4).convertTo[String])
Some(CoreTestCaseImpl(scriptSignature, scriptPubKey, flags,
expectedResult, "", elements.toString, Some(witness, amount)))
} else if (elements.size == 5) {
val scriptPubKeyBytes: scodec.bits.ByteVector = parseScriptPubKey(elements(1))
val scriptPubKeyBytes: ByteVector = parseScriptPubKey(elements(1))
val scriptPubKey = ScriptPubKey(scriptPubKeyBytes)
val scriptSignatureBytes: scodec.bits.ByteVector = parseScriptSignature(elements.head)
val scriptSignatureBytes: ByteVector = parseScriptSignature(elements.head)
val scriptSignature: ScriptSignature = ScriptSignature(scriptSignatureBytes)
val flags = elements(2).convertTo[String]
logger.info("Result: " + elements(3).convertTo[String])
Expand All @@ -73,9 +74,9 @@ object CoreTestCaseProtocol extends DefaultJsonProtocol {
val amount = Satoshis(Int64((witnessArray.elements.last.convertTo[Double] * 100000000L).toLong))
val stack = witnessArray.elements.slice(0, witnessArray.elements.size - 1).map(c => BitcoinSUtil.decodeHex(c.convertTo[String]))
val witness = ScriptWitness(stack.reverse)
val scriptPubKeyBytes: scodec.bits.ByteVector = parseScriptPubKey(elements(2))
val scriptPubKeyBytes: ByteVector = parseScriptPubKey(elements(2))
val scriptPubKey = ScriptPubKey(scriptPubKeyBytes)
val scriptSignatureBytes: scodec.bits.ByteVector = parseScriptSignature(elements(1))
val scriptSignatureBytes: ByteVector = parseScriptSignature(elements(1))
val scriptSignature: ScriptSignature = ScriptSignature(scriptSignatureBytes)
val flags = elements(3).convertTo[String]
logger.info("Result: " + elements(4).convertTo[String])
Expand All @@ -95,7 +96,7 @@ object CoreTestCaseProtocol extends DefaultJsonProtocol {
* @param element
* @return
*/
private def parseScriptSignature(element: JsValue): scodec.bits.ByteVector = {
private def parseScriptSignature(element: JsValue): ByteVector = {
val asm = ScriptParser.fromString(element.convertTo[String])
val bytes = BitcoinScriptUtil.asmToBytes(asm)
val compactSizeUInt = CompactSizeUInt.calculateCompactSizeUInt(bytes)
Expand All @@ -111,7 +112,7 @@ object CoreTestCaseProtocol extends DefaultJsonProtocol {
* @param element
* @return
*/
private def parseScriptPubKey(element: JsValue): scodec.bits.ByteVector = {
private def parseScriptPubKey(element: JsValue): ByteVector = {
val asm = ScriptParser.fromString(element.convertTo[String])
val bytes = BitcoinScriptUtil.asmToBytes(asm)
val compactSizeUInt = CompactSizeUInt.calculateCompactSizeUInt(bytes)
Expand Down
Expand Up @@ -10,7 +10,7 @@ class RawSerializerHelperSpec extends Properties("RawSerializerHelperSpec") {
property("serialization symmetry of txs") = {
Prop.forAll(TransactionGenerators.smallOutputs) { txs: Seq[TransactionOutput] =>
val serialized = RawSerializerHelper.writeCmpctSizeUInt(txs, { tx: TransactionOutput => tx.bytes })
val (deserialized, remaining) = RawSerializerHelper.parseCmpctSizeUIntSeq(serialized, TransactionOutput(_: scodec.bits.ByteVector))
val (deserialized, remaining) = RawSerializerHelper.parseCmpctSizeUIntSeq(serialized, TransactionOutput(_: ByteVector))
deserialized == txs && remaining == ByteVector.empty
}
}
Expand Down

0 comments on commit 1d74af0

Please sign in to comment.