@@ -25,6 +25,12 @@ use crate::lsps1;
25
25
use crate :: lsps2;
26
26
use crate :: lsps5;
27
27
28
+ use lightning:: io;
29
+ use lightning:: ln:: msgs:: DecodeError ;
30
+ use lightning:: util:: ser:: {
31
+ BigSize , FixedLengthReader , MaybeReadable , Readable , Writeable , Writer ,
32
+ } ;
33
+
28
34
/// An event which you should probably take some action in response to.
29
35
#[ derive( Debug , Clone , PartialEq , Eq ) ]
30
36
pub enum LiquidityEvent {
@@ -87,3 +93,53 @@ impl From<lsps5::event::LSPS5ServiceEvent> for LiquidityEvent {
87
93
Self :: LSPS5Service ( event)
88
94
}
89
95
}
96
+
97
+ impl Writeable for LiquidityEvent {
98
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
99
+ match self {
100
+ Self :: LSPS0Client ( _) => { } ,
101
+ Self :: LSPS1Client ( _) => { } ,
102
+ #[ cfg( lsps1_service) ]
103
+ Self :: LSPS1Service ( _) => { } ,
104
+ Self :: LSPS2Client ( _) => { } ,
105
+ Self :: LSPS2Service ( event) => {
106
+ 0u8 . write ( writer) ?;
107
+ event. write ( writer) ?;
108
+ } ,
109
+ Self :: LSPS5Client ( _) => { } ,
110
+ Self :: LSPS5Service ( event) => {
111
+ 2u8 . write ( writer) ?;
112
+ event. write ( writer) ?;
113
+ } ,
114
+ }
115
+ Ok ( ( ) )
116
+ }
117
+ }
118
+
119
+ impl MaybeReadable for LiquidityEvent {
120
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < Option < Self > , DecodeError > {
121
+ match Readable :: read ( reader) ? {
122
+ 0u8 => {
123
+ let event = Readable :: read ( reader) ?;
124
+ Ok ( Some ( LiquidityEvent :: LSPS2Service ( event) ) )
125
+ } ,
126
+ 2u8 => {
127
+ let event = Readable :: read ( reader) ?;
128
+ Ok ( Some ( LiquidityEvent :: LSPS5Service ( event) ) )
129
+ } ,
130
+ x if x % 2 == 1 => {
131
+ // If the event is of unknown type, assume it was written with `write_tlv_fields`,
132
+ // which prefixes the whole thing with a length BigSize. Because the event is
133
+ // odd-type unknown, we should treat it as `Ok(None)` even if it has some TLV
134
+ // fields that are even. Thus, we avoid using `read_tlv_fields` and simply read
135
+ // exactly the number of bytes specified, ignoring them entirely.
136
+ let tlv_len: BigSize = Readable :: read ( reader) ?;
137
+ FixedLengthReader :: new ( reader, tlv_len. 0 )
138
+ . eat_remaining ( )
139
+ . map_err ( |_| DecodeError :: ShortRead ) ?;
140
+ Ok ( None )
141
+ } ,
142
+ _ => Err ( DecodeError :: InvalidValue ) ,
143
+ }
144
+ }
145
+ }
0 commit comments