Skip to content

Commit a6908ab

Browse files
authored
Identity chains (#580)
* Events and generators * Add send events skeleton * Inject required dependencies * Added all populate block hooks, fixed tests * Actually send chain events and store them * Fix event types * Dynamic addition of new Nostr transports * Company invite event generator * Send company invite * Less verbose logging * Review fixes
1 parent 945a756 commit a6908ab

File tree

14 files changed

+768
-122
lines changed

14 files changed

+768
-122
lines changed

crates/bcr-ebill-api/src/service/company_service.rs

Lines changed: 212 additions & 5 deletions
Large diffs are not rendered by default.

crates/bcr-ebill-api/src/service/identity_service.rs

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use async_trait::async_trait;
1616
use bcr_ebill_core::identity::validation::{validate_create_identity, validate_update_identity};
1717
use bcr_ebill_core::identity::{ActiveIdentityState, IdentityType};
1818
use bcr_ebill_core::{NodeId, ServiceTraitBounds, ValidationError};
19+
use bcr_ebill_transport::NotificationServiceApi;
20+
use bcr_ebill_transport::event::identity_events::IdentityChainEvent;
1921
use log::{debug, error, info};
2022
use std::sync::Arc;
2123

@@ -102,6 +104,7 @@ pub struct IdentityService {
102104
file_upload_store: Arc<dyn FileUploadStoreApi>,
103105
file_upload_client: Arc<dyn FileStorageClientApi>,
104106
blockchain_store: Arc<dyn IdentityChainStoreApi>,
107+
notification_service: Arc<dyn NotificationServiceApi>,
105108
}
106109

107110
impl IdentityService {
@@ -110,12 +113,14 @@ impl IdentityService {
110113
file_upload_store: Arc<dyn FileUploadStoreApi>,
111114
file_upload_client: Arc<dyn FileStorageClientApi>,
112115
blockchain_store: Arc<dyn IdentityChainStoreApi>,
116+
notification_service: Arc<dyn NotificationServiceApi>,
113117
) -> Self {
114118
Self {
115119
store,
116120
file_upload_store,
117121
file_upload_client,
118122
blockchain_store,
123+
notification_service,
119124
}
120125
}
121126

@@ -159,6 +164,18 @@ impl IdentityService {
159164
nostr_hash: nostr_hash.to_string(),
160165
})
161166
}
167+
168+
async fn populate_block(
169+
&self,
170+
identity: &Identity,
171+
block: &IdentityBlock,
172+
keys: &BcrKeys,
173+
) -> Result<()> {
174+
self.notification_service
175+
.send_identity_chain_events(IdentityChainEvent::new(identity, block, keys))
176+
.await?;
177+
Ok(())
178+
}
162179
}
163180

164181
impl ServiceTraitBounds for IdentityService {}
@@ -315,8 +332,8 @@ impl IdentityServiceApi for IdentityService {
315332
timestamp,
316333
)?;
317334
self.blockchain_store.add_block(&new_block).await?;
318-
319335
self.store.save(&identity).await?;
336+
self.populate_block(&identity, &new_block, &keys).await?;
320337
debug!("updated identity");
321338
Ok(())
322339
}
@@ -421,6 +438,8 @@ impl IdentityServiceApi for IdentityService {
421438

422439
// persist the identity in the DB
423440
self.store.save(&identity).await?;
441+
self.populate_block(&identity, first_block, &keys).await?;
442+
424443
debug!("created identity");
425444
Ok(())
426445
}
@@ -524,8 +543,8 @@ impl IdentityServiceApi for IdentityService {
524543
timestamp,
525544
)?;
526545
self.blockchain_store.add_block(&new_block).await?;
527-
528546
self.store.save(&identity).await?;
547+
self.populate_block(&identity, &new_block, &keys).await?;
529548
debug!("deanonymized identity");
530549
Ok(())
531550
}
@@ -627,7 +646,7 @@ mod tests {
627646
external::file_storage::MockFileStorageClientApi,
628647
tests::tests::{
629648
MockFileUploadStoreApiMock, MockIdentityChainStoreApiMock, MockIdentityStoreApiMock,
630-
empty_identity, empty_optional_address, init_test_cfg,
649+
MockNotificationService, empty_identity, empty_optional_address, init_test_cfg,
631650
},
632651
};
633652
use mockall::predicate::eq;
@@ -638,18 +657,21 @@ mod tests {
638657
Arc::new(MockFileUploadStoreApiMock::new()),
639658
Arc::new(MockFileStorageClientApi::new()),
640659
Arc::new(MockIdentityChainStoreApiMock::new()),
660+
Arc::new(MockNotificationService::new()),
641661
)
642662
}
643663

644664
fn get_service_with_chain_storage(
645665
mock_storage: MockIdentityStoreApiMock,
646666
mock_chain_storage: MockIdentityChainStoreApiMock,
667+
notification: MockNotificationService,
647668
) -> IdentityService {
648669
IdentityService::new(
649670
Arc::new(mock_storage),
650671
Arc::new(MockFileUploadStoreApiMock::new()),
651672
Arc::new(MockFileStorageClientApi::new()),
652673
Arc::new(mock_chain_storage),
674+
Arc::new(notification),
653675
)
654676
}
655677

@@ -666,8 +688,13 @@ mod tests {
666688
.returning(|| Ok(BcrKeys::new()));
667689
let mut chain_storage = MockIdentityChainStoreApiMock::new();
668690
chain_storage.expect_add_block().returning(|_| Ok(()));
691+
let mut notification = MockNotificationService::new();
692+
notification
693+
.expect_send_identity_chain_events()
694+
.returning(|_| Ok(()))
695+
.once();
669696

670-
let service = get_service_with_chain_storage(storage, chain_storage);
697+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
671698
let res = service
672699
.create_identity(
673700
IdentityType::Ident,
@@ -691,6 +718,7 @@ mod tests {
691718
async fn create_anon_identity_baseline() {
692719
init_test_cfg();
693720
let mut storage = MockIdentityStoreApiMock::new();
721+
694722
storage
695723
.expect_get_or_create_key_pair()
696724
.returning(|| Ok(BcrKeys::new()));
@@ -700,8 +728,13 @@ mod tests {
700728
.returning(|| Ok(BcrKeys::new()));
701729
let mut chain_storage = MockIdentityChainStoreApiMock::new();
702730
chain_storage.expect_add_block().returning(|_| Ok(()));
731+
let mut notification = MockNotificationService::new();
732+
notification
733+
.expect_send_identity_chain_events()
734+
.returning(|_| Ok(()))
735+
.once();
703736

704-
let service = get_service_with_chain_storage(storage, chain_storage);
737+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
705738
let res = service
706739
.create_identity(
707740
IdentityType::Anon,
@@ -749,8 +782,13 @@ mod tests {
749782
.clone(),
750783
)
751784
});
785+
let mut notification = MockNotificationService::new();
786+
notification
787+
.expect_send_identity_chain_events()
788+
.returning(|_| Ok(()))
789+
.once();
752790

753-
let service = get_service_with_chain_storage(storage, chain_storage);
791+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
754792
let res = service
755793
.deanonymize_identity(
756794
IdentityType::Ident,
@@ -789,8 +827,10 @@ mod tests {
789827
});
790828
let mut chain_storage = MockIdentityChainStoreApiMock::new();
791829
chain_storage.expect_add_block().returning(|_| Ok(()));
830+
let mut notification = MockNotificationService::new();
831+
notification.expect_send_identity_chain_events().never();
792832

793-
let service = get_service_with_chain_storage(storage, chain_storage);
833+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
794834
let res = service
795835
.deanonymize_identity(
796836
IdentityType::Anon,
@@ -832,8 +872,10 @@ mod tests {
832872
});
833873
let mut chain_storage = MockIdentityChainStoreApiMock::new();
834874
chain_storage.expect_add_block().returning(|_| Ok(()));
875+
let mut notification = MockNotificationService::new();
876+
notification.expect_send_identity_chain_events().never();
835877

836-
let service = get_service_with_chain_storage(storage, chain_storage);
878+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
837879
let res = service
838880
.deanonymize_identity(
839881
IdentityType::Ident,
@@ -880,8 +922,13 @@ mod tests {
880922
)
881923
});
882924
chain_storage.expect_add_block().returning(|_| Ok(()));
925+
let mut notification = MockNotificationService::new();
926+
notification
927+
.expect_send_identity_chain_events()
928+
.returning(|_| Ok(()))
929+
.once();
883930

884-
let service = get_service_with_chain_storage(storage, chain_storage);
931+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
885932
let res = service
886933
.update_identity(
887934
Some("new_name".to_string()),
@@ -957,9 +1004,14 @@ mod tests {
9571004
.clone(),
9581005
)
9591006
});
960-
chain_storage.expect_add_block().returning(|_| Ok(()));
961-
962-
let service = get_service_with_chain_storage(storage, chain_storage);
1007+
chain_storage
1008+
.expect_add_block()
1009+
.returning(|_| Ok(()))
1010+
.once();
1011+
let mut notification = MockNotificationService::new();
1012+
notification.expect_send_identity_chain_events().never();
1013+
1014+
let service = get_service_with_chain_storage(storage, chain_storage, notification);
9631015
let res = service
9641016
.update_identity(
9651017
Some("new_name".to_string()),

0 commit comments

Comments
 (0)