owasm_kit/oei/
mod.rs

1mod raw;
2
3/// Returns the number of validators to asked to report data from raw requests.
4pub fn get_ask_count() -> i64 {
5    unsafe { raw::get_ask_count() }
6}
7
8/// Returns the minimum number of data reports as specified by the oracle request.
9pub fn get_min_count() -> i64 {
10    unsafe { raw::get_min_count() }
11}
12
13/// Return the prepare block time as specified by the oracle request.
14pub fn get_prepare_time() -> i64 {
15    unsafe { raw::get_prepare_time() }
16}
17
18/// Return the execute block time during the call of execution phase. Must only
19/// be called during execution phase.
20pub fn get_execute_time() -> i64 {
21    unsafe { raw::get_execute_time() }
22}
23
24/// Returns the number of validators that report data to this oracle request. Must
25/// only be called during execution phase.
26pub fn get_ans_count() -> i64 {
27    unsafe { raw::get_ans_count() }
28}
29
30/// Returns the raw calldata as specified when the oracle request is submitted.
31pub fn get_calldata() -> Vec<u8> {
32    unsafe {
33        let mut data = Vec::with_capacity(raw::get_span_size() as usize);
34        let len = raw::read_calldata(data.as_mut_ptr() as i64);
35        data.set_len(len as usize);
36        data
37    }
38}
39
40/// Saves the given data as the result of the oracle execution. Must only be called
41/// during execution phase and must be called exactly once.
42pub fn save_return_data(data: &[u8]) {
43    unsafe { raw::set_return_data(data.as_ptr() as i64, data.len() as i64) }
44}
45
46/// Issues a new raw request to the host environement using the specified data
47/// source ID and calldata, and assigns it to the given external ID. Must only be
48/// called during preparation phase.
49pub fn ask_external_data(eid: i64, did: i64, calldata: &[u8]) {
50    unsafe { raw::ask_external_data(eid, did, calldata.as_ptr() as i64, calldata.len() as i64) }
51}
52
53/// Returns the data reported from the given validator index for the given external
54/// data ID. Result is OK if the validator reports data with zero return status, and
55/// Err otherwise. Must only be called during execution phase.
56pub fn get_external_data(eid: i64, vid: i64) -> Result<String, i64> {
57    unsafe {
58        let status = raw::get_external_data_status(eid, vid);
59        if status != 0 {
60            Err(status)
61        } else {
62            let mut data = Vec::with_capacity(raw::get_span_size() as usize);
63            let len = raw::read_external_data(eid, vid, data.as_mut_ptr() as i64);
64            data.set_len(len as usize);
65            Ok(String::from_utf8_unchecked(data))
66        }
67    }
68}
69
70/// Return the verification result of ecvrf given a pubkey, a vrf proof, and the
71/// corresponding result.
72pub fn ecvrf_verify(y: &[u8], pi: &[u8], alpha: &[u8]) -> Result<bool, u32> {
73    unsafe {
74        match raw::ecvrf_verify(
75            y.as_ptr() as i64,
76            y.len() as i64,
77            pi.as_ptr() as i64,
78            pi.len() as i64,
79            alpha.as_ptr() as i64,
80            alpha.len() as i64,
81        ) {
82            0 => Ok(true),
83            1 => Ok(false),
84            x => Err(x),
85        }
86    }
87}