mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 17:41:08 +00:00
Kill the light client, CHTs and change tries. (#10080)
* Remove light client, change tries and CHTs * Update tests * fmt * Restore changes_root * Fixed benches * Cargo fmt * fmt * fmt
This commit is contained in:
@@ -12,7 +12,6 @@ publish = false
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
sc-light = { version = "4.0.0-dev", path = "../../../client/light" }
|
||||
sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" }
|
||||
sc-consensus = { version = "0.10.0-dev", path = "../../../client/consensus/common" }
|
||||
sc-block-builder = { version = "0.10.0-dev", path = "../../../client/block-builder" }
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
use sc_client_api::backend;
|
||||
use sp_api::{ApiExt, ProvideRuntimeApi};
|
||||
use sp_core::ChangesTrieConfiguration;
|
||||
|
||||
use sc_block_builder::BlockBuilderApi;
|
||||
|
||||
@@ -36,11 +35,6 @@ pub trait BlockBuilderExt {
|
||||
key: Vec<u8>,
|
||||
value: Option<Vec<u8>>,
|
||||
) -> Result<(), sp_blockchain::Error>;
|
||||
/// Add changes trie configuration update extrinsic to the block.
|
||||
fn push_changes_trie_configuration_update(
|
||||
&mut self,
|
||||
new_config: Option<ChangesTrieConfiguration>,
|
||||
) -> Result<(), sp_blockchain::Error>;
|
||||
}
|
||||
|
||||
impl<'a, A, B> BlockBuilderExt
|
||||
@@ -68,11 +62,4 @@ where
|
||||
) -> Result<(), sp_blockchain::Error> {
|
||||
self.push(substrate_test_runtime::Extrinsic::StorageChange(key, value))
|
||||
}
|
||||
|
||||
fn push_changes_trie_configuration_update(
|
||||
&mut self,
|
||||
new_config: Option<ChangesTrieConfiguration>,
|
||||
) -> Result<(), sp_blockchain::Error> {
|
||||
self.push(substrate_test_runtime::Extrinsic::ChangesTrieConfigUpdate(new_config))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,22 +24,18 @@ pub mod trait_tests;
|
||||
mod block_builder_ext;
|
||||
|
||||
pub use sc_consensus::LongestChain;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
pub use substrate_test_client::*;
|
||||
pub use substrate_test_runtime as runtime;
|
||||
|
||||
pub use self::block_builder_ext::BlockBuilderExt;
|
||||
|
||||
use sc_client_api::light::{
|
||||
Fetcher, RemoteBodyRequest, RemoteCallRequest, RemoteChangesRequest, RemoteHeaderRequest,
|
||||
RemoteReadChildRequest, RemoteReadRequest,
|
||||
};
|
||||
use sp_core::{
|
||||
sr25519,
|
||||
storage::{ChildInfo, Storage, StorageChild},
|
||||
ChangesTrieConfiguration, Pair,
|
||||
Pair,
|
||||
};
|
||||
use sp_runtime::traits::{Block as BlockT, Hash as HashT, HashFor, Header as HeaderT, NumberFor};
|
||||
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
|
||||
use substrate_test_runtime::genesismap::{additional_storage_with_genesis, GenesisConfig};
|
||||
|
||||
/// A prelude to import in tests.
|
||||
@@ -51,8 +47,8 @@ pub mod prelude {
|
||||
};
|
||||
// Client structs
|
||||
pub use super::{
|
||||
Backend, ExecutorDispatch, LightBackend, LightExecutor, LocalExecutorDispatch,
|
||||
NativeElseWasmExecutor, TestClient, TestClientBuilder, WasmExecutionMethod,
|
||||
Backend, ExecutorDispatch, LocalExecutorDispatch, NativeElseWasmExecutor, TestClient,
|
||||
TestClientBuilder, WasmExecutionMethod,
|
||||
};
|
||||
// Keyring
|
||||
pub use super::{AccountKeyring, Sr25519Keyring};
|
||||
@@ -84,26 +80,9 @@ pub type ExecutorDispatch = client::LocalCallExecutor<
|
||||
NativeElseWasmExecutor<LocalExecutorDispatch>,
|
||||
>;
|
||||
|
||||
/// Test client light database backend.
|
||||
pub type LightBackend = substrate_test_client::LightBackend<substrate_test_runtime::Block>;
|
||||
|
||||
/// Test client light executor.
|
||||
pub type LightExecutor = sc_light::GenesisCallExecutor<
|
||||
LightBackend,
|
||||
client::LocalCallExecutor<
|
||||
substrate_test_runtime::Block,
|
||||
sc_light::Backend<
|
||||
sc_client_db::light::LightStorage<substrate_test_runtime::Block>,
|
||||
HashFor<substrate_test_runtime::Block>,
|
||||
>,
|
||||
NativeElseWasmExecutor<LocalExecutorDispatch>,
|
||||
>,
|
||||
>;
|
||||
|
||||
/// Parameters of test-client builder with test-runtime.
|
||||
#[derive(Default)]
|
||||
pub struct GenesisParameters {
|
||||
changes_trie_config: Option<ChangesTrieConfiguration>,
|
||||
heap_pages_override: Option<u64>,
|
||||
extra_storage: Storage,
|
||||
wasm_code: Option<Vec<u8>>,
|
||||
@@ -112,7 +91,6 @@ pub struct GenesisParameters {
|
||||
impl GenesisParameters {
|
||||
fn genesis_config(&self) -> GenesisConfig {
|
||||
GenesisConfig::new(
|
||||
self.changes_trie_config.clone(),
|
||||
vec![
|
||||
sr25519::Public::from(Sr25519Keyring::Alice).into(),
|
||||
sr25519::Public::from(Sr25519Keyring::Bob).into(),
|
||||
@@ -215,12 +193,6 @@ pub trait TestClientBuilderExt<B>: Sized {
|
||||
/// Returns a mutable reference to the genesis parameters.
|
||||
fn genesis_init_mut(&mut self) -> &mut GenesisParameters;
|
||||
|
||||
/// Set changes trie configuration for genesis.
|
||||
fn changes_trie_config(mut self, config: Option<ChangesTrieConfiguration>) -> Self {
|
||||
self.genesis_init_mut().changes_trie_config = config;
|
||||
self
|
||||
}
|
||||
|
||||
/// Override the default value for Wasm heap pages.
|
||||
fn set_heap_pages(mut self, heap_pages: u64) -> Self {
|
||||
self.genesis_init_mut().heap_pages_override = Some(heap_pages);
|
||||
@@ -308,142 +280,11 @@ impl<B> TestClientBuilderExt<B>
|
||||
}
|
||||
}
|
||||
|
||||
/// Type of optional fetch callback.
|
||||
type MaybeFetcherCallback<Req, Resp> =
|
||||
Option<Box<dyn Fn(Req) -> Result<Resp, sp_blockchain::Error> + Send + Sync>>;
|
||||
|
||||
/// Type of fetcher future result.
|
||||
type FetcherFutureResult<Resp> = futures::future::Ready<Result<Resp, sp_blockchain::Error>>;
|
||||
|
||||
/// Implementation of light client fetcher used in tests.
|
||||
#[derive(Default)]
|
||||
pub struct LightFetcher {
|
||||
call: MaybeFetcherCallback<RemoteCallRequest<substrate_test_runtime::Header>, Vec<u8>>,
|
||||
body: MaybeFetcherCallback<
|
||||
RemoteBodyRequest<substrate_test_runtime::Header>,
|
||||
Vec<substrate_test_runtime::Extrinsic>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl LightFetcher {
|
||||
/// Sets remote call callback.
|
||||
pub fn with_remote_call(
|
||||
self,
|
||||
call: MaybeFetcherCallback<RemoteCallRequest<substrate_test_runtime::Header>, Vec<u8>>,
|
||||
) -> Self {
|
||||
LightFetcher { call, body: self.body }
|
||||
}
|
||||
|
||||
/// Sets remote body callback.
|
||||
pub fn with_remote_body(
|
||||
self,
|
||||
body: MaybeFetcherCallback<
|
||||
RemoteBodyRequest<substrate_test_runtime::Header>,
|
||||
Vec<substrate_test_runtime::Extrinsic>,
|
||||
>,
|
||||
) -> Self {
|
||||
LightFetcher { call: self.call, body }
|
||||
}
|
||||
}
|
||||
|
||||
impl Fetcher<substrate_test_runtime::Block> for LightFetcher {
|
||||
type RemoteHeaderResult = FetcherFutureResult<substrate_test_runtime::Header>;
|
||||
type RemoteReadResult = FetcherFutureResult<HashMap<Vec<u8>, Option<Vec<u8>>>>;
|
||||
type RemoteCallResult = FetcherFutureResult<Vec<u8>>;
|
||||
type RemoteChangesResult =
|
||||
FetcherFutureResult<Vec<(NumberFor<substrate_test_runtime::Block>, u32)>>;
|
||||
type RemoteBodyResult = FetcherFutureResult<Vec<substrate_test_runtime::Extrinsic>>;
|
||||
|
||||
fn remote_header(
|
||||
&self,
|
||||
_: RemoteHeaderRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteHeaderResult {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remote_read(
|
||||
&self,
|
||||
_: RemoteReadRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteReadResult {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remote_read_child(
|
||||
&self,
|
||||
_: RemoteReadChildRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteReadResult {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remote_call(
|
||||
&self,
|
||||
req: RemoteCallRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteCallResult {
|
||||
match self.call {
|
||||
Some(ref call) => futures::future::ready(call(req)),
|
||||
None => unimplemented!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn remote_changes(
|
||||
&self,
|
||||
_: RemoteChangesRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteChangesResult {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn remote_body(
|
||||
&self,
|
||||
req: RemoteBodyRequest<substrate_test_runtime::Header>,
|
||||
) -> Self::RemoteBodyResult {
|
||||
match self.body {
|
||||
Some(ref body) => futures::future::ready(body(req)),
|
||||
None => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates new client instance used for tests.
|
||||
pub fn new() -> Client<Backend> {
|
||||
TestClientBuilder::new().build()
|
||||
}
|
||||
|
||||
/// Creates new light client instance used for tests.
|
||||
pub fn new_light() -> (
|
||||
client::Client<
|
||||
LightBackend,
|
||||
LightExecutor,
|
||||
substrate_test_runtime::Block,
|
||||
substrate_test_runtime::RuntimeApi,
|
||||
>,
|
||||
Arc<LightBackend>,
|
||||
) {
|
||||
let storage = sc_client_db::light::LightStorage::new_test();
|
||||
let blockchain = Arc::new(sc_light::Blockchain::new(storage));
|
||||
let backend = Arc::new(LightBackend::new(blockchain));
|
||||
let executor = new_native_executor();
|
||||
let local_call_executor = client::LocalCallExecutor::new(
|
||||
backend.clone(),
|
||||
executor,
|
||||
Box::new(sp_core::testing::TaskExecutor::new()),
|
||||
Default::default(),
|
||||
)
|
||||
.expect("Creates LocalCallExecutor");
|
||||
let call_executor = LightExecutor::new(backend.clone(), local_call_executor);
|
||||
|
||||
(
|
||||
TestClientBuilder::with_backend(backend.clone())
|
||||
.build_with_executor(call_executor)
|
||||
.0,
|
||||
backend,
|
||||
)
|
||||
}
|
||||
|
||||
/// Creates new light client fetcher used for tests.
|
||||
pub fn new_light_fetcher() -> LightFetcher {
|
||||
LightFetcher::default()
|
||||
}
|
||||
|
||||
/// Create a new native executor.
|
||||
pub fn new_native_executor() -> sc_executor::NativeElseWasmExecutor<LocalExecutorDispatch> {
|
||||
sc_executor::NativeElseWasmExecutor::new(sc_executor::WasmExecutionMethod::Interpreted, None, 8)
|
||||
|
||||
@@ -23,7 +23,6 @@ use sc_service::client::genesis;
|
||||
use sp_core::{
|
||||
map,
|
||||
storage::{well_known_keys, Storage},
|
||||
ChangesTrieConfiguration,
|
||||
};
|
||||
use sp_io::hashing::{blake2_256, twox_128};
|
||||
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT};
|
||||
@@ -31,7 +30,6 @@ use std::collections::BTreeMap;
|
||||
|
||||
/// Configuration of a general Substrate test genesis block.
|
||||
pub struct GenesisConfig {
|
||||
changes_trie_config: Option<ChangesTrieConfiguration>,
|
||||
authorities: Vec<AuthorityId>,
|
||||
balances: Vec<(AccountId, u64)>,
|
||||
heap_pages_override: Option<u64>,
|
||||
@@ -41,7 +39,6 @@ pub struct GenesisConfig {
|
||||
|
||||
impl GenesisConfig {
|
||||
pub fn new(
|
||||
changes_trie_config: Option<ChangesTrieConfiguration>,
|
||||
authorities: Vec<AuthorityId>,
|
||||
endowed_accounts: Vec<AccountId>,
|
||||
balance: u64,
|
||||
@@ -49,7 +46,6 @@ impl GenesisConfig {
|
||||
extra_storage: Storage,
|
||||
) -> Self {
|
||||
GenesisConfig {
|
||||
changes_trie_config,
|
||||
authorities,
|
||||
balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(),
|
||||
heap_pages_override,
|
||||
@@ -77,9 +73,6 @@ impl GenesisConfig {
|
||||
.into_iter(),
|
||||
)
|
||||
.collect();
|
||||
if let Some(ref changes_trie_config) = self.changes_trie_config {
|
||||
map.insert(well_known_keys::CHANGES_TRIE_CONFIG.to_vec(), changes_trie_config.encode());
|
||||
}
|
||||
map.insert(twox_128(&b"sys:auth"[..])[..].to_vec(), self.authorities.encode());
|
||||
// Add the extra storage entries.
|
||||
map.extend(self.extra_storage.top.clone().into_iter());
|
||||
|
||||
@@ -28,7 +28,7 @@ use scale_info::TypeInfo;
|
||||
use sp_std::{marker::PhantomData, prelude::*};
|
||||
|
||||
use sp_application_crypto::{ecdsa, ed25519, sr25519, RuntimeAppPublic};
|
||||
use sp_core::{offchain::KeyTypeId, ChangesTrieConfiguration, OpaqueMetadata, RuntimeDebug};
|
||||
use sp_core::{offchain::KeyTypeId, OpaqueMetadata, RuntimeDebug};
|
||||
use sp_trie::{
|
||||
trie_types::{TrieDB, TrieDBMut},
|
||||
PrefixedMemoryDB, StorageProof,
|
||||
@@ -161,7 +161,6 @@ pub enum Extrinsic {
|
||||
},
|
||||
IncludeData(Vec<u8>),
|
||||
StorageChange(Vec<u8>, Option<Vec<u8>>),
|
||||
ChangesTrieConfigUpdate(Option<ChangesTrieConfiguration>),
|
||||
OffchainIndexSet(Vec<u8>, Vec<u8>),
|
||||
OffchainIndexClear(Vec<u8>),
|
||||
Store(Vec<u8>),
|
||||
@@ -197,8 +196,6 @@ impl BlindCheckable for Extrinsic {
|
||||
},
|
||||
Extrinsic::IncludeData(v) => Ok(Extrinsic::IncludeData(v)),
|
||||
Extrinsic::StorageChange(key, value) => Ok(Extrinsic::StorageChange(key, value)),
|
||||
Extrinsic::ChangesTrieConfigUpdate(new_config) =>
|
||||
Ok(Extrinsic::ChangesTrieConfigUpdate(new_config)),
|
||||
Extrinsic::OffchainIndexSet(key, value) => Ok(Extrinsic::OffchainIndexSet(key, value)),
|
||||
Extrinsic::OffchainIndexClear(key) => Ok(Extrinsic::OffchainIndexClear(key)),
|
||||
Extrinsic::Store(data) => Ok(Extrinsic::Store(data)),
|
||||
@@ -265,9 +262,9 @@ pub type BlockNumber = u64;
|
||||
/// Index of a transaction.
|
||||
pub type Index = u64;
|
||||
/// The item of a block digest.
|
||||
pub type DigestItem = sp_runtime::generic::DigestItem<H256>;
|
||||
pub type DigestItem = sp_runtime::generic::DigestItem;
|
||||
/// The digest of a block.
|
||||
pub type Digest = sp_runtime::generic::Digest<H256>;
|
||||
pub type Digest = sp_runtime::generic::Digest;
|
||||
/// A test block.
|
||||
pub type Block = sp_runtime::generic::Block<Header, Extrinsic>;
|
||||
/// A test block's header.
|
||||
@@ -1264,15 +1261,13 @@ fn test_witness(proof: StorageProof, root: crate::Hash) {
|
||||
let db: sp_trie::MemoryDB<crate::Hashing> = proof.into_memory_db();
|
||||
let backend = sp_state_machine::TrieBackend::<_, crate::Hashing>::new(db, root);
|
||||
let mut overlay = sp_state_machine::OverlayedChanges::default();
|
||||
let mut cache = sp_state_machine::StorageTransactionCache::<_, _, BlockNumber>::default();
|
||||
let mut cache = sp_state_machine::StorageTransactionCache::<_, _>::default();
|
||||
let mut ext = sp_state_machine::Ext::new(
|
||||
&mut overlay,
|
||||
&mut cache,
|
||||
&backend,
|
||||
#[cfg(feature = "std")]
|
||||
None,
|
||||
#[cfg(feature = "std")]
|
||||
None,
|
||||
);
|
||||
assert!(ext.storage(b"value3").is_some());
|
||||
assert!(ext.storage_root().as_slice() == &root[..]);
|
||||
|
||||
@@ -24,12 +24,8 @@ use crate::{
|
||||
use codec::{Decode, Encode, KeyedVec};
|
||||
use frame_support::{decl_module, decl_storage, storage};
|
||||
use frame_system::Config;
|
||||
use sp_core::{storage::well_known_keys, ChangesTrieConfiguration};
|
||||
use sp_io::{
|
||||
hashing::blake2_256,
|
||||
storage::{changes_root as storage_changes_root, root as storage_root},
|
||||
trie,
|
||||
};
|
||||
use sp_core::storage::well_known_keys;
|
||||
use sp_io::{hashing::blake2_256, storage::root as storage_root, trie};
|
||||
use sp_runtime::{
|
||||
generic,
|
||||
traits::Header as _,
|
||||
@@ -54,7 +50,6 @@ decl_storage! {
|
||||
Number get(fn number): Option<BlockNumber>;
|
||||
ParentHash get(fn parent_hash): Hash;
|
||||
NewAuthorities get(fn new_authorities): Option<Vec<AuthorityId>>;
|
||||
NewChangesTrieConfig get(fn new_changes_trie_config): Option<Option<ChangesTrieConfiguration>>;
|
||||
StorageDigest get(fn storage_digest): Option<Digest>;
|
||||
Authorities get(fn authorities) config(): Vec<AuthorityId>;
|
||||
}
|
||||
@@ -207,30 +202,17 @@ pub fn finalize_block() -> Header {
|
||||
let mut digest = <StorageDigest>::take().expect("StorageDigest is set by `initialize_block`");
|
||||
|
||||
let o_new_authorities = <NewAuthorities>::take();
|
||||
let new_changes_trie_config = <NewChangesTrieConfig>::take();
|
||||
|
||||
// This MUST come after all changes to storage are done. Otherwise we will fail the
|
||||
// “Storage root does not match that calculated” assertion.
|
||||
let storage_root =
|
||||
Hash::decode(&mut &storage_root()[..]).expect("`storage_root` is a valid hash");
|
||||
let storage_changes_root = storage_changes_root(&parent_hash.encode())
|
||||
.map(|r| Hash::decode(&mut &r[..]).expect("`storage_changes_root` is a valid hash"));
|
||||
|
||||
if let Some(storage_changes_root) = storage_changes_root {
|
||||
digest.push(generic::DigestItem::ChangesTrieRoot(storage_changes_root));
|
||||
}
|
||||
|
||||
if let Some(new_authorities) = o_new_authorities {
|
||||
digest.push(generic::DigestItem::Consensus(*b"aura", new_authorities.encode()));
|
||||
digest.push(generic::DigestItem::Consensus(*b"babe", new_authorities.encode()));
|
||||
}
|
||||
|
||||
if let Some(new_config) = new_changes_trie_config {
|
||||
digest.push(generic::DigestItem::ChangesTrieSignal(
|
||||
generic::ChangesTrieSignal::NewConfiguration(new_config),
|
||||
));
|
||||
}
|
||||
|
||||
Header { number, extrinsics_root, state_root: storage_root, parent_hash, digest }
|
||||
}
|
||||
|
||||
@@ -251,8 +233,6 @@ fn execute_transaction_backend(utx: &Extrinsic, extrinsic_index: u32) -> ApplyEx
|
||||
Extrinsic::IncludeData(_) => Ok(Ok(())),
|
||||
Extrinsic::StorageChange(key, value) =>
|
||||
execute_storage_change(key, value.as_ref().map(|v| &**v)),
|
||||
Extrinsic::ChangesTrieConfigUpdate(ref new_config) =>
|
||||
execute_changes_trie_config_update(new_config.clone()),
|
||||
Extrinsic::OffchainIndexSet(key, value) => {
|
||||
sp_io::offchain_index::set(&key, &value);
|
||||
Ok(Ok(()))
|
||||
@@ -311,18 +291,6 @@ fn execute_storage_change(key: &[u8], value: Option<&[u8]>) -> ApplyExtrinsicRes
|
||||
Ok(Ok(()))
|
||||
}
|
||||
|
||||
fn execute_changes_trie_config_update(
|
||||
new_config: Option<ChangesTrieConfiguration>,
|
||||
) -> ApplyExtrinsicResult {
|
||||
match new_config.clone() {
|
||||
Some(new_config) =>
|
||||
storage::unhashed::put_raw(well_known_keys::CHANGES_TRIE_CONFIG, &new_config.encode()),
|
||||
None => storage::unhashed::kill(well_known_keys::CHANGES_TRIE_CONFIG),
|
||||
}
|
||||
<NewChangesTrieConfig>::put(new_config);
|
||||
Ok(Ok(()))
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn info_expect_equal_hash(given: &Hash, expected: &Hash) {
|
||||
use sp_core::hexdisplay::HexDisplay;
|
||||
|
||||
Reference in New Issue
Block a user