@@ -8,10 +8,10 @@ use crate::accountant::{checked_conversion, comma_joined_stringifiable};
88use crate :: blockchain:: blockchain_bridge:: PendingPayableFingerprint ;
99use crate :: database:: rusqlite_wrappers:: ConnectionWrapper ;
1010use crate :: sub_lib:: wallet:: Wallet ;
11- use itertools:: Itertools ;
1211use masq_lib:: utils:: ExpectValue ;
1312use rusqlite:: Row ;
14- use std:: collections:: HashMap ;
13+ use std:: collections:: HashSet ;
14+ use std:: fmt:: Debug ;
1515use std:: str:: FromStr ;
1616use std:: time:: SystemTime ;
1717use 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+
2935pub 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
4349impl 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]
0 commit comments