kelk_env/
api.rs

1//! Defining the Kelk API trait.
2
3use core::any::Any;
4
5use crate::error::HostError;
6use alloc::vec::Vec;
7
8/// the storage APIs that should be provided by the host.
9/// It can't be copied or cloned since it doesn't have Copy and Clone traits.
10pub trait StorageAPI {
11    /// This API requests the host to read the slice of `data` from the storage file
12    /// at the given `offset`.
13    fn read(&self, offset: u32, data: &mut [u8]) -> Result<(), HostError>;
14
15    /// This API requests the host to write  the slice of `data` into the storage file
16    /// at the given `offset`
17    fn write(&self, offset: u32, data: &[u8]) -> Result<(), HostError>;
18
19    /// It is useful for downcasting the trait to the underling struct.
20    /// For example we can downcast the trait to the mocked object.
21    fn as_any(&mut self) -> &mut dyn Any;
22}
23
24/// the blockchain APIs that should be provided by the host.
25/// It can't be copied or cloned since it doesn't have Copy and Clone traits.
26pub trait BlockchainAPI {
27    /// This API requests the host to return the associated value to the given
28    /// `param_id`.
29    fn get_param(&self, param_id: u32) -> Result<Vec<u8>, HostError>;
30
31    /// It is useful for downcasting the trait to the underling struct.
32    /// For example we can downcast the trait to the mocked object.
33    fn as_any(&mut self) -> &mut dyn Any;
34}