Skip to content

Commit 0a0d8b3

Browse files
committed
Revert "f Use individual event structs"
This reverts commit fdef833.
1 parent a4922da commit 0a0d8b3

File tree

1 file changed

+79
-155
lines changed

1 file changed

+79
-155
lines changed

src/event.rs

Lines changed: 79 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//! The events emitted by [`LdkLite`] live here.
2-
//!
3-
//! [`LdkLite`]: [`crate::LdkLite`]
4-
51
use crate::{
62
hex_utils, ChannelManager, Error, FilesystemPersister, LdkLiteChainAccess, LdkLiteConfig,
73
NetworkGraph, PaymentInfo, PaymentInfoStorage, PaymentStatus,
@@ -39,44 +35,62 @@ pub(crate) const EVENTS_PERSISTENCE_KEY: &str = "events";
3935
#[derive(Debug, Clone)]
4036
pub enum LdkLiteEvent {
4137
/// A payment we sent was successful.
42-
PaymentSuccessful(PaymentSuccessfulEvent),
38+
PaymentSuccessful {
39+
/// The hash of the payment.
40+
payment_hash: PaymentHash,
41+
},
4342
/// A payment we sent has failed.
44-
PaymentFailed(PaymentFailedEvent),
43+
PaymentFailed {
44+
/// The hash of the payment.
45+
payment_hash: PaymentHash,
46+
},
4547
/// A payment has been received.
46-
PaymentReceived(PaymentReceivedEvent),
48+
PaymentReceived {
49+
/// The hash of the payment.
50+
payment_hash: PaymentHash,
51+
/// The value, in thousandths of a satoshi that has been received.
52+
amount_msat: u64,
53+
},
4754
// TODO: Implement after a corresponding LDK event is added.
48-
//ChannelOpened(ChannelOpenedEvent),
55+
//ChannelOpened {
56+
//},
4957
/// A channel has been closed.
50-
ChannelClosed(ChannelClosedEvent),
58+
ChannelClosed {
59+
/// The channel_id of the channel which has been closed.
60+
channel_id: [u8; 32],
61+
},
5162
// TODO: Implement on-chain events when better integrating with BDK wallet sync.
52-
//OnChainPaymentSent(OnChainPaymentSentEvent),
53-
//OnChainPaymentReceived(OnChainPaymentReceivedEvent),
54-
}
55-
56-
trait EventType {
57-
const TYPE: u8;
58-
}
59-
60-
fn write_event<T: EventType + Writeable, W: Writer>(
61-
event: &T, writer: &mut W,
62-
) -> Result<(), lightning::io::Error> {
63-
T::TYPE.write(writer)?;
64-
event.write(writer)?;
65-
Ok(())
63+
//OnChainPaymentSent {
64+
//},
65+
//OnChainPaymentReceived {
66+
//}
6667
}
6768

6869
impl Readable for LdkLiteEvent {
6970
fn read<R: lightning::io::Read>(
7071
reader: &mut R,
7172
) -> Result<Self, lightning::ln::msgs::DecodeError> {
7273
match Readable::read(reader)? {
73-
PaymentSuccessfulEvent::TYPE => Ok(Self::PaymentSuccessful(Readable::read(reader)?)),
74-
PaymentFailedEvent::TYPE => Ok(Self::PaymentFailed(Readable::read(reader)?)),
75-
PaymentReceivedEvent::TYPE => Ok(Self::PaymentReceived(Readable::read(reader)?)),
76-
// ChannelOpenedEvent::TYPE => {
74+
0u8 => {
75+
let payment_hash: PaymentHash = Readable::read(reader)?;
76+
Ok(Self::PaymentSuccessful { payment_hash })
77+
}
78+
1u8 => {
79+
let payment_hash: PaymentHash = Readable::read(reader)?;
80+
Ok(Self::PaymentFailed { payment_hash })
81+
}
82+
2u8 => {
83+
let payment_hash: PaymentHash = Readable::read(reader)?;
84+
let amount_msat: u64 = Readable::read(reader)?;
85+
Ok(Self::PaymentReceived { payment_hash, amount_msat })
86+
}
87+
//3u8 => {
7788
// TODO ChannelOpened
7889
//}
79-
ChannelClosedEvent::TYPE => Ok(Self::ChannelClosed(Readable::read(reader)?)),
90+
4u8 => {
91+
let channel_id: [u8; 32] = Readable::read(reader)?;
92+
Ok(Self::ChannelClosed { channel_id })
93+
}
8094
//5u8 => {
8195
// TODO OnChainPaymentSent
8296
//}
@@ -91,126 +105,36 @@ impl Readable for LdkLiteEvent {
91105
impl Writeable for LdkLiteEvent {
92106
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
93107
match self {
94-
Self::PaymentSuccessful(event) => write_event(event, writer)?,
95-
Self::PaymentFailed(event) => write_event(event, writer)?,
96-
Self::PaymentReceived(event) => write_event(event, writer)?,
97-
Self::ChannelClosed(event) => write_event(event, writer)?,
108+
Self::PaymentSuccessful { payment_hash } => {
109+
0u8.write(writer)?;
110+
payment_hash.write(writer)?;
111+
Ok(())
112+
}
113+
Self::PaymentFailed { payment_hash } => {
114+
1u8.write(writer)?;
115+
payment_hash.write(writer)?;
116+
Ok(())
117+
}
118+
Self::PaymentReceived { payment_hash, amount_msat } => {
119+
2u8.write(writer)?;
120+
payment_hash.write(writer)?;
121+
amount_msat.write(writer)?;
122+
Ok(())
123+
}
124+
//Self::ChannelOpened { .. } => {
125+
//TODO
126+
//}
127+
Self::ChannelClosed { channel_id } => {
128+
4u8.write(writer)?;
129+
channel_id.write(writer)?;
130+
Ok(())
131+
} //Self::OnChainPaymentSent { .. } => {
132+
//TODO
133+
//}
134+
//Self::OnChainPaymentReceived { .. } => {
135+
//TODO
136+
//}
98137
}
99-
Ok(())
100-
}
101-
}
102-
103-
/// A payment we sent was successful.
104-
#[derive(Debug, Clone)]
105-
pub struct PaymentSuccessfulEvent {
106-
/// The hash of the payment.
107-
pub payment_hash: PaymentHash,
108-
}
109-
110-
impl EventType for PaymentSuccessfulEvent {
111-
const TYPE: u8 = 0u8;
112-
}
113-
114-
impl Readable for PaymentSuccessfulEvent {
115-
fn read<R: lightning::io::Read>(
116-
reader: &mut R,
117-
) -> Result<Self, lightning::ln::msgs::DecodeError> {
118-
let payment_hash: PaymentHash = Readable::read(reader)?;
119-
Ok(Self { payment_hash })
120-
}
121-
}
122-
123-
impl Writeable for PaymentSuccessfulEvent {
124-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
125-
self.payment_hash.write(writer)?;
126-
Ok(())
127-
}
128-
}
129-
130-
/// A payment we sent has failed.
131-
#[derive(Debug, Clone)]
132-
pub struct PaymentFailedEvent {
133-
/// The hash of the payment.
134-
pub payment_hash: PaymentHash,
135-
}
136-
137-
impl EventType for PaymentFailedEvent {
138-
const TYPE: u8 = 1u8;
139-
}
140-
141-
impl Readable for PaymentFailedEvent {
142-
fn read<R: lightning::io::Read>(
143-
reader: &mut R,
144-
) -> Result<Self, lightning::ln::msgs::DecodeError> {
145-
let payment_hash: PaymentHash = Readable::read(reader)?;
146-
Ok(Self { payment_hash })
147-
}
148-
}
149-
150-
impl Writeable for PaymentFailedEvent {
151-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
152-
self.payment_hash.write(writer)?;
153-
Ok(())
154-
}
155-
}
156-
157-
/// A payment has been received.
158-
#[derive(Debug, Clone)]
159-
pub struct PaymentReceivedEvent {
160-
/// The hash of the payment.
161-
pub payment_hash: PaymentHash,
162-
/// The value, in thousandths of a satoshi that has been received.
163-
pub amount_msat: u64,
164-
}
165-
166-
impl EventType for PaymentReceivedEvent {
167-
const TYPE: u8 = 2u8;
168-
}
169-
170-
impl Readable for PaymentReceivedEvent {
171-
fn read<R: lightning::io::Read>(
172-
reader: &mut R,
173-
) -> Result<Self, lightning::ln::msgs::DecodeError> {
174-
let payment_hash: PaymentHash = Readable::read(reader)?;
175-
let amount_msat: u64 = Readable::read(reader)?;
176-
Ok(Self { payment_hash, amount_msat })
177-
}
178-
}
179-
180-
impl Writeable for PaymentReceivedEvent {
181-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
182-
Self::TYPE.write(writer)?;
183-
self.payment_hash.write(writer)?;
184-
self.amount_msat.write(writer)?;
185-
Ok(())
186-
}
187-
}
188-
189-
/// A channel has been closed.
190-
#[derive(Debug, Clone)]
191-
pub struct ChannelClosedEvent {
192-
/// The channel_id of the channel which has been closed.
193-
pub channel_id: [u8; 32],
194-
}
195-
196-
impl EventType for ChannelClosedEvent {
197-
const TYPE: u8 = 4u8;
198-
}
199-
200-
impl Readable for ChannelClosedEvent {
201-
fn read<R: lightning::io::Read>(
202-
reader: &mut R,
203-
) -> Result<Self, lightning::ln::msgs::DecodeError> {
204-
let channel_id: [u8; 32] = Readable::read(reader)?;
205-
Ok(Self { channel_id })
206-
}
207-
}
208-
209-
impl Writeable for ChannelClosedEvent {
210-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
211-
Self::TYPE.write(writer)?;
212-
self.channel_id.write(writer)?;
213-
Ok(())
214138
}
215139
}
216140

@@ -378,10 +302,10 @@ impl LdkEventHandler for LdkLiteEventHandler {
378302
};
379303
self.channel_manager.claim_funds(payment_preimage.unwrap());
380304
self.event_queue
381-
.add_event(LdkLiteEvent::PaymentReceived(PaymentReceivedEvent {
305+
.add_event(LdkLiteEvent::PaymentReceived {
382306
payment_hash: *payment_hash,
383307
amount_msat: *amount_msat,
384-
}))
308+
})
385309
.unwrap();
386310
}
387311
LdkEvent::PaymentClaimed { payment_hash, purpose, amount_msat } => {
@@ -446,9 +370,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
446370
}
447371
}
448372
self.event_queue
449-
.add_event(LdkLiteEvent::PaymentSuccessful(PaymentSuccessfulEvent {
373+
.add_event(LdkLiteEvent::PaymentSuccessful {
450374
payment_hash: *payment_hash,
451-
}))
375+
})
452376
.unwrap();
453377
}
454378
LdkEvent::PaymentFailed { payment_hash, .. } => {
@@ -464,9 +388,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
464388
payment.status = PaymentStatus::Failed;
465389
}
466390
self.event_queue
467-
.add_event(LdkLiteEvent::PaymentFailed(PaymentFailedEvent {
391+
.add_event(LdkLiteEvent::PaymentFailed {
468392
payment_hash: *payment_hash,
469-
}))
393+
})
470394
.unwrap();
471395
}
472396

@@ -578,9 +502,9 @@ impl LdkEventHandler for LdkLiteEventHandler {
578502
reason
579503
);
580504
self.event_queue
581-
.add_event(LdkLiteEvent::ChannelClosed(ChannelClosedEvent {
505+
.add_event(LdkLiteEvent::ChannelClosed {
582506
channel_id: *channel_id,
583-
}))
507+
})
584508
.unwrap();
585509
}
586510
LdkEvent::DiscardFunding { .. } => {}

0 commit comments

Comments
 (0)