Skip to content

Commit

Permalink
fix: Updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshit933 committed Aug 13, 2024
1 parent cabe25a commit b92d1a5
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 230 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lampo-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ chrono = { version = "0.4", features = ["std", "clock"], default-features = fals
serde_json = "1.0"
serde = "1.0"
hex = "0.4.3"
lightning-liquidity = "0.1.0-alpha.4"
13 changes: 13 additions & 0 deletions lampo-common/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct LampoConf {
pub announce_addr: Option<String>,
// Should be something like liquidity=consumer, liquidity=provider or none
pub liquidity: Option<String>,
pub lsp_node_id: Option<String>,
pub lsp_socket_addr: Option<String>,
}

impl Default for LampoConf {
Expand Down Expand Up @@ -54,6 +56,8 @@ impl Default for LampoConf {
alias: None,
announce_addr: None,
liquidity: None,
lsp_node_id: None,
lsp_socket_addr: None,
}
}
}
Expand Down Expand Up @@ -123,6 +127,11 @@ impl LampoConf {
let input_path = path;
let path = Self::normalize_root_dir(&conf.root_path, conf.network);
conf.root_path = path.clone();
// Must be used when we act as a liquidity provider
conf.ldk_conf = UserConfig {
accept_intercept_htlcs: true,
..Default::default()
};

let lampo_file = format!("{}/lampo.conf", conf.path());

Expand Down Expand Up @@ -234,6 +243,8 @@ impl TryFrom<String> for LampoConf {
let alias = conf.get_conf("alias").unwrap_or(None);
let announce_addr = conf.get_conf("announce-addr").unwrap_or(None);
let liquidity = conf.get_conf("liquidity").unwrap_or(None);
let lsp_node_id = conf.get_conf("lsp-node-id").unwrap_or(None);
let lsp_socket_addr = conf.get_conf("lsp-socket-addr").unwrap_or(None);

Ok(Self {
inner: Some(conf),
Expand All @@ -252,6 +263,8 @@ impl TryFrom<String> for LampoConf {
alias,
announce_addr,
liquidity,
lsp_node_id,
lsp_socket_addr,
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion lampo-common/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Events commands
pub mod liquidity;
pub mod ln;
pub mod onchain;

Expand All @@ -7,6 +8,7 @@ use std::sync::{Arc, Mutex};
use crate::chan;
use crate::event::ln::LightningEvent;
use crate::event::onchain::OnChainEvent;
use liquidity::LiquidityEvent;

/// Publishes events to subscribers.
#[derive(Clone)]
Expand Down Expand Up @@ -64,6 +66,6 @@ impl<T: Clone> Subscriber<T> {
pub enum Event {
Lightning(LightningEvent),
OnChain(OnChainEvent),
Liquidity(String),
Liquidity(LiquidityEvent),
Inventory,
}
56 changes: 56 additions & 0 deletions lampo-common/src/event/liquidity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use bitcoin::secp256k1::PublicKey;
use lightning::events::HTLCDestination;
use lightning::ln::{channelmanager::InterceptId, ChannelId, PaymentHash};
use lightning_liquidity::{lsps0::ser::RequestId, lsps2::msgs::OpeningFeeParams};

#[derive(Debug, Clone)]
pub enum LiquidityEvent {
OpenParamsReady {
counterparty_node_id: PublicKey,
opening_fee_params_menu: Vec<OpeningFeeParams>,
},
InvoiceparamsReady {
counterparty_node_id: PublicKey,
intercept_scid: u64,
cltv_expiry_delta: u32,
},
BuyRequest {
request_id: RequestId,
counterparty_node_id: PublicKey,
opening_fee_params: OpeningFeeParams,
payment_size_msat: Option<u64>,
},
Geinfo {
request_id: RequestId,
counterparty_node_id: PublicKey,
token: Option<String>,
},
OpenChannel {
their_network_key: PublicKey,
amt_to_forward_msat: u64,
opening_fee_msat: u64,
user_channel_id: u128,
intercept_scid: u64,
},
HTLCHandlingFailed {
prev_channel_id: ChannelId,
failed_next_destination: HTLCDestination,
},
HTLCIntercepted {
intercept_id: InterceptId,
requested_next_hop_scid: u64,
payment_hash: PaymentHash,
inbound_amount_msat: u64,
expected_outbound_amount_msat: u64,
},
PaymentForwarded {
prev_channel_id: Option<ChannelId>,
next_channel_id: Option<ChannelId>,
prev_user_channel_id: Option<u128>,
next_user_channel_id: Option<u128>,
total_fee_earned_msat: Option<u64>,
skimmed_fee_msat: Option<u64>,
claim_from_onchain_tx: bool,
outbound_amount_forwarded_msat: Option<u64>,
},
}
20 changes: 15 additions & 5 deletions lampo-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ macro_rules! wait {
pub struct LampoTesting {
inner: Arc<LampoHandler>,
root_path: Arc<TempDir>,
liquidity: Option<RefCell<LampoLiquidityManager>>,
liquidity: Option<Arc<LampoLiquidityManager>>,
pub port: u64,
pub wallet: Arc<dyn WalletManager>,
pub mnemonic: String,
pub btc: Arc<BtcNode>,
pub info: response::GetInfo,
pub lampod: Option<Arc<LampoDaemon>>,
}

impl LampoTesting {
Expand Down Expand Up @@ -147,12 +148,18 @@ impl LampoTesting {
root_path: dir.into(),
info,
liquidity: None,
lampod: None,
})
}

// Use this for liquidity.
// FIXME: Integrate this with new method
pub fn new_liquidity(btc: Arc<BtcNode>, liquidity: String) -> error::Result<Self> {
pub fn new_liquidity(
btc: Arc<BtcNode>,
liquidity: String,
lsp_node_id: Option<String>,
lsp_socket_addr: Option<String>,
) -> error::Result<Self> {
let dir = tempfile::tempdir()?;

// SAFETY: this should be safe because if the system has no
Expand All @@ -168,6 +175,8 @@ impl LampoTesting {
let core_url = format!("127.0.0.1:{}", btc.port);
if liquidity == "consumer" {
lampo_conf.configure_as_liquidity_consumer();
lampo_conf.lsp_node_id = lsp_node_id;
lampo_conf.lsp_socket_addr = lsp_socket_addr;
} else {
lampo_conf.configure_as_liquidity_provider();
}
Expand Down Expand Up @@ -231,7 +240,8 @@ impl LampoTesting {
btc,
root_path: Arc::new(dir),
info,
liquidity,
liquidity: Some(liquidity),
lampod: Some(lampod),
})
}

Expand Down Expand Up @@ -267,7 +277,7 @@ impl LampoTesting {
self.root_path.clone()
}

pub fn liquidity(&self) -> Option<RefCell<LampoLiquidityManager>> {
self.liquidity.clone()
pub fn liquidity(&self) -> Arc<LampoLiquidityManager> {
self.liquidity.as_ref().unwrap().clone()
}
}
27 changes: 17 additions & 10 deletions lampod/src/actions/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use lampo_common::chan;
use lampo_common::error;
use lampo_common::error::Ok;
use lampo_common::event::liquidity::LiquidityEvent;
use lampo_common::event::ln::LightningEvent;
use lampo_common::event::{Emitter, Event, Subscriber};
use lampo_common::handler::Handler as EventHandler;
Expand Down Expand Up @@ -33,9 +34,7 @@ pub struct LampoHandler {
wallet_manager: Arc<dyn WalletManager>,
chain_manager: Arc<LampoChainManager>,
external_handlers: RefCell<Vec<Arc<dyn ExternalHandler>>>,
// Question: How about Option<Arc<Mutex<LampoLiquidityManager>>>
liquidity_manager: Option<RefCell<LampoLiquidityManager>>,
// Do this inside liquidity
liquidity_manager: Option<Arc<LampoLiquidityManager>>,
emitter: Emitter<Event>,
subscriber: Subscriber<Event>,
}
Expand Down Expand Up @@ -146,7 +145,7 @@ impl Handler for LampoHandler {
} => {
let liquidity_manager = self.liquidity_manager.clone();
if let Some(ref liq_manager) = liquidity_manager {
let _ = liquidity_manager.unwrap().borrow().channel_ready(user_channel_id, &channel_id, &counterparty_node_id);
let _ = liquidity_manager.unwrap().channel_ready(user_channel_id, &channel_id, &counterparty_node_id);
}
log::info!("channel ready with node `{counterparty_node_id}`, and channel type {channel_type}");
self.emit(Event::Lightning(LightningEvent::ChannelReady {
Expand Down Expand Up @@ -294,21 +293,29 @@ impl Handler for LampoHandler {
Ok(())
},
ldk::events::Event::HTLCIntercepted { intercept_id, requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat } => {
// TODO: Emit this event
log::info!("Intecepted an HTLC");
let liquidity_manager = self.liquidity_manager.clone();
if liquidity_manager.is_some() {
liquidity_manager.unwrap().borrow().htlc_intercepted(requested_next_hop_scid, intercept_id, expected_outbound_amount_msat, payment_hash)?;
if let Some(ref liq_manager) = liquidity_manager {
let _ = liquidity_manager.unwrap().htlc_intercepted(requested_next_hop_scid, intercept_id, inbound_amount_msat, payment_hash);
}

let event = LiquidityEvent::HTLCIntercepted { intercept_id, requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat };
self.emit(Event::Liquidity(event));
log::info!("Successfully emitted the Intercepted event!");
Ok(())
},
ldk::events::Event::HTLCHandlingFailed { prev_channel_id, failed_next_destination } => {
todo!()
log::info!("HTLC Handling failed");
let event = LiquidityEvent::HTLCHandlingFailed { prev_channel_id, failed_next_destination };
self.emit(Event::Liquidity(event));
Ok(())
}
ldk::events::Event::PaymentForwarded { prev_channel_id, next_channel_id, prev_user_channel_id, next_user_channel_id, total_fee_earned_msat, skimmed_fee_msat, claim_from_onchain_tx, outbound_amount_forwarded_msat } => {
todo!()
log::info!("Payment Forwarded");
let event = LiquidityEvent::PaymentForwarded { prev_channel_id, next_channel_id, prev_user_channel_id, next_user_channel_id, total_fee_earned_msat, skimmed_fee_msat, claim_from_onchain_tx, outbound_amount_forwarded_msat };
self.emit(Event::Liquidity(event));
Ok(())
}

_ => Err(error::anyhow!("unexpected ldk event: {:?}", event)),
}
}
Expand Down
Loading

0 comments on commit b92d1a5

Please sign in to comment.