From d5375125ca7c59fcc1ac72dc1b2ac251cbc80323 Mon Sep 17 00:00:00 2001 From: Shamil Gadelshin Date: Tue, 7 May 2024 14:49:18 +0300 Subject: [PATCH] Introduce clear_gap API for Snap sync downstream --- substrate/client/api/src/in_mem.rs | 4 ++++ substrate/client/db/src/lib.rs | 13 +++++++++++++ .../network/sync/src/state_request_handler.rs | 5 ++++- substrate/client/network/sync/src/strategy.rs | 2 +- substrate/client/service/src/client/client.rs | 12 ++++++++++++ substrate/client/service/src/lib.rs | 10 +++++++++- substrate/primitives/blockchain/src/backend.rs | 3 +++ 7 files changed, 46 insertions(+), 3 deletions(-) diff --git a/substrate/client/api/src/in_mem.rs b/substrate/client/api/src/in_mem.rs index ba89aede9147..8bae4bc65a62 100644 --- a/substrate/client/api/src/in_mem.rs +++ b/substrate/client/api/src/in_mem.rs @@ -433,6 +433,10 @@ impl blockchain::Backend for Blockchain { ) -> sp_blockchain::Result>>> { unimplemented!("Not supported by the in-mem backend.") } + + fn clear_block_gap(&self) -> sp_blockchain::Result<()> { + unimplemented!("Not supported by the in-mem backend.") + } } impl backend::AuxStore for Blockchain { diff --git a/substrate/client/db/src/lib.rs b/substrate/client/db/src/lib.rs index ba0cbc09d53d..b64552f78f51 100644 --- a/substrate/client/db/src/lib.rs +++ b/substrate/client/db/src/lib.rs @@ -790,6 +790,18 @@ impl sc_client_api::blockchain::Backend for BlockchainDb ClientResult<()> { + debug!(target: "sync", "Clear block gap."); + + let mut transaction = Transaction::new(); + transaction.remove(columns::META, meta_keys::BLOCK_GAP); + self.db.commit(transaction)?; + + self.update_block_gap(None); + + Ok(()) + } } impl HeaderMetadata for BlockchainDb { @@ -1768,6 +1780,7 @@ impl Backend { for m in meta_updates { self.blockchain.update_meta(m); } + self.blockchain.update_block_gap(block_gap); Ok(()) diff --git a/substrate/client/network/sync/src/state_request_handler.rs b/substrate/client/network/sync/src/state_request_handler.rs index 0e713626ecaa..138fbd7b06d5 100644 --- a/substrate/client/network/sync/src/state_request_handler.rs +++ b/substrate/client/network/sync/src/state_request_handler.rs @@ -76,7 +76,10 @@ pub fn generate_protocol_config< } /// Generate the state protocol name from the genesis hash and fork id. -fn generate_protocol_name>(genesis_hash: Hash, fork_id: Option<&str>) -> String { +pub fn generate_protocol_name>( + genesis_hash: Hash, + fork_id: Option<&str>, +) -> String { let genesis_hash = genesis_hash.as_ref(); if let Some(fork_id) = fork_id { format!("/{}/{}/state/2", array_bytes::bytes2hex("", genesis_hash), fork_id) diff --git a/substrate/client/network/sync/src/strategy.rs b/substrate/client/network/sync/src/strategy.rs index d6b17c0063ec..5f2ad4eca098 100644 --- a/substrate/client/network/sync/src/strategy.rs +++ b/substrate/client/network/sync/src/strategy.rs @@ -20,7 +20,7 @@ //! and specific syncing algorithms. pub mod chain_sync; -mod state; +pub(crate) mod state; pub mod state_sync; pub mod warp; diff --git a/substrate/client/service/src/client/client.rs b/substrate/client/service/src/client/client.rs index a2c9212f7b9c..d68a4d010471 100644 --- a/substrate/client/service/src/client/client.rs +++ b/substrate/client/service/src/client/client.rs @@ -119,6 +119,18 @@ where _phantom: PhantomData, } +impl crate::ClientExt for Client +where + B: backend::Backend, + E: CallExecutor + Send + Sync, + Block: BlockT, + RA: Sync + Send, +{ + fn clear_block_gap(&self) -> sp_blockchain::Result<()> { + self.backend.blockchain().clear_block_gap() + } +} + /// Used in importing a block, where additional changes are made after the runtime /// executed. enum PrePostHeader { diff --git a/substrate/client/service/src/lib.rs b/substrate/client/service/src/lib.rs index 63be296d1b21..f54146a58979 100644 --- a/substrate/client/service/src/lib.rs +++ b/substrate/client/service/src/lib.rs @@ -40,7 +40,9 @@ use codec::{Decode, Encode}; use futures::{pin_mut, FutureExt, StreamExt}; use jsonrpsee::RpcModule; use log::{debug, error, warn}; -use sc_client_api::{blockchain::HeaderBackend, BlockBackend, BlockchainEvents, ProofProvider}; +use sc_client_api::{ + backend, blockchain::HeaderBackend, BlockBackend, BlockchainEvents, ProofProvider, +}; use sc_network::{ config::MultiaddrWithPeerId, service::traits::NetworkService, NetworkBackend, NetworkBlock, NetworkPeers, NetworkStateInfo, @@ -100,6 +102,12 @@ const DEFAULT_PROTOCOL_ID: &str = "sup"; #[derive(Clone)] pub struct RpcHandlers(Arc>); +/// Provides extended functions for `Client` to enable fast-sync. +pub trait ClientExt> { + /// Clear block gap after initial block insertion. + fn clear_block_gap(&self) -> sp_blockchain::Result<()>; +} + impl RpcHandlers { /// Starts an RPC query. /// diff --git a/substrate/primitives/blockchain/src/backend.rs b/substrate/primitives/blockchain/src/backend.rs index fd0c5795cbfd..b763135f7da4 100644 --- a/substrate/primitives/blockchain/src/backend.rs +++ b/substrate/primitives/blockchain/src/backend.rs @@ -514,6 +514,9 @@ pub trait Backend: return Ok(result); } + + /// Clears the block gap from DB after the fast-sync. + fn clear_block_gap(&self) -> Result<()>; } /// Result of [`Backend::displaced_leaves_after_finalizing`].