Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unique message id to requests/responses #56

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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