Skip to content
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
217 changes: 212 additions & 5 deletions crates/bcr-ebill-api/src/service/company_service.rs

Large diffs are not rendered by default.

76 changes: 64 additions & 12 deletions crates/bcr-ebill-api/src/service/identity_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use async_trait::async_trait;
use bcr_ebill_core::identity::validation::{validate_create_identity, validate_update_identity};
use bcr_ebill_core::identity::{ActiveIdentityState, IdentityType};
use bcr_ebill_core::{NodeId, ServiceTraitBounds, ValidationError};
use bcr_ebill_transport::NotificationServiceApi;
use bcr_ebill_transport::event::identity_events::IdentityChainEvent;
use log::{debug, error, info};
use std::sync::Arc;

Expand Down Expand Up @@ -102,6 +104,7 @@ pub struct IdentityService {
file_upload_store: Arc<dyn FileUploadStoreApi>,
file_upload_client: Arc<dyn FileStorageClientApi>,
blockchain_store: Arc<dyn IdentityChainStoreApi>,
notification_service: Arc<dyn NotificationServiceApi>,
}

impl IdentityService {
Expand All @@ -110,12 +113,14 @@ impl IdentityService {
file_upload_store: Arc<dyn FileUploadStoreApi>,
file_upload_client: Arc<dyn FileStorageClientApi>,
blockchain_store: Arc<dyn IdentityChainStoreApi>,
notification_service: Arc<dyn NotificationServiceApi>,
) -> Self {
Self {
store,
file_upload_store,
file_upload_client,
blockchain_store,
notification_service,
}
}

Expand Down Expand Up @@ -159,6 +164,18 @@ impl IdentityService {
nostr_hash: nostr_hash.to_string(),
})
}

async fn populate_block(
&self,
identity: &Identity,
block: &IdentityBlock,
keys: &BcrKeys,
) -> Result<()> {
self.notification_service
.send_identity_chain_events(IdentityChainEvent::new(identity, block, keys))
.await?;
Ok(())
}
}

impl ServiceTraitBounds for IdentityService {}
Expand Down Expand Up @@ -315,8 +332,8 @@ impl IdentityServiceApi for IdentityService {
timestamp,
)?;
self.blockchain_store.add_block(&new_block).await?;

self.store.save(&identity).await?;
self.populate_block(&identity, &new_block, &keys).await?;
debug!("updated identity");
Ok(())
}
Expand Down Expand Up @@ -421,6 +438,8 @@ impl IdentityServiceApi for IdentityService {

// persist the identity in the DB
self.store.save(&identity).await?;
self.populate_block(&identity, first_block, &keys).await?;

debug!("created identity");
Ok(())
}
Expand Down Expand Up @@ -524,8 +543,8 @@ impl IdentityServiceApi for IdentityService {
timestamp,
)?;
self.blockchain_store.add_block(&new_block).await?;

self.store.save(&identity).await?;
self.populate_block(&identity, &new_block, &keys).await?;
debug!("deanonymized identity");
Ok(())
}
Expand Down Expand Up @@ -627,7 +646,7 @@ mod tests {
external::file_storage::MockFileStorageClientApi,
tests::tests::{
MockFileUploadStoreApiMock, MockIdentityChainStoreApiMock, MockIdentityStoreApiMock,
empty_identity, empty_optional_address, init_test_cfg,
MockNotificationService, empty_identity, empty_optional_address, init_test_cfg,
},
};
use mockall::predicate::eq;
Expand All @@ -638,18 +657,21 @@ mod tests {
Arc::new(MockFileUploadStoreApiMock::new()),
Arc::new(MockFileStorageClientApi::new()),
Arc::new(MockIdentityChainStoreApiMock::new()),
Arc::new(MockNotificationService::new()),
)
}

fn get_service_with_chain_storage(
mock_storage: MockIdentityStoreApiMock,
mock_chain_storage: MockIdentityChainStoreApiMock,
notification: MockNotificationService,
) -> IdentityService {
IdentityService::new(
Arc::new(mock_storage),
Arc::new(MockFileUploadStoreApiMock::new()),
Arc::new(MockFileStorageClientApi::new()),
Arc::new(mock_chain_storage),
Arc::new(notification),
)
}

Expand All @@ -666,8 +688,13 @@ mod tests {
.returning(|| Ok(BcrKeys::new()));
let mut chain_storage = MockIdentityChainStoreApiMock::new();
chain_storage.expect_add_block().returning(|_| Ok(()));
let mut notification = MockNotificationService::new();
notification
.expect_send_identity_chain_events()
.returning(|_| Ok(()))
.once();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.create_identity(
IdentityType::Ident,
Expand All @@ -691,6 +718,7 @@ mod tests {
async fn create_anon_identity_baseline() {
init_test_cfg();
let mut storage = MockIdentityStoreApiMock::new();

storage
.expect_get_or_create_key_pair()
.returning(|| Ok(BcrKeys::new()));
Expand All @@ -700,8 +728,13 @@ mod tests {
.returning(|| Ok(BcrKeys::new()));
let mut chain_storage = MockIdentityChainStoreApiMock::new();
chain_storage.expect_add_block().returning(|_| Ok(()));
let mut notification = MockNotificationService::new();
notification
.expect_send_identity_chain_events()
.returning(|_| Ok(()))
.once();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.create_identity(
IdentityType::Anon,
Expand Down Expand Up @@ -749,8 +782,13 @@ mod tests {
.clone(),
)
});
let mut notification = MockNotificationService::new();
notification
.expect_send_identity_chain_events()
.returning(|_| Ok(()))
.once();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.deanonymize_identity(
IdentityType::Ident,
Expand Down Expand Up @@ -789,8 +827,10 @@ mod tests {
});
let mut chain_storage = MockIdentityChainStoreApiMock::new();
chain_storage.expect_add_block().returning(|_| Ok(()));
let mut notification = MockNotificationService::new();
notification.expect_send_identity_chain_events().never();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.deanonymize_identity(
IdentityType::Anon,
Expand Down Expand Up @@ -832,8 +872,10 @@ mod tests {
});
let mut chain_storage = MockIdentityChainStoreApiMock::new();
chain_storage.expect_add_block().returning(|_| Ok(()));
let mut notification = MockNotificationService::new();
notification.expect_send_identity_chain_events().never();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.deanonymize_identity(
IdentityType::Ident,
Expand Down Expand Up @@ -880,8 +922,13 @@ mod tests {
)
});
chain_storage.expect_add_block().returning(|_| Ok(()));
let mut notification = MockNotificationService::new();
notification
.expect_send_identity_chain_events()
.returning(|_| Ok(()))
.once();

let service = get_service_with_chain_storage(storage, chain_storage);
let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.update_identity(
Some("new_name".to_string()),
Expand Down Expand Up @@ -957,9 +1004,14 @@ mod tests {
.clone(),
)
});
chain_storage.expect_add_block().returning(|_| Ok(()));

let service = get_service_with_chain_storage(storage, chain_storage);
chain_storage
.expect_add_block()
.returning(|_| Ok(()))
.once();
let mut notification = MockNotificationService::new();
notification.expect_send_identity_chain_events().never();

let service = get_service_with_chain_storage(storage, chain_storage, notification);
let res = service
.update_identity(
Some("new_name".to_string()),
Expand Down
Loading
Loading