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

Commit 9ce0774

Browse files
bkchrNikVolf
andauthored
Make transaction pool prune transactions only of canonical blocks (#6123)
* Make tx pool aware of retracted fork blocks * Make it compile * Update client/transaction-pool/src/lib.rs Co-authored-by: Nikolay Volf <nikvolf@gmail.com> * Fix doc test * Simplify the implementation * Send tree route as arc to prevent heavy clones * Switch to use `ExtrinsicHash` to make it more clear * Fix benchmark Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
1 parent 252b146 commit 9ce0774

File tree

31 files changed

+800
-351
lines changed

31 files changed

+800
-351
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/node/src/service.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ macro_rules! new_full_start {
3434
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
3535

3636
let builder = sc_service::ServiceBuilder::new_full::<
37-
node_template_runtime::opaque::Block, node_template_runtime::RuntimeApi, crate::service::Executor
37+
node_template_runtime::opaque::Block,
38+
node_template_runtime::RuntimeApi,
39+
crate::service::Executor
3840
>($config)?
3941
.with_select_chain(|_config, backend| {
4042
Ok(sc_consensus::LongestChain::new(backend.clone()))
4143
})?
42-
.with_transaction_pool(|config, client, _fetcher, prometheus_registry| {
43-
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
44-
Ok(sc_transaction_pool::BasicPool::new(config, std::sync::Arc::new(pool_api), prometheus_registry))
44+
.with_transaction_pool(|builder| {
45+
let pool_api = sc_transaction_pool::FullChainApi::new(
46+
builder.client().clone(),
47+
);
48+
Ok(sc_transaction_pool::BasicPool::new(
49+
builder.config().transaction_pool.clone(),
50+
std::sync::Arc::new(pool_api),
51+
builder.prometheus_registry(),
52+
))
4553
})?
4654
.with_import_queue(|
4755
_config,
@@ -199,13 +207,19 @@ pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceE
199207
.with_select_chain(|_config, backend| {
200208
Ok(LongestChain::new(backend.clone()))
201209
})?
202-
.with_transaction_pool(|config, client, fetcher, prometheus_registry| {
203-
let fetcher = fetcher
210+
.with_transaction_pool(|builder| {
211+
let fetcher = builder.fetcher()
204212
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
205213

206-
let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone());
214+
let pool_api = sc_transaction_pool::LightChainApi::new(
215+
builder.client().clone(),
216+
fetcher.clone(),
217+
);
207218
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
208-
config, Arc::new(pool_api), prometheus_registry, sc_transaction_pool::RevalidationType::Light,
219+
builder.config().transaction_pool.clone(),
220+
Arc::new(pool_api),
221+
builder.prometheus_registry(),
222+
sc_transaction_pool::RevalidationType::Light,
209223
);
210224
Ok(pool)
211225
})?

bin/node/cli/src/service.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ macro_rules! new_full_start {
5353
.with_select_chain(|_config, backend| {
5454
Ok(sc_consensus::LongestChain::new(backend.clone()))
5555
})?
56-
.with_transaction_pool(|config, client, _fetcher, prometheus_registry| {
57-
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
56+
.with_transaction_pool(|builder| {
57+
let pool_api = sc_transaction_pool::FullChainApi::new(
58+
builder.client().clone(),
59+
);
60+
let config = builder.config();
61+
5862
Ok(sc_transaction_pool::BasicPool::new(
59-
config,
63+
config.transaction_pool.clone(),
6064
std::sync::Arc::new(pool_api),
61-
prometheus_registry,
65+
builder.prometheus_registry(),
6266
))
6367
})?
6468
.with_import_queue(|
@@ -323,12 +327,18 @@ pub fn new_light(config: Configuration)
323327
.with_select_chain(|_config, backend| {
324328
Ok(LongestChain::new(backend.clone()))
325329
})?
326-
.with_transaction_pool(|config, client, fetcher, prometheus_registry| {
327-
let fetcher = fetcher
330+
.with_transaction_pool(|builder| {
331+
let fetcher = builder.fetcher()
328332
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
329-
let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone());
333+
let pool_api = sc_transaction_pool::LightChainApi::new(
334+
builder.client().clone(),
335+
fetcher,
336+
);
330337
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
331-
config, Arc::new(pool_api), prometheus_registry, sc_transaction_pool::RevalidationType::Light,
338+
builder.config().transaction_pool.clone(),
339+
Arc::new(pool_api),
340+
builder.prometheus_registry(),
341+
sc_transaction_pool::RevalidationType::Light,
332342
);
333343
Ok(pool)
334344
})?
@@ -481,7 +491,7 @@ mod tests {
481491
ChainEvent::NewBlock {
482492
is_new_best: true,
483493
id: parent_id.clone(),
484-
retracted: vec![],
494+
tree_route: None,
485495
header: parent_header.clone(),
486496
},
487497
)

client/api/src/backend.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ pub struct ImportSummary<Block: BlockT> {
6767
pub is_new_best: bool,
6868
/// Optional storage changes.
6969
pub storage_changes: Option<(StorageCollection, ChildStorageCollection)>,
70-
/// Blocks that got retracted because of this one got imported.
71-
pub retracted: Vec<Block::Hash>,
70+
/// Tree route from old best to new best.
71+
///
72+
/// If `None`, there was no re-org while importing.
73+
pub tree_route: Option<sp_blockchain::TreeRoute<Block>>,
7274
}
7375

7476
/// Import operation wrapper

client/api/src/client.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
//! A set of APIs supported by the client along with their primitives.
1818
19-
use std::{fmt, collections::HashSet};
19+
use std::{fmt, collections::HashSet, sync::Arc};
2020
use sp_core::storage::StorageKey;
2121
use sp_runtime::{
2222
traits::{Block as BlockT, NumberFor},
@@ -234,8 +234,10 @@ pub struct BlockImportNotification<Block: BlockT> {
234234
pub header: Block::Header,
235235
/// Is this the new best block.
236236
pub is_new_best: bool,
237-
/// List of retracted blocks ordered by block number.
238-
pub retracted: Vec<Block::Hash>,
237+
/// Tree route from old best to new best.
238+
///
239+
/// If `None`, there was no re-org while importing.
240+
pub tree_route: Option<Arc<sp_blockchain::TreeRoute<Block>>>,
239241
}
240242

241243
/// Summary of a finalized block.

client/basic-authorship/src/basic_authorship.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,14 @@ mod tests {
331331
use parking_lot::Mutex;
332332
use sp_consensus::{BlockOrigin, Proposer};
333333
use substrate_test_runtime_client::{
334-
prelude::*,
335-
runtime::{Extrinsic, Transfer},
334+
prelude::*, TestClientBuilder, runtime::{Extrinsic, Transfer}, TestClientBuilderExt,
336335
};
337336
use sp_transaction_pool::{ChainEvent, MaintainedTransactionPool, TransactionSource};
338337
use sc_transaction_pool::{BasicPool, FullChainApi};
339338
use sp_api::Core;
340-
use backend::Backend;
341339
use sp_blockchain::HeaderBackend;
342340
use sp_runtime::traits::NumberFor;
341+
use sc_client_api::Backend;
343342

344343
const SOURCE: TransactionSource = TransactionSource::External;
345344

@@ -357,7 +356,7 @@ mod tests {
357356
{
358357
ChainEvent::NewBlock {
359358
id: BlockId::Number(block_number.into()),
360-
retracted: vec![],
359+
tree_route: None,
361360
is_new_best: true,
362361
header,
363362
}
@@ -452,8 +451,7 @@ mod tests {
452451

453452
#[test]
454453
fn proposed_storage_changes_should_match_execute_block_storage_changes() {
455-
let (client, backend) = substrate_test_runtime_client::TestClientBuilder::new()
456-
.build_with_backend();
454+
let (client, backend) = TestClientBuilder::new().build_with_backend();
457455
let client = Arc::new(client);
458456
let txpool = Arc::new(
459457
BasicPool::new(
@@ -473,7 +471,9 @@ mod tests {
473471
futures::executor::block_on(
474472
txpool.maintain(chain_event(
475473
0,
476-
client.header(&BlockId::Number(0u64)).expect("header get error").expect("there should be header")
474+
client.header(&BlockId::Number(0u64))
475+
.expect("header get error")
476+
.expect("there should be header"),
477477
))
478478
);
479479

@@ -500,8 +500,11 @@ mod tests {
500500
backend.changes_trie_storage(),
501501
).unwrap();
502502

503-
let storage_changes = api.into_storage_changes(&state, changes_trie_state.as_ref(), genesis_hash)
504-
.unwrap();
503+
let storage_changes = api.into_storage_changes(
504+
&state,
505+
changes_trie_state.as_ref(),
506+
genesis_hash,
507+
).unwrap();
505508

506509
assert_eq!(
507510
proposal.storage_changes.transaction_storage_root,

client/basic-authorship/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
//! # use sp_consensus::{Environment, Proposer, RecordProof};
2626
//! # use sp_runtime::generic::BlockId;
2727
//! # use std::{sync::Arc, time::Duration};
28-
//! # use substrate_test_runtime_client::{self, runtime::{Extrinsic, Transfer}, AccountKeyring};
28+
//! # use substrate_test_runtime_client::{
29+
//! # runtime::{Extrinsic, Transfer}, AccountKeyring,
30+
//! # DefaultTestClientBuilderExt, TestClientBuilderExt,
31+
//! # };
2932
//! # use sc_transaction_pool::{BasicPool, FullChainApi};
3033
//! # let client = Arc::new(substrate_test_runtime_client::new());
3134
//! # let txpool = Arc::new(BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone())), None).0);

client/consensus/manual-seal/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub async fn run_manual_seal<B, CB, E, C, A, SC, S, T>(
9898
inherent_data_providers: InherentDataProviders,
9999
)
100100
where
101-
A: txpool::ChainApi<Block=B, Hash=<B as BlockT>::Hash> + 'static,
101+
A: txpool::ChainApi<Block=B> + 'static,
102102
B: BlockT + 'static,
103103
C: HeaderBackend<B> + Finalizer<B, CB> + 'static,
104104
CB: ClientBackend<B> + 'static,
@@ -158,7 +158,7 @@ pub async fn run_instant_seal<B, CB, E, C, A, SC, T>(
158158
inherent_data_providers: InherentDataProviders,
159159
)
160160
where
161-
A: txpool::ChainApi<Block=B, Hash=<B as BlockT>::Hash> + 'static,
161+
A: txpool::ChainApi<Block=B> + 'static,
162162
B: BlockT + 'static,
163163
C: HeaderBackend<B> + Finalizer<B, CB> + 'static,
164164
CB: ClientBackend<B> + 'static,
@@ -417,7 +417,7 @@ mod tests {
417417
id: BlockId::Number(1),
418418
header: client.header(&BlockId::Number(1)).expect("db error").expect("imported above"),
419419
is_new_best: true,
420-
retracted: vec![],
420+
tree_route: None,
421421
}).await;
422422

423423
let (tx1, rx1) = futures::channel::oneshot::channel();

client/consensus/manual-seal/src/seal_new_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub async fn seal_new_block<B, SC, HB, E, T, P>(
8787
E: Environment<B>,
8888
<E as Environment<B>>::Error: std::fmt::Display,
8989
<E::Proposer as Proposer<B>>::Error: std::fmt::Display,
90-
P: txpool::ChainApi<Block=B, Hash=<B as BlockT>::Hash>,
90+
P: txpool::ChainApi<Block=B>,
9191
SC: SelectChain<B>,
9292
{
9393
let future = async {

client/finality-grandpa/src/until_imported.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ mod tests {
585585
origin: BlockOrigin::File,
586586
header,
587587
is_new_best: false,
588-
retracted: vec![],
588+
tree_route: None,
589589
}).unwrap();
590590
}
591591
}

0 commit comments

Comments
 (0)