use alloc::{boxed::Box, vec::Vec};
use core::any::{Any, TypeId};
use crate::storage::{ChildInfo, StateVersion, TrackedStorageKey};
pub use extensions::{Extension, ExtensionStore, Extensions, TransactionType};
pub use scope_limited::{set_and_run_with_externalities, with_externalities};
mod extensions;
mod scope_limited;
#[derive(Debug)]
pub enum Error {
ExtensionAlreadyRegistered,
ExtensionsAreNotSupported,
ExtensionIsNotRegistered(TypeId),
StorageUpdateFailed(&'static str),
}
#[derive(codec::Encode, codec::Decode)]
#[must_use]
pub struct MultiRemovalResults {
pub maybe_cursor: Option<Vec<u8>>,
pub backend: u32,
pub unique: u32,
pub loops: u32,
}
impl MultiRemovalResults {
pub fn deconstruct(self) -> (Option<Vec<u8>>, u32, u32, u32) {
(self.maybe_cursor, self.backend, self.unique, self.loops)
}
}
pub trait Externalities: ExtensionStore {
fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>);
fn storage(&mut self, key: &[u8]) -> Option<Vec<u8>>;
fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn child_storage(&mut 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(&mut self, key: &[u8]) -> bool {
self.storage(key).is_some()
}
fn exists_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> bool {
self.child_storage(child_info, key).is_some()
}
fn next_storage_key(&mut self, key: &[u8]) -> Option<Vec<u8>>;
fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults;
fn clear_prefix(
&mut self,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults;
fn clear_child_prefix(
&mut self,
child_info: &ChildInfo,
prefix: &[u8],
maybe_limit: Option<u32>,
maybe_cursor: Option<&[u8]>,
) -> MultiRemovalResults;
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, state_version: StateVersion) -> Vec<u8>;
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
state_version: StateVersion,
) -> Vec<u8>;
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>);
fn storage_start_transaction(&mut self);
fn storage_rollback_transaction(&mut self) -> Result<(), ()>;
fn storage_commit_transaction(&mut self) -> Result<(), ()>;
fn storage_index_transaction(&mut self, _index: u32, _hash: &[u8], _size: u32) {
unimplemented!("storage_index_transaction");
}
fn storage_renew_transaction_index(&mut self, _index: u32, _hash: &[u8]) {
unimplemented!("storage_renew_transaction_index");
}
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>);
fn proof_size(&self) -> Option<u32> {
None
}
fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)>;
}
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(<dyn 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>())
}
}