3
3
use std:: collections:: { BTreeMap , BTreeSet } ;
4
4
5
5
use mina_p2p_messages:: v2;
6
+ use openmina_core:: transaction:: TransactionPoolMessageSource ;
6
7
use p2p:: P2pNetworkPubsubMessageCacheId ;
7
8
use redux:: Timestamp ;
8
9
use serde:: { Deserialize , Serialize } ;
@@ -16,11 +17,11 @@ use crate::p2p::PeerId;
16
17
static EMPTY_PEER_TX_CANDIDATES : BTreeMap < TransactionHash , TransactionPoolCandidateState > =
17
18
BTreeMap :: new ( ) ;
18
19
19
- type NextBatch = Option < (
20
+ type NextBatch = (
20
21
PeerId ,
21
22
Vec < TransactionWithHash > ,
22
- Option < P2pNetworkPubsubMessageCacheId > ,
23
- ) > ;
23
+ TransactionPoolMessageSource ,
24
+ ) ;
24
25
25
26
#[ derive( Serialize , Deserialize , Debug , Clone , Default ) ]
26
27
pub struct TransactionPoolCandidatesState {
@@ -236,18 +237,15 @@ impl TransactionPoolCandidatesState {
236
237
} )
237
238
}
238
239
239
- /// Get next batch of transactions to verify,
240
+ /// Get next batch of transactions to verify,
240
241
/// first checks if there are any transactions to verify from pubsub
241
242
/// after that checks for transactions from peers
242
- pub fn get_batch_to_verify ( & self ) -> NextBatch {
243
- if let Some ( batch) = self . next_batch_from_pubsub ( ) {
244
- return Some ( batch) ;
245
- }
246
-
247
- self . next_batch_from_peers ( )
243
+ pub fn get_batch_to_verify ( & self ) -> Option < NextBatch > {
244
+ self . next_batch_from_pubsub ( )
245
+ . or_else ( || self . next_batch_from_peers ( ) )
248
246
}
249
247
250
- fn next_batch_from_peers ( & self ) -> NextBatch {
248
+ fn next_batch_from_peers ( & self ) -> Option < NextBatch > {
251
249
for ( hash, peers) in self . by_hash . iter ( ) {
252
250
if let Some ( res) = None . or_else ( || {
253
251
for peer_id in peers {
@@ -263,7 +261,7 @@ impl TransactionPoolCandidatesState {
263
261
} )
264
262
. cloned ( )
265
263
. collect ( ) ;
266
- return Some ( ( * peer_id, transactions, None ) ) ;
264
+ return Some ( ( * peer_id, transactions, TransactionPoolMessageSource :: None ) ) ;
267
265
}
268
266
}
269
267
None
@@ -275,7 +273,7 @@ impl TransactionPoolCandidatesState {
275
273
None
276
274
}
277
275
278
- fn next_batch_from_pubsub ( & self ) -> NextBatch {
276
+ fn next_batch_from_pubsub ( & self ) -> Option < NextBatch > {
279
277
let ( message_id, ( peer_id, transaction_hashes) ) = self . by_message_id . iter ( ) . next ( ) ?;
280
278
let transactions = self
281
279
. by_peer
@@ -294,8 +292,13 @@ impl TransactionPoolCandidatesState {
294
292
. cloned ( )
295
293
. collect ( ) ;
296
294
297
- return Some ( ( * peer_id, transactions, Some ( * message_id) ) ) ;
295
+ Some ( (
296
+ * peer_id,
297
+ transactions,
298
+ TransactionPoolMessageSource :: pubsub ( * message_id) ,
299
+ ) )
298
300
}
301
+
299
302
pub fn verify_pending (
300
303
& mut self ,
301
304
time : Timestamp ,
@@ -325,30 +328,31 @@ impl TransactionPoolCandidatesState {
325
328
_time : Timestamp ,
326
329
peer_id : & PeerId ,
327
330
verify_id : ( ) ,
328
- from_source : & Option < P2pNetworkPubsubMessageCacheId > ,
331
+ from_source : & TransactionPoolMessageSource ,
329
332
_result : Result < ( ) , ( ) > ,
330
333
) {
331
- if let Some ( from_source) = from_source {
332
- let Some ( ( _, transactions) ) = self . by_message_id . remove ( from_source) else {
333
- return ;
334
- } ;
334
+ match from_source {
335
+ TransactionPoolMessageSource :: Pubsub { id } => {
336
+ let Some ( ( _, transactions) ) = self . by_message_id . remove ( id) else {
337
+ return ;
338
+ } ;
335
339
336
- for hash in transactions {
337
- self . transaction_remove ( & hash) ;
340
+ for hash in transactions {
341
+ self . transaction_remove ( & hash) ;
342
+ }
338
343
}
339
-
340
- return ;
341
- }
342
-
343
- if let Some ( peer_transactions) = self . by_peer . get_mut ( peer_id) {
344
- let txs_to_remove = peer_transactions
345
- . iter ( )
346
- . filter ( |( _, job_state) | job_state. pending_verify_id ( ) == Some ( verify_id) )
347
- . map ( |( hash, _) | hash. clone ( ) )
348
- . collect :: < Vec < _ > > ( ) ;
349
-
350
- for hash in txs_to_remove {
351
- self . transaction_remove ( & hash) ;
344
+ _ => {
345
+ if let Some ( peer_transactions) = self . by_peer . get_mut ( peer_id) {
346
+ let txs_to_remove = peer_transactions
347
+ . iter ( )
348
+ . filter ( |( _, job_state) | job_state. pending_verify_id ( ) == Some ( verify_id) )
349
+ . map ( |( hash, _) | hash. clone ( ) )
350
+ . collect :: < Vec < _ > > ( ) ;
351
+
352
+ for hash in txs_to_remove {
353
+ self . transaction_remove ( & hash) ;
354
+ }
355
+ }
352
356
}
353
357
}
354
358
}
@@ -423,20 +427,6 @@ impl TransactionPoolCandidatesState {
423
427
!peers. is_empty ( )
424
428
} )
425
429
}
426
-
427
- pub fn remove_pubsub_transactions ( & mut self , message_id : & P2pNetworkPubsubMessageCacheId ) {
428
- let Some ( ( peer_id, transactions) ) = self . by_message_id . remove ( message_id) else {
429
- return ;
430
- } ;
431
-
432
- let Some ( peer_transactions) = self . by_peer . get_mut ( & peer_id) else {
433
- return ;
434
- } ;
435
-
436
- transactions. into_iter ( ) . for_each ( |tx| {
437
- peer_transactions. remove ( & tx) ;
438
- } ) ;
439
- }
440
430
}
441
431
442
432
impl TransactionPoolCandidateState {
0 commit comments