Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Fix labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Jan 29, 2024
1 parent cbefe8b commit e77a11d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
4 changes: 1 addition & 3 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
}

// Fallback to node_manager invoice creation if no federation invoice created
let inv = self.node_manager.create_invoice(amount).await?;
self.storage
.set_invoice_labels(inv.bolt11.clone().expect("just created"), labels)?;
let inv = self.node_manager.create_invoice(amount, labels).await?;

Ok(inv)
}
Expand Down
44 changes: 39 additions & 5 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ impl<S: MutinyStorage> Node<S> {
&self,
amount_sat: Option<u64>,
route_hints: Option<Vec<PhantomRouteHints>>,
labels: Vec<String>,
) -> Result<Bolt11Invoice, MutinyError> {
match self.lsp_client.as_ref() {
Some(lsp) => {
Expand Down Expand Up @@ -1044,6 +1045,7 @@ impl<S: MutinyStorage> Node<S> {
Some(amount_minus_fee),
Some(lsp_fee.fee_amount_msat),
route_hints,
labels,
)
.await?;

Expand Down Expand Up @@ -1080,7 +1082,12 @@ impl<S: MutinyStorage> Node<S> {
AnyLsp::Lsps(client) => {
if has_inbound_capacity {
Ok(self
.create_internal_invoice(Some(amount_sat), None, route_hints)
.create_internal_invoice(
Some(amount_sat),
None,
route_hints,
labels,
)
.await?)
} else {
let lsp_invoice = match client
Expand All @@ -1096,6 +1103,7 @@ impl<S: MutinyStorage> Node<S> {
invoice.clone(),
Some(amount_sat * 1_000),
Some(lsp_fee.fee_amount_msat),
labels,
)
.await?;

Expand All @@ -1112,7 +1120,7 @@ impl<S: MutinyStorage> Node<S> {
}
}
None => Ok(self
.create_internal_invoice(amount_sat, None, route_hints)
.create_internal_invoice(amount_sat, None, route_hints, labels)
.await?),
}
}
Expand All @@ -1122,6 +1130,7 @@ impl<S: MutinyStorage> Node<S> {
amount_sat: Option<u64>,
fee_amount_msat: Option<u64>,
route_hints: Option<Vec<PhantomRouteHints>>,
labels: Vec<String>,
) -> Result<Bolt11Invoice, MutinyError> {
let amount_msat = amount_sat.map(|s| s * 1_000);
// Set description to empty string to make smallest possible invoice/QR code
Expand Down Expand Up @@ -1175,7 +1184,7 @@ impl<S: MutinyStorage> Node<S> {
MutinyError::InvoiceCreationFailed
})?;

self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat)
self.save_invoice_payment_info(invoice.clone(), amount_msat, fee_amount_msat, labels)
.await?;

log_info!(self.logger, "SUCCESS: generated invoice: {invoice}");
Expand All @@ -1188,6 +1197,7 @@ impl<S: MutinyStorage> Node<S> {
invoice: Bolt11Invoice,
amount_msat: Option<u64>,
fee_amount_msat: Option<u64>,
labels: Vec<String>,
) -> Result<(), MutinyError> {
let last_update = utils::now().as_secs();
let payment_hash = PaymentHash(invoice.payment_hash().into_inner());
Expand All @@ -1212,6 +1222,8 @@ impl<S: MutinyStorage> Node<S> {
MutinyError::InvoiceCreationFailed
})?;

self.persister.storage.set_invoice_labels(invoice, labels)?;

Ok(())
}

Expand Down Expand Up @@ -2546,6 +2558,7 @@ mod tests {
#[cfg(target_arch = "wasm32")]
mod wasm_test {
use crate::event::{MillisatAmount, PaymentInfo};
use crate::labels::LabelStorage;
use crate::storage::MemoryStorage;
use crate::test_utils::create_node;
use crate::HTLCStatus;
Expand Down Expand Up @@ -2573,8 +2586,13 @@ mod wasm_test {
let now = crate::utils::now().as_secs();

let amount_sats = 1_000;
let label = "test".to_string();
let labels = vec![label.clone()];

let invoice = node.create_invoice(Some(amount_sats), None).await.unwrap();
let invoice = node
.create_invoice(Some(amount_sats), None, labels.clone())
.await
.unwrap();

assert_eq!(invoice.amount_milli_satoshis(), Some(amount_sats * 1000));
match invoice.description() {
Expand All @@ -2596,16 +2614,32 @@ mod wasm_test {
assert_eq!(from_storage.amount_sats, Some(amount_sats));
assert_eq!(from_storage.status, HTLCStatus::Pending);
assert_eq!(from_storage.fees_paid, None);
assert_eq!(from_storage.labels, labels.clone());
assert!(from_storage.inbound);
assert!(from_storage.last_updated >= now);

// check labels

let invoice_labels = storage.get_invoice_labels().unwrap();
assert_eq!(invoice_labels.len(), 1);
assert_eq!(invoice_labels.get(&invoice).cloned(), Some(labels));

let label_item = storage.get_label("test").unwrap().unwrap();

assert!(label_item.last_used_time >= now);
assert!(label_item.addresses.is_empty());
assert_eq!(label_item.invoices, vec![invoice]);
}

#[test]
async fn test_fail_own_invoice() {
let storage = MemoryStorage::default();
let node = create_node(storage).await;

let invoice = node.create_invoice(Some(10_000), None).await.unwrap();
let invoice = node
.create_invoice(Some(10_000), None, vec![])
.await
.unwrap();

let result = node
.pay_invoice_with_timeout(&invoice, None, None, vec![])
Expand Down
10 changes: 8 additions & 2 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,11 @@ impl<S: MutinyStorage> NodeManager<S> {
///
/// If the manager has more than one node it will create a phantom invoice.
/// If there is only one node it will create an invoice just for that node.
pub async fn create_invoice(&self, amount: Option<u64>) -> Result<MutinyInvoice, MutinyError> {
pub async fn create_invoice(
&self,
amount: Option<u64>,
labels: Vec<String>,
) -> Result<MutinyInvoice, MutinyError> {
let nodes = self.nodes.lock().await;
let use_phantom = nodes.len() > 1 && self.lsp_config.is_none();
if nodes.len() == 0 {
Expand All @@ -1393,7 +1397,9 @@ impl<S: MutinyStorage> NodeManager<S> {
} else {
return Err(MutinyError::WalletOperationFailed);
};
let invoice = first_node.create_invoice(amount, route_hints).await?;
let invoice = first_node
.create_invoice(amount, route_hints, labels)
.await?;

Ok(invoice.into())
}
Expand Down
19 changes: 8 additions & 11 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,20 +1051,17 @@ impl MutinyWallet {
let activity = self.inner.node_manager.get_label_activity(&label).await?;
let mut activity: Vec<ActivityItem> = activity.into_iter().map(|a| a.into()).collect();

// add contact to the activity item if it is one
let Some(contact) = self.inner.node_manager.get_contact(&label)? else {
return Ok(JsValue::from_serde(&activity)?);
// add contact to the activity item it has one, otherwise return the activity list
let contact = match self.inner.node_manager.get_contact(&label)? {
Some(contact) => contact,
None => return Ok(JsValue::from_serde(&activity)?),
};

// if we have a contact, add it to the activity item, remove it as a label
// This is the same as we do in get_activity
for a in activity.iter_mut() {
// find labels that have a contact and add them to the item
for a_label in a.labels.iter() {
if label == *a_label {
a.contacts
.push(TagItem::from((a_label.clone(), contact.clone())));
}
}
// remove labels that have the contact to prevent duplicates
a.contacts
.push(TagItem::from((label.clone(), contact.clone())));
a.labels.retain(|l| l != &label);
}

Expand Down

0 comments on commit e77a11d

Please sign in to comment.