Skip to content

Commit

Permalink
Introduce clear_gap API for Snap sync downstream
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin authored and nazar-pc committed Aug 12, 2024
1 parent 9897ffa commit d537512
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions substrate/client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ impl<Block: BlockT> blockchain::Backend<Block> for Blockchain<Block> {
) -> sp_blockchain::Result<Option<Vec<Vec<u8>>>> {
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<Block: BlockT> backend::AuxStore for Blockchain<Block> {
Expand Down
13 changes: 13 additions & 0 deletions substrate/client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,18 @@ impl<Block: BlockT> sc_client_api::blockchain::Backend<Block> for BlockchainDb<B
Err(sp_blockchain::Error::Backend(format!("Error decoding body list: {}", err))),
}
}

fn clear_block_gap(&self) -> 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<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> {
Expand Down Expand Up @@ -1768,6 +1780,7 @@ impl<Block: BlockT> Backend<Block> {
for m in meta_updates {
self.blockchain.update_meta(m);
}

self.blockchain.update_block_gap(block_gap);

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion substrate/client/network/sync/src/state_request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
pub fn generate_protocol_name<Hash: AsRef<[u8]>>(
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)
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/sync/src/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions substrate/client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ where
_phantom: PhantomData<RA>,
}

impl<B, E, Block, RA> crate::ClientExt<Block, B> for Client<B, E, Block, RA>
where
B: backend::Backend<Block>,
E: CallExecutor<Block> + 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<H> {
Expand Down
10 changes: 9 additions & 1 deletion substrate/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -100,6 +102,12 @@ const DEFAULT_PROTOCOL_ID: &str = "sup";
#[derive(Clone)]
pub struct RpcHandlers(Arc<RpcModule<()>>);

/// Provides extended functions for `Client` to enable fast-sync.
pub trait ClientExt<Block: BlockT, B: backend::Backend<Block>> {
/// Clear block gap after initial block insertion.
fn clear_block_gap(&self) -> sp_blockchain::Result<()>;
}

impl RpcHandlers {
/// Starts an RPC query.
///
Expand Down
3 changes: 3 additions & 0 deletions substrate/primitives/blockchain/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ pub trait Backend<Block: BlockT>:

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`].
Expand Down

0 comments on commit d537512

Please sign in to comment.