11use crate :: txs_pool:: pool:: TransactionPoolService ;
2+ use crate :: txs_pool:: trace:: { Action , TxTrace } ;
23use crate :: txs_pool:: types:: * ;
34use channel:: select;
45use channel:: { self , Receiver } ;
@@ -22,6 +23,7 @@ use std::io::Read;
2223use std:: path:: Path ;
2324use std:: sync:: Arc ;
2425use std:: time;
26+ use tempfile:: TempPath ;
2527
2628macro_rules! expect_output_parent {
2729 ( $pool: expr, $expected: pat, $( $output: expr ) ,+ ) => {
@@ -469,6 +471,100 @@ fn test_switch_fork() {
469471 assert_eq ! ( mtxs, vec![ txs[ 3 ] . clone( ) , txs[ 6 ] . clone( ) , txs[ 5 ] . clone( ) ] ) ;
470472}
471473
474+ fn prepare_trace (
475+ pool : & mut TestPool < ChainKVStore < MemoryKeyValueDB > > ,
476+ faketime_file : & TempPath ,
477+ ) -> Transaction {
478+ let tx = test_transaction ( & [ OutPoint :: new ( pool. tx_hash . clone ( ) , 0 ) ] , 2 ) ;
479+
480+ let block_number = { pool. shared . tip_header ( ) . read ( ) . number ( ) } ;
481+
482+ pool. service . trace_transaction ( tx. clone ( ) ) . unwrap ( ) ;
483+ let prop_ids = pool. service . prepare_proposal ( 10 ) ;
484+
485+ assert_eq ! ( 1 , prop_ids. len( ) ) ;
486+ assert_eq ! ( prop_ids[ 0 ] , tx. proposal_short_id( ) ) ;
487+
488+ let header = HeaderBuilder :: default ( ) . number ( block_number + 1 ) . build ( ) ;
489+ let block = BlockBuilder :: default ( )
490+ . header ( header)
491+ . proposal_transactions ( vec ! [ tx. proposal_short_id( ) ] )
492+ . build ( ) ;
493+
494+ faketime:: write_millis ( faketime_file, 9102 ) . expect ( "write millis" ) ;
495+
496+ pool. service . reconcile_block ( & block) ;
497+ tx
498+ }
499+
500+ #[ cfg( not( disable_faketime) ) ]
501+ #[ test]
502+ fn test_get_transaction_traces ( ) {
503+ let mut pool = TestPool :: < ChainKVStore < MemoryKeyValueDB > > :: simple ( ) ;
504+ let faketime_file = faketime:: millis_tempfile ( 8102 ) . expect ( "create faketime file" ) ;
505+ faketime:: enable ( & faketime_file) ;
506+
507+ let tx = prepare_trace ( & mut pool, & faketime_file) ;
508+ let tx_hash = tx. hash ( ) ;
509+
510+ let trace = pool. service . get_transaction_traces ( & tx_hash) ;
511+ match trace. map ( |t| t. as_slice ( ) ) {
512+ Some (
513+ [ TxTrace {
514+ action : Action :: AddPending ,
515+ time : 8102 ,
516+ ..
517+ } , TxTrace {
518+ action : Action :: Proposed ,
519+ info : proposal_info,
520+ time : 9102 ,
521+ } , TxTrace {
522+ action : Action :: AddCommit ,
523+ time : 9102 ,
524+ ..
525+ } ] ,
526+ ) => assert_eq ! (
527+ proposal_info,
528+ concat!( "ProposalShortId(0xda495f694cac79513d00) proposed " ,
529+ "in block number(2)-hash(0xb42c5305777987f80112e862a3e722c1d0e68c671f1d8920d16ebfc6783a6467)" )
530+ ) ,
531+ _ => assert ! ( false ) ,
532+ }
533+
534+ faketime:: write_millis ( & faketime_file, 9103 ) . expect ( "write millis" ) ;
535+ let block = apply_transactions ( vec ! [ tx. clone( ) ] , vec ! [ ] , & mut pool) ;
536+ let trace = pool. service . get_transaction_traces ( & tx_hash) ;
537+ match trace. map ( |t| t. as_slice ( ) ) {
538+ Some (
539+ [ TxTrace {
540+ action : Action :: AddPending ,
541+ time : 8102 ,
542+ ..
543+ } , TxTrace {
544+ action : Action :: Proposed ,
545+ time : 9102 ,
546+ ..
547+ } , TxTrace {
548+ action : Action :: AddCommit ,
549+ time : 9102 ,
550+ ..
551+ } , TxTrace {
552+ action : Action :: Committed ,
553+ info : committed_info,
554+ time : 9103 ,
555+ } ] ,
556+ ) => assert_eq ! (
557+ committed_info,
558+ & format!(
559+ "committed in block number({:?})-hash({:#x})" ,
560+ block. header( ) . number( ) ,
561+ block. header( ) . hash( )
562+ )
563+ ) ,
564+ _ => assert ! ( false ) ,
565+ }
566+ }
567+
472568struct TestPool < CI > {
473569 service : TransactionPoolService < CI > ,
474570 chain : ChainController ,
@@ -500,6 +596,7 @@ impl<CI: ChainIndex + 'static> TestPool<CI> {
500596 max_proposal_size : 1000 ,
501597 max_cache_size : 1000 ,
502598 max_pending_size : 1000 ,
599+ trace : Some ( 100 ) ,
503600 } ,
504601 shared. clone ( ) ,
505602 notify. clone ( ) ,
0 commit comments