Skip to main content

Storage

Trait Storage 

Source
pub trait Storage: Clone {
    type Witness: Witness + Send + Sync;
    type RuntimeConfig;
    type Proof: Serialize + DeserializeOwned + Debug + Clone + BorshSerialize + BorshDeserialize;
    type Root: Serialize + DeserializeOwned + Debug + Clone + BorshSerialize + BorshDeserialize + Eq + AsRef<[u8]> + Into<[u8; 32]>;
    type StateUpdate;

    // Required methods
    fn with_config(config: Self::RuntimeConfig) -> Result<Self, Error>;
    fn get(
        &self,
        key: &StorageKey,
        witness: &Self::Witness,
    ) -> Option<StorageValue>;
    fn compute_state_update(
        &self,
        state_accesses: OrderedReadsAndWrites,
        witness: &Self::Witness,
    ) -> Result<(Self::Root, Self::StateUpdate), Error>;
    fn commit(
        &self,
        node_batch: &Self::StateUpdate,
        accessory_update: &OrderedReadsAndWrites,
    );
    fn open_proof(
        state_root: Self::Root,
        proof: StorageProof<Self::Proof>,
    ) -> Result<(StorageKey, Option<StorageValue>), Error>;
    fn is_empty(&self) -> bool;

    // Provided methods
    fn get_accessory(&self, _key: &StorageKey) -> Option<StorageValue> { ... }
    fn validate_and_commit(
        &self,
        state_accesses: OrderedReadsAndWrites,
        witness: &Self::Witness,
    ) -> Result<Self::Root, Error> { ... }
    fn validate_and_commit_with_accessory_update(
        &self,
        state_accesses: OrderedReadsAndWrites,
        witness: &Self::Witness,
        accessory_update: &OrderedReadsAndWrites,
    ) -> Result<Self::Root, Error> { ... }
}
Expand description

An interface for storing and retrieving values in the storage.

Required Associated Types§

Source

type Witness: Witness + Send + Sync

The witness type for this storage instance.

Source

type RuntimeConfig

The runtime config for this storage instance.

Source

type Proof: Serialize + DeserializeOwned + Debug + Clone + BorshSerialize + BorshDeserialize

A cryptographic proof that a particular key has a particular value, or is absent.

Source

type Root: Serialize + DeserializeOwned + Debug + Clone + BorshSerialize + BorshDeserialize + Eq + AsRef<[u8]> + Into<[u8; 32]>

A cryptographic commitment to the contents of this storage

Source

type StateUpdate

State update that will be committed to the database.

Required Methods§

Source

fn with_config(config: Self::RuntimeConfig) -> Result<Self, Error>

Creates a new instance of this Storage type, with some configuration options.

Source

fn get(&self, key: &StorageKey, witness: &Self::Witness) -> Option<StorageValue>

Returns the value corresponding to the key or None if key is absent.

Source

fn compute_state_update( &self, state_accesses: OrderedReadsAndWrites, witness: &Self::Witness, ) -> Result<(Self::Root, Self::StateUpdate), Error>

Calculates new state root but does not commit any changes to the database.

Source

fn commit( &self, node_batch: &Self::StateUpdate, accessory_update: &OrderedReadsAndWrites, )

Commits state changes to the database.

Source

fn open_proof( state_root: Self::Root, proof: StorageProof<Self::Proof>, ) -> Result<(StorageKey, Option<StorageValue>), Error>

Opens a storage access proof and validates it against a state root. It returns a result with the opened leaf (key, value) pair in case of success.

Source

fn is_empty(&self) -> bool

Indicates if storage is empty or not. Useful during initialization.

Provided Methods§

Source

fn get_accessory(&self, _key: &StorageKey) -> Option<StorageValue>

Returns the value corresponding to the key or None if key is absent.

§About accessory state

This method is blanket-implemented to return None. Only native execution environments (i.e. outside of the zmVM) SHOULD override this method to return a value. This is because accessory state MUST NOT be readable from within the zmVM.

Source

fn validate_and_commit( &self, state_accesses: OrderedReadsAndWrites, witness: &Self::Witness, ) -> Result<Self::Root, Error>

Validate all of the storage accesses in a particular cache log, returning the new state root after applying all writes. This function is equivalent to calling: self.compute_state_update & self.commit

Source

fn validate_and_commit_with_accessory_update( &self, state_accesses: OrderedReadsAndWrites, witness: &Self::Witness, accessory_update: &OrderedReadsAndWrites, ) -> Result<Self::Root, Error>

A version of Storage::validate_and_commit that allows for “accessory” non-JMT updates. See sov_db::NativeDB for more information about accessory state.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§