Trait near_vm_logic::External
source · pub trait External {
// Required methods
fn storage_set(
&mut self,
key: &[u8],
value: &[u8]
) -> Result<(), VMLogicError>;
fn storage_get<'a>(
&'a self,
key: &[u8],
mode: StorageGetMode
) -> Result<Option<Box<dyn ValuePtr + 'a>>, VMLogicError>;
fn storage_remove(&mut self, key: &[u8]) -> Result<(), VMLogicError>;
fn storage_remove_subtree(
&mut self,
prefix: &[u8]
) -> Result<(), VMLogicError>;
fn storage_has_key(&mut self, key: &[u8]) -> Result<bool, VMLogicError>;
fn generate_data_id(&mut self) -> CryptoHash;
fn get_trie_nodes_count(&self) -> TrieNodesCount;
fn validator_stake(
&self,
account_id: &AccountId
) -> Result<Option<Balance>, VMLogicError>;
fn validator_total_stake(&self) -> Result<Balance, VMLogicError>;
}
Expand description
An external blockchain interface for the Runtime logic
Required Methods§
sourcefn storage_set(&mut self, key: &[u8], value: &[u8]) -> Result<(), VMLogicError>
fn storage_set(&mut self, key: &[u8], value: &[u8]) -> Result<(), VMLogicError>
Write value
to the key
of the storage trie associated with the current account.
Example
assert_eq!(external.storage_set(b"key42", b"value1337"), Ok(()));
// Should return an old value if the key exists
assert_eq!(external.storage_set(b"key42", b"new_value"), Ok(()));
sourcefn storage_get<'a>(
&'a self,
key: &[u8],
mode: StorageGetMode
) -> Result<Option<Box<dyn ValuePtr + 'a>>, VMLogicError>
fn storage_get<'a>( &'a self, key: &[u8], mode: StorageGetMode ) -> Result<Option<Box<dyn ValuePtr + 'a>>, VMLogicError>
Read key
from the storage trie associated with the current account.
Arguments
-
key
- the key to read -
mode
- whether the lookup will be performed through flat storage or trie
Errors
This function could return near_vm_errors::VMRunnerError::ExternalError
.
Example
external.storage_set(b"key42", b"value1337").unwrap();
assert_eq!(external.storage_get(b"key42", StorageGetMode::Trie).unwrap().map(|ptr| ptr.deref().unwrap()), Some(b"value1337".to_vec()));
// Returns Ok(None) if there is no value for a key
assert_eq!(external.storage_get(b"no_key", StorageGetMode::Trie).unwrap().map(|ptr| ptr.deref().unwrap()), None);
sourcefn storage_remove(&mut self, key: &[u8]) -> Result<(), VMLogicError>
fn storage_remove(&mut self, key: &[u8]) -> Result<(), VMLogicError>
Removes the key
from the storage trie associated with the current account.
The operation will succeed even if the key
does not exist.
Arguments
key
- the key to remove
Example
external.storage_set(b"key42", b"value1337").unwrap();
// Returns Ok if exists
assert_eq!(external.storage_remove(b"key42"), Ok(()));
// Returns Ok if there was no value
assert_eq!(external.storage_remove(b"no_value_key"), Ok(()));
sourcefn storage_remove_subtree(&mut self, prefix: &[u8]) -> Result<(), VMLogicError>
fn storage_remove_subtree(&mut self, prefix: &[u8]) -> Result<(), VMLogicError>
Note: The method is currently unused and untested.
Removes all keys with a given prefix
from the storage trie associated with current
account.
Arguments
prefix
- a prefix for all keys to remove
Errors
This function could return [near_vm_errors::VMError
].
Example
external.storage_set(b"key1", b"value1337").unwrap();
external.storage_set(b"key2", b"value1337").unwrap();
assert_eq!(external.storage_remove_subtree(b"key"), Ok(()));
assert!(!external.storage_has_key(b"key1").unwrap());
assert!(!external.storage_has_key(b"key2").unwrap());
sourcefn storage_has_key(&mut self, key: &[u8]) -> Result<bool, VMLogicError>
fn storage_has_key(&mut self, key: &[u8]) -> Result<bool, VMLogicError>
Check whether the key
is present in the storage trie associated with the current account.
Returns Ok(true)
if key is present, Ok(false)
if the key is not present.
Arguments
key
- a key to check
Errors
This function could return [near_vm_errors::VMError
].
Example
external.storage_set(b"key42", b"value1337").unwrap();
// Returns value if exists
assert_eq!(external.storage_has_key(b"key42"), Ok(true));
// Returns None if there was no value
assert_eq!(external.storage_has_key(b"no_value_key"), Ok(false));
fn generate_data_id(&mut self) -> CryptoHash
sourcefn get_trie_nodes_count(&self) -> TrieNodesCount
fn get_trie_nodes_count(&self) -> TrieNodesCount
Returns amount of touched trie nodes by storage operations
sourcefn validator_stake(
&self,
account_id: &AccountId
) -> Result<Option<Balance>, VMLogicError>
fn validator_stake( &self, account_id: &AccountId ) -> Result<Option<Balance>, VMLogicError>
Returns the validator stake for given account in the current epoch.
If the account is not a validator, returns None
.
sourcefn validator_total_stake(&self) -> Result<Balance, VMLogicError>
fn validator_total_stake(&self) -> Result<Balance, VMLogicError>
Returns total stake of validators in the current epoch.