Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit cada84b

Browse files
committed
review fixes
1 parent 3e84acc commit cada84b

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

client/transaction-pool/graph/src/pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub trait ChainApi: Send + Sync {
8787
/// Returns hash and encoding length of the extrinsic.
8888
fn hash_and_length(&self, uxt: &ExtrinsicFor<Self>) -> (Self::Hash, usize);
8989

90-
/// Return body
90+
/// Returns a block body given the block id.
9191
fn block_body(&self, at: &BlockId<Self::Block>) -> Self::BodyFuture;
9292
}
9393

client/transaction-pool/src/lib.rs

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ pub enum RevalidationType {
5858
/// Light revalidation type.
5959
///
6060
/// 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.
6264
Light,
6365

6466
/// Full revalidation type.
6567
///
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.
6870
Full,
6971
}
7072

@@ -191,30 +193,38 @@ enum RevalidationStrategy<N> {
191193
Light(RevalidationStatus<N>)
192194
}
193195

196+
struct RevalidationAction {
197+
revalidate: bool,
198+
resubmit: bool,
199+
revalidate_amount: Option<usize>,
200+
}
201+
194202
impl<N: Clone + Copy + SimpleArithmetic> RevalidationStrategy<N> {
195203
pub fn clear(&mut self) {
196204
if let Self::Light(status) = self { status.clear() }
197205
}
198206

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(
204208
&mut self,
205209
block: N,
206210
revalidate_time_period: Option<std::time::Duration>,
207211
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 {
215213
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+
}
218228
}
219229
}
220230
}
@@ -226,7 +236,7 @@ impl<N: Clone + Copy + SimpleArithmetic> RevalidationStatus<N> {
226236
}
227237

228238
/// Returns true if revalidation is required.
229-
pub fn is_required(
239+
pub fn next_required(
230240
&mut self,
231241
block: N,
232242
revalidate_time_period: Option<std::time::Duration>,
@@ -258,7 +268,9 @@ where
258268
Block: BlockT,
259269
PoolApi: 'static + sc_transaction_graph::ChainApi<Block=Block, Hash=Block::Hash, Error=error::Error>,
260270
{
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+
{
262274
let id = id.clone();
263275
let pool = self.pool.clone();
264276
let api = self.api.clone();
@@ -271,20 +283,12 @@ where
271283
}
272284
};
273285

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();
288292
let retracted = retracted.to_vec();
289293

290294
async move {
@@ -293,25 +297,27 @@ where
293297
log::warn!("Prune known transactions: error request {:?}!", e);
294298
None
295299
})
296-
.unwrap_or(Vec::new())
300+
.unwrap_or_default()
297301
.into_iter()
298302
.map(|tx| pool.hash_of(&tx))
299303
.collect::<Vec<_>>();
300304

301305
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);
303307
}
304308

305-
if is_resubmit_required {
309+
if next_action.resubmit {
306310
let mut resubmit_transactions = Vec::new();
307311

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);
315321
}
316322
if let Err(e) = pool.submit_at(&id, resubmit_transactions, true).await {
317323
log::debug!(target: "txpool",
@@ -320,13 +326,13 @@ where
320326
}
321327
}
322328

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);
326332
}
327333
}
328334

329-
revalidation_status.lock().clear();
335+
revalidation_strategy.lock().clear();
330336
}.boxed()
331337
}
332338
}

client/transaction-pool/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ fn should_correctly_prune_transactions_providing_more_than_one_tag() {
274274
}
275275

276276
#[test]
277-
fn maintaince_prune() {
277+
fn should_prune_old_during_maintenance() {
278278
let xt = uxt(Alice, 209);
279279

280280
let pool = maintained_pool();
@@ -290,7 +290,7 @@ fn maintaince_prune() {
290290

291291

292292
#[test]
293-
fn maintaince_revalidate() {
293+
fn should_revalidate_during_maintenance() {
294294
let xt1 = uxt(Alice, 209);
295295
let xt2 = uxt(Alice, 210);
296296

0 commit comments

Comments
 (0)