Skip to content

Commit 617f973

Browse files
authored
Merge pull request joemphilips#123 from canndrew/upstream-master-with-chain-hash-reversal
Fix chain hashes being serialized in reverse
2 parents 97c62d4 + d328472 commit 617f973

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

src/DotNetLightning.Core/Serialize/Msgs/Msgs.fs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ with
516516
interface IChannelMsg
517517
interface ILightningSerializable<OpenChannelMsg> with
518518
member this.Deserialize(ls) =
519-
this.Chainhash <- ls.ReadUInt256(false)
519+
this.Chainhash <- ls.ReadUInt256(true)
520520
this.TemporaryChannelId <- ChannelId(ls.ReadUInt256(true))
521521
this.FundingSatoshis <- Money.Satoshis(ls.ReadUInt64(false))
522522
this.PushMSat <- LNMoney.MilliSatoshis(ls.ReadUInt64(false))
@@ -538,7 +538,7 @@ with
538538
if (ls.Position = ls.Length) then None else
539539
ls.ReadWithLen() |> Script |> Some
540540
member this.Serialize(ls) =
541-
ls.Write(this.Chainhash, false)
541+
ls.Write(this.Chainhash, true)
542542
ls.Write(this.TemporaryChannelId.Value, true)
543543
ls.Write(this.FundingSatoshis.Satoshi, false)
544544
ls.Write(this.PushMSat.MilliSatoshi, false)
@@ -1111,7 +1111,7 @@ with
11111111
member this.Deserialize(ls) =
11121112
this.Features <-
11131113
ls.ReadWithLen() |> FeatureBit.CreateUnsafe
1114-
this.ChainHash <- ls.ReadUInt256(false)
1114+
this.ChainHash <- ls.ReadUInt256(true)
11151115
this.ShortChannelId <- ls.ReadUInt64(false) |> ShortChannelId.FromUInt64
11161116
this.NodeId1 <- ls.ReadPubKey() |> NodeId
11171117
this.NodeId2 <- ls.ReadPubKey() |> NodeId
@@ -1120,7 +1120,7 @@ with
11201120
this.ExcessData <- match ls.TryReadAll() with Some b -> b | None -> [||]
11211121
member this.Serialize(ls) =
11221122
ls.WriteWithLen(this.Features.ToByteArray())
1123-
ls.Write(this.ChainHash, false)
1123+
ls.Write(this.ChainHash, true)
11241124
ls.Write(this.ShortChannelId)
11251125
ls.Write(this.NodeId1.Value)
11261126
ls.Write(this.NodeId2.Value)
@@ -1169,7 +1169,7 @@ type UnsignedChannelUpdateMsg = {
11691169
interface IRoutingMsg
11701170
interface ILightningSerializable<UnsignedChannelUpdateMsg> with
11711171
member this.Deserialize(ls: LightningReaderStream): unit =
1172-
this.ChainHash <- ls.ReadUInt256(false)
1172+
this.ChainHash <- ls.ReadUInt256(true)
11731173
this.ShortChannelId <- ls.ReadUInt64(false) |> ShortChannelId.FromUInt64
11741174
this.Timestamp <- ls.ReadUInt32(false)
11751175
this.MessageFlags <- ls.ReadByte()
@@ -1184,7 +1184,7 @@ type UnsignedChannelUpdateMsg = {
11841184
else
11851185
None
11861186
member this.Serialize(ls: LightningWriterStream): unit =
1187-
ls.Write(this.ChainHash, false)
1187+
ls.Write(this.ChainHash, true)
11881188
ls.Write(this.ShortChannelId)
11891189
ls.Write(this.Timestamp, false)
11901190
ls.Write(this.MessageFlags)
@@ -1441,7 +1441,7 @@ type QueryShortChannelIdsMsg = {
14411441
interface IQueryMsg
14421442
interface ILightningSerializable<QueryShortChannelIdsMsg> with
14431443
member this.Deserialize(ls: LightningReaderStream) =
1444-
this.ChainHash <- ls.ReadUInt256(false)
1444+
this.ChainHash <- ls.ReadUInt256(true)
14451445
let shortIdsWithFlag = ls.ReadWithLen()
14461446
this.ShortIdsEncodingType <- LanguagePrimitives.EnumOfValue<byte, EncodingType>(shortIdsWithFlag.[0])
14471447
let shortIds =
@@ -1463,7 +1463,7 @@ type QueryShortChannelIdsMsg = {
14631463
this.ShortIds <- shortIds
14641464
this.TLVs <- tlvs
14651465
member this.Serialize(ls) =
1466-
ls.Write(this.ChainHash, false)
1466+
ls.Write(this.ChainHash, true)
14671467
let encodedIds = this.ShortIds |> Encoder.encodeShortChannelIds (this.ShortIdsEncodingType)
14681468
[[|(byte)this.ShortIdsEncodingType|]; encodedIds]
14691469
|> Array.concat
@@ -1479,14 +1479,14 @@ type ReplyShortChannelIdsEndMsg = {
14791479
interface IQueryMsg
14801480
interface ILightningSerializable<ReplyShortChannelIdsEndMsg> with
14811481
member this.Deserialize(ls) =
1482-
this.ChainHash <- ls.ReadUInt256(false)
1482+
this.ChainHash <- ls.ReadUInt256(true)
14831483
this.Complete <-
14841484
let b = ls.ReadByte()
14851485
if (b = 0uy) then false else
14861486
if (b = 1uy) then true else
14871487
raise <| FormatException(sprintf "reply_short_channel_ids has unknown byte in `complete` field %A" b)
14881488
member this.Serialize(ls) =
1489-
ls.Write(this.ChainHash, false)
1489+
ls.Write(this.ChainHash, true)
14901490
ls.Write(if (this.Complete) then 1uy else 0uy)
14911491
[<CLIMutable>]
14921492
type QueryChannelRangeMsg = {
@@ -1499,15 +1499,15 @@ type QueryChannelRangeMsg = {
14991499
interface IQueryMsg
15001500
interface ILightningSerializable<QueryChannelRangeMsg> with
15011501
member this.Deserialize(ls) =
1502-
this.ChainHash <- ls.ReadUInt256(false)
1502+
this.ChainHash <- ls.ReadUInt256(true)
15031503
this.FirstBlockNum <- ls.ReadUInt32(false) |> BlockHeight
15041504
this.NumberOfBlocks <- ls.ReadUInt32(false)
15051505
this.TLVs <-
15061506
let r = ls.ReadTLVStream()
15071507
r
15081508
|> Array.map(QueryChannelRangeTLV.FromGenericTLV)
15091509
member this.Serialize(ls) =
1510-
ls.Write(this.ChainHash, false)
1510+
ls.Write(this.ChainHash, true)
15111511
ls.Write(this.FirstBlockNum.Value, false)
15121512
ls.Write(this.NumberOfBlocks, false)
15131513
this.TLVs |> Array.map(fun tlv -> tlv.ToGenericTLV()) |> ls.WriteTLVStream
@@ -1526,7 +1526,7 @@ type ReplyChannelRangeMsg = {
15261526
interface IQueryMsg
15271527
interface ILightningSerializable<ReplyChannelRangeMsg> with
15281528
member this.Deserialize(ls) =
1529-
this.ChainHash <- ls.ReadUInt256(false)
1529+
this.ChainHash <- ls.ReadUInt256(true)
15301530
this.FirstBlockNum <- ls.ReadUInt32(false) |> BlockHeight
15311531
this.NumOfBlocks <- ls.ReadUInt32(false)
15321532
this.Complete <-
@@ -1541,7 +1541,7 @@ type ReplyChannelRangeMsg = {
15411541
this.TLVs <-
15421542
ls.ReadTLVStream() |> Array.map(ReplyChannelRangeTLV.FromGenericTLV)
15431543
member this.Serialize(ls) =
1544-
ls.Write(this.ChainHash, false)
1544+
ls.Write(this.ChainHash, true)
15451545
ls.Write(this.FirstBlockNum.Value, false)
15461546
ls.Write(this.NumOfBlocks, false)
15471547
ls.Write(if this.Complete then 1uy else 0uy)
@@ -1561,11 +1561,11 @@ type GossipTimestampFilterMsg = {
15611561
interface IQueryMsg
15621562
interface ILightningSerializable<GossipTimestampFilterMsg> with
15631563
member this.Deserialize(ls) =
1564-
this.ChainHash <- ls.ReadUInt256(false)
1564+
this.ChainHash <- ls.ReadUInt256(true)
15651565
this.FirstTimestamp <- ls.ReadUInt32(false)
15661566
this.TimestampRange <- ls.ReadUInt32(false)
15671567
member this.Serialize(ls) =
1568-
ls.Write(this.ChainHash, false)
1568+
ls.Write(this.ChainHash, true)
15691569
ls.Write(this.FirstTimestamp, false)
15701570
ls.Write(this.TimestampRange, false)
15711571

src/DotNetLightning.Core/Serialize/TLVs.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ type InitTLV =
1717
if rem <> 0 then raise <| FormatException(sprintf "Bogus length for TLV in init message (%d), remainder was (%d)" tlv.Value.Length rem) else
1818
let result = Array.zeroCreate n
1919
for i in 0..n - 1 do
20-
result.[i] <- tlv.Value.[(i * 32)..((i * 32) + 31)] |> fun x -> uint256(x, false)
20+
result.[i] <- tlv.Value.[(i * 32)..((i * 32) + 31)] |> fun x -> uint256(x, true)
2121
result |> Networks
2222
| _ -> Unknown (tlv)
2323

2424
member this.ToGenericTLV() =
2525
match this with
2626
| Networks networks ->
27-
let v = networks |> Array.map(fun x -> x.ToBytes(false)) |> Array.concat
27+
let v = networks |> Array.map(fun x -> x.ToBytes(true)) |> Array.concat
2828
{ GenericTLV.Type = 1UL; Value = v }
2929
| Unknown tlv -> tlv
3030

tests/DotNetLightning.Core.Tests/Serialization.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module SerializationTest =
124124

125125
let unsignedChannelAnnoucement = {
126126
Features = features
127-
ChainHash = if (not nonbitcoinChainHash) then uint256(hex.DecodeData("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
127+
ChainHash = if (not nonbitcoinChainHash) then uint256(hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
128128
ShortChannelId = ShortChannelId.FromUInt64(2316138423780173UL)
129129
NodeId1 = NodeId(privKey1.PubKey)
130130
NodeId2 = NodeId(privKey2.PubKey)
@@ -143,7 +143,7 @@ module SerializationTest =
143143
let mutable expected = hex.DecodeData("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a1735b6a427e80d5fe7cd90a2f4ee08dc9c27cda7c35a4172e5d85b12c49d4232537e98f9b1f3c5e6989a8b9644e90e8918127680dbd0d4043510840fc0f1e11a216c280b5395a2546e7e4b2663e04f811622f15a4f91e83aa2e92ba2a573c139142c54ae63072a1ec1ee7dc0c04bde5c847806172aa05c92c22ae8e308d1d2692b12cc195ce0a2d1bda6a88befa19fa07f51caa75ce83837f28965600b8aacab0855ffb0e741ec5f7c41421e9829a9d48611c8c831f71be5ea73e66594977ffd")
144144
expected <- Array.append expected (hex.DecodeData("0000"))
145145
if nonbitcoinChainHash then
146-
expected <- Array.append expected (hex.DecodeData("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000"))
146+
expected <- Array.append expected (hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
147147
else
148148
expected <- Array.append expected (hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"))
149149
expected <- Array.append expected (hex.DecodeData("00083a840000034d031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b"))
@@ -417,7 +417,7 @@ module SerializationTest =
417417
let channelUpdateTestCore (nonBitcoinChainHash: bool, direction: bool, disable: bool, htlcMaximumMSat: bool) =
418418
let sig1 = signMessageWith privKey1 "01010101010101010101010101010101"
419419
let unsignedChannelUpdateMsg = {
420-
UnsignedChannelUpdateMsg.ChainHash = if (not nonBitcoinChainHash) then uint256(hex.DecodeData("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
420+
UnsignedChannelUpdateMsg.ChainHash = if (not nonBitcoinChainHash) then uint256(hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
421421
ShortChannelId = ShortChannelId.FromUInt64(2316138423780173UL)
422422
Timestamp = 20190119u
423423
MessageFlags = (if htlcMaximumMSat then 1uy else 0uy)
@@ -442,7 +442,7 @@ module SerializationTest =
442442
let actual = channelUpdateMsg.ToBytes()
443443
let mutable expected = hex.DecodeData("d977cb9b53d93a6ff64bb5f1e158b4094b66e798fb12911168a3ccdf80a83096340a6a95da0ae8d9f776528eecdbb747eb6b545495a4319ed5378e35b21e073a")
444444
if nonBitcoinChainHash then
445-
expected <- Array.append expected (hex.DecodeData("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000"))
445+
expected <- Array.append expected (hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
446446
else
447447
expected <- Array.append expected (hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"))
448448
expected <- Array.append expected (hex.DecodeData("00083a840000034d013413a7"))
@@ -469,7 +469,7 @@ module SerializationTest =
469469
testCase "open_channel" <| fun _ ->
470470
let openChannelTestCore(nonBitcoinChainHash: bool, randomBit: bool, shutdown: bool) =
471471
let openChannelMsg = {
472-
Chainhash = if (not nonBitcoinChainHash) then uint256(hex.DecodeData("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
472+
Chainhash = if (not nonBitcoinChainHash) then uint256(hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")) else uint256(hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
473473
TemporaryChannelId = ChannelId(uint256([| for _ in 0..31 -> 2uy |]))
474474
FundingSatoshis = Money.Satoshis(1311768467284833366UL)
475475
PushMSat = LNMoney.MilliSatoshis(2536655962884945560L)
@@ -492,7 +492,7 @@ module SerializationTest =
492492
let actual = openChannelMsg.ToBytes()
493493
let mutable expected = [||]
494494
if nonBitcoinChainHash then
495-
expected <- Array.append expected (hex.DecodeData("43497fd7f826957108f4a30fd9cec3aeba79972084e90ead01ea330900000000"))
495+
expected <- Array.append expected (hex.DecodeData("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"))
496496
else
497497
expected <- Array.append expected (hex.DecodeData("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"))
498498
expected <- Array.append expected (hex.DecodeData("02020202020202020202020202020202020202020202020202020202020202021234567890123456233403289122369832144668701144767633030896203198784335490624111800083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a"))

0 commit comments

Comments
 (0)