Skip to content

Commit 3adf8e3

Browse files
webwarrior-wsknocte
authored andcommitted
Core(Serialization): added warning message type
Added message type 1 (warning) as described in https://github.com/lightning/bolts/blob/master/01-messaging.md#the-error-and-warning-messages Added test which mirrors test for error message
1 parent 9ce1b59 commit 3adf8e3

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/DotNetLightning.Core/Serialization/Msgs/Msgs.fs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ module internal TypeFlag =
6262
[<Literal>]
6363
let Error = 17us
6464

65+
[<Literal>]
66+
let Warning = 1us
67+
6568
[<Literal>]
6669
let Ping = 18us
6770

@@ -210,6 +213,7 @@ module ILightningSerializable =
210213
match t with
211214
| TypeFlag.Init -> deserialize<InitMsg>(ls) :> ILightningMsg
212215
| TypeFlag.Error -> deserialize<ErrorMsg>(ls) :> ILightningMsg
216+
| TypeFlag.Warning -> deserialize<WarningMsg>(ls) :> ILightningMsg
213217
| TypeFlag.Ping -> deserialize<PingMsg>(ls) :> ILightningMsg
214218
| TypeFlag.Pong -> deserialize<PongMsg>(ls) :> ILightningMsg
215219
| TypeFlag.OpenChannel ->
@@ -272,6 +276,11 @@ module ILightningSerializable =
272276

273277
(d :> ILightningSerializable<ErrorMsg>)
274278
.Serialize(ls)
279+
| :? WarningMsg as warningMsg ->
280+
ls.Write(TypeFlag.Warning, false)
281+
282+
(warningMsg :> ILightningSerializable<WarningMsg>)
283+
.Serialize ls
275284
| :? PingMsg as d ->
276285
ls.Write(TypeFlag.Ping, false)
277286

@@ -1809,6 +1818,50 @@ and WhichChannel =
18091818
| SpecificChannel of ChannelId
18101819
| All
18111820

1821+
1822+
[<RequireQualifiedAccess>]
1823+
[<CLIMutable>]
1824+
type WarningMsg =
1825+
{
1826+
mutable ChannelId: WhichChannel
1827+
mutable Data: array<byte>
1828+
}
1829+
1830+
interface ISetupMsg
1831+
1832+
interface ILightningSerializable<WarningMsg> with
1833+
member this.Deserialize readStream =
1834+
match readStream.ReadUInt256 true with
1835+
| id when id = uint256.Zero -> this.ChannelId <- All
1836+
| id -> this.ChannelId <- SpecificChannel(ChannelId id)
1837+
1838+
this.Data <- readStream.ReadWithLen()
1839+
1840+
member this.Serialize writeStream =
1841+
match this.ChannelId with
1842+
| SpecificChannel(ChannelId id) -> writeStream.Write(id.ToBytes())
1843+
| All -> writeStream.Write(Array.zeroCreate 32)
1844+
1845+
writeStream.WriteWithLen this.Data
1846+
1847+
member this.GetFailureMsgData() =
1848+
let minPrintableAsciiChar = 32uy
1849+
1850+
let isPrintableAsciiChar(asciiChar: byte) =
1851+
asciiChar >= minPrintableAsciiChar
1852+
1853+
let isPrintableAsciiString =
1854+
this.Data |> Array.forall isPrintableAsciiChar
1855+
1856+
if isPrintableAsciiString then
1857+
System.Text.ASCIIEncoding.ASCII.GetString this.Data
1858+
else
1859+
Seq.fold
1860+
(fun msg (asciiChar: byte) -> sprintf "%s %02x" msg asciiChar)
1861+
"<warning contains non-printable binary data>:"
1862+
this.Data
1863+
1864+
18121865
#nowarn "0044" // "This construct is deprecated" warning
18131866
// sadly we don't have a way to restore warnings yet.
18141867
// ref: https://github.com/fsharp/fslang-suggestions/issues/278

tests/DotNetLightning.Core.Tests/Serialization.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,25 @@ module SerializationTest =
18141814

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

1817+
testCase "warning"
1818+
<| fun _ ->
1819+
let warningMsg =
1820+
{
1821+
WarningMsg.ChannelId =
1822+
WhichChannel.SpecificChannel(
1823+
ChannelId(
1824+
uint256 [| for _ in 0..31 -> 2uy |]
1825+
)
1826+
)
1827+
WarningMsg.Data = ascii.GetBytes "rust-lightning"
1828+
}
1829+
1830+
let expected =
1831+
hex.DecodeData
1832+
"0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67"
1833+
1834+
Expect.equal (warningMsg.ToBytes()) expected ""
1835+
18171836
testCase "ping"
18181837
<| fun _ ->
18191838
let pingMsg =

tests/DotNetLightning.Core.Tests/SerializationPropertyTests.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ let testList2 =
5757
testPropertyWithConfig config "pong"
5858
<| fun (msg: PongMsg) -> Expect.equal (msg.Clone()) (msg) ""
5959

60+
testPropertyWithConfig config "warning"
61+
<| fun (msg: WarningMsg) -> Expect.equal (msg.Clone()) (msg) ""
62+
6063
testPropertyWithConfig config "open_channel"
6164
<| fun (msg: OpenChannelMsg) -> Expect.equal (msg.Clone()) (msg) ""
6265

0 commit comments

Comments
 (0)