Skip to content

Commit 79e34c3

Browse files
authored
* implementing TransactionHashes struct * fixed test payable_scanner_handles_sent_payable_message * fixing code for tests * fixing code for test payable_scanner_panics_when_fingerprints_for_correct_payments_not_found * last test fixed * prepared for the code review * fixed compiler issues * add rpc server to test masq_erc20_contract_exists_on_polygon_mumbai_integration * implemented commnets from code review * fixing last comments from review 2
1 parent 7227f15 commit 79e34c3

File tree

6 files changed

+221
-179
lines changed

6 files changed

+221
-179
lines changed

node/src/accountant/db_access_objects/pending_payable_dao.rs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use crate::accountant::{checked_conversion, comma_joined_stringifiable};
88
use crate::blockchain::blockchain_bridge::PendingPayableFingerprint;
99
use crate::database::rusqlite_wrappers::ConnectionWrapper;
1010
use crate::sub_lib::wallet::Wallet;
11-
use itertools::Itertools;
1211
use masq_lib::utils::ExpectValue;
1312
use rusqlite::Row;
14-
use std::collections::HashMap;
13+
use std::collections::HashSet;
14+
use std::fmt::Debug;
1515
use std::str::FromStr;
1616
use std::time::SystemTime;
1717
use web3::types::H256;
@@ -26,9 +26,15 @@ pub enum PendingPayableDaoError {
2626
ErrorMarkFailed(String),
2727
}
2828

29+
#[derive(Debug)]
30+
pub struct TransactionHashes {
31+
pub rowid_results: Vec<(u64, H256)>,
32+
pub no_rowid_results: Vec<H256>,
33+
}
34+
2935
pub trait PendingPayableDao {
3036
// Note that the order of the returned results is not guaranteed
31-
fn fingerprints_rowids(&self, hashes: &[H256]) -> Vec<(Option<u64>, H256)>;
37+
fn fingerprints_rowids(&self, hashes: &[H256]) -> TransactionHashes;
3238
fn return_all_errorless_fingerprints(&self) -> Vec<PendingPayableFingerprint>;
3339
fn insert_new_fingerprints(
3440
&self,
@@ -41,35 +47,43 @@ pub trait PendingPayableDao {
4147
}
4248

4349
impl PendingPayableDao for PendingPayableDaoReal<'_> {
44-
fn fingerprints_rowids(&self, hashes: &[H256]) -> Vec<(Option<u64>, H256)> {
45-
fn hash_and_rowid_in_single_row(row: &Row) -> rusqlite::Result<(H256, u64)> {
50+
fn fingerprints_rowids(&self, hashes: &[H256]) -> TransactionHashes {
51+
//Vec<(Option<u64>, H256)> {
52+
fn hash_and_rowid_in_single_row(row: &Row) -> rusqlite::Result<(u64, H256)> {
4653
let hash_str: String = row.get(0).expectv("hash");
4754
let hash = H256::from_str(&hash_str[2..]).expect("hash inserted right turned wrong");
4855
let sqlite_signed_rowid: i64 = row.get(1).expectv("rowid");
4956
let rowid = u64::try_from(sqlite_signed_rowid).expect("SQlite goes from 1 to i64:MAX");
50-
Ok((hash, rowid))
57+
Ok((rowid, hash))
5158
}
5259

5360
let sql = format!(
5461
"select transaction_hash, rowid from pending_payable where transaction_hash in ({})",
5562
comma_joined_stringifiable(hashes, |hash| format!("'{:?}'", hash))
5663
);
64+
5765
let all_found_records = self
5866
.conn
5967
.prepare(&sql)
6068
.expect("Internal error")
6169
.query_map([], hash_and_rowid_in_single_row)
6270
.expect("map query failed")
6371
.vigilant_flatten()
64-
.collect::<HashMap<H256, u64>>();
65-
let hashes_of_missing_rowids = hashes
72+
.collect::<Vec<(u64, H256)>>();
73+
let hashes_of_found_records = all_found_records
6674
.iter()
67-
.filter(|hash| !all_found_records.keys().contains(hash));
68-
all_found_records
75+
.map(|(_, hash)| *hash)
76+
.collect::<HashSet<H256>>();
77+
let hashes_of_missing_rowids = hashes
6978
.iter()
70-
.map(|(hash, rowid)| (Some(*rowid), *hash))
71-
.chain(hashes_of_missing_rowids.map(|hash| (None, *hash)))
72-
.collect()
79+
.filter(|hash| !hashes_of_found_records.contains(hash))
80+
.cloned()
81+
.collect();
82+
83+
TransactionHashes {
84+
rowid_results: all_found_records,
85+
no_rowid_results: hashes_of_missing_rowids,
86+
}
7387
}
7488

7589
fn return_all_errorless_fingerprints(&self) -> Vec<PendingPayableFingerprint> {
@@ -403,21 +417,21 @@ mod tests {
403417

404418
let result = subject.fingerprints_rowids(&[hash_1, hash_2]);
405419

406-
let first_expected_pair = (Some(1), hash_1);
420+
let first_expected_pair = &(1, hash_1);
407421
assert!(
408-
result.contains(&first_expected_pair),
422+
result.rowid_results.contains(first_expected_pair),
409423
"Returned rowid pairs should have contained {:?} but all it did is {:?}",
410424
first_expected_pair,
411-
result
425+
result.rowid_results
412426
);
413-
let second_expected_pair = (Some(2), hash_2);
427+
let second_expected_pair = &(2, hash_2);
414428
assert!(
415-
result.contains(&second_expected_pair),
429+
result.rowid_results.contains(second_expected_pair),
416430
"Returned rowid pairs should have contained {:?} but all it did is {:?}",
417431
second_expected_pair,
418-
result
432+
result.rowid_results
419433
);
420-
assert_eq!(result.len(), 2);
434+
assert_eq!(result.rowid_results.len(), 2);
421435
}
422436

423437
#[test]
@@ -447,15 +461,8 @@ mod tests {
447461

448462
let result = subject.fingerprints_rowids(&[hash_1, hash_2, hash_3, hash_4]);
449463

450-
assert_eq!(
451-
result,
452-
vec![
453-
(Some(2), hash_3),
454-
(None, hash_1),
455-
(None, hash_2),
456-
(None, hash_4)
457-
]
458-
)
464+
assert_eq!(result.rowid_results, vec![(2, hash_3),]);
465+
assert_eq!(result.no_rowid_results, vec![hash_1, hash_2, hash_4]);
459466
}
460467

461468
#[test]

node/src/accountant/mod.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ mod tests {
989989
PayableAccount, PayableDaoError, PayableDaoFactory,
990990
};
991991
use crate::accountant::db_access_objects::pending_payable_dao::{
992-
PendingPayable, PendingPayableDaoError,
992+
PendingPayable, PendingPayableDaoError, TransactionHashes,
993993
};
994994
use crate::accountant::db_access_objects::receivable_dao::ReceivableAccount;
995995
use crate::accountant::db_access_objects::utils::{from_time_t, to_time_t, CustomQuery};
@@ -1349,8 +1349,11 @@ mod tests {
13491349
#[test]
13501350
fn sent_payable_with_response_skeleton_sends_scan_response_to_ui_gateway() {
13511351
let config = bc_from_earning_wallet(make_wallet("earning_wallet"));
1352-
let pending_payable_dao = PendingPayableDaoMock::default()
1353-
.fingerprints_rowids_result(vec![(Some(1), make_tx_hash(123))]);
1352+
let pending_payable_dao =
1353+
PendingPayableDaoMock::default().fingerprints_rowids_result(TransactionHashes {
1354+
rowid_results: vec![(1, make_tx_hash(123))],
1355+
no_rowid_results: vec![],
1356+
});
13541357
let payable_dao = PayableDaoMock::default().mark_pending_payables_rowids_result(Ok(()));
13551358
let subject = AccountantBuilder::default()
13561359
.pending_payable_daos(vec![ForPayableScanner(pending_payable_dao)])
@@ -1751,7 +1754,10 @@ mod tests {
17511754
let expected_rowid = 45623;
17521755
let pending_payable_dao = PendingPayableDaoMock::default()
17531756
.fingerprints_rowids_params(&fingerprints_rowids_params_arc)
1754-
.fingerprints_rowids_result(vec![(Some(expected_rowid), expected_hash)]);
1757+
.fingerprints_rowids_result(TransactionHashes {
1758+
rowid_results: vec![(expected_rowid, expected_hash)],
1759+
no_rowid_results: vec![],
1760+
});
17551761
let payable_dao = PayableDaoMock::new()
17561762
.mark_pending_payables_rowids_params(&mark_pending_payables_rowids_params_arc)
17571763
.mark_pending_payables_rowids_result(Ok(()));
@@ -3307,10 +3313,13 @@ mod tests {
33073313
..fingerprint_2_first_round.clone()
33083314
};
33093315
let pending_payable_dao_for_payable_scanner = PendingPayableDaoMock::default()
3310-
.fingerprints_rowids_result(vec![
3311-
(Some(rowid_for_account_1), pending_tx_hash_1),
3312-
(Some(rowid_for_account_2), pending_tx_hash_2),
3313-
]);
3316+
.fingerprints_rowids_result(TransactionHashes {
3317+
rowid_results: vec![
3318+
(rowid_for_account_1, pending_tx_hash_1),
3319+
(rowid_for_account_2, pending_tx_hash_2),
3320+
],
3321+
no_rowid_results: vec![],
3322+
});
33143323
let mut pending_payable_dao_for_pending_payable_scanner = PendingPayableDaoMock::new()
33153324
.return_all_errorless_fingerprints_params(&return_all_errorless_fingerprints_params_arc)
33163325
.return_all_errorless_fingerprints_result(vec![])
@@ -3327,10 +3336,13 @@ mod tests {
33273336
fingerprint_2_third_round,
33283337
])
33293338
.return_all_errorless_fingerprints_result(vec![fingerprint_2_fourth_round.clone()])
3330-
.fingerprints_rowids_result(vec![
3331-
(Some(rowid_for_account_1), pending_tx_hash_1),
3332-
(Some(rowid_for_account_2), pending_tx_hash_2),
3333-
])
3339+
.fingerprints_rowids_result(TransactionHashes {
3340+
rowid_results: vec![
3341+
(rowid_for_account_1, pending_tx_hash_1),
3342+
(rowid_for_account_2, pending_tx_hash_2),
3343+
],
3344+
no_rowid_results: vec![],
3345+
})
33343346
.increment_scan_attempts_params(&update_fingerprint_params_arc)
33353347
.increment_scan_attempts_result(Ok(()))
33363348
.increment_scan_attempts_result(Ok(()))

0 commit comments

Comments
 (0)