Skip to content

Commit 3f6d4ee

Browse files
tgmichelashutoshvarma
authored andcommitted
Improve block import notification strategy (polkadot-evm#1030)
* Improve block import notification strategy * oops * taplo * clippy * Notify only when not major syncing
1 parent aad580c commit 3f6d4ee

File tree

11 files changed

+442
-8
lines changed

11 files changed

+442
-8
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/subs
7474
sc-telemetry = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
7575
sc-transaction-pool = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
7676
sc-transaction-pool-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
77+
sc-utils = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master" }
7778
# Substrate Primitive
7879
sp-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
7980
sp-block-builder = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

client/mapping-sync/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,30 @@ targets = ["x86_64-unknown-linux-gnu"]
1414
futures = "0.3.25"
1515
futures-timer = "3.0.1"
1616
log = "0.4.17"
17+
parking_lot = "0.12.1"
1718
# Substrate
1819
sc-client-api = { workspace = true }
1920
sp-api = { workspace = true }
2021
sp-blockchain = { workspace = true }
22+
sp-consensus = { workspace = true, features = ["default"] }
2123
sp-runtime = { workspace = true }
2224
# Frontier
2325
fc-db = { workspace = true }
2426
fc-storage = { workspace = true }
2527
fp-consensus = { workspace = true, features = ["default"] }
2628
fp-rpc = { workspace = true, features = ["default"] }
29+
sc-utils = { workspace = true }
30+
31+
[dev-dependencies]
32+
ethereum = { workspace = true, features = ["with-codec"] }
33+
ethereum-types = { workspace = true }
34+
tempfile = "3.3.0"
35+
tokio = { version = "1.24", features = ["sync"] }
36+
#Frontier
37+
fp-storage = { workspace = true, features = ["default"] }
38+
frontier-template-runtime = { workspace = true, features = ["default"] }
39+
# Substrate
40+
sc-block-builder = { workspace = true }
41+
sc-client-db = { workspace = true }
42+
sp-core = { workspace = true, features = ["default"] }
43+
substrate-test-runtime-client = { workspace = true }

client/mapping-sync/src/lib.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ use std::sync::Arc;
2929
use sc_client_api::backend::{Backend, StorageProvider};
3030
use sp_api::{ApiExt, ProvideRuntimeApi};
3131
use sp_blockchain::{Backend as _, HeaderBackend};
32+
use sp_consensus::SyncOracle;
3233
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero};
3334
// Frontier
3435
use fc_storage::OverrideHandle;
3536
use fp_consensus::{FindLogError, Hashes, Log, PostLog, PreLog};
3637
use fp_rpc::EthereumRuntimeRPCApi;
3738

39+
pub type EthereumBlockNotificationSinks<T> =
40+
parking_lot::Mutex<Vec<sc_utils::mpsc::TracingUnboundedSender<T>>>;
41+
42+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
43+
pub struct EthereumBlockNotification<Block: BlockT> {
44+
pub is_new_best: bool,
45+
pub hash: Block::Hash,
46+
}
47+
3848
pub fn sync_block<Block: BlockT, C, BE>(
3949
client: &C,
4050
overrides: Arc<OverrideHandle<Block>>,
@@ -160,6 +170,10 @@ pub fn sync_one_block<Block: BlockT, C, BE>(
160170
frontier_backend: &fc_db::Backend<Block>,
161171
sync_from: <Block::Header as HeaderT>::Number,
162172
strategy: SyncStrategy,
173+
sync_oracle: Arc<dyn SyncOracle + Send + Sync + 'static>,
174+
pubsub_notification_sinks: Arc<
175+
EthereumBlockNotificationSinks<EthereumBlockNotification<Block>>,
176+
>,
163177
) -> Result<bool, String>
164178
where
165179
C: ProvideRuntimeApi<Block>,
@@ -208,7 +222,6 @@ where
208222
frontier_backend
209223
.meta()
210224
.write_current_syncing_tips(current_syncing_tips)?;
211-
Ok(true)
212225
} else {
213226
if SyncStrategy::Parachain == strategy
214227
&& operating_header.number() > &client.info().best_number
@@ -221,8 +234,22 @@ where
221234
frontier_backend
222235
.meta()
223236
.write_current_syncing_tips(current_syncing_tips)?;
224-
Ok(true)
225237
}
238+
// Notify on import and remove closed channels.
239+
// Only notify when the node is node in major syncing.
240+
let sinks = &mut pubsub_notification_sinks.lock();
241+
sinks.retain(|sink| {
242+
if !sync_oracle.is_major_syncing() {
243+
let hash = operating_header.hash();
244+
let is_new_best = client.info().best_hash == hash;
245+
sink.unbounded_send(EthereumBlockNotification { is_new_best, hash })
246+
.is_ok()
247+
} else {
248+
// Remove from the pool if in major syncing.
249+
false
250+
}
251+
});
252+
Ok(true)
226253
}
227254

228255
pub fn sync_blocks<Block: BlockT, C, BE>(
@@ -233,6 +260,10 @@ pub fn sync_blocks<Block: BlockT, C, BE>(
233260
limit: usize,
234261
sync_from: <Block::Header as HeaderT>::Number,
235262
strategy: SyncStrategy,
263+
sync_oracle: Arc<dyn SyncOracle + Send + Sync + 'static>,
264+
pubsub_notification_sinks: Arc<
265+
EthereumBlockNotificationSinks<EthereumBlockNotification<Block>>,
266+
>,
236267
) -> Result<bool, String>
237268
where
238269
C: ProvideRuntimeApi<Block>,
@@ -251,6 +282,8 @@ where
251282
frontier_backend,
252283
sync_from,
253284
strategy,
285+
sync_oracle.clone(),
286+
pubsub_notification_sinks.clone(),
254287
)?;
255288
}
256289

0 commit comments

Comments
 (0)