Skip to content

Commit

Permalink
Remove import_raw_block.
Browse files Browse the repository at this point in the history
  • Loading branch information
shamil-gadelshin committed May 1, 2024
1 parent 58eaa4c commit e85d5c1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 68 deletions.
54 changes: 2 additions & 52 deletions substrate/client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use super::block_rules::{BlockRules, LookupResult as BlockLookupResult};
use crate::client::notification_pinning::NotificationPinningWorker;
use log::{debug, error, info, trace, warn};
use log::{debug, info, trace, warn};
use parking_lot::{Mutex, RwLock};
use prometheus_endpoint::Registry;
use rand::Rng;
Expand Down Expand Up @@ -55,7 +55,6 @@ use sp_blockchain::{
};
use sp_consensus::{BlockOrigin, BlockStatus, Error as ConsensusError};

use codec::Encode;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedSender};
use sp_core::{
storage::{
Expand Down Expand Up @@ -86,7 +85,6 @@ use std::{
sync::Arc,
};

use crate::{ClientExt, RawBlockData};
#[cfg(feature = "test-helpers")]
use {
super::call_executor::LocalCallExecutor, sc_client_api::in_mem, sp_core::traits::CodeExecutor,
Expand Down Expand Up @@ -120,23 +118,7 @@ where
_phantom: PhantomData<RA>,
}

pub type BlockWeight = u128;

/// Write the cumulative chain-weight of a block ot aux storage.
fn write_block_weight<H: Encode, F, R>(block_hash: H, block_weight: BlockWeight, write_aux: F) -> R
where
F: FnOnce(&[(Vec<u8>, &[u8])]) -> R,
{
let key = block_weight_key(block_hash);
block_weight.using_encoded(|s| write_aux(&[(key, s)]))
}

/// The aux storage key used to store the block weight of the given block hash.
fn block_weight_key<H: Encode>(block_hash: H) -> Vec<u8> {
(b"block_weight", block_hash).encode()
}

impl<B, E, Block, RA> ClientExt<Block, B> for Client<B, E, Block, 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,
Expand All @@ -145,38 +127,6 @@ where
<Client<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api: CoreApi<Block> + ApiExt<Block>,
RA: Sync + Send,
{
fn import_raw_block(&self, raw_block: RawBlockData<Block>) -> Result<(), Error> {
let hash = raw_block.hash;
let number = *raw_block.header.number();
debug!("Importing raw block: {number:?} - {hash:?} ");

let mut import_block = BlockImportParams::new(BlockOrigin::FastSync, raw_block.header);
import_block.justifications = raw_block.justifications;
import_block.body = raw_block.block_body;
import_block.state_action = StateAction::Skip;
import_block.finalized = true;
import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
import_block.import_existing = false;

// Set zero block weight to allow the execution of the following blocks.
write_block_weight(hash, 0, |values| {
import_block
.auxiliary
.extend(values.iter().map(|(k, v)| (k.to_vec(), Some(v.to_vec()))))
});

let result = self
.lock_import_and_run(|operation| self.apply_block(operation, import_block, None))
.map_err(|e| {
error!("Error during importing of the raw block: {}", e);
ConsensusError::ClientImport(e.to_string())
})?;

debug!("Raw block imported: {number:?} - {hash:?}. Result: {result:?}");

Ok(())
}

/// Apply a checked and validated block to an operation.
fn apply_block(
&self,
Expand Down
17 changes: 1 addition & 16 deletions substrate/client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pub use sc_rpc::{
pub use sc_tracing::TracingReceiver;
pub use sc_transaction_pool::Options as TransactionPoolOptions;
pub use sc_transaction_pool_api::{error::IntoPoolError, InPoolTransaction, TransactionPool};
use sp_runtime::Justifications;
#[doc(hidden)]
pub use std::{ops::Deref, result::Result, sync::Arc};
pub use task_manager::{SpawnTaskHandle, Task, TaskManager, TaskRegistry, DEFAULT_GROUP_NAME};
Expand All @@ -101,23 +100,9 @@ const DEFAULT_PROTOCOL_ID: &str = "sup";
#[derive(Clone)]
pub struct RpcHandlers(Arc<RpcModule<()>>);

#[derive(Clone, Debug)]
/// Data container to insert the block into the BlockchainDb without checks.
pub struct RawBlockData<Block: BlockT> {
/// Block hash
pub hash: Block::Hash,
/// Block header
pub header: Block::Header,
/// Extrinsics of the block
pub block_body: Option<Vec<Block::Extrinsic>>,
/// Justifications of the block
pub justifications: Option<Justifications>,
}

/// Provides extended functions for `Client` to enable fast-sync.
pub trait ClientExt<Block: BlockT, B: backend::Backend<Block>> {
/// Insert block into BlockchainDb bypassing checks.
fn import_raw_block(&self, raw_block: RawBlockData<Block>) -> Result<(), sp_blockchain::Error>;
/// Apply a checked and validated block to an operation.
fn apply_block(
&self,
operation: &mut ClientImportOperation<Block, B>,
Expand Down

0 comments on commit e85d5c1

Please sign in to comment.