consensus: remove caching functionality from block import pipeline (#13551)

* consensus: remove caching functionality from block import pipeline

* client: update docs on Verifier::verify

* node: fix block production benchmark
This commit is contained in:
André Silva
2023-03-07 11:19:19 +00:00
committed by GitHub
parent d86a32630b
commit 13b0f24abd
29 changed files with 103 additions and 214 deletions
@@ -137,7 +137,7 @@ fn import_block(
params.state_action =
StateAction::ApplyChanges(sc_consensus::StorageChanges::Changes(built.storage_changes));
params.fork_choice = Some(ForkChoiceStrategy::LongestChain);
futures::executor::block_on(client.import_block(params, Default::default()))
futures::executor::block_on(client.import_block(params))
.expect("importing a block doesn't fail");
}
+1 -1
View File
@@ -760,7 +760,7 @@ mod tests {
);
params.fork_choice = Some(ForkChoiceStrategy::LongestChain);
futures::executor::block_on(block_import.import_block(params, Default::default()))
futures::executor::block_on(block_import.import_block(params))
.expect("error importing test block");
},
|service, _| {
+2 -4
View File
@@ -682,10 +682,8 @@ impl BenchContext {
assert_eq!(self.client.chain_info().best_number, 0);
assert_eq!(
futures::executor::block_on(
self.client.import_block(import_params, Default::default())
)
.expect("Failed to import block"),
futures::executor::block_on(self.client.import_block(import_params))
.expect("Failed to import block"),
ImportResult::Imported(ImportedAux {
header_only: false,
clear_justification_requests: false,
+5 -9
View File
@@ -18,12 +18,10 @@
//! Substrate Client data backend
use crate::{
blockchain::{well_known_cache_keys, Backend as BlockchainBackend},
UsageInfo,
};
use std::collections::HashSet;
use parking_lot::RwLock;
use sp_blockchain;
use sp_consensus::BlockOrigin;
use sp_core::offchain::OffchainStorage;
use sp_runtime::{
@@ -35,7 +33,8 @@ use sp_state_machine::{
OffchainChangesCollection, StorageCollection, StorageIterator,
};
use sp_storage::{ChildInfo, StorageData, StorageKey};
use std::collections::{HashMap, HashSet};
use crate::{blockchain::Backend as BlockchainBackend, UsageInfo};
pub use sp_state_machine::{Backend as StateBackend, KeyValueStates};
@@ -179,9 +178,6 @@ pub trait BlockImportOperation<Block: BlockT> {
state: NewBlockState,
) -> sp_blockchain::Result<()>;
/// Update cached data.
fn update_cache(&mut self, cache: HashMap<well_known_cache_keys::Id, Vec<u8>>);
/// Inject storage data into the database.
fn update_db_storage(
&mut self,
+1 -3
View File
@@ -40,7 +40,7 @@ use std::{
use crate::{
backend::{self, NewBlockState},
blockchain::{self, well_known_cache_keys::Id as CacheKeyId, BlockStatus, HeaderBackend},
blockchain::{self, BlockStatus, HeaderBackend},
leaves::LeafSet,
UsageInfo,
};
@@ -549,8 +549,6 @@ where
Ok(())
}
fn update_cache(&mut self, _cache: HashMap<CacheKeyId, Vec<u8>>) {}
fn update_db_storage(
&mut self,
update: <InMemoryBackend<HashFor<Block>> as StateBackend<HashFor<Block>>>::Transaction,
@@ -34,7 +34,7 @@ use sc_consensus_slots::{check_equivocation, CheckedHeader, InherentDataProvider
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_TRACE};
use sp_api::{ApiExt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{well_known_cache_keys::Id as CacheKeyId, HeaderBackend};
use sp_blockchain::HeaderBackend;
use sp_consensus::Error as ConsensusError;
use sp_consensus_aura::{digests::CompatibleDigestItem, inherents::AuraInherentData, AuraApi};
use sp_consensus_slots::Slot;
@@ -184,7 +184,7 @@ where
async fn verify(
&mut self,
mut block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<B, ()>, String> {
// Skip checks that include execution, if being told so or when importing only state.
//
// This is done for example when gap syncing and it is expected that the block after the gap
@@ -194,7 +194,7 @@ where
// When we are importing only the state of a block, it will be the best block.
block.fork_choice = Some(ForkChoiceStrategy::Custom(block.with_state()));
return Ok((block, Default::default()))
return Ok(block)
}
let hash = block.header.hash();
@@ -278,7 +278,7 @@ where
block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
block.post_hash = Some(hash);
Ok((block, None))
Ok(block)
},
CheckedHeader::Deferred(a, b) => {
debug!(target: LOG_TARGET, "Checking {:?} failed; {:?}, {:?}.", hash, a, b);
+9 -16
View File
@@ -67,7 +67,7 @@
#![warn(missing_docs)]
use std::{
collections::{HashMap, HashSet},
collections::HashSet,
future::Future,
pin::Pin,
sync::Arc,
@@ -114,9 +114,7 @@ use sp_blockchain::{
Backend as _, BlockStatus, Error as ClientError, ForkBackend, HeaderBackend, HeaderMetadata,
Result as ClientResult,
};
use sp_consensus::{
BlockOrigin, CacheKeyId, Environment, Error as ConsensusError, Proposer, SelectChain,
};
use sp_consensus::{BlockOrigin, Environment, Error as ConsensusError, Proposer, SelectChain};
use sp_consensus_babe::inherents::BabeInherentData;
use sp_consensus_slots::Slot;
use sp_core::{crypto::ByteArray, ExecutionContext};
@@ -1131,9 +1129,6 @@ where
}
}
type BlockVerificationResult<Block> =
Result<(BlockImportParams<Block, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String>;
#[async_trait::async_trait]
impl<Block, Client, SelectChain, CIDP> Verifier<Block>
for BabeVerifier<Block, Client, SelectChain, CIDP>
@@ -1153,7 +1148,7 @@ where
async fn verify(
&mut self,
mut block: BlockImportParams<Block, ()>,
) -> BlockVerificationResult<Block> {
) -> Result<BlockImportParams<Block, ()>, String> {
trace!(
target: LOG_TARGET,
"Verifying origin: {:?} header: {:?} justification(s): {:?} body: {:?}",
@@ -1177,7 +1172,7 @@ where
// read it from the state after import. We also skip all verifications
// because there's no parent state and we trust the sync module to verify
// that the state is correct and finalized.
return Ok((block, Default::default()))
return Ok(block)
}
debug!(
@@ -1296,7 +1291,7 @@ where
);
block.post_hash = Some(hash);
Ok((block, Default::default()))
Ok(block)
},
CheckedHeader::Deferred(a, b) => {
debug!(target: LOG_TARGET, "Checking {:?} failed; {:?}, {:?}.", hash, a, b);
@@ -1368,7 +1363,6 @@ where
async fn import_state(
&mut self,
mut block: BlockImportParams<Block, sp_api::TransactionFor<Client, Block>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, ConsensusError> {
let hash = block.post_hash();
let parent_hash = *block.header.parent_hash();
@@ -1383,7 +1377,7 @@ where
});
// First make the client import the state.
let import_result = self.inner.import_block(block, new_cache).await;
let import_result = self.inner.import_block(block).await;
let aux = match import_result {
Ok(ImportResult::Imported(aux)) => aux,
Ok(r) =>
@@ -1433,7 +1427,6 @@ where
async fn import_block(
&mut self,
mut block: BlockImportParams<Block, Self::Transaction>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let hash = block.post_hash();
let number = *block.header.number();
@@ -1454,11 +1447,11 @@ where
// In case of initial sync intermediates should not be present...
let _ = block.remove_intermediate::<BabeIntermediate<Block>>(INTERMEDIATE_KEY);
block.fork_choice = Some(ForkChoiceStrategy::Custom(false));
return self.inner.import_block(block, new_cache).await.map_err(Into::into)
return self.inner.import_block(block).await.map_err(Into::into)
}
if block.with_state() {
return self.import_state(block, new_cache).await
return self.import_state(block).await
}
let pre_digest = find_pre_digest::<Block>(&block.header).expect(
@@ -1694,7 +1687,7 @@ where
epoch_changes.release_mutex()
};
let import_result = self.inner.import_block(block, new_cache).await;
let import_result = self.inner.import_block(block).await;
// revert to the original epoch changes in case there's an error
// importing the block
+3 -4
View File
@@ -209,9 +209,8 @@ where
async fn import_block(
&mut self,
block: BlockImportParams<TestBlock, Self::Transaction>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
Ok(self.0.import_block(block, new_cache).await.expect("importing block failed"))
Ok(self.0.import_block(block).await.expect("importing block failed"))
}
async fn check_block(
@@ -258,7 +257,7 @@ impl Verifier<TestBlock> for TestVerifier {
async fn verify(
&mut self,
mut block: BlockImportParams<TestBlock, ()>,
) -> Result<(BlockImportParams<TestBlock, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<TestBlock, ()>, String> {
// apply post-sealing mutations (i.e. stripping seal, if desired).
(self.mutator)(&mut block.header, Stage::PostSeal);
self.inner.verify(block).await
@@ -743,7 +742,7 @@ async fn propose_and_import_block<Transaction: Send + 'static>(
import
.insert_intermediate(INTERMEDIATE_KEY, BabeIntermediate::<TestBlock> { epoch_descriptor });
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
let import_result = block_import.import_block(import, Default::default()).await.unwrap();
let import_result = block_import.import_block(import).await.unwrap();
match import_result {
ImportResult::Imported(_) => {},
@@ -16,13 +16,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::sync::Arc;
use log::debug;
use sp_consensus_beefy::{BeefyApi, BEEFY_ENGINE_ID};
use std::{collections::HashMap, sync::Arc};
use sp_api::{ProvideRuntimeApi, TransactionFor};
use sp_blockchain::well_known_cache_keys;
use sp_consensus::Error as ConsensusError;
use sp_consensus_beefy::{BeefyApi, BEEFY_ENGINE_ID};
use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT, NumberFor},
EncodedJustification,
@@ -132,7 +132,6 @@ where
async fn import_block(
&mut self,
mut block: BlockImportParams<Block, Self::Transaction>,
new_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let hash = block.post_hash();
let number = *block.header.number();
@@ -146,7 +145,7 @@ where
});
// Run inner block import.
let inner_import_result = self.inner.import_block(block, new_cache).await?;
let inner_import_result = self.inner.import_block(block).await?;
match (beefy_encoded, &inner_import_result) {
(Some(encoded), ImportResult::Imported(_)) => {
@@ -61,7 +61,7 @@ use sp_runtime::{
traits::{Header as HeaderT, NumberFor},
BuildStorage, DigestItem, EncodedJustification, Justifications, Storage,
};
use std::{collections::HashMap, marker::PhantomData, sync::Arc, task::Poll};
use std::{marker::PhantomData, sync::Arc, task::Poll};
use substrate_test_runtime_client::{runtime::Header, ClientExt};
use tokio::time::Duration;
@@ -766,14 +766,11 @@ async fn beefy_importing_justifications() {
// Import block 1 without justifications.
assert_eq!(
block_import
.import_block(params(block.clone(), None), HashMap::new())
.await
.unwrap(),
block_import.import_block(params(block.clone(), None)).await.unwrap(),
ImportResult::Imported(ImportedAux { is_new_best: true, ..Default::default() }),
);
assert_eq!(
block_import.import_block(params(block, None), HashMap::new()).await.unwrap(),
block_import.import_block(params(block, None)).await.unwrap(),
ImportResult::AlreadyInChain,
);
@@ -788,7 +785,7 @@ async fn beefy_importing_justifications() {
let encoded = versioned_proof.encode();
let justif = Some(Justifications::from((BEEFY_ENGINE_ID, encoded)));
assert_eq!(
block_import.import_block(params(block, justif), HashMap::new()).await.unwrap(),
block_import.import_block(params(block, justif)).await.unwrap(),
ImportResult::Imported(ImportedAux {
bad_justification: false,
is_new_best: true,
@@ -820,7 +817,7 @@ async fn beefy_importing_justifications() {
let justif = Some(Justifications::from((BEEFY_ENGINE_ID, encoded)));
let mut justif_recv = justif_stream.subscribe(100_000);
assert_eq!(
block_import.import_block(params(block, justif), HashMap::new()).await.unwrap(),
block_import.import_block(params(block, justif)).await.unwrap(),
ImportResult::Imported(ImportedAux {
bad_justification: false,
is_new_best: true,
@@ -856,7 +853,7 @@ async fn beefy_importing_justifications() {
let justif = Some(Justifications::from((BEEFY_ENGINE_ID, encoded)));
let mut justif_recv = justif_stream.subscribe(100_000);
assert_eq!(
block_import.import_block(params(block, justif), HashMap::new()).await.unwrap(),
block_import.import_block(params(block, justif)).await.unwrap(),
ImportResult::Imported(ImportedAux {
// Still `false` because we don't want to fail import on bad BEEFY justifications.
bad_justification: false,
@@ -25,7 +25,7 @@ use sp_runtime::{
};
use std::{any::Any, borrow::Cow, collections::HashMap, sync::Arc};
use sp_consensus::{BlockOrigin, CacheKeyId, Error};
use sp_consensus::{BlockOrigin, Error};
/// Block import result.
#[derive(Debug, PartialEq, Eq)]
@@ -348,12 +348,9 @@ pub trait BlockImport<B: BlockT> {
) -> Result<ImportResult, Self::Error>;
/// Import a block.
///
/// Cached data can be accessed through the blockchain cache.
async fn import_block(
&mut self,
block: BlockImportParams<B, Self::Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error>;
}
@@ -374,14 +371,11 @@ where
}
/// Import a block.
///
/// Cached data can be accessed through the blockchain cache.
async fn import_block(
&mut self,
block: BlockImportParams<B, Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(**self).import_block(block, cache).await
(**self).import_block(block).await
}
}
@@ -405,9 +399,8 @@ where
async fn import_block(
&mut self,
block: BlockImportParams<B, Transaction>,
cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(&**self).import_block(block, cache).await
(&**self).import_block(block).await
}
}
@@ -27,9 +27,9 @@
//! instantiated. The `BasicQueue` and `BasicVerifier` traits allow serial
//! queues to be instantiated simply.
use std::{collections::HashMap, iter::FromIterator};
use log::{debug, trace};
use sp_consensus::{error::Error as ConsensusError, BlockOrigin};
use sp_runtime::{
traits::{Block as BlockT, Header as _, NumberFor},
Justifications,
@@ -42,8 +42,8 @@ use crate::{
},
metrics::Metrics,
};
pub use basic_queue::BasicQueue;
use sp_consensus::{error::Error as ConsensusError, BlockOrigin, CacheKeyId};
const LOG_TARGET: &str = "sync::import-queue";
@@ -96,13 +96,12 @@ pub struct IncomingBlock<B: BlockT> {
/// Verify a justification of a block
#[async_trait::async_trait]
pub trait Verifier<B: BlockT>: Send + Sync {
/// Verify the given data and return the BlockImportParams and an optional
/// new set of validators to import. If not, err with an Error-Message
/// presented to the User in the logs.
/// Verify the given block data and return the `BlockImportParams` to
/// continue the block import process.
async fn verify(
&mut self,
block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String>;
) -> Result<BlockImportParams<B, ()>, String>;
}
/// Blocks import queue API.
@@ -328,7 +327,7 @@ pub(crate) async fn import_single_block_metered<
import_block.state_action = StateAction::ExecuteIfPossible;
}
let (import_block, maybe_keys) = verifier.verify(import_block).await.map_err(|msg| {
let import_block = verifier.verify(import_block).await.map_err(|msg| {
if let Some(ref peer) = peer {
trace!(
target: LOG_TARGET,
@@ -351,9 +350,8 @@ pub(crate) async fn import_single_block_metered<
metrics.report_verification(true, started.elapsed());
}
let cache = HashMap::from_iter(maybe_keys.unwrap_or_default());
let import_block = import_block.clear_storage_changes_and_mutate();
let imported = import_handle.import_block(import_block, cache).await;
let imported = import_handle.import_block(import_block).await;
if let Some(metrics) = metrics.as_ref() {
metrics.report_verification_and_import(started.elapsed());
}
@@ -504,19 +504,18 @@ mod tests {
block_import::{
BlockCheckParams, BlockImport, BlockImportParams, ImportResult, JustificationImport,
},
import_queue::{CacheKeyId, Verifier},
import_queue::Verifier,
};
use futures::{executor::block_on, Future};
use sp_test_primitives::{Block, BlockNumber, Extrinsic, Hash, Header};
use std::collections::HashMap;
#[async_trait::async_trait]
impl Verifier<Block> for () {
async fn verify(
&mut self,
block: BlockImportParams<Block, ()>,
) -> Result<(BlockImportParams<Block, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
Ok((BlockImportParams::new(block.origin, block.header), None))
) -> Result<BlockImportParams<Block, ()>, String> {
Ok(BlockImportParams::new(block.origin, block.header))
}
}
@@ -535,7 +534,6 @@ mod tests {
async fn import_block(
&mut self,
_block: BlockImportParams<Block, Self::Transaction>,
_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
Ok(ImportResult::imported(true))
}
@@ -29,7 +29,7 @@ use sc_consensus::{
use sc_telemetry::TelemetryHandle;
use sc_utils::mpsc::TracingUnboundedSender;
use sp_api::{Core, RuntimeApiInfo, TransactionFor};
use sp_blockchain::{well_known_cache_keys, BlockStatus};
use sp_blockchain::BlockStatus;
use sp_consensus::{BlockOrigin, Error as ConsensusError, SelectChain};
use sp_consensus_grandpa::{ConsensusLog, GrandpaApi, ScheduledChange, SetId, GRANDPA_ENGINE_ID};
use sp_core::hashing::twox_128;
@@ -460,13 +460,12 @@ where
async fn import_state(
&mut self,
mut block: BlockImportParams<Block, TransactionFor<Client, Block>>,
new_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, ConsensusError> {
let hash = block.post_hash();
let number = *block.header.number();
// Force imported state finality.
block.finalized = true;
let import_result = (&*self.inner).import_block(block, new_cache).await;
let import_result = (&*self.inner).import_block(block).await;
match import_result {
Ok(ImportResult::Imported(aux)) => {
// We've just imported a new state. We trust the sync module has verified
@@ -526,7 +525,6 @@ where
async fn import_block(
&mut self,
mut block: BlockImportParams<Block, Self::Transaction>,
new_cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let hash = block.post_hash();
let number = *block.header.number();
@@ -537,14 +535,14 @@ where
Ok(BlockStatus::InChain) => {
// Strip justifications when re-importing an existing block.
let _justifications = block.justifications.take();
return (&*self.inner).import_block(block, new_cache).await
return (&*self.inner).import_block(block).await
},
Ok(BlockStatus::Unknown) => {},
Err(e) => return Err(ConsensusError::ClientImport(e.to_string())),
}
if block.with_state() {
return self.import_state(block, new_cache).await
return self.import_state(block).await
}
if number <= self.inner.info().finalized_number {
@@ -570,7 +568,7 @@ where
},
);
}
return (&*self.inner).import_block(block, new_cache).await
return (&*self.inner).import_block(block).await
}
// on initial sync we will restrict logging under info to avoid spam.
@@ -580,7 +578,7 @@ where
// we don't want to finalize on `inner.import_block`
let mut justifications = block.justifications.take();
let import_result = (&*self.inner).import_block(block, new_cache).await;
let import_result = (&*self.inner).import_block(block).await;
let mut imported_aux = {
match import_result {
@@ -47,10 +47,7 @@ use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT},
Justifications,
};
use std::{
collections::{HashMap, HashSet},
pin::Pin,
};
use std::{collections::HashSet, pin::Pin};
use substrate_test_runtime_client::runtime::BlockNumber;
use tokio::runtime::Handle;
@@ -906,7 +903,7 @@ async fn allows_reimporting_change_blocks() {
};
assert_eq!(
block_import.import_block(block(), HashMap::new()).await.unwrap(),
block_import.import_block(block()).await.unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: true,
clear_justification_requests: false,
@@ -916,10 +913,7 @@ async fn allows_reimporting_change_blocks() {
}),
);
assert_eq!(
block_import.import_block(block(), HashMap::new()).await.unwrap(),
ImportResult::AlreadyInChain
);
assert_eq!(block_import.import_block(block()).await.unwrap(), ImportResult::AlreadyInChain);
}
#[tokio::test]
@@ -955,7 +949,7 @@ async fn test_bad_justification() {
};
assert_eq!(
block_import.import_block(block(), HashMap::new()).await.unwrap(),
block_import.import_block(block()).await.unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: true,
clear_justification_requests: false,
@@ -965,10 +959,7 @@ async fn test_bad_justification() {
}),
);
assert_eq!(
block_import.import_block(block(), HashMap::new()).await.unwrap(),
ImportResult::AlreadyInChain
);
assert_eq!(block_import.import_block(block()).await.unwrap(), ImportResult::AlreadyInChain);
}
#[tokio::test]
@@ -1938,7 +1929,7 @@ async fn imports_justification_for_regular_blocks_on_import() {
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
assert_eq!(
block_import.import_block(import, HashMap::new()).await.unwrap(),
block_import.import_block(import).await.unwrap(),
ImportResult::Imported(ImportedAux {
needs_justification: false,
clear_justification_requests: false,
@@ -35,7 +35,6 @@ use std::{marker::PhantomData, sync::Arc};
use sc_consensus::{BlockImportParams, ForkChoiceStrategy, Verifier};
use sp_api::{ProvideRuntimeApi, TransactionFor};
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_consensus::CacheKeyId;
use sp_consensus_babe::{
digests::{NextEpochDescriptor, PreDigest, SecondaryPlainPreDigest},
inherents::BabeInherentData,
@@ -99,7 +98,7 @@ where
async fn verify(
&mut self,
mut import_params: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<B, ()>, String> {
import_params.finalized = false;
import_params.fork_choice = Some(ForkChoiceStrategy::LongestChain);
@@ -128,7 +127,7 @@ where
import_params
.insert_intermediate(INTERMEDIATE_KEY, BabeIntermediate::<B> { epoch_descriptor });
Ok((import_params, None))
Ok(import_params)
}
}
@@ -27,7 +27,7 @@ use sc_consensus::{
import_queue::{BasicQueue, BoxBlockImport, Verifier},
};
use sp_blockchain::HeaderBackend;
use sp_consensus::{CacheKeyId, Environment, Proposer, SelectChain};
use sp_consensus::{Environment, Proposer, SelectChain};
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
use std::{marker::PhantomData, sync::Arc};
@@ -62,10 +62,10 @@ impl<B: BlockT> Verifier<B> for ManualSealVerifier {
async fn verify(
&mut self,
mut block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<B, ()>, String> {
block.finalized = false;
block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
Ok((block, None))
Ok(block)
}
}
@@ -27,7 +27,7 @@ use sp_blockchain::HeaderBackend;
use sp_consensus::{self, BlockOrigin, Environment, Proposer, SelectChain};
use sp_inherents::{CreateInherentDataProviders, InherentDataProvider};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use std::{collections::HashMap, sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration};
/// max duration for creating a proposal in secs
pub const MAX_PROPOSAL_DURATION: u64 = 10;
@@ -153,7 +153,7 @@ pub async fn seal_block<B, BI, SC, C, E, TP, CIDP, P>(
let mut post_header = header.clone();
post_header.digest_mut().logs.extend(params.post_digests.iter().cloned());
match block_import.import_block(params, HashMap::new()).await? {
match block_import.import_block(params).await? {
ImportResult::Imported(aux) =>
Ok(CreatedBlock { hash: <B as BlockT>::Header::hash(&post_header), aux }),
other => Err(other.into()),
+5 -6
View File
@@ -55,7 +55,7 @@ use sc_consensus::{
};
use sp_api::ProvideRuntimeApi;
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{well_known_cache_keys::Id as CacheKeyId, HeaderBackend};
use sp_blockchain::HeaderBackend;
use sp_consensus::{Environment, Error as ConsensusError, Proposer, SelectChain, SyncOracle};
use sp_consensus_pow::{Seal, TotalDifficulty, POW_ENGINE_ID};
use sp_core::ExecutionContext;
@@ -65,7 +65,7 @@ use sp_runtime::{
traits::{Block as BlockT, Header as HeaderT},
RuntimeString,
};
use std::{cmp::Ordering, collections::HashMap, marker::PhantomData, sync::Arc, time::Duration};
use std::{cmp::Ordering, marker::PhantomData, sync::Arc, time::Duration};
const LOG_TARGET: &str = "pow";
@@ -325,7 +325,6 @@ where
async fn import_block(
&mut self,
mut block: BlockImportParams<B, Self::Transaction>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let best_header = self
.select_chain
@@ -399,7 +398,7 @@ where
));
}
self.inner.import_block(block, new_cache).await.map_err(Into::into)
self.inner.import_block(block).await.map_err(Into::into)
}
}
@@ -449,7 +448,7 @@ where
async fn verify(
&mut self,
mut block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<B, ()>, String> {
let hash = block.header.hash();
let (checked_header, seal) = self.check_header(block.header)?;
@@ -459,7 +458,7 @@ where
block.insert_intermediate(INTERMEDIATE_KEY, intermediate);
block.post_hash = Some(hash);
Ok((block, None))
Ok(block)
}
}
+1 -2
View File
@@ -32,7 +32,6 @@ use sp_runtime::{
DigestItem,
};
use std::{
collections::HashMap,
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
@@ -203,7 +202,7 @@ where
let header = import_block.post_header();
let mut block_import = self.block_import.lock();
match block_import.import_block(import_block, HashMap::default()).await {
match block_import.import_block(import_block).await {
Ok(res) => {
res.handle_justification(
&header.hash(),
+1 -1
View File
@@ -424,7 +424,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
);
let header = block_import_params.post_header();
match self.block_import().import_block(block_import_params, Default::default()).await {
match self.block_import().import_block(block_import_params).await {
Ok(res) => {
res.handle_justification(
&header.hash(),
+2 -6
View File
@@ -68,8 +68,8 @@ use sc_client_api::{
use sc_state_db::{IsPruned, LastCanonicalized, StateDb};
use sp_arithmetic::traits::Saturating;
use sp_blockchain::{
well_known_cache_keys, Backend as _, CachedHeaderMetadata, Error as ClientError, HeaderBackend,
HeaderMetadata, HeaderMetadataCache, Result as ClientResult,
Backend as _, CachedHeaderMetadata, Error as ClientError, HeaderBackend, HeaderMetadata,
HeaderMetadataCache, Result as ClientResult,
};
use sp_core::{
offchain::OffchainOverlayedChange,
@@ -913,10 +913,6 @@ impl<Block: BlockT> sc_client_api::backend::BlockImportOperation<Block>
Ok(())
}
fn update_cache(&mut self, _cache: HashMap<well_known_cache_keys::Id, Vec<u8>>) {
// Currently cache isn't implemented on full nodes.
}
fn update_db_storage(&mut self, update: PrefixedMemoryDB<HashFor<Block>>) -> ClientResult<()> {
self.db_updates = update;
Ok(())
@@ -33,7 +33,7 @@ use sc_network_sync::{
service::network::{NetworkServiceHandle, NetworkServiceProvider},
state_request_handler::StateRequestHandler,
};
use sp_runtime::traits::{Block as BlockT, Header as _};
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;
use substrate_test_runtime_client::{
runtime::{Block as TestBlock, Hash as TestHash},
@@ -135,31 +135,10 @@ impl TestNetworkBuilder {
async fn verify(
&mut self,
mut block: sc_consensus::BlockImportParams<B, ()>,
) -> Result<
(
sc_consensus::BlockImportParams<B, ()>,
Option<Vec<(sp_blockchain::well_known_cache_keys::Id, Vec<u8>)>>,
),
String,
> {
let maybe_keys = block
.header
.digest()
.log(|l| {
l.try_as_raw(sp_runtime::generic::OpaqueDigestItemId::Consensus(b"aura"))
.or_else(|| {
l.try_as_raw(sp_runtime::generic::OpaqueDigestItemId::Consensus(
b"babe",
))
})
})
.map(|blob| {
vec![(sp_blockchain::well_known_cache_keys::AUTHORITIES, blob.to_vec())]
});
) -> Result<sc_consensus::BlockImportParams<B, ()>, String> {
block.finalized = self.0;
block.fork_choice = Some(sc_consensus::ForkChoiceStrategy::LongestChain);
Ok((block, maybe_keys))
Ok(block)
}
}
+8 -24
View File
@@ -67,7 +67,6 @@ use sc_network_sync::{
};
use sc_service::client::Client;
use sp_blockchain::{
well_known_cache_keys::{self, Id as CacheKeyId},
Backend as BlockchainBackend, HeaderBackend, Info as BlockchainInfo, Result as ClientResult,
};
use sp_consensus::{
@@ -77,7 +76,7 @@ use sp_consensus::{
use sp_core::H256;
use sp_runtime::{
codec::{Decode, Encode},
generic::{BlockId, OpaqueDigestItemId},
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, NumberFor},
Justification, Justifications,
};
@@ -112,20 +111,12 @@ impl<B: BlockT> Verifier<B> for PassThroughVerifier {
async fn verify(
&mut self,
mut block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
let maybe_keys = block
.header
.digest()
.log(|l| {
l.try_as_raw(OpaqueDigestItemId::Consensus(b"aura"))
.or_else(|| l.try_as_raw(OpaqueDigestItemId::Consensus(b"babe")))
})
.map(|blob| vec![(well_known_cache_keys::AUTHORITIES, blob.to_vec())]);
) -> Result<BlockImportParams<B, ()>, String> {
if block.fork_choice.is_none() {
block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
};
block.finalized = self.finalized;
Ok((block, maybe_keys))
Ok(block)
}
}
@@ -224,9 +215,8 @@ impl BlockImport<Block> for PeersClient {
async fn import_block(
&mut self,
block: BlockImportParams<Block, ()>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
self.client.import_block(block.clear_storage_changes_and_mutate(), cache).await
self.client.import_block(block.clear_storage_changes_and_mutate()).await
}
}
@@ -392,15 +382,10 @@ where
let mut import_block = BlockImportParams::new(origin, header.clone());
import_block.body = if headers_only { None } else { Some(block.extrinsics) };
import_block.fork_choice = Some(fork_choice);
let (import_block, cache) =
let import_block =
futures::executor::block_on(self.verifier.verify(import_block)).unwrap();
let cache = if let Some(cache) = cache {
cache.into_iter().collect()
} else {
Default::default()
};
futures::executor::block_on(self.block_import.import_block(import_block, cache))
futures::executor::block_on(self.block_import.import_block(import_block))
.expect("block_import failed");
if announce_block {
self.sync_service.announce_block(hash, None);
@@ -633,9 +618,8 @@ where
async fn import_block(
&mut self,
block: BlockImportParams<Block, Self::Transaction>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
self.inner.import_block(block.clear_storage_changes_and_mutate(), cache).await
self.inner.import_block(block.clear_storage_changes_and_mutate()).await
}
}
@@ -650,7 +634,7 @@ impl<B: BlockT> Verifier<B> for VerifierAdapter<B> {
async fn verify(
&mut self,
block: BlockImportParams<B, ()>,
) -> Result<(BlockImportParams<B, ()>, Option<Vec<(CacheKeyId, Vec<u8>)>>), String> {
) -> Result<BlockImportParams<B, ()>, String> {
let hash = block.header.hash();
self.verifier.lock().await.verify(block).await.map_err(|e| {
self.failed_verifications.lock().insert(hash, e.clone());
+4 -10
View File
@@ -51,8 +51,8 @@ use sp_api::{
ProvideRuntimeApi,
};
use sp_blockchain::{
self as blockchain, well_known_cache_keys::Id as CacheKeyId, Backend as ChainBackend,
CachedHeaderMetadata, Error, HeaderBackend as ChainHeaderBackend, HeaderMetadata,
self as blockchain, Backend as ChainBackend, CachedHeaderMetadata, Error,
HeaderBackend as ChainHeaderBackend, HeaderMetadata,
};
use sp_consensus::{BlockOrigin, BlockStatus, Error as ConsensusError};
@@ -504,7 +504,6 @@ where
&self,
operation: &mut ClientImportOperation<Block, B>,
import_block: BlockImportParams<Block, backend::TransactionFor<B, Block>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
storage_changes: Option<
sc_consensus::StorageChanges<Block, backend::TransactionFor<B, Block>>,
>,
@@ -559,7 +558,6 @@ where
body,
indexed_body,
storage_changes,
new_cache,
finalized,
auxiliary,
fork_choice,
@@ -599,7 +597,6 @@ where
storage_changes: Option<
sc_consensus::StorageChanges<Block, backend::TransactionFor<B, Block>>,
>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
finalized: bool,
aux: Vec<(Vec<u8>, Option<Vec<u8>>)>,
fork_choice: ForkChoiceStrategy,
@@ -712,7 +709,6 @@ where
},
};
operation.op.update_cache(new_cache);
storage_changes
},
None => None,
@@ -1770,7 +1766,6 @@ where
async fn import_block(
&mut self,
mut import_block: BlockImportParams<Block, backend::TransactionFor<B, Block>>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
let span = tracing::span!(tracing::Level::DEBUG, "import_block");
let _enter = span.enter();
@@ -1785,7 +1780,7 @@ where
};
self.lock_import_and_run(|operation| {
self.apply_block(operation, import_block, new_cache, storage_changes)
self.apply_block(operation, import_block, storage_changes)
})
.map_err(|e| {
warn!("Block import error: {}", e);
@@ -1875,9 +1870,8 @@ where
async fn import_block(
&mut self,
import_block: BlockImportParams<Block, Self::Transaction>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(&*self).import_block(import_block, new_cache).await
(&*self).import_block(import_block).await
}
async fn check_block(
@@ -930,7 +930,7 @@ fn finality_target_with_best_not_on_longest_chain() {
let mut import_params = BlockImportParams::new(BlockOrigin::Own, header);
import_params.body = Some(extrinsics);
import_params.fork_choice = Some(ForkChoiceStrategy::Custom(false));
block_on(client.import_block(import_params, Default::default())).unwrap();
block_on(client.import_block(import_params)).unwrap();
// double check that B3 is still the best...
assert_eq!(client.info().best_hash, b3.hash());
@@ -1963,7 +1963,7 @@ fn cleans_up_closed_notification_sinks_on_block_import() {
let mut import = BlockImportParams::new(origin, header);
import.body = Some(extrinsics);
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
block_on(client.import_block(import, Default::default())).unwrap();
block_on(client.import_block(import)).unwrap();
};
// after importing a block we should still have 4 notification sinks
@@ -288,18 +288,3 @@ pub enum BlockStatus {
/// Not in the queue or the blockchain.
Unknown,
}
/// A list of all well known keys in the blockchain cache.
pub mod well_known_cache_keys {
/// The type representing cache keys.
pub type Id = sp_consensus::CacheKeyId;
/// A list of authorities.
pub const AUTHORITIES: Id = *b"auth";
/// Current Epoch data.
pub const EPOCH: Id = *b"epch";
/// Changes trie configuration.
pub const CHANGES_TRIE_CONFIG: Id = *b"chtr";
}
@@ -39,9 +39,6 @@ pub use select_chain::SelectChain;
pub use sp_inherents::InherentData;
pub use sp_state_machine::Backend as StateBackend;
/// Type of keys in the blockchain cache that consensus module could use for its needs.
pub type CacheKeyId = [u8; 4];
/// Block status.
#[derive(Debug, PartialEq, Eq)]
pub enum BlockStatus {
@@ -17,7 +17,6 @@
//! Client extension for tests.
use codec::alloc::collections::hash_map::HashMap;
use sc_client_api::{backend::Finalizer, client::BlockBackend};
use sc_consensus::{BlockImport, BlockImportParams, ForkChoiceStrategy};
use sc_service::client::Client;
@@ -100,7 +99,7 @@ where
import.body = Some(extrinsics);
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_as_best(
@@ -113,7 +112,7 @@ where
import.body = Some(extrinsics);
import.fork_choice = Some(ForkChoiceStrategy::Custom(true));
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_as_final(
@@ -127,7 +126,7 @@ where
import.finalized = true;
import.fork_choice = Some(ForkChoiceStrategy::Custom(true));
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_justified(
@@ -143,7 +142,7 @@ where
import.finalized = true;
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
}
@@ -162,7 +161,7 @@ where
import.body = Some(extrinsics);
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_as_best(
@@ -175,7 +174,7 @@ where
import.body = Some(extrinsics);
import.fork_choice = Some(ForkChoiceStrategy::Custom(true));
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_as_final(
@@ -189,7 +188,7 @@ where
import.finalized = true;
import.fork_choice = Some(ForkChoiceStrategy::Custom(true));
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
async fn import_justified(
@@ -205,6 +204,6 @@ where
import.finalized = true;
import.fork_choice = Some(ForkChoiceStrategy::LongestChain);
BlockImport::import_block(self, import, HashMap::new()).await.map(|_| ())
BlockImport::import_block(self, import).await.map(|_| ())
}
}