@@ -41,6 +41,7 @@ use crate::{error, CONFIG};
41
41
use async_trait:: async_trait;
42
42
use borsh:: to_vec;
43
43
use borsh_derive:: { BorshDeserialize , BorshSerialize } ;
44
+ use futures:: future:: try_join_all;
44
45
use log:: info;
45
46
#[ cfg( test) ]
46
47
use mockall:: automock;
@@ -934,13 +935,30 @@ impl BillService {
934
935
. await ?;
935
936
let first_version_bill = chain. get_first_version_bill ( & bill_keys) ?;
936
937
let time_of_drawing = first_version_bill. signing_timestamp ;
937
- let bill_participants = chain. get_all_nodes_from_bill ( & bill_keys) ?;
938
- let endorsements_count = chain. get_endorsements_count ( ) ;
939
938
939
+ // handle expensive deserialization and decryption logic in parallel on a blocking thread
940
+ // pool as not to block the task queue
941
+ let chain_clone = chain. clone ( ) ;
942
+ let keys_clone = bill_keys. clone ( ) ;
943
+ let bill_participants_handle =
944
+ tokio:: task:: spawn_blocking ( move || chain_clone. get_all_nodes_from_bill ( & keys_clone) ) ;
945
+ let chain_clone = chain. clone ( ) ;
946
+ let keys_clone = bill_keys. clone ( ) ;
947
+ let chain_to_return_handle = tokio:: task:: spawn_blocking ( move || {
948
+ BillBlockchainToReturn :: new ( chain_clone, & keys_clone)
949
+ } ) ;
950
+ let ( bill_participants_res, chain_to_return_res) =
951
+ tokio:: try_join!( bill_participants_handle, chain_to_return_handle) . map_err ( |e| {
952
+ error ! ( "couldn't get data from bill chain blocks {bill_id}: {e}" ) ;
953
+ Error :: Blockchain ( blockchain:: Error :: BlockchainParse )
954
+ } ) ?;
955
+ let bill_participants = bill_participants_res?;
956
+ let chain_to_return = chain_to_return_res?;
957
+
958
+ let endorsements_count = chain. get_endorsements_count ( ) ;
940
959
let mut in_recourse = false ;
941
960
let mut link_to_pay_recourse = "" . to_string ( ) ;
942
961
let mut link_for_buy = "" . to_string ( ) ;
943
- let chain_to_return = BillBlockchainToReturn :: new ( chain. clone ( ) , & bill_keys) ?;
944
962
let endorsed = chain. block_with_operation_code_exists ( BillOpCode :: Endorse ) ;
945
963
let accepted = chain. block_with_operation_code_exists ( BillOpCode :: Accept ) ;
946
964
let last_offer_to_sell_block_waiting_for_payment =
@@ -1038,6 +1056,9 @@ impl BillService {
1038
1056
let address_to_pay = self
1039
1057
. bitcoin_client
1040
1058
. get_address_to_pay ( & bill_keys. public_key , holder_public_key) ?;
1059
+ let mempool_link_for_address_to_pay = self
1060
+ . bitcoin_client
1061
+ . get_mempool_link_for_address ( & address_to_pay) ;
1041
1062
let mut paid = false ;
1042
1063
if requested_to_pay {
1043
1064
paid = self . store . is_paid ( & bill. id ) . await ?;
@@ -1083,6 +1104,7 @@ impl BillService {
1083
1104
recoursee,
1084
1105
link_to_pay_recourse,
1085
1106
address_to_pay,
1107
+ mempool_link_for_address_to_pay,
1086
1108
chain_of_blocks : chain_to_return,
1087
1109
files : bill. files ,
1088
1110
active_notification,
@@ -1838,50 +1860,57 @@ impl BillServiceApi for BillService {
1838
1860
}
1839
1861
1840
1862
async fn get_bills_from_all_identities ( & self ) -> Result < Vec < BitcreditBillToReturn > > {
1841
- let mut res = vec ! [ ] ;
1842
1863
let bill_ids = self . store . get_ids ( ) . await ?;
1843
1864
let identity = self . identity_store . get ( ) . await ?;
1844
- let current_timestamp = external:: time:: TimeApi :: get_atomic_time ( ) . await . timestamp ;
1845
-
1846
- for bill_id in bill_ids {
1847
- let bill = self
1848
- . get_full_bill ( & bill_id, & identity, & identity. node_id , current_timestamp)
1849
- . await ?;
1850
- res. push ( bill)
1851
- }
1865
+ let current_timestamp = util:: date:: now ( ) . timestamp ( ) as u64 ;
1866
+
1867
+ let tasks = bill_ids. iter ( ) . map ( |id| {
1868
+ let identity_clone = identity. clone ( ) ;
1869
+ async move {
1870
+ self . get_full_bill (
1871
+ id,
1872
+ & identity_clone,
1873
+ & identity_clone. node_id ,
1874
+ current_timestamp,
1875
+ )
1876
+ . await
1877
+ }
1878
+ } ) ;
1879
+ let bills = try_join_all ( tasks) . await ?;
1852
1880
1853
- Ok ( res )
1881
+ Ok ( bills )
1854
1882
}
1855
1883
1856
1884
async fn get_bills (
1857
1885
& self ,
1858
1886
current_identity_node_id : & str ,
1859
1887
) -> Result < Vec < BitcreditBillToReturn > > {
1860
- let mut res = vec ! [ ] ;
1861
1888
let bill_ids = self . store . get_ids ( ) . await ?;
1862
1889
let identity = self . identity_store . get ( ) . await ?;
1863
- let current_timestamp = external:: time:: TimeApi :: get_atomic_time ( ) . await . timestamp ;
1864
-
1865
- for bill_id in bill_ids {
1866
- let bill = self
1867
- . get_full_bill (
1868
- & bill_id,
1869
- & identity,
1890
+ let current_timestamp = util:: date:: now ( ) . timestamp ( ) as u64 ;
1891
+
1892
+ let tasks = bill_ids. iter ( ) . map ( |id| {
1893
+ let identity_clone = identity. clone ( ) ;
1894
+ async move {
1895
+ self . get_full_bill (
1896
+ id,
1897
+ & identity_clone,
1870
1898
current_identity_node_id,
1871
1899
current_timestamp,
1872
1900
)
1873
- . await ?;
1874
- // filter for currently active identity
1875
- if bill
1876
- . bill_participants
1877
- . iter ( )
1878
- . any ( |p| p == current_identity_node_id)
1879
- {
1880
- res. push ( bill) ;
1901
+ . await
1881
1902
}
1882
- }
1883
-
1884
- Ok ( res)
1903
+ } ) ;
1904
+ let bills = try_join_all ( tasks) . await ?;
1905
+
1906
+ Ok ( bills
1907
+ . into_iter ( )
1908
+ . filter ( |b| {
1909
+ b. bill_participants
1910
+ . iter ( )
1911
+ . any ( |p| p == current_identity_node_id)
1912
+ } )
1913
+ . collect ( ) )
1885
1914
}
1886
1915
1887
1916
async fn get_combined_bitcoin_key_for_bill (
@@ -3233,6 +3262,7 @@ pub struct BitcreditBillToReturn {
3233
3262
pub link_to_pay : String ,
3234
3263
pub link_to_pay_recourse : String ,
3235
3264
pub address_to_pay : String ,
3265
+ pub mempool_link_for_address_to_pay : String ,
3236
3266
pub chain_of_blocks : BillBlockchainToReturn ,
3237
3267
pub files : Vec < File > ,
3238
3268
/// The currently active notification for this bill if any
@@ -3479,6 +3509,13 @@ pub mod tests {
3479
3509
bitcoin_client
3480
3510
. expect_get_address_to_pay ( )
3481
3511
. returning ( |_, _| Ok ( String :: from ( "1Jfn2nZcJ4T7bhE8FdMRz8T3P3YV4LsWn2" ) ) ) ;
3512
+ bitcoin_client
3513
+ . expect_get_mempool_link_for_address ( )
3514
+ . returning ( |_| {
3515
+ String :: from (
3516
+ "http://blockstream.info/testnet/address/1Jfn2nZcJ4T7bhE8FdMRz8T3P3YV4LsWn2" ,
3517
+ )
3518
+ } ) ;
3482
3519
bitcoin_client. expect_generate_link_to_pay ( ) . returning ( |_, _, _| String :: from ( "bitcoin:1Jfn2nZcJ4T7bhE8FdMRz8T3P3YV4LsWn2?amount=0.01&message=Payment in relation to bill some bill" ) ) ;
3483
3520
BillService :: new (
3484
3521
Client :: new (
0 commit comments