Skip to content

Commit cffdf7e

Browse files
authored
Merge pull request #374 from G8XSU/replay-events
Replay events on event handling failures due to persistence failures.
2 parents 26e61e8 + 5594560 commit cffdf7e

File tree

1 file changed

+133
-101
lines changed

1 file changed

+133
-101
lines changed

src/event.rs

+133-101
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,13 @@ where
498498
status: Some(PaymentStatus::Failed),
499499
..PaymentDetailsUpdate::new(payment_id)
500500
};
501-
self.payment_store.update(&update).unwrap_or_else(|e| {
502-
log_error!(self.logger, "Failed to access payment store: {}", e);
503-
panic!("Failed to access payment store");
504-
});
505-
return Ok(());
501+
match self.payment_store.update(&update) {
502+
Ok(_) => return Ok(()),
503+
Err(e) => {
504+
log_error!(self.logger, "Failed to access payment store: {}", e);
505+
return Err(ReplayEvent());
506+
},
507+
};
506508
}
507509

508510
if info.status == PaymentStatus::Succeeded
@@ -520,11 +522,13 @@ where
520522
status: Some(PaymentStatus::Failed),
521523
..PaymentDetailsUpdate::new(payment_id)
522524
};
523-
self.payment_store.update(&update).unwrap_or_else(|e| {
524-
log_error!(self.logger, "Failed to access payment store: {}", e);
525-
panic!("Failed to access payment store");
526-
});
527-
return Ok(());
525+
match self.payment_store.update(&update) {
526+
Ok(_) => return Ok(()),
527+
Err(e) => {
528+
log_error!(self.logger, "Failed to access payment store: {}", e);
529+
return Err(ReplayEvent());
530+
},
531+
};
528532
}
529533

530534
let max_total_opening_fee_msat = match info.kind {
@@ -559,11 +563,13 @@ where
559563
status: Some(PaymentStatus::Failed),
560564
..PaymentDetailsUpdate::new(payment_id)
561565
};
562-
self.payment_store.update(&update).unwrap_or_else(|e| {
563-
log_error!(self.logger, "Failed to access payment store: {}", e);
564-
panic!("Failed to access payment store");
565-
});
566-
return Ok(());
566+
match self.payment_store.update(&update) {
567+
Ok(_) => return Ok(()),
568+
Err(e) => {
569+
log_error!(self.logger, "Failed to access payment store: {}", e);
570+
return Err(ReplayEvent());
571+
},
572+
};
567573
}
568574

569575
// If this is known by the store but ChannelManager doesn't know the preimage,
@@ -577,22 +583,23 @@ where
577583
"We would have registered the preimage if we knew"
578584
);
579585

580-
self.event_queue
581-
.add_event(Event::PaymentClaimable {
582-
payment_id,
583-
payment_hash,
584-
claimable_amount_msat: amount_msat,
585-
claim_deadline,
586-
})
587-
.unwrap_or_else(|e| {
586+
let event = Event::PaymentClaimable {
587+
payment_id,
588+
payment_hash,
589+
claimable_amount_msat: amount_msat,
590+
claim_deadline,
591+
};
592+
match self.event_queue.add_event(event) {
593+
Ok(_) => return Ok(()),
594+
Err(e) => {
588595
log_error!(
589596
self.logger,
590597
"Failed to push to event queue: {}",
591598
e
592599
);
593-
panic!("Failed to push to event queue");
594-
});
595-
return Ok(());
600+
return Err(ReplayEvent());
601+
},
602+
};
596603
}
597604
},
598605
_ => {},
@@ -715,10 +722,13 @@ where
715722
status: Some(PaymentStatus::Failed),
716723
..PaymentDetailsUpdate::new(payment_id)
717724
};
718-
self.payment_store.update(&update).unwrap_or_else(|e| {
719-
log_error!(self.logger, "Failed to access payment store: {}", e);
720-
panic!("Failed to access payment store");
721-
});
725+
match self.payment_store.update(&update) {
726+
Ok(_) => return Ok(()),
727+
Err(e) => {
728+
log_error!(self.logger, "Failed to access payment store: {}", e);
729+
return Err(ReplayEvent());
730+
},
731+
};
722732
}
723733
},
724734
LdkEvent::PaymentClaimed {
@@ -796,20 +806,22 @@ where
796806
payment_id,
797807
e
798808
);
799-
panic!("Failed to access payment store");
809+
return Err(ReplayEvent());
800810
},
801811
}
802812

803-
self.event_queue
804-
.add_event(Event::PaymentReceived {
805-
payment_id: Some(payment_id),
806-
payment_hash,
807-
amount_msat,
808-
})
809-
.unwrap_or_else(|e| {
813+
let event = Event::PaymentReceived {
814+
payment_id: Some(payment_id),
815+
payment_hash,
816+
amount_msat,
817+
};
818+
match self.event_queue.add_event(event) {
819+
Ok(_) => return Ok(()),
820+
Err(e) => {
810821
log_error!(self.logger, "Failed to push to event queue: {}", e);
811-
panic!("Failed to push to event queue");
812-
});
822+
return Err(ReplayEvent());
823+
},
824+
};
813825
},
814826
LdkEvent::PaymentSent {
815827
payment_id,
@@ -832,10 +844,13 @@ where
832844
..PaymentDetailsUpdate::new(payment_id)
833845
};
834846

835-
self.payment_store.update(&update).unwrap_or_else(|e| {
836-
log_error!(self.logger, "Failed to access payment store: {}", e);
837-
panic!("Failed to access payment store");
838-
});
847+
match self.payment_store.update(&update) {
848+
Ok(_) => {},
849+
Err(e) => {
850+
log_error!(self.logger, "Failed to access payment store: {}", e);
851+
return Err(ReplayEvent());
852+
},
853+
};
839854

840855
self.payment_store.get(&payment_id).map(|payment| {
841856
log_info!(
@@ -852,17 +867,19 @@ where
852867
hex_utils::to_string(&payment_preimage.0)
853868
);
854869
});
870+
let event = Event::PaymentSuccessful {
871+
payment_id: Some(payment_id),
872+
payment_hash,
873+
fee_paid_msat,
874+
};
855875

856-
self.event_queue
857-
.add_event(Event::PaymentSuccessful {
858-
payment_id: Some(payment_id),
859-
payment_hash,
860-
fee_paid_msat,
861-
})
862-
.unwrap_or_else(|e| {
876+
match self.event_queue.add_event(event) {
877+
Ok(_) => return Ok(()),
878+
Err(e) => {
863879
log_error!(self.logger, "Failed to push to event queue: {}", e);
864-
panic!("Failed to push to event queue");
865-
});
880+
return Err(ReplayEvent());
881+
},
882+
};
866883
},
867884
LdkEvent::PaymentFailed { payment_id, payment_hash, reason, .. } => {
868885
log_info!(
@@ -877,20 +894,23 @@ where
877894
status: Some(PaymentStatus::Failed),
878895
..PaymentDetailsUpdate::new(payment_id)
879896
};
880-
self.payment_store.update(&update).unwrap_or_else(|e| {
881-
log_error!(self.logger, "Failed to access payment store: {}", e);
882-
panic!("Failed to access payment store");
883-
});
884-
self.event_queue
885-
.add_event(Event::PaymentFailed {
886-
payment_id: Some(payment_id),
887-
payment_hash,
888-
reason,
889-
})
890-
.unwrap_or_else(|e| {
897+
match self.payment_store.update(&update) {
898+
Ok(_) => {},
899+
Err(e) => {
900+
log_error!(self.logger, "Failed to access payment store: {}", e);
901+
return Err(ReplayEvent());
902+
},
903+
};
904+
905+
let event =
906+
Event::PaymentFailed { payment_id: Some(payment_id), payment_hash, reason };
907+
match self.event_queue.add_event(event) {
908+
Ok(_) => return Ok(()),
909+
Err(e) => {
891910
log_error!(self.logger, "Failed to push to event queue: {}", e);
892-
panic!("Failed to push to event queue");
893-
});
911+
return Err(ReplayEvent());
912+
},
913+
};
894914
},
895915

896916
LdkEvent::PaymentPathSuccessful { .. } => {},
@@ -915,12 +935,13 @@ where
915935
}
916936
},
917937
LdkEvent::SpendableOutputs { outputs, channel_id } => {
918-
self.output_sweeper
919-
.track_spendable_outputs(outputs, channel_id, true, None)
920-
.unwrap_or_else(|_| {
938+
match self.output_sweeper.track_spendable_outputs(outputs, channel_id, true, None) {
939+
Ok(_) => return Ok(()),
940+
Err(_) => {
921941
log_error!(self.logger, "Failed to track spendable outputs");
922-
panic!("Failed to track spendable outputs");
923-
});
942+
return Err(ReplayEvent());
943+
},
944+
};
924945
},
925946
LdkEvent::OpenChannelRequest {
926947
temporary_channel_id,
@@ -1111,18 +1132,22 @@ where
11111132
channel_id,
11121133
counterparty_node_id,
11131134
);
1114-
self.event_queue
1115-
.add_event(Event::ChannelPending {
1116-
channel_id,
1117-
user_channel_id: UserChannelId(user_channel_id),
1118-
former_temporary_channel_id: former_temporary_channel_id.unwrap(),
1119-
counterparty_node_id,
1120-
funding_txo,
1121-
})
1122-
.unwrap_or_else(|e| {
1135+
1136+
let event = Event::ChannelPending {
1137+
channel_id,
1138+
user_channel_id: UserChannelId(user_channel_id),
1139+
former_temporary_channel_id: former_temporary_channel_id.unwrap(),
1140+
counterparty_node_id,
1141+
funding_txo,
1142+
};
1143+
match self.event_queue.add_event(event) {
1144+
Ok(_) => {},
1145+
Err(e) => {
11231146
log_error!(self.logger, "Failed to push to event queue: {}", e);
1124-
panic!("Failed to push to event queue");
1125-
});
1147+
return Err(ReplayEvent());
1148+
},
1149+
};
1150+
11261151
let network_graph = self.network_graph.read_only();
11271152
let channels =
11281153
self.channel_manager.list_channels_with_counterparty(&counterparty_node_id);
@@ -1164,16 +1189,19 @@ where
11641189
channel_id,
11651190
counterparty_node_id,
11661191
);
1167-
self.event_queue
1168-
.add_event(Event::ChannelReady {
1169-
channel_id,
1170-
user_channel_id: UserChannelId(user_channel_id),
1171-
counterparty_node_id: Some(counterparty_node_id),
1172-
})
1173-
.unwrap_or_else(|e| {
1192+
1193+
let event = Event::ChannelReady {
1194+
channel_id,
1195+
user_channel_id: UserChannelId(user_channel_id),
1196+
counterparty_node_id: Some(counterparty_node_id),
1197+
};
1198+
match self.event_queue.add_event(event) {
1199+
Ok(_) => {},
1200+
Err(e) => {
11741201
log_error!(self.logger, "Failed to push to event queue: {}", e);
1175-
panic!("Failed to push to event queue");
1176-
});
1202+
return Err(ReplayEvent());
1203+
},
1204+
};
11771205
},
11781206
LdkEvent::ChannelClosed {
11791207
channel_id,
@@ -1183,17 +1211,21 @@ where
11831211
..
11841212
} => {
11851213
log_info!(self.logger, "Channel {} closed due to: {}", channel_id, reason);
1186-
self.event_queue
1187-
.add_event(Event::ChannelClosed {
1188-
channel_id,
1189-
user_channel_id: UserChannelId(user_channel_id),
1190-
counterparty_node_id,
1191-
reason: Some(reason),
1192-
})
1193-
.unwrap_or_else(|e| {
1214+
1215+
let event = Event::ChannelClosed {
1216+
channel_id,
1217+
user_channel_id: UserChannelId(user_channel_id),
1218+
counterparty_node_id,
1219+
reason: Some(reason),
1220+
};
1221+
1222+
match self.event_queue.add_event(event) {
1223+
Ok(_) => {},
1224+
Err(e) => {
11941225
log_error!(self.logger, "Failed to push to event queue: {}", e);
1195-
panic!("Failed to push to event queue");
1196-
});
1226+
return Err(ReplayEvent());
1227+
},
1228+
};
11971229
},
11981230
LdkEvent::DiscardFunding { .. } => {},
11991231
LdkEvent::HTLCIntercepted { .. } => {},

0 commit comments

Comments
 (0)