Skip to content

Commit

Permalink
feat: tack payment on cln nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Aug 24, 2023
1 parent df54576 commit 0c9b464
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 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,44 @@ 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()));
},
response = self
.client
.list_pays(ListpaysRequest {
payment_hash: Some(hash.0.to_vec()),
..Default::default()
}) => {
let ListpaysResponse { pays } = response.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,
};

break Ok(PaymentResult {
// Task: https://github.com/bitcoin-dev-project/sim-ln/issues/26#issuecomment-1691780018
htlc_count: 1,
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 0c9b464

Please sign in to comment.