Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ object CommonCodecs {

val uint64: Codec[UInt64] = bytes(8).xmap(b => UInt64(b), a => a.toByteVector.padLeft(8))

val uint64L: Codec[UInt64] = bytes(8).xmap(b => UInt64(b.reverse), a => a.toByteVector.padLeft(8).reverse)

/**
* We impose a minimal encoding on some values (such as varint and truncated int) to ensure that signed hashes can be
* re-computed correctly.
Expand All @@ -73,9 +71,9 @@ object CommonCodecs {
// See https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers for reference.
val varint: Codec[UInt64] = discriminatorWithDefault(
discriminated[UInt64].by(uint8L)
.\(0xff) { case i if i >= UInt64(0x100000000L) => i }(minimalvalue(uint64L, UInt64(0x100000000L)))
.\(0xfe) { case i if i >= UInt64(0x10000) => i }(minimalvalue(uint32L.xmap(UInt64(_), _.toBigInt.toLong), UInt64(0x10000)))
.\(0xfd) { case i if i >= UInt64(0xfd) => i }(minimalvalue(uint16L.xmap(UInt64(_), _.toBigInt.toInt), UInt64(0xfd))),
.\(0xff) { case i if i >= UInt64(0x100000000L) => i }(minimalvalue(uint64, UInt64(0x100000000L)))
.\(0xfe) { case i if i >= UInt64(0x10000) => i }(minimalvalue(uint32.xmap(UInt64(_), _.toBigInt.toLong), UInt64(0x10000)))
.\(0xfd) { case i if i >= UInt64(0xfd) => i }(minimalvalue(uint16.xmap(UInt64(_), _.toBigInt.toInt), UInt64(0xfd))),
uint8L.xmap(UInt64(_), _.toBigInt.toInt)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,16 @@ class CommonCodecsSpec extends FunSuite {
}
}

test("encode/decode with uint64L codec") {
val expected = Map(
UInt64(0) -> hex"00 00 00 00 00 00 00 00",
UInt64(42) -> hex"2a 00 00 00 00 00 00 00",
UInt64(6211610197754262546L) -> hex"12 34 56 78 90 12 34 56",
UInt64(hex"ff ff ff ff ff ff ff ff") -> hex"ff ff ff ff ff ff ff ff"
).mapValues(_.toBitVector)

for ((uint, ref) <- expected) {
val encoded = uint64L.encode(uint).require
assert(ref === encoded)
val decoded = uint64L.decode(encoded).require.value
assert(uint === decoded)
}
}

test("encode/decode with varint codec") {
val expected = Map(
UInt64(0L) -> hex"00",
UInt64(42L) -> hex"2a",
UInt64(253L) -> hex"fd fd 00",
UInt64(254L) -> hex"fd fe 00",
UInt64(255L) -> hex"fd ff 00",
UInt64(550L) -> hex"fd 26 02",
UInt64(998000L) -> hex"fe 70 3a 0f 00",
UInt64(6211610197754262546L) -> hex"ff 12 34 56 78 90 12 34 56",
UInt64(253L) -> hex"fd 00 fd",
UInt64(254L) -> hex"fd 00 fe",
UInt64(255L) -> hex"fd 00 ff",
UInt64(550L) -> hex"fd 02 26",
UInt64(998000L) -> hex"fe 00 0f 3a 70",
UInt64(1311768467284833366L) -> hex"ff 12 34 56 78 90 12 34 56",
UInt64.MaxValue -> hex"ff ff ff ff ff ff ff ff ff"
).mapValues(_.toBitVector)

Expand All @@ -93,30 +77,30 @@ class CommonCodecsSpec extends FunSuite {
hex"ff", // truncated
hex"ff 12 34 56 78", // truncated
hex"fd 00 00", // not minimally-encoded
hex"fd fc 00", // not minimally-encoded
hex"fd 00 fc", // not minimally-encoded
hex"fe 00 00 00 00", // not minimally-encoded
hex"fe ff ff 00 00", // not minimally-encoded
hex"fe 00 00 ff ff", // not minimally-encoded
hex"ff 00 00 00 00 00 00 00 00", // not minimally-encoded
hex"ff ff ff ff 01 00 00 00 00", // not minimally-encoded
hex"ff ff ff ff ff 00 00 00 00" // not minimally-encoded
hex"ff 00 00 00 00 01 ff ff ff", // not minimally-encoded
hex"ff 00 00 00 00 ff ff ff ff" // not minimally-encoded
).map(_.toBitVector)

for (testCase <- testCases) {
assert(varint.decode(testCase).isFailure, testCase.toByteVector)
}
}

test("encode/decode with varlong codec") {
test("encode/decode with varintoverflow codec") {
val expected = Map(
0L -> hex"00",
42L -> hex"2a",
253L -> hex"fd fd 00",
254L -> hex"fd fe 00",
255L -> hex"fd ff 00",
550L -> hex"fd 26 02",
998000L -> hex"fe 70 3a 0f 00",
6211610197754262546L -> hex"ff 12 34 56 78 90 12 34 56",
Long.MaxValue -> hex"ff ff ff ff ff ff ff ff 7f"
253L -> hex"fd 00 fd",
254L -> hex"fd 00 fe",
255L -> hex"fd 00 ff",
550L -> hex"fd 02 26",
998000L -> hex"fe 00 0f 3a 70",
1311768467284833366L -> hex"ff 12 34 56 78 90 12 34 56",
Long.MaxValue -> hex"ff 7f ff ff ff ff ff ff ff"
).mapValues(_.toBitVector)

for ((long, ref) <- expected) {
Expand All @@ -127,9 +111,9 @@ class CommonCodecsSpec extends FunSuite {
}
}

test("decode invalid varlong") {
test("decode invalid varintoverflow") {
val testCases = Seq(
hex"ff 00 00 00 00 00 00 00 80",
hex"ff 80 00 00 00 00 00 00 00",
hex"ff ff ff ff ff ff ff ff ff"
).map(_.toBitVector)

Expand Down
42 changes: 21 additions & 21 deletions eclair-core/src/test/scala/fr/acinq/eclair/wire/TlvCodecsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ class TlvCodecsSpec extends FunSuite {
val testCases = Seq(
(hex"", TlvStream[TestTlv]()),
(hex"21 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(33, hex"")))),
(hex"fd0102 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(513, hex"")))),
(hex"fdfd00 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(253, hex"")))),
(hex"fdff00 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(255, hex"")))),
(hex"fe01000002 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(33554433, hex"")))),
(hex"ff0100000000000002 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(144115188075855873L, hex"")))),
(hex"fd0201 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(513, hex"")))),
(hex"fd00fd 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(253, hex"")))),
(hex"fd00ff 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(255, hex"")))),
(hex"fe02000001 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(33554433, hex"")))),
(hex"ff0200000000000001 00", TlvStream[TestTlv](Nil, Seq(GenericTlv(144115188075855873L, hex"")))),
(hex"01 00", TlvStream[TestTlv](TestType1(0))),
(hex"01 01 01", TlvStream[TestTlv](TestType1(1))),
(hex"01 01 2a", TlvStream[TestTlv](TestType1(42))),
Expand All @@ -163,9 +163,9 @@ class TlvCodecsSpec extends FunSuite {
(hex"01 08 0100000000000000", TlvStream[TestTlv](TestType1(72057594037927936L))),
(hex"02 08 0000000000000226", TlvStream[TestTlv](TestType2(ShortChannelId(550)))),
(hex"03 31 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb 0000000000000231 0000000000000451", TlvStream[TestTlv](TestType3(PublicKey(hex"023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb"), 561, 1105))),
(hex"fdfe00 02 0226", TlvStream[TestTlv](TestType254(550))),
(hex"fd00fe 02 0226", TlvStream[TestTlv](TestType254(550))),
(hex"01020231 02080000000000000451 033102eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368661900000000000002310000000000000451", TlvStream[TestTlv](TestType1(561), TestType2(ShortChannelId(1105)), TestType3(PublicKey(hex"02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619"), 561, 1105))),
(hex"01020231 0b020451 fdfe0002002a", TlvStream[TestTlv](Seq(TestType1(561), TestType254(42)), Seq(GenericTlv(11, hex"0451"))))
(hex"01020231 0b020451 fd00fe02002a", TlvStream[TestTlv](Seq(TestType1(561), TestType254(42)), Seq(GenericTlv(11, hex"0451"))))
)

for ((bin, expected) <- testCases) {
Expand All @@ -182,26 +182,26 @@ class TlvCodecsSpec extends FunSuite {
hex"fd",
hex"fd01",
// Not minimally encoded type.
hex"fd0100 00",
hex"fd0001 00",
// Missing length.
hex"fd0101",
// Length truncated.
hex"0f fd",
hex"0f fd02",
// Not minimally encoded length.
hex"0f fd0100 00",
hex"0f fe01000000 00",
hex"0f fd0001 00",
hex"0f fe00000001 00",
// Missing value.
hex"0f fd0226",
hex"0f fd2602",
// Value truncated.
hex"0f fd0102 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
hex"0f fd0201 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
// Unknown even type.
hex"12 00",
hex"0a 00",
hex"fd0201 00",
hex"fe02000001 00",
hex"fd0102 00",
hex"fe01000002 00",
hex"01020101 0a0101",
hex"ff0200000000000001 00",
hex"ff0100000000000002 00",
// Invalid TestTlv1.
hex"01 01 00", // not minimally-encoded
hex"01 02 0001", // not minimally-encoded
Expand All @@ -220,9 +220,9 @@ class TlvCodecsSpec extends FunSuite {
hex"03 30 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb000000000000000100000000000001", // invalid length
hex"03 32 023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001000000000000000001", // invalid length
// Invalid TestTlv254.
hex"fdfe00 00", // invalid length
hex"fdfe00 01 01", // invalid length
hex"fdfe00 03 010101", // invalid length
hex"fd00fe 00", // invalid length
hex"fd00fe 01 01", // invalid length
hex"fd00fe 03 010101", // invalid length
// Invalid multi-record streams.
hex"01012a 02", // valid tlv record followed by invalid tlv record (length missing)
hex"01012a 0208", // valid tlv record followed by invalid tlv record (value missing)
Expand All @@ -243,7 +243,7 @@ class TlvCodecsSpec extends FunSuite {
test("encode/decode length-prefixed tlv stream") {
val testCases = Seq(
hex"41 01020231 02080000000000000451 033102eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368661900000000000002310000000000000451",
hex"fd4d01 01020231 02080000000000000451 033102eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368661900000000000002310000000000000451 ff6543210987654321 fd0001 10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010010101010101"
hex"fd014d 01020231 02080000000000000451 033102eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368661900000000000002310000000000000451 ff6543210987654321 fd0100 10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010010101010101"
)

for (testCase <- testCases) {
Expand Down Expand Up @@ -274,8 +274,8 @@ class TlvCodecsSpec extends FunSuite {

test("encode unordered tlv stream (codec should sort appropriately)") {
val stream = TlvStream[TestTlv](Seq(TestType254(42), TestType1(42)), Seq(GenericTlv(13, hex"2a"), GenericTlv(11, hex"2b")))
assert(testTlvStreamCodec.encode(stream).require.toByteVector === hex"01012a 0b012b 0d012a fdfe0002002a")
assert(lengthPrefixedTestTlvStreamCodec.encode(stream).require.toByteVector === hex"0f 01012a 0b012b 0d012a fdfe0002002a")
assert(testTlvStreamCodec.encode(stream).require.toByteVector === hex"01012a 0b012b 0d012a fd00fe02002a")
assert(lengthPrefixedTestTlvStreamCodec.encode(stream).require.toByteVector === hex"0f 01012a 0b012b 0d012a fd00fe02002a")
}

test("encode invalid tlv stream") {
Expand Down