Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve toBigInt and toByteVector conversion for UInt64
Browse files Browse the repository at this point in the history
araspitzu committed Sep 4, 2019
1 parent fd94338 commit 8c60d6a
Showing 2 changed files with 19 additions and 11 deletions.
7 changes: 2 additions & 5 deletions eclair-core/src/main/scala/fr/acinq/eclair/UInt64.scala
Original file line number Diff line number Diff line change
@@ -33,12 +33,9 @@ case class UInt64(private val underlying: Long) extends Ordered[UInt64] {
def <=(other: MilliSatoshi): Boolean = compare(other) <= 0
def >=(other: MilliSatoshi): Boolean = compare(other) >= 0

def toByteVector: ByteVector = underlying match {
case 0 => hex"00"
case _ => ByteVector.fromLong(underlying).dropWhile(_ == 0)
}
def toByteVector: ByteVector = ByteVector.fromLong(underlying)

def toBigInt: BigInt = BigInt(signum = 1, toByteVector.toArray)
def toBigInt: BigInt = (BigInt(underlying >>> 1) << 1) + (underlying & 1)

override def toString: String = UnsignedLongs.toString(underlying, 10)
}
23 changes: 17 additions & 6 deletions eclair-core/src/test/scala/fr/acinq/eclair/UInt64Spec.scala
Original file line number Diff line number Diff line change
@@ -38,22 +38,33 @@ class UInt64Spec extends FunSuite {
assert(z < a && z < b && z < c && z < l && c < l && l < l1 && l < b && l < a)
assert(a == a)
assert(a == UInt64.MaxValue)

assert(l.toByteVector == hex"7fffffffffffffff")
assert(l.toString == Long.MaxValue.toString)
assert(l.toBigInt == BigInt(Long.MaxValue))

assert(l1.toByteVector == hex"8000000000000000")
assert(l1.toString == "9223372036854775808")
assert(l1.toBigInt == BigInt("9223372036854775808"))
assert(a.toByteVector === hex"0xffffffffffffffff")

assert(a.toByteVector === hex"ffffffffffffffff")
assert(a.toString === "18446744073709551615") // 2^64 - 1
assert(b.toByteVector === hex"0xfffffffffffffffe")
assert(a.toBigInt === BigInt("18446744073709551615"))

assert(b.toByteVector === hex"fffffffffffffffe")
assert(b.toString === "18446744073709551614")
assert(c.toByteVector === hex"0x2a")
assert(b.toBigInt === BigInt("18446744073709551614"))

assert(c.toByteVector === hex"00000000000002a")
assert(c.toString === "42")
assert(z.toByteVector === hex"0x00")
assert(c.toBigInt === BigInt("42"))

assert(z.toByteVector === hex"000000000000000")
assert(z.toString === "0")
assert(UInt64(hex"0xff").toByteVector == hex"0xff")
assert(UInt64(hex"0x800").toByteVector == hex"0x800")
assert(z.toBigInt === BigInt("0"))

assert(UInt64(hex"ff").toByteVector == hex"0000000000000ff")
assert(UInt64(hex"800").toByteVector == hex"000000000000800")
}

test("use unsigned comparison when comparing millisatoshis to uint64") {

0 comments on commit 8c60d6a

Please sign in to comment.