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 7f4232e
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 133 deletions.
91 changes: 67 additions & 24 deletions src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ use fedimint_core::config::ClientConfig;
use fedimint_core::Amount;
use fedimint_ln_common::lightning_invoice::Bolt11Invoice;
use tokio::sync::mpsc;
use uuid::Uuid;

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

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

#[derive(Debug, Clone)]
pub struct CoreUIMsgPacket {
pub id: Option<Uuid>,
pub msg: CoreUIMsg,
}

#[derive(Debug, Clone)]
pub enum CoreUIMsg {
Sending,
Expand All @@ -58,7 +71,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 +81,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: Uuid, 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: Uuid, 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: Uuid, 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: Uuid) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::ReceiveOnChain,
id,
})
.await;
}

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

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

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

pub async fn get_seed_words(&self) {
self.msg_send(UICoreMsg::GetSeedWords).await;
pub async fn get_seed_words(&self, id: Uuid) {
self.msg_send(UICoreMsgPacket {
msg: UICoreMsg::GetSeedWords,
id,
})
.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 7f4232e

Please sign in to comment.