Skip to content

Commit

Permalink
sim-lib: shutdown simulation on terminal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Sep 25, 2023
1 parent 988f908 commit e2b66c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion sim-lib/src/cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ impl LightningNode for ClnNode {
amount_msat: Some(Amount { msat: amount_msat }),
..Default::default()
})
.await?;
.await
.map_err(|s| {
// TODO: Parse CLN rpc status to determine if it's a terminal error or not.
LightningError::TerminalRpcError(format!("{:?}", s))
})?;
let KeysendResponse { payment_hash, .. } = response.into_inner();
let slice: [u8; 32] = payment_hash
.as_slice()
Expand Down
28 changes: 17 additions & 11 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ pub enum LightningError {
GetNodeInfoError(String),
#[error("Config validation failed {0}")]
ValidationError(String),
#[error("RPC error: {0:?}")]
ClnRpcError(#[from] tonic::Status),
#[error("RPC error: {0:?}")]
LndRpcError(#[from] tonic_lnd::tonic::Status),
#[error("Terminal RPC error: {0:?}")]
TerminalRpcError(String),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -418,6 +416,7 @@ impl Simulation {
node.clone(),
receiver,
output_sender.clone(),
shutdown.clone(),
));

// Add the producer channel to our map so that various activity descriptions can use it. We may have multiple
Expand Down Expand Up @@ -448,6 +447,7 @@ async fn consume_events(
node: Arc<Mutex<dyn LightningNode + Send>>,
mut receiver: Receiver<SimulationEvent>,
sender: Sender<SimulationOutput>,
shutdown: Trigger,
) {
let node_id = node.lock().await.get_info().pubkey;
log::debug!("Started consumer for {}.", node_id);
Expand Down Expand Up @@ -478,13 +478,19 @@ async fn consume_events(
SimulationOutput::SendPaymentSuccess(payment)
}
Err(e) => {
log::error!(
"Error while sending payment {} -> {}. Error {}.",
node_id,
dest,
e
);
SimulationOutput::SendPaymentFailure(payment, PaymentResult::default())
log::error!("Error while sending payment {} -> {}", node_id, dest);

match e {
LightningError::TerminalRpcError(s) => {
log::error!("Simulation terminated with error: {s}");
shutdown.trigger();
break;
}
_ => SimulationOutput::SendPaymentFailure(
payment,
PaymentResult::default(),
),
}
}
};

Expand Down
11 changes: 9 additions & 2 deletions sim-lib/src/lnd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ impl LightningNode for LndNode {
fee_limit_msat: i64::max_value(),
..Default::default()
})
.await?;
.await
.map_err(|s| {
// TODO: Parse LND rpc status to determine if it's a terminal error or not.
LightningError::TerminalRpcError(format!("{:?}", s))
})?;

let mut stream = response.into_inner();

let payment_hash = match stream.message().await? {
let payment_hash = match stream.message().await.map_err(|s| {
// TODO: Parse LND rpc status to determine if it's a terminal error or not.
LightningError::TerminalRpcError(format!("{:?}", s))
})? {
Some(payment) => string_to_payment_hash(&payment.payment_hash)?,
None => return Err(LightningError::SendPaymentError("No payment".to_string())),
};
Expand Down

0 comments on commit e2b66c8

Please sign in to comment.