@@ -58,13 +58,15 @@ pub enum RevalidationType {
58
58
/// Light revalidation type.
59
59
///
60
60
/// During maintaince, transaction pool makes periodic revalidation
61
- /// depending on number of blocks or time passed.
61
+ /// of all transactions depending on number of blocks or time passed.
62
+ /// Also this kind of revalidation does not resubmit transactions from
63
+ /// retracted blocks, since it is too expensive.
62
64
Light ,
63
65
64
66
/// Full revalidation type.
65
67
///
66
- /// During maintaince, transaction pool revalidates some transactions
67
- /// from the pool of valid transactions.
68
+ /// During maintaince, transaction pool revalidates some fixed amount of
69
+ /// transactions from the pool of valid transactions.
68
70
Full ,
69
71
}
70
72
@@ -191,30 +193,38 @@ enum RevalidationStrategy<N> {
191
193
Light ( RevalidationStatus < N > )
192
194
}
193
195
196
+ struct RevalidationAction {
197
+ revalidate : bool ,
198
+ resubmit : bool ,
199
+ revalidate_amount : Option < usize > ,
200
+ }
201
+
194
202
impl < N : Clone + Copy + SimpleArithmetic > RevalidationStrategy < N > {
195
203
pub fn clear ( & mut self ) {
196
204
if let Self :: Light ( status) = self { status. clear ( ) }
197
205
}
198
206
199
- pub fn resubmit_required ( & mut self ) -> bool {
200
- if let Self :: Light ( _) = self { return false } else { return true }
201
- }
202
-
203
- pub fn is_required (
207
+ pub fn next (
204
208
& mut self ,
205
209
block : N ,
206
210
revalidate_time_period : Option < std:: time:: Duration > ,
207
211
revalidate_block_period : Option < N > ,
208
- ) -> bool {
209
- if let Self :: Light ( status) = self {
210
- status. is_required ( block, revalidate_time_period, revalidate_block_period)
211
- } else { true }
212
- }
213
-
214
- pub fn amount ( & self ) -> Option < usize > {
212
+ ) -> RevalidationAction {
215
213
match self {
216
- Self :: Light ( _) => None ,
217
- Self :: Always => Some ( 16 ) ,
214
+ Self :: Light ( status) => RevalidationAction {
215
+ revalidate : status. next_required (
216
+ block,
217
+ revalidate_time_period,
218
+ revalidate_block_period
219
+ ) ,
220
+ resubmit : false ,
221
+ revalidate_amount : None ,
222
+ } ,
223
+ Self :: Always => RevalidationAction {
224
+ revalidate : true ,
225
+ resubmit : true ,
226
+ revalidate_amount : Some ( 16 ) ,
227
+ }
218
228
}
219
229
}
220
230
}
@@ -226,7 +236,7 @@ impl<N: Clone + Copy + SimpleArithmetic> RevalidationStatus<N> {
226
236
}
227
237
228
238
/// Returns true if revalidation is required.
229
- pub fn is_required (
239
+ pub fn next_required (
230
240
& mut self ,
231
241
block : N ,
232
242
revalidate_time_period : Option < std:: time:: Duration > ,
@@ -258,7 +268,9 @@ where
258
268
Block : BlockT ,
259
269
PoolApi : ' static + sc_transaction_graph:: ChainApi < Block =Block , Hash =Block :: Hash , Error =error:: Error > ,
260
270
{
261
- fn maintain ( & self , id : & BlockId < Self :: Block > , retracted : & [ BlockHash < Self > ] ) -> Pin < Box < dyn Future < Output =( ) > + Send > > {
271
+ fn maintain ( & self , id : & BlockId < Self :: Block > , retracted : & [ BlockHash < Self > ] )
272
+ -> Pin < Box < dyn Future < Output =( ) > + Send > >
273
+ {
262
274
let id = id. clone ( ) ;
263
275
let pool = self . pool . clone ( ) ;
264
276
let api = self . api . clone ( ) ;
@@ -271,20 +283,12 @@ where
271
283
}
272
284
} ;
273
285
274
- let ( is_revalidation_required, is_resubmit_required) = {
275
- let mut lock = self . revalidation_strategy . lock ( ) ;
276
- (
277
- lock. is_required (
278
- block_number,
279
- Some ( std:: time:: Duration :: from_secs ( 60 ) ) ,
280
- Some ( 20 . into ( ) ) ,
281
- ) ,
282
- lock. resubmit_required ( )
283
- )
284
- } ;
285
-
286
- let revalidation_status = self . revalidation_strategy . clone ( ) ;
287
- let revalidation_amount = revalidation_status. lock ( ) . amount ( ) ;
286
+ let next_action = self . revalidation_strategy . lock ( ) . next (
287
+ block_number,
288
+ Some ( std:: time:: Duration :: from_secs ( 60 ) ) ,
289
+ Some ( 20 . into ( ) ) ,
290
+ ) ;
291
+ let revalidation_strategy = self . revalidation_strategy . clone ( ) ;
288
292
let retracted = retracted. to_vec ( ) ;
289
293
290
294
async move {
@@ -293,25 +297,27 @@ where
293
297
log:: warn!( "Prune known transactions: error request {:?}!" , e) ;
294
298
None
295
299
} )
296
- . unwrap_or ( Vec :: new ( ) )
300
+ . unwrap_or_default ( )
297
301
. into_iter ( )
298
302
. map ( |tx| pool. hash_of ( & tx) )
299
303
. collect :: < Vec < _ > > ( ) ;
300
304
301
305
if let Err ( e) = pool. prune_known ( & id, & hashes) {
302
- log:: warn !( "Cannot prune known in the pool {:?}!" , e) ;
306
+ log:: error !( "Cannot prune known in the pool {:?}!" , e) ;
303
307
}
304
308
305
- if is_resubmit_required {
309
+ if next_action . resubmit {
306
310
let mut resubmit_transactions = Vec :: new ( ) ;
307
311
308
- for retracted_hash in retracted. into_iter ( ) {
309
- let txes = api. block_body ( & BlockId :: hash ( retracted_hash. clone ( ) ) ) . await
310
- . unwrap_or ( None )
311
- . unwrap_or ( Vec :: new ( ) ) ;
312
- for tx in txes {
313
- resubmit_transactions. push ( tx)
314
- }
312
+ for retracted_hash in retracted {
313
+ let block_transactions = api. block_body ( & BlockId :: hash ( retracted_hash. clone ( ) ) ) . await
314
+ . unwrap_or_else ( |e| {
315
+ log:: warn!( "Failed to fetch block body {:?}!" , e) ;
316
+ None
317
+ } )
318
+ . unwrap_or_default ( ) ;
319
+
320
+ resubmit_transactions. extend ( block_transactions) ;
315
321
}
316
322
if let Err ( e) = pool. submit_at ( & id, resubmit_transactions, true ) . await {
317
323
log:: debug!( target: "txpool" ,
@@ -320,13 +326,13 @@ where
320
326
}
321
327
}
322
328
323
- if is_revalidation_required {
324
- if let Err ( e) = pool. revalidate_ready ( & id, revalidation_amount ) . await {
325
- log:: warn!( "revalidate ready failed {:?}" , e) ;
329
+ if next_action . revalidate {
330
+ if let Err ( e) = pool. revalidate_ready ( & id, next_action . revalidate_amount ) . await {
331
+ log:: warn!( "Revalidate ready failed {:?}" , e) ;
326
332
}
327
333
}
328
334
329
- revalidation_status . lock ( ) . clear ( ) ;
335
+ revalidation_strategy . lock ( ) . clear ( ) ;
330
336
} . boxed ( )
331
337
}
332
338
}
0 commit comments