mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-22 12:41:04 +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:
@@ -28,8 +28,6 @@ use sp_core::storage::{well_known_keys::is_child_storage_key, ChildInfo, Tracked
|
||||
use sp_externalities::{Extension, ExtensionStore, Externalities};
|
||||
use sp_trie::{empty_child_trie_root, trie_types::Layout};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use crate::changes_trie::State as ChangesTrieState;
|
||||
use crate::{log_error, trace, warn, StorageTransactionCache};
|
||||
use sp_std::{
|
||||
any::{Any, TypeId},
|
||||
@@ -90,62 +88,52 @@ impl<B: error::Error, E: error::Error> error::Error for Error<B, E> {
|
||||
}
|
||||
|
||||
/// Wraps a read-only backend, call executor, and current overlayed changes.
|
||||
pub struct Ext<'a, H, N, B>
|
||||
pub struct Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
B: 'a + Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
/// The overlayed changes to write to.
|
||||
overlay: &'a mut OverlayedChanges,
|
||||
/// The storage backend to read from.
|
||||
backend: &'a B,
|
||||
/// The cache for the storage transactions.
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H, N>,
|
||||
/// Changes trie state to read from.
|
||||
#[cfg(feature = "std")]
|
||||
changes_trie_state: Option<ChangesTrieState<'a, H, N>>,
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H>,
|
||||
/// Pseudo-unique id used for tracing.
|
||||
pub id: u16,
|
||||
/// Dummy usage of N arg.
|
||||
_phantom: sp_std::marker::PhantomData<N>,
|
||||
/// Extensions registered with this instance.
|
||||
#[cfg(feature = "std")]
|
||||
extensions: Option<OverlayedExtensions<'a>>,
|
||||
}
|
||||
|
||||
impl<'a, H, N, B> Ext<'a, H, N, B>
|
||||
impl<'a, H, B> Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
B: Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
/// Create a new `Ext`.
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub fn new(
|
||||
overlay: &'a mut OverlayedChanges,
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H, N>,
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H>,
|
||||
backend: &'a B,
|
||||
) -> Self {
|
||||
Ext { overlay, backend, id: 0, storage_transaction_cache, _phantom: Default::default() }
|
||||
Ext { overlay, backend, id: 0, storage_transaction_cache }
|
||||
}
|
||||
|
||||
/// Create a new `Ext` from overlayed changes and read-only backend
|
||||
#[cfg(feature = "std")]
|
||||
pub fn new(
|
||||
overlay: &'a mut OverlayedChanges,
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H, N>,
|
||||
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H>,
|
||||
backend: &'a B,
|
||||
changes_trie_state: Option<ChangesTrieState<'a, H, N>>,
|
||||
extensions: Option<&'a mut sp_externalities::Extensions>,
|
||||
) -> Self {
|
||||
Self {
|
||||
overlay,
|
||||
backend,
|
||||
changes_trie_state,
|
||||
storage_transaction_cache,
|
||||
id: rand::random(),
|
||||
_phantom: Default::default(),
|
||||
extensions: extensions.map(OverlayedExtensions::new),
|
||||
}
|
||||
}
|
||||
@@ -159,12 +147,11 @@ where
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl<'a, H, N, B> Ext<'a, H, N, B>
|
||||
impl<'a, H, B> Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + 'static,
|
||||
B: 'a + Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
pub fn storage_pairs(&self) -> Vec<(StorageKey, StorageValue)> {
|
||||
use std::collections::HashMap;
|
||||
@@ -181,12 +168,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, H, N, B> Externalities for Ext<'a, H, N, B>
|
||||
impl<'a, H, B> Externalities for Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
B: Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>) {
|
||||
self.overlay.set_offchain_storage(key, value)
|
||||
@@ -644,54 +630,6 @@ where
|
||||
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn storage_changes_root(&mut self, _parent_hash: &[u8]) -> Result<Option<Vec<u8>>, ()> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn storage_changes_root(&mut self, mut parent_hash: &[u8]) -> Result<Option<Vec<u8>>, ()> {
|
||||
let _guard = guard();
|
||||
if let Some(ref root) = self.storage_transaction_cache.changes_trie_transaction_storage_root
|
||||
{
|
||||
trace!(
|
||||
target: "state",
|
||||
method = "ChangesRoot",
|
||||
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
|
||||
parent_hash = %HexDisplay::from(&parent_hash),
|
||||
?root,
|
||||
cached = true,
|
||||
);
|
||||
|
||||
Ok(Some(root.encode()))
|
||||
} else {
|
||||
let root = self.overlay.changes_trie_root(
|
||||
self.backend,
|
||||
self.changes_trie_state.as_ref(),
|
||||
Decode::decode(&mut parent_hash).map_err(|e| {
|
||||
trace!(
|
||||
target: "state",
|
||||
error = %e,
|
||||
"Failed to decode changes root parent hash",
|
||||
)
|
||||
})?,
|
||||
true,
|
||||
self.storage_transaction_cache,
|
||||
);
|
||||
|
||||
trace!(
|
||||
target: "state",
|
||||
method = "ChangesRoot",
|
||||
ext_id = %HexDisplay::from(&self.id.to_le_bytes()),
|
||||
parent_hash = %HexDisplay::from(&parent_hash),
|
||||
?root,
|
||||
cached = false,
|
||||
);
|
||||
|
||||
root.map(|r| r.map(|o| o.encode()))
|
||||
}
|
||||
}
|
||||
|
||||
fn storage_start_transaction(&mut self) {
|
||||
self.overlay.start_transaction()
|
||||
}
|
||||
@@ -710,13 +648,7 @@ where
|
||||
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
|
||||
}
|
||||
self.overlay
|
||||
.drain_storage_changes(
|
||||
self.backend,
|
||||
#[cfg(feature = "std")]
|
||||
None,
|
||||
Default::default(),
|
||||
self.storage_transaction_cache,
|
||||
)
|
||||
.drain_storage_changes(self.backend, Default::default(), self.storage_transaction_cache)
|
||||
.expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.mark_dirty();
|
||||
@@ -731,13 +663,7 @@ where
|
||||
}
|
||||
let changes = self
|
||||
.overlay
|
||||
.drain_storage_changes(
|
||||
self.backend,
|
||||
#[cfg(feature = "std")]
|
||||
None,
|
||||
Default::default(),
|
||||
self.storage_transaction_cache,
|
||||
)
|
||||
.drain_storage_changes(self.backend, Default::default(), self.storage_transaction_cache)
|
||||
.expect(EXT_NOT_ALLOWED_TO_FAIL);
|
||||
self.backend
|
||||
.commit(
|
||||
@@ -778,12 +704,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, H, N, B> Ext<'a, H, N, B>
|
||||
impl<'a, H, B> Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
B: Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
fn limit_remove_from_backend(
|
||||
&mut self,
|
||||
@@ -869,12 +794,11 @@ impl<'a> StorageAppend<'a> {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
impl<'a, H, N, B> ExtensionStore for Ext<'a, H, N, B>
|
||||
impl<'a, H, B> ExtensionStore for Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
B: Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
fn extension_by_type_id(&mut self, _type_id: TypeId) -> Option<&mut dyn Any> {
|
||||
None
|
||||
@@ -897,11 +821,10 @@ where
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'a, H, N, B> ExtensionStore for Ext<'a, H, N, B>
|
||||
impl<'a, H, B> ExtensionStore for Ext<'a, H, B>
|
||||
where
|
||||
H: Hasher,
|
||||
B: 'a + Backend<H>,
|
||||
N: crate::changes_trie::BlockNumber,
|
||||
{
|
||||
fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
|
||||
self.extensions.as_mut().and_then(|exts| exts.get_mut(type_id))
|
||||
@@ -938,86 +861,16 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
changes_trie::{
|
||||
Configuration as ChangesTrieConfiguration, InMemoryStorage as TestChangesTrieStorage,
|
||||
},
|
||||
InMemoryBackend,
|
||||
};
|
||||
use crate::InMemoryBackend;
|
||||
use codec::Encode;
|
||||
use hex_literal::hex;
|
||||
use num_traits::Zero;
|
||||
use sp_core::{
|
||||
map,
|
||||
storage::{well_known_keys::EXTRINSIC_INDEX, Storage, StorageChild},
|
||||
Blake2Hasher, H256,
|
||||
storage::{Storage, StorageChild},
|
||||
Blake2Hasher,
|
||||
};
|
||||
|
||||
type TestBackend = InMemoryBackend<Blake2Hasher>;
|
||||
type TestExt<'a> = Ext<'a, Blake2Hasher, u64, TestBackend>;
|
||||
|
||||
fn prepare_overlay_with_changes() -> OverlayedChanges {
|
||||
let mut changes = OverlayedChanges::default();
|
||||
changes.set_collect_extrinsics(true);
|
||||
changes.set_extrinsic_index(1);
|
||||
changes.set_storage(vec![1], Some(vec![100]));
|
||||
changes.set_storage(EXTRINSIC_INDEX.to_vec(), Some(3u32.encode()));
|
||||
changes.set_offchain_storage(b"k1", Some(b"v1"));
|
||||
changes.set_offchain_storage(b"k2", Some(b"v2"));
|
||||
changes
|
||||
}
|
||||
|
||||
fn changes_trie_config() -> ChangesTrieConfiguration {
|
||||
ChangesTrieConfiguration { digest_interval: 0, digest_levels: 0 }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_changes_root_is_none_when_storage_is_not_provided() {
|
||||
let mut overlay = prepare_overlay_with_changes();
|
||||
let mut cache = StorageTransactionCache::default();
|
||||
let backend = TestBackend::default();
|
||||
let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
assert_eq!(ext.storage_changes_root(&H256::default().encode()).unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_changes_root_is_none_when_state_is_not_provided() {
|
||||
let mut overlay = prepare_overlay_with_changes();
|
||||
let mut cache = StorageTransactionCache::default();
|
||||
let backend = TestBackend::default();
|
||||
let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
assert_eq!(ext.storage_changes_root(&H256::default().encode()).unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_changes_root_is_some_when_extrinsic_changes_are_non_empty() {
|
||||
let mut overlay = prepare_overlay_with_changes();
|
||||
let mut cache = StorageTransactionCache::default();
|
||||
let storage = TestChangesTrieStorage::with_blocks(vec![(99, Default::default())]);
|
||||
let state = Some(ChangesTrieState::new(changes_trie_config(), Zero::zero(), &storage));
|
||||
let backend = TestBackend::default();
|
||||
let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, state, None);
|
||||
assert_eq!(
|
||||
ext.storage_changes_root(&H256::default().encode()).unwrap(),
|
||||
Some(hex!("bb0c2ef6e1d36d5490f9766cfcc7dfe2a6ca804504c3bb206053890d6dd02376").to_vec()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_changes_root_is_some_when_extrinsic_changes_are_empty() {
|
||||
let mut overlay = prepare_overlay_with_changes();
|
||||
let mut cache = StorageTransactionCache::default();
|
||||
overlay.set_collect_extrinsics(false);
|
||||
overlay.set_storage(vec![1], None);
|
||||
let storage = TestChangesTrieStorage::with_blocks(vec![(99, Default::default())]);
|
||||
let state = Some(ChangesTrieState::new(changes_trie_config(), Zero::zero(), &storage));
|
||||
let backend = TestBackend::default();
|
||||
let mut ext = TestExt::new(&mut overlay, &mut cache, &backend, state, None);
|
||||
assert_eq!(
|
||||
ext.storage_changes_root(&H256::default().encode()).unwrap(),
|
||||
Some(hex!("96f5aae4690e7302737b6f9b7f8567d5bbb9eac1c315f80101235a92d9ec27f4").to_vec()),
|
||||
);
|
||||
}
|
||||
type TestExt<'a> = Ext<'a, Blake2Hasher, TestBackend>;
|
||||
|
||||
#[test]
|
||||
fn next_storage_key_works() {
|
||||
@@ -1035,7 +888,7 @@ mod tests {
|
||||
}
|
||||
.into();
|
||||
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
// next_backend < next_overlay
|
||||
assert_eq!(ext.next_storage_key(&[5]), Some(vec![10]));
|
||||
@@ -1051,7 +904,7 @@ mod tests {
|
||||
|
||||
drop(ext);
|
||||
overlay.set_storage(vec![50], Some(vec![50]));
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
// next_overlay exist but next_backend doesn't exist
|
||||
assert_eq!(ext.next_storage_key(&[40]), Some(vec![50]));
|
||||
@@ -1079,7 +932,7 @@ mod tests {
|
||||
}
|
||||
.into();
|
||||
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
assert_eq!(ext.next_storage_key(&[5]), Some(vec![30]));
|
||||
|
||||
@@ -1110,7 +963,7 @@ mod tests {
|
||||
}
|
||||
.into();
|
||||
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
// next_backend < next_overlay
|
||||
assert_eq!(ext.next_child_storage_key(child_info, &[5]), Some(vec![10]));
|
||||
@@ -1126,7 +979,7 @@ mod tests {
|
||||
|
||||
drop(ext);
|
||||
overlay.set_child_storage(child_info, vec![50], Some(vec![50]));
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
// next_overlay exist but next_backend doesn't exist
|
||||
assert_eq!(ext.next_child_storage_key(child_info, &[40]), Some(vec![50]));
|
||||
@@ -1155,7 +1008,7 @@ mod tests {
|
||||
}
|
||||
.into();
|
||||
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
assert_eq!(ext.child_storage(child_info, &[10]), Some(vec![10]));
|
||||
assert_eq!(
|
||||
@@ -1192,7 +1045,7 @@ mod tests {
|
||||
}
|
||||
.into();
|
||||
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
|
||||
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None);
|
||||
|
||||
use sp_core::storage::well_known_keys;
|
||||
let mut ext = ext;
|
||||
|
||||
Reference in New Issue
Block a user