This repository was archived by the owner on Nov 6, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +56
-1
lines changed Expand file tree Collapse file tree 2 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,15 @@ impl txpool::Scoring<VerifiedTransaction> for NonceAndGasPrice {
108108 }
109109 }
110110
111+ // Always kick out non-local transactions in favour of local ones.
112+ if new. priority ( ) . is_local ( ) && !old. priority ( ) . is_local ( ) {
113+ return true ;
114+ }
115+ // And never kick out local transactions in favour of external ones.
116+ if !new. priority ( ) . is_local ( ) && old. priority . is_local ( ) {
117+ return false ;
118+ }
119+
111120 self . choose ( old, new) == txpool:: scoring:: Choice :: ReplaceOld
112121 }
113122}
@@ -119,6 +128,21 @@ mod tests {
119128 use pool:: tests:: tx:: { Tx , TxExt } ;
120129 use txpool:: Scoring ;
121130
131+ #[ test]
132+ fn should_replace_non_local_transaction_with_local_one ( ) {
133+ // given
134+ let scoring = NonceAndGasPrice ( PrioritizationStrategy :: GasPriceOnly ) ;
135+ let tx1 = Tx :: default ( ) . signed ( ) . verified ( ) ;
136+ let tx2 = {
137+ let mut tx = Tx :: default ( ) . signed ( ) . verified ( ) ;
138+ tx. priority = :: pool:: Priority :: Local ;
139+ tx
140+ } ;
141+
142+ assert ! ( scoring. should_replace( & tx1, & tx2) ) ;
143+ assert ! ( !scoring. should_replace( & tx2, & tx1) ) ;
144+ }
145+
122146 #[ test]
123147 fn should_calculate_score_correctly ( ) {
124148 // given
Original file line number Diff line number Diff line change @@ -766,4 +766,35 @@ fn should_reject_big_transaction() {
766766 verifier:: Transaction :: Local ( PendingTransaction :: new( big_tx, transaction:: Condition :: Timestamp ( 1000 ) . into( ) ) )
767767 ] ) ;
768768 assert_eq ! ( res, vec![ Err ( transaction:: Error :: TooBig ) ] ) ;
769- }
769+ }
770+
771+ #[ test]
772+ fn should_include_local_transaction_to_a_full_pool ( ) {
773+ // given
774+ let txq = TransactionQueue :: new (
775+ txpool:: Options {
776+ max_count : 1 ,
777+ max_per_sender : 2 ,
778+ max_mem_usage : 50
779+ } ,
780+ verifier:: Options {
781+ minimal_gas_price : 1 . into ( ) ,
782+ block_gas_limit : 1_000_000 . into ( ) ,
783+ tx_gas_limit : 1_000_000 . into ( ) ,
784+ } ,
785+ PrioritizationStrategy :: GasPriceOnly ,
786+ ) ;
787+ let tx1 = Tx :: gas_price ( 10_000 ) . signed ( ) . unverified ( ) ;
788+ let tx2 = Tx :: gas_price ( 1 ) . signed ( ) . local ( ) ;
789+
790+ let res = txq. import ( TestClient :: new ( ) . with_balance ( 1_000_000_000 ) , vec ! [ tx1] ) ;
791+ assert_eq ! ( res, vec![ Ok ( ( ) ) ] ) ;
792+ assert_eq ! ( txq. status( ) . status. transaction_count, 1 ) ;
793+
794+ // when
795+ let res = txq. import ( TestClient :: new ( ) , vec ! [ tx2] ) ;
796+ assert_eq ! ( res, vec![ Ok ( ( ) ) ] ) ;
797+
798+ // then
799+ assert_eq ! ( txq. status( ) . status. transaction_count, 1 ) ;
800+ }
You can’t perform that action at this time.
0 commit comments