Trait cosmwasm_vm::Storage

source ·
pub trait Storage {
    // Required methods
    fn get(&self, key: &[u8]) -> BackendResult<Option<Vec<u8>>>;
    fn scan(
        &mut self,
        start: Option<&[u8]>,
        end: Option<&[u8]>,
        order: Order
    ) -> BackendResult<u32>;
    fn next(&mut self, iterator_id: u32) -> BackendResult<Option<Record>>;
    fn set(&mut self, key: &[u8], value: &[u8]) -> BackendResult<()>;
    fn remove(&mut self, key: &[u8]) -> BackendResult<()>;

    // Provided methods
    fn next_value(&mut self, iterator_id: u32) -> BackendResult<Option<Vec<u8>>> { ... }
    fn next_key(&mut self, iterator_id: u32) -> BackendResult<Option<Vec<u8>>> { ... }
}
Expand description

Access to the VM’s backend storage, i.e. the chain

Required Methods§

source

fn get(&self, key: &[u8]) -> BackendResult<Option<Vec<u8>>>

Returns Err on error. Returns Ok(None) when key does not exist. Returns Ok(Some(Vec)) when key exists.

Note: Support for differentiating between a non-existent key and a key with empty value is not great yet and might not be possible in all backends. But we’re trying to get there.

source

fn scan( &mut self, start: Option<&[u8]>, end: Option<&[u8]>, order: Order ) -> BackendResult<u32>

Allows iteration over a set of key/value pairs, either forwards or backwards. Returns an interator ID that is unique within the Storage instance.

The bound start is inclusive and end is exclusive.

If start is lexicographically greater than or equal to end, an empty range is described, mo matter of the order.

This call must not change data in the storage, but creating and storing a new iterator can be a mutating operation on the Storage implementation. The implementation must ensure that iterator IDs are assigned in a deterministic manner as this is environment data that is injected into the contract.

source

fn next(&mut self, iterator_id: u32) -> BackendResult<Option<Record>>

Returns the next element of the iterator with the given ID.

If the ID is not found, a BackendError::IteratorDoesNotExist is returned.

This call must not change data in the storage, but incrementing an iterator can be a mutating operation on the Storage implementation.

source

fn set(&mut self, key: &[u8], value: &[u8]) -> BackendResult<()>

source

fn remove(&mut self, key: &[u8]) -> BackendResult<()>

Removes a database entry at key.

The current interface does not allow to differentiate between a key that existed before and one that didn’t exist. See https://github.com/CosmWasm/cosmwasm/issues/290

Provided Methods§

source

fn next_value(&mut self, iterator_id: u32) -> BackendResult<Option<Vec<u8>>>

Returns the next value of the iterator with the given ID. Since the iterator is incremented, the corresponding key will never be accessible.

If the ID is not found, a BackendError::IteratorDoesNotExist is returned.

The default implementation uses Storage::next and discards the key. More efficient implementations might be possible depending on the storage.

source

fn next_key(&mut self, iterator_id: u32) -> BackendResult<Option<Vec<u8>>>

Returns the next key of the iterator with the given ID. Since the iterator is incremented, the corresponding value will never be accessible.

If the ID is not found, a BackendError::IteratorDoesNotExist is returned.

The default implementation uses Storage::next and discards the value. More efficient implementations might be possible depending on the storage.

Implementors§