Skip to content

Commit

Permalink
feat: track payment on cln nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Aug 25, 2023
1 parent f57ff45 commit 4b9dd98
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions sim-lib/src/cln.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use std::time::Duration;

use async_trait::async_trait;
use bitcoin::secp256k1::PublicKey;
use cln_grpc::pb::{
node_client::NodeClient, Amount, GetinfoRequest, GetinfoResponse, KeysendRequest,
KeysendResponse, ListnodesRequest,
listpays_pays::ListpaysPaysStatus, node_client::NodeClient, Amount, GetinfoRequest,
GetinfoResponse, KeysendRequest, KeysendResponse, ListnodesRequest, ListpaysRequest,
ListpaysResponse,
};
use lightning::ln::features::NodeFeatures;
use lightning::ln::PaymentHash;

use tokio::fs::File;
use tokio::io::{AsyncReadExt, Error};
use tokio::time;
use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity};
use triggered::Listener;

use crate::{ClnConnection, LightningError, LightningNode, NodeInfo, PaymentResult};
use crate::{
ClnConnection, LightningError, LightningNode, NodeInfo, PaymentOutcome, PaymentResult,
};

pub struct ClnNode {
pub client: NodeClient<Channel>,
Expand Down Expand Up @@ -101,10 +107,47 @@ impl LightningNode for ClnNode {

async fn track_payment(
&mut self,
_hash: PaymentHash,
_shutdown: Listener,
hash: PaymentHash,
shutdown: Listener,
) -> Result<PaymentResult, LightningError> {
unimplemented!()
loop {
tokio::select! {
biased;
_ = shutdown.clone() => {
break Err(LightningError::TrackPaymentError("Shutdown before tracking results".to_string()));
},
_ = time::sleep(Duration::from_secs(1)) => {
let ListpaysResponse { pays } = self
.client
.list_pays(ListpaysRequest {
payment_hash: Some(hash.0.to_vec()),
..Default::default()
})
.await
.map_err(|err| LightningError::TrackPaymentError(err.to_string()))?
.into_inner();

if let Some(pay) = pays.first() {
let payment_status = ListpaysPaysStatus::from_i32(pay.status)
.ok_or(LightningError::TrackPaymentError("Invalid payment status".to_string()))?;

let payment_outcome = match payment_status {
ListpaysPaysStatus::Pending => continue,
ListpaysPaysStatus::Complete => PaymentOutcome::Success,
// Task: https://github.com/bitcoin-dev-project/sim-ln/issues/26#issuecomment-1691780018
ListpaysPaysStatus::Failed => PaymentOutcome::UnexpectedError,
};
let htlc_count = pay.number_of_parts.unwrap_or(1).try_into().map_err(|_| LightningError::TrackPaymentError("Invalid number of parts".to_string()))?;
break Ok(PaymentResult {
htlc_count,
payment_outcome,
});
}
},
}

time::sleep(Duration::from_millis(100)).await;
}
}

async fn get_node_features(&mut self, node: PublicKey) -> Result<NodeFeatures, LightningError> {
Expand Down

0 comments on commit 4b9dd98

Please sign in to comment.