sidevm_env/
ocall_def.rs

1use super::*;
2use crate::args_stack::{I32Convertible, RetDecode, StackedArgs};
3use crate::tls::{TlsClientConfig, TlsServerConfig};
4use std::borrow::Cow;
5
6/// All ocall definitions for pink Sidevm.
7#[sidevm_macro::ocall]
8pub trait OcallFuncs {
9    /// Close given resource by id.
10    #[ocall(id = 101)]
11    fn close(resource_id: i32) -> Result<()>;
12
13    /// Poll given resource by id and return a dynamic sized data.
14    #[ocall(id = 102, encode_output)]
15    fn poll(waker_id: i32, resource_id: i32) -> Result<Vec<u8>>;
16
17    /// Poll given resource to read data. Low level support for AsyncRead.
18    #[ocall(id = 103)]
19    fn poll_read(waker_id: i32, resource_id: i32, data: &mut [u8]) -> Result<u32>;
20
21    /// Poll given resource to write data. Low level support for AsyncWrite.
22    #[ocall(id = 104)]
23    fn poll_write(waker_id: i32, resource_id: i32, data: &[u8]) -> Result<u32>;
24
25    /// Shutdown a socket
26    #[ocall(id = 105)]
27    fn poll_shutdown(waker_id: i32, resource_id: i32) -> Result<()>;
28
29    /// Poll given resource to generate a new resource id.
30    #[ocall(id = 106)]
31    fn poll_res(waker_id: i32, resource_id: i32) -> Result<i32>;
32
33    /// Mark a task as ready for next polling
34    #[ocall(id = 109)]
35    fn mark_task_ready(task_id: i32) -> Result<()>;
36
37    /// Get the next waken up task id.
38    #[ocall(id = 110)]
39    fn next_ready_task() -> Result<i32>;
40
41    /// Enable logging for ocalls
42    #[ocall(id = 111)]
43    fn enable_ocall_trace(enable: bool) -> Result<()>;
44
45    /// Get awake wakers
46    #[ocall(id = 112, encode_output)]
47    fn awake_wakers() -> Result<Vec<i32>>;
48
49    /// Get random number
50    #[ocall(id = 113)]
51    fn getrandom(buf: &mut [u8]) -> Result<()>;
52
53    /// Create a timer given a duration of time in milliseconds.
54    #[ocall(id = 201)]
55    fn create_timer(timeout: i32) -> Result<i32>;
56
57    /// Send data to a oneshot channel.
58    #[ocall(id = 202)]
59    fn oneshot_send(resource_id: i32, data: &[u8]) -> Result<()>;
60
61    /// Percentage of the gas remaining to the next breath
62    #[ocall(id = 203)]
63    fn gas_remaining() -> Result<u8>;
64
65    /// Create a TCP socket, bind to given address and listen to incoming connections.
66    ///
67    /// If `tls_config` is not `None`, then the socket will be TLS encrypted.
68    /// Invoke tcp_accept on the returned resource_id to accept incoming connections.
69    #[ocall(id = 210, encode_input)]
70    fn tcp_listen(addr: Cow<str>, tls_config: Option<TlsServerConfig>) -> Result<i32>;
71
72    /// Accept incoming TCP connections.
73    #[ocall(id = 211, encode_output)]
74    fn tcp_accept(waker_id: i32, resource_id: i32) -> Result<(i32, String)>;
75
76    /// Accept incoming TCP connections without returning the remote address.
77    #[ocall(id = 212)]
78    fn tcp_accept_no_addr(waker_id: i32, resource_id: i32) -> Result<i32>;
79
80    /// Initiate a TCP connection to a remote endpoint.
81    #[ocall(id = 213)]
82    fn tcp_connect(host: &str, port: u16) -> Result<i32>;
83
84    /// Initiate a TLS/TCP connection to a remote endpoint.
85    #[ocall(id = 214, encode_input)]
86    fn tcp_connect_tls(host: String, port: u16, config: TlsClientConfig) -> Result<i32>;
87
88    /// Print log message.
89    #[ocall(id = 220)]
90    fn log(level: log::Level, message: &str) -> Result<()>;
91
92    /// Get value from the local cache.
93    #[ocall(id = 230, encode_output)]
94    fn local_cache_get(key: &[u8]) -> Result<Option<Vec<u8>>>;
95
96    /// Set value to the local cache.
97    #[ocall(id = 231)]
98    fn local_cache_set(key: &[u8], value: &[u8]) -> Result<()>;
99
100    /// Set expiration time for a key in the local cache.
101    #[ocall(id = 232)]
102    fn local_cache_set_expiration(key: &[u8], expire_after_secs: u64) -> Result<()>;
103
104    /// Remove a value from the local cache.
105    ///
106    /// Returns the previous value if it existed.
107    #[ocall(id = 233, encode_output)]
108    fn local_cache_remove(key: &[u8]) -> Result<Option<Vec<u8>>>;
109
110    /// Create input channel
111    #[ocall(id = 240, encode_output)]
112    fn create_input_channel(ch: InputChannel) -> Result<i32>;
113}
114
115#[repr(u8)]
116#[derive(Clone, Copy, Debug, PartialEq, Eq)]
117pub enum InputChannel {
118    /// Input channel for system messages such as receiving log events from other contracts.
119    SystemMessage = 1,
120    /// Input channel for general messages pushed from pink contract part of this fat contract.
121    GeneralMessage = 2,
122    /// Input channel for queries from external RPC requests.
123    Query = 3,
124}
125
126impl I32Convertible for InputChannel {
127    fn to_i32(&self) -> i32 {
128        *self as i32
129    }
130    fn from_i32(i: i32) -> Result<Self> {
131        match i {
132            1 => Ok(InputChannel::SystemMessage),
133            2 => Ok(InputChannel::GeneralMessage),
134            3 => Ok(InputChannel::Query),
135            _ => Err(OcallError::InvalidParameter),
136        }
137    }
138}