referrerpolicy=no-referrer-when-downgrade
polkadot_sdk_frame::testing_prelude

Type Alias TestExternalities

pub type TestExternalities = TestExternalities<Blake2Hasher>;
Expand description

Type alias for Externalities implementation used in tests.

Aliased Type§

struct TestExternalities {
    pub backend: TrieBackend<MemoryDB<Blake2Hasher, PrefixedKey<Blake2Hasher>, Vec<u8>>, Blake2Hasher>,
    pub extensions: Extensions,
    pub state_version: StateVersion,
    /* private fields */
}

Fields§

§backend: TrieBackend<MemoryDB<Blake2Hasher, PrefixedKey<Blake2Hasher>, Vec<u8>>, Blake2Hasher>

Storage backend.

§extensions: Extensions

Extensions.

§state_version: StateVersion

State version to use during tests.

Implementations

§

impl<H> TestExternalities<H>
where H: Hasher + 'static, <H as Hasher>::Out: Ord + 'static + Codec,

pub fn ext( &mut self, ) -> Ext<'_, H, TrieBackend<MemoryDB<H, PrefixedKey<H>, Vec<u8>>, H>>

Get externalities implementation.

pub fn new(storage: Storage) -> TestExternalities<H>

Create a new instance of TestExternalities with storage.

pub fn new_with_state_version( storage: Storage, state_version: StateVersion, ) -> TestExternalities<H>

Create a new instance of TestExternalities with storage for a given state version.

pub fn new_empty() -> TestExternalities<H>

New empty test externalities.

pub fn new_with_code(code: &[u8], storage: Storage) -> TestExternalities<H>

Create a new instance of TestExternalities with code and storage.

pub fn new_with_code_and_state( code: &[u8], storage: Storage, state_version: StateVersion, ) -> TestExternalities<H>

Create a new instance of TestExternalities with code and storage for a given state version.

pub fn overlayed_changes(&self) -> &OverlayedChanges<H>

Returns the overlayed changes.

pub fn persist_offchain_overlay(&mut self)

Move offchain changes from overlay to the persistent store.

pub fn offchain_db(&self) -> TestPersistentOffchainDB

A shared reference type around the offchain worker storage.

pub fn batch_insert<I>(&mut self, kvs: I)
where I: IntoIterator<Item = (Vec<u8>, Vec<u8>)>,

Batch insert key/values into backend

pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>)

Insert key/value into backend

pub fn insert_child(&mut self, c: ChildInfo, k: Vec<u8>, v: Vec<u8>)

Insert key/value into backend.

This only supports inserting keys in child tries.

pub fn register_extension<E>(&mut self, ext: E)
where E: Any + Extension,

Registers the given extension for this instance.

pub fn from_raw_snapshot( raw_storage: Vec<(Vec<u8>, (Vec<u8>, i32))>, storage_root: <H as Hasher>::Out, state_version: StateVersion, ) -> TestExternalities<H>

Sets raw storage key/values and a root.

This can be used as a fast way to restore the storage state from a backup because the trie does not need to be computed.

pub fn into_raw_snapshot( self, ) -> (Vec<(Vec<u8>, (Vec<u8>, i32))>, <H as Hasher>::Out)

Drains the underlying raw storage key/values and returns the root hash.

Useful for backing up the storage in a format that can be quickly re-loaded.

pub fn as_backend( &mut self, ) -> TrieBackend<MemoryDB<H, PrefixedKey<H>, Vec<u8>>, H>

Return a new backend with all pending changes.

In contrast to commit_all this will not panic if there are open transactions.

pub fn commit_all(&mut self) -> Result<(), String>

Commit all pending changes to the underlying backend.

§Panic

This will panic if there are still open transactions.

pub fn execute_with<R>(&mut self, execute: impl FnOnce() -> R) -> R

Execute the given closure while self is set as externalities.

Returns the result of the given closure.

pub fn execute_and_prove<R>( &mut self, execute: impl FnOnce() -> R, ) -> (R, StorageProof)

Execute the given closure while self, with proving_backend as backend, is set as externalities.

This implementation will wipe the proof recorded in between calls. Consecutive calls will get their own proof from scratch.

pub fn execute_with_safe<R>( &mut self, f: impl FnOnce() -> R + UnwindSafe, ) -> Result<R, String>

Execute the given closure while self is set as externalities.

Returns the result of the given closure, if no panics occurred. Otherwise, returns Err.

§

impl<H> TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + 'static + Codec,

pub fn eq(&mut self, other: &mut TestExternalities<H>) -> bool

This doesn’t test if they are in the same state, only if they contains the same data at this state

Trait Implementations

§

impl<H> Debug for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + Codec,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<H> Default for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + 'static + Codec,

§

fn default() -> TestExternalities<H>

Returns the “default value” for a type. Read more
§

impl<H> ExtensionStore for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + Codec,

§

fn extension_by_type_id( &mut self, type_id: TypeId, ) -> Option<&mut (dyn Any + 'static)>

Tries to find a registered extension by the given type_id and returns it as a &mut dyn Any. Read more
§

fn register_extension_with_type_id( &mut self, type_id: TypeId, extension: Box<dyn Extension>, ) -> Result<(), Error>

Register extension extension with specified type_id. Read more
§

fn deregister_extension_by_type_id( &mut self, type_id: TypeId, ) -> Result<(), Error>

Deregister extension with specified ‘type_id’ and drop it. Read more
§

impl<H> ExternalitiesExt for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + Codec,

§

fn extension<T>(&mut self) -> Option<&mut T>
where T: Any + Extension,

Tries to find a registered extension and returns a mutable reference.
§

fn register_extension<T>(&mut self, ext: T) -> Result<(), Error>
where T: Extension,

Register extension ext. Read more
§

fn deregister_extension<T>(&mut self) -> Result<(), Error>
where T: Extension,

Deregister and drop extension of T type. Read more
§

impl<H> From<(Storage, StateVersion)> for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + 'static + Codec,

§

fn from(_: (Storage, StateVersion)) -> TestExternalities<H>

Converts to this type from the input type.
§

impl<H> From<Storage> for TestExternalities<H>
where H: Hasher, <H as Hasher>::Out: Ord + 'static + Codec,

§

fn from(storage: Storage) -> TestExternalities<H>

Converts to this type from the input type.