Merge pull request #7 from TomMcCabe/refactor_number_system

increase test coverage for CompactSizeUInt
This commit is contained in:
Chris Stewart 2016-07-17 19:20:39 -05:00 committed by GitHub
commit 6f89f622f7
2 changed files with 40 additions and 9 deletions

View file

@ -102,9 +102,9 @@ object CompactSizeUInt extends Factory[CompactSizeUInt] {
if (UInt64(Seq(bytes.head)).underlying < 253)
CompactSizeUInt(UInt64(Seq(bytes.head)),1)
//16 bit number
else if (UInt64(Seq(bytes.head)).underlying == 253) CompactSizeUInt(UInt64(bytes.slice(1,3).reverse),3)
else if (UInt64(Seq(bytes.head)).toInt == 253) CompactSizeUInt(UInt64(bytes.slice(1,3).reverse),3)
//32 bit number
else if (UInt64(Seq(bytes.head)).underlying == 254) CompactSizeUInt(UInt64(bytes.slice(1,5).reverse),5)
else if (UInt64(Seq(bytes.head)).toInt == 254) CompactSizeUInt(UInt64(bytes.slice(1,5).reverse),5)
//64 bit number
else CompactSizeUInt(UInt64(bytes.slice(1,9).reverse),9)
}
@ -138,7 +138,7 @@ object CompactSizeUInt extends Factory[CompactSizeUInt] {
CompactSizeUInt(UInt64(script.bytes.size),1)
} else if (script.bytes.size <= 0xffff) {
CompactSizeUInt(UInt64(script.bytes.size),3)
} else if (script.bytes.size <= 0xffffffff) {
} else if (script.bytes.size <= 0xffffffffL) {
CompactSizeUInt(UInt64(script.bytes.size),5)
}
else CompactSizeUInt(UInt64(script.bytes.size),9)
@ -155,7 +155,7 @@ object CompactSizeUInt extends Factory[CompactSizeUInt] {
CompactSizeUInt(UInt64(scriptPubKey.bytes.size),1)
} else if (scriptPubKey.bytes.size <= 0xffff) {
CompactSizeUInt(UInt64(scriptPubKey.bytes.size),3)
} else if (scriptPubKey.bytes.size <= 0xffffffff) {
} else if (scriptPubKey.bytes.size <= 0xffffffffL) {
CompactSizeUInt(UInt64(scriptPubKey.bytes.size),5)
} else CompactSizeUInt(UInt64(scriptPubKey.bytes.size),9)
}
@ -165,8 +165,6 @@ object CompactSizeUInt extends Factory[CompactSizeUInt] {
private def parseLong(bytes : List[Byte]) : Long = parseLong(BitcoinSUtil.encodeHex(bytes))
private def parseLong(byte : Byte) : Long = parseLong(List(byte))
private def parseLong(bytes : Seq[Byte]) : Long = parseLong(bytes.toList)
}

View file

@ -2,7 +2,7 @@ package org.bitcoins.core.protocol
import org.bitcoins.core.number.UInt64
import org.bitcoins.core.protocol.script.ScriptSignature
import org.bitcoins.core.util.TestUtil
import org.bitcoins.core.util.{BitcoinSUtil, TestUtil}
import org.scalatest.{FlatSpec, MustMatchers}
/**
@ -43,7 +43,18 @@ class CompactSizeUIntTest extends FlatSpec with MustMatchers {
compactSizeUInt.hex must be ("fd0302")
}
it must "calculate correct compact size uint for a number 500,000 bytes long" in {
val byteSeq500000Size = for (_ <- 0 until 500000) yield 0.toByte
val compactSizeUInt = CompactSizeUInt.calculateCompactSizeUInt(byteSeq500000Size)
compactSizeUInt must be (CompactSizeUInt(UInt64(500000), 5))
compactSizeUInt.hex must be ("fe20a10700")
}
it must "parse a compact size uint from bytes" in {
val str = "fd0302"
val bytes = BitcoinSUtil.decodeHex(str)
CompactSizeUInt.fromBytes(bytes) must be (CompactSizeUInt(UInt64(515), 3))
}
it must "parse a variable length integer (VarInt)" in {
val str = "fdfd00"
@ -52,8 +63,11 @@ class CompactSizeUIntTest extends FlatSpec with MustMatchers {
val str1 = "00"
CompactSizeUInt.parseCompactSizeUInt(str1) must be (CompactSizeUInt(UInt64.zero,1))
val str2 = "ffffffffff"
CompactSizeUInt.parseCompactSizeUInt(str2) must be (CompactSizeUInt(UInt64(4294967295L),9))
val str2 = "fe20a10700"
CompactSizeUInt.parseCompactSizeUInt(str2) must be (CompactSizeUInt(UInt64(500000)))
val str3 = "ffffffffff"
CompactSizeUInt.parseCompactSizeUInt(str3) must be (CompactSizeUInt(UInt64(4294967295L),9))
}
@ -70,4 +84,23 @@ class CompactSizeUIntTest extends FlatSpec with MustMatchers {
CompactSizeUInt.parseCompactSizeUInt(ScriptSignature.empty) must be (CompactSizeUInt(UInt64.zero,1))
}
it must "parse variable length integer of script sig at least 0xffff bytes in length, and greater than 0xffffffff" in {
CompactSizeUInt.parseCompactSizeUInt(ScriptSignature(TestUtil.rawP2shInputScriptLargeSignature * 50)) must be (CompactSizeUInt(UInt64(30300), 3))
CompactSizeUInt.parseCompactSizeUInt(ScriptSignature(TestUtil.rawP2shInputScriptLargeSignature * 120)) must be (CompactSizeUInt(UInt64(72720), 5))
}
it must "parse 32 bit number and 64 bit number as compactsizeuints" in {
val bit32 = 254.toByte
val bit64 = 255.toByte
CompactSizeUInt.parseCompactSizeUIntSize(bit32) must be (5)
CompactSizeUInt.parseCompactSizeUIntSize(bit64) must be (9)
}
it must "intercept a failed requirement when the byte array size is zero" in {
intercept[IllegalArgumentException] {
val emptyBytes : Seq[Byte] = Seq()
CompactSizeUInt.parseCompactSizeUInt(emptyBytes)
}
}
}