Skip to content

Commit

Permalink
Core(Serialization): added warning message type
Browse files Browse the repository at this point in the history
* Added message type 1 (warning) as described in [1].
* Added test which mirrors test for error message.

[1] https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages

Upstream PR: joemphilips#229
  • Loading branch information
webwarrior-ws authored and knocte committed Aug 24, 2022
1 parent b67e13f commit 5c85f50
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/DotNetLightning.Core/Serialization/Msgs/Msgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ module internal TypeFlag =
[<Literal>]
let Error = 17us

[<Literal>]
let Warning = 1us

[<Literal>]
let Ping = 18us

Expand Down Expand Up @@ -210,6 +213,7 @@ module ILightningSerializable =
match t with
| TypeFlag.Init -> deserialize<InitMsg>(ls) :> ILightningMsg
| TypeFlag.Error -> deserialize<ErrorMsg>(ls) :> ILightningMsg
| TypeFlag.Warning -> deserialize<WarningMsg>(ls) :> ILightningMsg
| TypeFlag.Ping -> deserialize<PingMsg>(ls) :> ILightningMsg
| TypeFlag.Pong -> deserialize<PongMsg>(ls) :> ILightningMsg
| TypeFlag.OpenChannel ->
Expand Down Expand Up @@ -272,6 +276,11 @@ module ILightningSerializable =

(d :> ILightningSerializable<ErrorMsg>)
.Serialize(ls)
| :? WarningMsg as warningMsg ->
ls.Write(TypeFlag.Warning, false)

(warningMsg :> ILightningSerializable<WarningMsg>)
.Serialize ls
| :? PingMsg as d ->
ls.Write(TypeFlag.Ping, false)

Expand Down Expand Up @@ -1809,6 +1818,50 @@ and WhichChannel =
| SpecificChannel of ChannelId
| All


[<RequireQualifiedAccess>]
[<CLIMutable>]
type WarningMsg =
{
mutable ChannelId: WhichChannel
mutable Data: array<byte>
}

interface ISetupMsg

interface ILightningSerializable<WarningMsg> with
member this.Deserialize readStream =
match readStream.ReadUInt256 true with
| id when id = uint256.Zero -> this.ChannelId <- All
| id -> this.ChannelId <- SpecificChannel(ChannelId id)

this.Data <- readStream.ReadWithLen()

member this.Serialize writeStream =
match this.ChannelId with
| SpecificChannel(ChannelId id) -> writeStream.Write(id.ToBytes())
| All -> writeStream.Write(Array.zeroCreate 32)

writeStream.WriteWithLen this.Data

member this.GetFailureMsgData() =
let minPrintableAsciiChar = 32uy

let isPrintableAsciiChar(asciiChar: byte) =
asciiChar >= minPrintableAsciiChar

let isPrintableAsciiString =
this.Data |> Array.forall isPrintableAsciiChar

if isPrintableAsciiString then
System.Text.ASCIIEncoding.ASCII.GetString this.Data
else
Seq.fold
(fun msg (asciiChar: byte) -> sprintf "%s %02x" msg asciiChar)
"<warning contains non-printable binary data>:"
this.Data


#nowarn "0044" // "This construct is deprecated" warning
// sadly we don't have a way to restore warnings yet.
// ref: https://github.com/fsharp/fslang-suggestions/issues/278
Expand Down
19 changes: 19 additions & 0 deletions tests/DotNetLightning.Core.Tests/Serialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,25 @@ module SerializationTest =

Expect.equal (errorMsg.ToBytes()) (expected) ""

testCase "warning"
<| fun _ ->
let warningMsg =
{
WarningMsg.ChannelId =
WhichChannel.SpecificChannel(
ChannelId(
uint256 [| for _ in 0..31 -> 2uy |]
)
)
WarningMsg.Data = ascii.GetBytes "rust-lightning"
}

let expected =
hex.DecodeData
"0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67"

Expect.equal (warningMsg.ToBytes()) expected ""

testCase "ping"
<| fun _ ->
let pingMsg =
Expand Down

0 comments on commit 5c85f50

Please sign in to comment.