Skip to content

Commit

Permalink
Add unique message id to requests/responses
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed May 17, 2024
1 parent ee60537 commit 34d452d
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 112 deletions.
82 changes: 62 additions & 20 deletions src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use fedimint_core::Amount;
use fedimint_ln_common::lightning_invoice::Bolt11Invoice;
use tokio::sync::mpsc;

#[derive(Debug, Clone)]
pub struct UICoreMsgPacket {
pub msg: UICoreMsg,
pub id: String,
}

#[derive(Debug, Clone)]
pub enum UICoreMsg {
SendLightning(Bolt11Invoice),
Expand Down Expand Up @@ -33,6 +39,12 @@ pub enum ReceiveSuccessMsg {
Onchain { txid: Txid },
}

#[derive(Debug, Clone)]
pub struct CoreUIMsgPacket {
pub id: String,
pub msg: CoreUIMsg,
}

#[derive(Debug, Clone)]
pub enum CoreUIMsg {
Sending,
Expand All @@ -58,7 +70,7 @@ pub enum CoreUIMsg {

#[derive(Debug)]
pub struct UIHandle {
ui_to_core_tx: mpsc::Sender<UICoreMsg>,
ui_to_core_tx: mpsc::Sender<UICoreMsgPacket>,
}

#[derive(Debug, Clone)]
Expand All @@ -68,61 +80,91 @@ pub enum BridgeError {
}

impl UIHandle {
pub async fn msg_send(&self, msg: UICoreMsg) {
pub async fn msg_send(&self, msg: UICoreMsgPacket) {
self.ui_to_core_tx.send(msg).await.unwrap();
}

pub async fn send_lightning(&self, invoice: Bolt11Invoice) {
self.msg_send(UICoreMsg::SendLightning(invoice)).await;
pub async fn send_lightning(&self, id: String, invoice: Bolt11Invoice) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::SendLightning(invoice),
id,
})
.await;
}

pub async fn send_onchain(&self, address: Address, amount_sats: Option<u64>) {
self.msg_send(UICoreMsg::SendOnChain {
address,
amount_sats,
pub async fn send_onchain(&self, id: String, address: Address, amount_sats: Option<u64>) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::SendOnChain {
address,
amount_sats,
},
id,
})
.await;
}

pub async fn receive(&self, amount: u64) {
self.msg_send(UICoreMsg::ReceiveLightning(Amount::from_sats(amount)))
.await;
pub async fn receive(&self, id: String, amount: u64) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::ReceiveLightning(Amount::from_sats(amount)),
id,
})
.await;
}

pub async fn receive_onchain(&self) {
self.msg_send(UICoreMsg::ReceiveOnChain).await;
pub async fn receive_onchain(&self, id: String) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::ReceiveOnChain,
id,
})
.await;
}

pub async fn unlock(&self, password: String) {
self.msg_send(UICoreMsg::Unlock(password)).await;
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::Unlock(password),
id: "".to_string(),
})
.await;
}

pub async fn add_federation(&self, invite: InviteCode) {
self.msg_send(UICoreMsg::AddFederation(invite)).await;
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::AddFederation(invite),
id: "".to_string(),
})
.await;
}

pub async fn peek_federation(&self, invite: InviteCode) {
self.msg_send(UICoreMsg::GetFederationInfo(invite)).await;
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::GetFederationInfo(invite),
id: "".to_string(),
})
.await;
}

pub async fn get_seed_words(&self) {
self.msg_send(UICoreMsg::GetSeedWords).await;
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::GetSeedWords,
id: "".to_string(),
})
.await;
}
}

impl CoreHandle {
pub async fn recv(&mut self) -> Option<UICoreMsg> {
pub async fn recv(&mut self) -> Option<UICoreMsgPacket> {
self.core_from_ui_rx.recv().await
}
}

#[derive(Debug)]
pub struct CoreHandle {
core_from_ui_rx: mpsc::Receiver<UICoreMsg>,
core_from_ui_rx: mpsc::Receiver<UICoreMsgPacket>,
}

pub fn create_handles() -> (UIHandle, CoreHandle) {
let (ui_to_core_tx, core_from_ui_rx) = mpsc::channel::<UICoreMsg>(50);
let (ui_to_core_tx, core_from_ui_rx) = mpsc::channel::<UICoreMsgPacket>(50);

let ui_handle = UIHandle { ui_to_core_tx };

Expand Down
Loading

0 comments on commit 34d452d

Please sign in to comment.