Skip to content

Commit d537512

Browse files
shamil-gadelshinnazar-pc
authored andcommitted
Introduce clear_gap API for Snap sync downstream
1 parent 9897ffa commit d537512

7 files changed

Lines changed: 46 additions & 3 deletions

File tree

substrate/client/api/src/in_mem.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ impl<Block: BlockT> blockchain::Backend<Block> for Blockchain<Block> {
433433
) -> sp_blockchain::Result<Option<Vec<Vec<u8>>>> {
434434
unimplemented!("Not supported by the in-mem backend.")
435435
}
436+
437+
fn clear_block_gap(&self) -> sp_blockchain::Result<()> {
438+
unimplemented!("Not supported by the in-mem backend.")
439+
}
436440
}
437441

438442
impl<Block: BlockT> backend::AuxStore for Blockchain<Block> {

substrate/client/db/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,18 @@ impl<Block: BlockT> sc_client_api::blockchain::Backend<Block> for BlockchainDb<B
790790
Err(sp_blockchain::Error::Backend(format!("Error decoding body list: {}", err))),
791791
}
792792
}
793+
794+
fn clear_block_gap(&self) -> ClientResult<()> {
795+
debug!(target: "sync", "Clear block gap.");
796+
797+
let mut transaction = Transaction::new();
798+
transaction.remove(columns::META, meta_keys::BLOCK_GAP);
799+
self.db.commit(transaction)?;
800+
801+
self.update_block_gap(None);
802+
803+
Ok(())
804+
}
793805
}
794806

795807
impl<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> {
@@ -1768,6 +1780,7 @@ impl<Block: BlockT> Backend<Block> {
17681780
for m in meta_updates {
17691781
self.blockchain.update_meta(m);
17701782
}
1783+
17711784
self.blockchain.update_block_gap(block_gap);
17721785

17731786
Ok(())

substrate/client/network/sync/src/state_request_handler.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ pub fn generate_protocol_config<
7676
}
7777

7878
/// Generate the state protocol name from the genesis hash and fork id.
79-
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
79+
pub fn generate_protocol_name<Hash: AsRef<[u8]>>(
80+
genesis_hash: Hash,
81+
fork_id: Option<&str>,
82+
) -> String {
8083
let genesis_hash = genesis_hash.as_ref();
8184
if let Some(fork_id) = fork_id {
8285
format!("/{}/{}/state/2", array_bytes::bytes2hex("", genesis_hash), fork_id)

substrate/client/network/sync/src/strategy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! and specific syncing algorithms.
2121
2222
pub mod chain_sync;
23-
mod state;
23+
pub(crate) mod state;
2424
pub mod state_sync;
2525
pub mod warp;
2626

substrate/client/service/src/client/client.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ where
119119
_phantom: PhantomData<RA>,
120120
}
121121

122+
impl<B, E, Block, RA> crate::ClientExt<Block, B> for Client<B, E, Block, RA>
123+
where
124+
B: backend::Backend<Block>,
125+
E: CallExecutor<Block> + Send + Sync,
126+
Block: BlockT,
127+
RA: Sync + Send,
128+
{
129+
fn clear_block_gap(&self) -> sp_blockchain::Result<()> {
130+
self.backend.blockchain().clear_block_gap()
131+
}
132+
}
133+
122134
/// Used in importing a block, where additional changes are made after the runtime
123135
/// executed.
124136
enum PrePostHeader<H> {

substrate/client/service/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ use codec::{Decode, Encode};
4040
use futures::{pin_mut, FutureExt, StreamExt};
4141
use jsonrpsee::RpcModule;
4242
use log::{debug, error, warn};
43-
use sc_client_api::{blockchain::HeaderBackend, BlockBackend, BlockchainEvents, ProofProvider};
43+
use sc_client_api::{
44+
backend, blockchain::HeaderBackend, BlockBackend, BlockchainEvents, ProofProvider,
45+
};
4446
use sc_network::{
4547
config::MultiaddrWithPeerId, service::traits::NetworkService, NetworkBackend, NetworkBlock,
4648
NetworkPeers, NetworkStateInfo,
@@ -100,6 +102,12 @@ const DEFAULT_PROTOCOL_ID: &str = "sup";
100102
#[derive(Clone)]
101103
pub struct RpcHandlers(Arc<RpcModule<()>>);
102104

105+
/// Provides extended functions for `Client` to enable fast-sync.
106+
pub trait ClientExt<Block: BlockT, B: backend::Backend<Block>> {
107+
/// Clear block gap after initial block insertion.
108+
fn clear_block_gap(&self) -> sp_blockchain::Result<()>;
109+
}
110+
103111
impl RpcHandlers {
104112
/// Starts an RPC query.
105113
///

substrate/primitives/blockchain/src/backend.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ pub trait Backend<Block: BlockT>:
514514

515515
return Ok(result);
516516
}
517+
518+
/// Clears the block gap from DB after the fast-sync.
519+
fn clear_block_gap(&self) -> Result<()>;
517520
}
518521

519522
/// Result of [`Backend::displaced_leaves_after_finalizing`].

0 commit comments

Comments
 (0)