Skip to content

Commit 7a8bb0e

Browse files
authored
implement find and sync bill from dht (#380)
1 parent e725935 commit 7a8bb0e

File tree

5 files changed

+22
-31
lines changed

5 files changed

+22
-31
lines changed

src/dht/client.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,16 +869,20 @@ impl Client {
869869
pub async fn receive_updates_for_all_bills_topics(&mut self) -> Result<()> {
870870
let bill_ids = self.bill_store.get_ids().await?;
871871

872-
for bill in bill_ids {
873-
let event =
874-
GossipsubEvent::new(GossipsubEventId::CommandGetBillBlockchain, vec![0; 24]);
875-
let message = event.to_byte_array()?;
876-
877-
self.add_message_to_bill_topic(message, &bill).await?;
872+
for bill_id in bill_ids {
873+
self.receive_updates_for_bill_topic(&bill_id).await?;
878874
}
879875
Ok(())
880876
}
881877

878+
pub async fn receive_updates_for_bill_topic(&mut self, bill_id: &str) -> Result<()> {
879+
let event = GossipsubEvent::new(GossipsubEventId::CommandGetBillBlockchain, vec![0; 24]);
880+
let message = event.to_byte_array()?;
881+
882+
self.add_message_to_bill_topic(message, bill_id).await?;
883+
Ok(())
884+
}
885+
882886
// -------------------------------------------------------------
883887
// Utility Functions for the DHT -------------------------------
884888
// -------------------------------------------------------------

src/dht/event_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ impl EventLoop {
342342
if let Ok(mut local_chain) =
343343
self.bill_blockchain_store.get_chain(bill_id).await
344344
{
345+
info!("Received blockchain for bill: {bill_id} from peer: {node_id}, local block height: {}, remote block height: {}", local_chain.block_height(), remote_chain.block_height());
345346
let blocks_to_add = local_chain
346347
.get_blocks_to_add_from_other_chain(&remote_chain);
347348
for block in blocks_to_add {

src/service/bill_service.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,8 @@ pub trait BillServiceApi: Send + Sync {
306306
/// Gets the bill for the given bill id
307307
async fn get_bill(&self, bill_id: &str) -> Result<BitcreditBill>;
308308

309-
/// Try to get the given bill from the dht and saves it locally, if found
310-
async fn find_bill_in_dht(
311-
&self,
312-
bill_id: &str,
313-
caller_public_data: &IdentityPublicData,
314-
caller_keys: &BcrKeys,
315-
) -> Result<()>;
309+
/// Try to get the given bill chain from the dht and sync the blocks, if found
310+
async fn find_and_sync_with_bill_in_dht(&self, bill_id: &str) -> Result<()>;
316311

317312
/// Gets the keys for a given bill
318313
async fn get_bill_keys(&self, bill_id: &str) -> Result<BillKeys>;
@@ -1930,20 +1925,12 @@ impl BillServiceApi for BillService {
19301925
Ok(bill)
19311926
}
19321927

1933-
async fn find_bill_in_dht(
1934-
&self,
1935-
bill_id: &str,
1936-
caller_public_data: &IdentityPublicData,
1937-
caller_keys: &BcrKeys,
1938-
) -> Result<()> {
1939-
self.client
1940-
.clone()
1941-
.get_bill_data_from_the_network(
1942-
bill_id,
1943-
&caller_public_data.node_id,
1944-
&caller_keys.get_private_key_string(),
1945-
)
1946-
.await?;
1928+
async fn find_and_sync_with_bill_in_dht(&self, bill_id: &str) -> Result<()> {
1929+
if !self.store.exists(bill_id).await {
1930+
return Err(Error::NotFound);
1931+
}
1932+
let mut dht_client = self.client.clone();
1933+
dht_client.receive_updates_for_bill_topic(bill_id).await?;
19471934
Ok(())
19481935
}
19491936

src/web/handlers/bill.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,14 @@ pub async fn numbers_to_words_for_sum(
249249
}
250250

251251
#[get("/dht/<bill_id>")]
252-
pub async fn find_bill_in_dht(
252+
pub async fn find_and_sync_with_bill_in_dht(
253253
_identity: IdentityCheck,
254254
state: &State<ServiceContext>,
255255
bill_id: &str,
256256
) -> Result<Status> {
257-
let (caller_public_data, caller_keys) = get_signer_public_data_and_keys(state).await?;
258257
state
259258
.bill_service
260-
.find_bill_in_dht(bill_id, &caller_public_data, &caller_keys)
259+
.find_and_sync_with_bill_in_dht(bill_id)
261260
.await?;
262261
Ok(Status::Ok)
263262
}

src/web/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn rocket_main(context: ServiceContext) -> Rocket<Build> {
145145
handlers::bill::check_payment,
146146
handlers::bill::bitcoin_key,
147147
handlers::bill::numbers_to_words_for_sum,
148-
handlers::bill::find_bill_in_dht,
148+
handlers::bill::find_and_sync_with_bill_in_dht,
149149
handlers::bill::check_dht_for_bills,
150150
handlers::bill::holder,
151151
handlers::bill::search,

0 commit comments

Comments
 (0)