Skip to content

Commit 0d29adb

Browse files
tomproCopilot
andauthored
Switch to new chain transport (#565)
* Remove all DM transport payload * Validate sender for action events * Tests for action event validation * Valid flag on chain events * Respect valid flag on queries * Chain invite tests * Update crates/bcr-ebill-transport/src/handler/bill_action_event_handler.rs Co-authored-by: Copilot <[email protected]> * Reorder event sending, fix some log warinings * Fix all cargo fmt errors --------- Co-authored-by: Copilot <[email protected]>
1 parent da3048f commit 0d29adb

File tree

27 files changed

+751
-869
lines changed

27 files changed

+751
-869
lines changed

crates/bcr-ebill-api/src/external/bitcoin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl BitcoinClientApi for BitcoinClient {
210210

211211
fn generate_link_to_pay(&self, address: &str, sum: u64, message: &str) -> String {
212212
let btc_sum = util::currency::sat_to_btc(sum);
213-
let link = format!("bitcoin:{}?amount={}&message={}", address, btc_sum, message);
213+
let link = format!("bitcoin:{address}?amount={btc_sum}&message={message}");
214214
link
215215
}
216216

crates/bcr-ebill-api/src/external/mint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl MintClientApi for MintClient {
238238
async fn get_keyset_info(&self, mint_url: &str, keyset_id: &str) -> Result<cdk02::KeySet> {
239239
let base = reqwest::Url::parse(mint_url).map_err(|_| Error::InvalidMintUrl)?;
240240
let url = base
241-
.join(&format!("/v1/keys/{}", keyset_id))
241+
.join(&format!("/v1/keys/{keyset_id}"))
242242
.expect("keys relative path");
243243
let res = reqwest::Client::new().get(url).send().await.map_err(|e| {
244244
log::error!("Error getting keyset info from mint {mint_url}: {e}");

crates/bcr-ebill-api/src/service/notification_service/default_service.rs

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ impl DefaultNotificationService {
120120
.await
121121
{
122122
error!(
123-
"Failed to send block notification, will add it to retry queue: {}",
124-
e
123+
"Failed to send block notification, will add it to retry queue: {e}"
125124
);
126125
let queue_message = NostrQueuedMessage {
127126
id: uuid::Uuid::new_v4().to_string(),
@@ -134,18 +133,15 @@ impl DefaultNotificationService {
134133
.add_message(queue_message, Self::NOSTR_MAX_RETRIES)
135134
.await
136135
{
137-
error!("Failed to add block notification to retry queue: {}", e);
136+
error!("Failed to add block notification to retry queue: {e}");
138137
}
139138
}
140139
} else {
141-
warn!(
142-
"Failed to find recipient in contacts for node_id: {}",
143-
node_id
144-
);
140+
warn!("Failed to find recipient in contacts for node_id: {node_id}");
145141
}
146142
}
147143
} else {
148-
warn!("No transport node found for sender node_id: {}", sender);
144+
warn!("No transport node found for sender node_id: {sender}");
149145
}
150146
Ok(())
151147
}
@@ -206,6 +202,7 @@ impl DefaultNotificationService {
206202
received: block_event.data.block.timestamp,
207203
time: event.created_at.as_u64(),
208204
payload: event,
205+
valid: true,
209206
})
210207
.await
211208
.map_err(|_| {
@@ -263,8 +260,8 @@ impl NotificationServiceApi for DefaultNotificationService {
263260
None,
264261
);
265262

266-
self.send_all_events(&event.sender(), all_events).await?;
267263
self.send_bill_chain_events(event).await?;
264+
self.send_all_events(&event.sender(), all_events).await?;
268265
Ok(())
269266
}
270267

@@ -277,8 +274,8 @@ impl NotificationServiceApi for DefaultNotificationService {
277274
None,
278275
None,
279276
);
280-
self.send_all_events(&event.sender(), all_events).await?;
281277
self.send_bill_chain_events(event).await?;
278+
self.send_all_events(&event.sender(), all_events).await?;
282279
Ok(())
283280
}
284281

@@ -294,8 +291,8 @@ impl NotificationServiceApi for DefaultNotificationService {
294291
None,
295292
None,
296293
);
297-
self.send_all_events(&event.sender(), all_events).await?;
298294
self.send_bill_chain_events(event).await?;
295+
self.send_all_events(&event.sender(), all_events).await?;
299296
Ok(())
300297
}
301298

@@ -308,8 +305,8 @@ impl NotificationServiceApi for DefaultNotificationService {
308305
None,
309306
None,
310307
);
311-
self.send_all_events(&event.sender(), all_events).await?;
312308
self.send_bill_chain_events(event).await?;
309+
self.send_all_events(&event.sender(), all_events).await?;
313310
Ok(())
314311
}
315312

@@ -322,8 +319,8 @@ impl NotificationServiceApi for DefaultNotificationService {
322319
None,
323320
None,
324321
);
325-
self.send_all_events(&event.sender(), all_events).await?;
326322
self.send_bill_chain_events(event).await?;
323+
self.send_all_events(&event.sender(), all_events).await?;
327324
Ok(())
328325
}
329326

@@ -336,8 +333,8 @@ impl NotificationServiceApi for DefaultNotificationService {
336333
None,
337334
None,
338335
);
339-
self.send_all_events(&bill.sender(), all_events).await?;
340336
self.send_bill_chain_events(bill).await?;
337+
self.send_all_events(&bill.sender(), all_events).await?;
341338
Ok(())
342339
}
343340

@@ -354,8 +351,8 @@ impl NotificationServiceApi for DefaultNotificationService {
354351
None,
355352
None,
356353
);
357-
self.send_all_events(&event.sender(), all_events).await?;
358354
self.send_bill_chain_events(event).await?;
355+
self.send_all_events(&event.sender(), all_events).await?;
359356
Ok(())
360357
}
361358

@@ -372,8 +369,8 @@ impl NotificationServiceApi for DefaultNotificationService {
372369
None,
373370
None,
374371
);
375-
self.send_all_events(&event.sender(), all_events).await?;
376372
self.send_bill_chain_events(event).await?;
373+
self.send_all_events(&event.sender(), all_events).await?;
377374
Ok(())
378375
}
379376

@@ -390,8 +387,8 @@ impl NotificationServiceApi for DefaultNotificationService {
390387
None,
391388
None,
392389
);
393-
self.send_all_events(&event.sender(), all_events).await?;
394390
self.send_bill_chain_events(event).await?;
391+
self.send_all_events(&event.sender(), all_events).await?;
395392
Ok(())
396393
}
397394

@@ -406,8 +403,6 @@ impl NotificationServiceApi for DefaultNotificationService {
406403
bill_id: bill.id.clone(),
407404
action_type: Some(ActionType::CheckBill),
408405
sum: Some(bill.sum),
409-
keys: None,
410-
blocks: vec![],
411406
});
412407
if let Some(node) = self.notification_transport.get(sender_node_id) {
413408
node.send_private_event(mint, event.try_into()?).await?;
@@ -451,8 +446,6 @@ impl NotificationServiceApi for DefaultNotificationService {
451446
bill_id: bill_id.to_owned(),
452447
action_type: Some(ActionType::CheckBill),
453448
sum,
454-
keys: None,
455-
blocks: vec![],
456449
};
457450
for (_, recipient) in unique {
458451
let event = Event::new_bill(payload.clone());
@@ -479,8 +472,8 @@ impl NotificationServiceApi for DefaultNotificationService {
479472
Some(BillEventType::BillBlock),
480473
None,
481474
);
482-
self.send_all_events(&event.sender(), all_events).await?;
483475
self.send_bill_chain_events(event).await?;
476+
self.send_all_events(&event.sender(), all_events).await?;
484477
}
485478
Ok(())
486479
}
@@ -503,7 +496,7 @@ impl NotificationServiceApi for DefaultNotificationService {
503496
validate_node_id_network(node_id)?;
504497
}
505498
let result = self.notification_store.list(filter).await.map_err(|e| {
506-
error!("Failed to get client notifications: {}", e);
499+
error!("Failed to get client notifications: {e}");
507500
Error::Persistence("Failed to get client notifications".to_string())
508501
})?;
509502
Ok(result)
@@ -515,7 +508,7 @@ impl NotificationServiceApi for DefaultNotificationService {
515508
.mark_as_done(notification_id)
516509
.await
517510
.map_err(|e| {
518-
error!("Failed to mark notification as done: {}", e);
511+
error!("Failed to mark notification as done: {e}");
519512
Error::Persistence("Failed to mark notification as done".to_string())
520513
})?;
521514
Ok(())
@@ -559,10 +552,7 @@ impl NotificationServiceApi for DefaultNotificationService {
559552
.bill_notification_sent(bill_id, block_height, action)
560553
.await
561554
.map_err(|e| {
562-
error!(
563-
"Failed to check if bill notification was already sent: {}",
564-
e
565-
);
555+
error!("Failed to check if bill notification was already sent: {e}");
566556
Error::Persistence(
567557
"Failed to check if bill notification was already sent".to_string(),
568558
)
@@ -581,7 +571,7 @@ impl NotificationServiceApi for DefaultNotificationService {
581571
.set_bill_notification_sent(bill_id, block_height, action)
582572
.await
583573
.map_err(|e| {
584-
error!("Failed to mark bill notification as sent: {}", e);
574+
error!("Failed to mark bill notification as sent: {e}");
585575
Error::Persistence("Failed to mark bill notification as sent".to_string())
586576
})?;
587577
Ok(())
@@ -604,21 +594,21 @@ impl NotificationServiceApi for DefaultNotificationService {
604594
)
605595
.await
606596
{
607-
error!("Failed to send retry message: {}", e);
597+
error!("Failed to send retry message: {e}");
608598
failed_ids.push(queued_message.id.clone());
609599
} else if let Err(e) = self
610600
.queued_message_store
611601
.succeed_retry(&queued_message.id)
612602
.await
613603
{
614-
error!("Failed to mark retry message as sent: {}", e);
604+
error!("Failed to mark retry message as sent: {e}");
615605
}
616606
}
617607
}
618608

619609
for failed in failed_ids {
620610
if let Err(e) = self.queued_message_store.fail_retry(&failed).await {
621-
error!("Failed to store failed retry attemt: {}", e);
611+
error!("Failed to store failed retry attemt: {e}");
622612
}
623613
}
624614
Ok(())
@@ -1248,32 +1238,32 @@ mod tests {
12481238
let mut mock = MockNotificationJsonTransport::new();
12491239
mock.expect_get_sender_node_id().returning(node_id_test);
12501240

1251-
mock.expect_send_private_event()
1252-
.returning(|_, _| Ok(()))
1253-
.once();
1254-
12551241
mock.expect_send_private_event()
12561242
.withf(move |_, e| {
12571243
let r: Result<Event<ChainInvite>> = e.clone().try_into();
1258-
r.is_err()
1244+
r.is_ok()
12591245
})
1260-
.returning(|_, _| Err(Error::Network("Failed to send".to_string())));
1246+
.returning(|_, _| Ok(()));
12611247

12621248
mock.expect_send_public_chain_event()
12631249
.returning(|_, _, _, _, _, _, _| Ok(get_test_nostr_event()));
12641250

1265-
mock.expect_send_private_event()
1266-
.withf(move |_, e| {
1267-
let r: Result<Event<ChainInvite>> = e.clone().try_into();
1268-
r.is_ok()
1269-
})
1270-
.returning(|_, _| Ok(()));
1271-
12721251
let mock_event_store = setup_event_store_expectations(
12731252
chain.get_latest_block().previous_hash.to_owned().as_str(),
12741253
&bill.id,
12751254
);
12761255

1256+
mock.expect_send_private_event()
1257+
.returning(|_, _| Ok(()))
1258+
.once();
1259+
1260+
mock.expect_send_private_event()
1261+
.withf(move |_, e| {
1262+
let r: Result<Event<ChainInvite>> = e.clone().try_into();
1263+
r.is_err()
1264+
})
1265+
.returning(|_, _| Err(Error::Network("Failed to send".to_string())));
1266+
12771267
let mut queue_mock = MockNostrQueuedMessageStore::new();
12781268
queue_mock
12791269
.expect_add_message()

crates/bcr-ebill-api/src/service/notification_service/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ pub async fn create_nostr_clients(
4040
) -> Result<Vec<Arc<NostrClient>>> {
4141
// primary identity is required to launch
4242
let keys = identity_store.get_or_create_key_pair().await.map_err(|e| {
43-
error!(
44-
"Failed to get or create nostr key pair for nostr client: {}",
45-
e
46-
);
43+
error!("Failed to get or create nostr key pair for nostr client: {e}");
4744
Error::Crypto("Failed to get or create nostr key pair".to_string())
4845
})?;
4946
let nostr_name = match identity_store.get().await {
@@ -62,7 +59,7 @@ pub async fn create_nostr_clients(
6259
let companies = match company_store.get_all().await {
6360
Ok(companies) => companies,
6461
Err(e) => {
65-
error!("Failed to get companies for nostr client: {}", e);
62+
error!("Failed to get companies for nostr client: {e}");
6663
HashMap::new()
6764
}
6865
};
@@ -137,7 +134,7 @@ pub async fn create_nostr_consumer(
137134
};
138135

139136
let processor = Arc::new(BillChainEventProcessor::new(
140-
bill_blockchain_store,
137+
bill_blockchain_store.clone(),
141138
bill_store.clone(),
142139
transport.clone(),
143140
nostr_contact_store,

crates/bcr-ebill-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,6 @@ pub enum ValidationError {
616616

617617
impl From<crate::blockchain::Error> for ValidationError {
618618
fn from(e: crate::blockchain::Error) -> Self {
619-
ValidationError::Blockchain(format!("Blockchain error: {}", e))
619+
ValidationError::Blockchain(format!("Blockchain error: {e}"))
620620
}
621621
}

crates/bcr-ebill-core/src/notification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub enum NotificationType {
6464

6565
impl Display for NotificationType {
6666
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67-
f.write_str(format!("{:?}", self).as_str())
67+
f.write_str(format!("{self:?}").as_str())
6868
}
6969
}
7070

crates/bcr-ebill-persistence/src/db/backup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl BackupStoreApi for SurrealBackupStore {
7171
let _ = self
7272
.db()
7373
.await?
74-
.query(format!("REMOVE DATABASE {}", name))
74+
.query(format!("REMOVE DATABASE {name}"))
7575
.await?;
7676
Ok(())
7777
}

crates/bcr-ebill-persistence/src/db/bill.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl BillStoreApi for SurrealBillStore {
5151
.map(|id| {
5252
(
5353
SurrealBillStore::CACHE_TABLE.to_owned(),
54-
format!("{}{}", id, identity_node_id),
54+
format!("{id}{identity_node_id}"),
5555
)
5656
.into()
5757
})
@@ -76,7 +76,7 @@ impl BillStoreApi for SurrealBillStore {
7676
) -> Result<Option<BitcreditBillResult>> {
7777
let result: Option<BitcreditBillResultDb> = self
7878
.db
79-
.select_one(Self::CACHE_TABLE, format!("{}{}", id, identity_node_id))
79+
.select_one(Self::CACHE_TABLE, format!("{id}{identity_node_id}"))
8080
.await?;
8181
match result {
8282
None => Ok(None),
@@ -96,11 +96,7 @@ impl BillStoreApi for SurrealBillStore {
9696
let entity: BitcreditBillResultDb = (bill, identity_node_id).into();
9797
let _: Option<BitcreditBillResultDb> = self
9898
.db
99-
.upsert(
100-
Self::CACHE_TABLE,
101-
format!("{}{}", id, identity_node_id),
102-
entity,
103-
)
99+
.upsert(Self::CACHE_TABLE, format!("{id}{identity_node_id}"), entity)
104100
.await?;
105101
Ok(())
106102
}

0 commit comments

Comments
 (0)