#![cfg_attr(not(feature = "std"), no_std)]
use tetcore_std::{any::{Any, TypeId}, vec::Vec, boxed::Box};
use tetcore_storage::{ChildInfo, TrackedStorageKey};
pub use scope_limited::{set_and_run_with_externalities, with_externalities};
pub use extensions::{Extension, Extensions, ExtensionStore};
mod extensions;
mod scope_limited;
#[derive(Debug)]
pub enum Error {
ExtensionAlreadyRegistered,
ExtensionsAreNotSupported,
ExtensionIsNotRegistered(TypeId),
StorageUpdateFailed(&'static str),
}
pub trait Externalities: ExtensionStore {
fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>);
fn storage(&self, key: &[u8]) -> Option<Vec<u8>>;
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage_hash(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>>;
fn child_storage(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>>;
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.place_storage(key, Some(value));
}
fn set_child_storage(
&mut self,
child_info: &ChildInfo,
key: Vec<u8>,
value: Vec<u8>,
) {
self.place_child_storage(child_info, key, Some(value))
}
fn clear_storage(&mut self, key: &[u8]) {
self.place_storage(key.to_vec(), None);
}
fn clear_child_storage(
&mut self,
child_info: &ChildInfo,
key: &[u8],
) {
self.place_child_storage(child_info, key.to_vec(), None)
}
fn exists_storage(&self, key: &[u8]) -> bool {
self.storage(key).is_some()
}
fn exists_child_storage(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> bool {
self.child_storage(child_info, key).is_some()
}
fn next_storage_key(&self, key: &[u8]) -> Option<Vec<u8>>;
fn next_child_storage_key(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>>;
fn kill_child_storage(&mut self, child_info: &ChildInfo, limit: Option<u32>) -> bool;
fn clear_prefix(&mut self, prefix: &[u8]);
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
);
fn place_storage(&mut self, key: Vec<u8>, value: Option<Vec<u8>>);
fn place_child_storage(
&mut self,
child_info: &ChildInfo,
key: Vec<u8>,
value: Option<Vec<u8>>,
);
fn storage_root(&mut self) -> Vec<u8>;
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
) -> Vec<u8>;
fn storage_append(
&mut self,
key: Vec<u8>,
value: Vec<u8>,
);
fn storage_changes_root(&mut self, parent: &[u8]) -> Result<Option<Vec<u8>>, ()>;
fn storage_start_transaction(&mut self);
fn storage_rollback_transaction(&mut self) -> Result<(), ()>;
fn storage_commit_transaction(&mut self) -> Result<(), ()>;
fn wipe(&mut self);
fn commit(&mut self);
fn read_write_count(&self) -> (u32, u32, u32, u32);
fn reset_read_write_count(&mut self);
fn get_whitelist(&self) -> Vec<TrackedStorageKey>;
fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>);
}
pub trait ExternalitiesExt {
fn extension<T: Any + Extension>(&mut self) -> Option<&mut T>;
fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error>;
fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error>;
}
impl ExternalitiesExt for &mut dyn Externalities {
fn extension<T: Any + Extension>(&mut self) -> Option<&mut T> {
self.extension_by_type_id(TypeId::of::<T>()).and_then(Any::downcast_mut)
}
fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error> {
self.register_extension_with_type_id(TypeId::of::<T>(), Box::new(ext))
}
fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error> {
self.deregister_extension_by_type_id(TypeId::of::<T>())
}
}