1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Wrapped native functions for services to use
//!
//! Native functions give services the ability to print debugging messages,
//! abort transactions on errors, access databases and event logs, and
//! synchronously call other services. There aren't many native functions
//! since services implement most psibase functionality.
//!
//! These functions wrap the [Raw Native Functions](crate::native_raw).

use crate::{native_raw, AccountNumber, DbId, ToKey};
use fracpack::{Pack, Unpack, UnpackOwned};

/// Write message to console
///
/// Message should be UTF8.
pub fn write_console_bytes(message: &[u8]) {
    unsafe {
        if let Some(first) = message.first() {
            native_raw::writeConsole(first, message.len() as u32);
        }
    }
}

/// Write message to console
pub fn write_console(message: &str) {
    write_console_bytes(message.as_bytes());
}

/// Abort with message
///
/// Message should be UTF8.
pub fn abort_message_bytes(message: &[u8]) -> ! {
    unsafe {
        if let Some(first) = message.first() {
            native_raw::abortMessage(first, message.len() as u32)
        } else {
            native_raw::abortMessage(std::ptr::null(), 0)
        }
    }
}

/// Abort with message
pub fn abort_message(message: &str) -> ! {
    abort_message_bytes(message.as_bytes());
}

/// Abort with message if condition is false
pub fn check(condition: bool, message: &str) {
    if !condition {
        abort_message_bytes(message.as_bytes());
    }
}

/// Abort with message if optional value is empty
pub fn check_some<T>(opt_value: Option<T>, message: &str) -> T {
    if opt_value.is_none() {
        abort_message_bytes(message.as_bytes());
    }
    opt_value.unwrap()
}

/// Abort with message if optional has value
pub fn check_none<T>(opt_value: Option<T>, message: &str) {
    if opt_value.is_some() {
        abort_message_bytes(message.as_bytes());
    }
}

/// Get the most-recent result when the size is known in advance
///
/// Other functions set the result.
pub fn get_result_bytes(size: u32) -> Vec<u8> {
    let mut result = Vec::with_capacity(size as usize);
    if size > 0 {
        unsafe {
            let actual_size = native_raw::getResult(result.as_mut_ptr(), size, 0);
            result.set_len(std::cmp::min(size, actual_size) as usize);
        }
    }
    result
}

/// Get the most-recent key
///
/// Other functions set the key.
pub fn get_key_bytes() -> Vec<u8> {
    unsafe {
        let size = native_raw::getKey(std::ptr::null_mut(), 0);
        let mut result = Vec::with_capacity(size as usize);
        if size > 0 {
            native_raw::getKey(result.as_mut_ptr(), size);
            result.set_len(size as usize);
        }
        result
    }
}

/// Get the currently-executing action.
///
/// If the contract, while handling action A, calls itself with action B:
///    * Before the call to B, [get_current_action] returns A.
///    * After the call to B, [get_current_action] returns B.
///    * After B returns, [get_current_action] returns A.
///
/// Note: The above only applies if the contract uses [crate::native_raw::call].
pub fn get_current_action_bytes() -> Vec<u8> {
    let size;
    unsafe {
        size = native_raw::getCurrentAction();
    };
    get_result_bytes(size)
}

/// Get the currently-executing action.
///
/// This version creates an extra copy of [crate::Action::rawData];
/// consider using [with_current_action] instead.
///
/// If the contract, while handling action A, calls itself with action B:
///    * Before the call to B, [get_current_action] returns A.
///    * After the call to B, [get_current_action] returns B.
///    * After B returns, [get_current_action] returns A.
///
/// Note: The above only applies if the contract uses [crate::native_raw::call].
pub fn get_current_action() -> crate::Action {
    let bytes = get_current_action_bytes();
    <crate::Action>::unpack(&bytes[..], &mut 0).unwrap() // unwrap won't panic
}

/// Get the currently-executing action and pass it to `f`.
///
/// This is more efficient than [get_current_action] since it avoids
/// creating an extra copy of [crate::Action::rawData].
///
/// If the contract, while handling action A, calls itself with action B:
///    * Before the call to B, [get_current_action] returns A.
///    * After the call to B, [get_current_action] returns B.
///    * After B returns, [get_current_action] returns A.
///
/// Note: The above only applies if the contract uses [crate::native_raw::call].
pub fn with_current_action<R, F: Fn(crate::SharedAction) -> R>(f: F) -> R {
    let bytes = get_current_action_bytes();
    let act = <crate::SharedAction>::unpack(&bytes[..], &mut 0).unwrap(); // unwrap won't panic
    f(act)
}

/// Set the currently-executing action's return value
pub fn set_retval<T: Pack>(val: &T) {
    let bytes = val.packed();
    unsafe { native_raw::setRetval(bytes.as_ptr(), bytes.len() as u32) };
}

fn get_optional_result_bytes(size: u32) -> Option<Vec<u8>> {
    if size < u32::MAX {
        Some(get_result_bytes(size))
    } else {
        None
    }
}

/// Set a key-value pair
///
/// If key already exists, then replace the existing value.
pub fn kv_put_bytes(db: DbId, key: &[u8], value: &[u8]) {
    unsafe {
        native_raw::kvPut(
            db,
            key.as_ptr(),
            key.len() as u32,
            value.as_ptr(),
            value.len() as u32,
        )
    }
}

/// Set a key-value pair
///
/// If key already exists, then replace the existing value.
pub fn kv_put<K: ToKey, V: Pack>(db: DbId, key: &K, value: &V) {
    kv_put_bytes(db, &key.to_key(), &value.packed())
}

/// Add a sequentially-numbered record
///
/// Returns the id.
pub fn put_sequential_bytes(db: DbId, value: &[u8]) -> u64 {
    unsafe { native_raw::putSequential(db, value.as_ptr(), value.len() as u32) }
}

/// Add a sequentially-numbered record
///
/// Returns the id.
pub fn put_sequential<Type: Pack, V: Pack>(
    db: DbId,
    service: AccountNumber,
    ty: &Type,
    value: &V,
) -> u64 {
    let mut packed = Vec::new();
    service.pack(&mut packed);
    ty.pack(&mut packed);
    value.pack(&mut packed);
    put_sequential_bytes(db, &packed)
}

/// Remove a key-value pair if it exists
pub fn kv_remove_bytes(db: DbId, key: &[u8]) {
    unsafe { native_raw::kvRemove(db, key.as_ptr(), key.len() as u32) }
}

/// Remove a key-value pair if it exists
pub fn kv_remove<K: ToKey>(db: DbId, key: &K) {
    kv_remove_bytes(db, &key.to_key())
}

/// Get a key-value pair, if any
pub fn kv_get_bytes(db: DbId, key: &[u8]) -> Option<Vec<u8>> {
    let size = unsafe { native_raw::kvGet(db, key.as_ptr(), key.len() as u32) };
    get_optional_result_bytes(size)
}

/// Get a key-value pair, if any
pub fn kv_get<V: UnpackOwned, K: ToKey>(db: DbId, key: &K) -> Result<Option<V>, fracpack::Error> {
    if let Some(v) = kv_get_bytes(db, &key.to_key()) {
        Ok(Some(V::unpacked(&v)?))
    } else {
        Ok(None)
    }
}

/// Get the first key-value pair which is greater than or equal to the provided
/// key
///
/// If one is found, and the first `match_key_size` bytes of the found key
/// matches the provided key, then returns the value. Use [get_key_bytes] to get
/// the found key.
pub fn kv_greater_equal_bytes(db: DbId, key: &[u8], match_key_size: u32) -> Option<Vec<u8>> {
    let size =
        unsafe { native_raw::kvGreaterEqual(db, key.as_ptr(), key.len() as u32, match_key_size) };
    get_optional_result_bytes(size)
}

/// Get the first key-value pair which is greater than or equal to the provided
/// key
///
/// If one is found, and the first `match_key_size` bytes of the found key
/// matches the provided key, then returns the value. Use [get_key_bytes] to get
/// the found key.
pub fn kv_greater_equal<K: ToKey, V: UnpackOwned>(
    db_id: DbId,
    key: &K,
    match_key_size: u32,
) -> Option<V> {
    let bytes = kv_greater_equal_bytes(db_id, &key.to_key(), match_key_size);
    bytes.map(|v| V::unpack(&v[..], &mut 0).unwrap())
}

/// Get the key-value pair immediately-before provided key
///
/// If one is found, and the first `match_key_size` bytes of the found key
/// matches the provided key, then returns the value. Use [get_key_bytes] to get
/// the found key.
pub fn kv_less_than_bytes(db: DbId, key: &[u8], match_key_size: u32) -> Option<Vec<u8>> {
    let size =
        unsafe { native_raw::kvLessThan(db, key.as_ptr(), key.len() as u32, match_key_size) };
    get_optional_result_bytes(size)
}

/// Get the key-value pair immediately-before provided key
///
/// If one is found, and the first `match_key_size` bytes of the found key
/// matches the provided key, then returns the value. Use [get_key_bytes] to get
/// the found key.
pub fn kv_less_than<K: ToKey, V: UnpackOwned>(
    db_id: DbId,
    key: &K,
    match_key_size: u32,
) -> Option<V> {
    let bytes = kv_less_than_bytes(db_id, &key.to_key(), match_key_size);
    bytes.map(|v| V::unpack(&v[..], &mut 0).unwrap())
}

/// Get the maximum key-value pair which has key as a prefix
///
/// If one is found, then returns the value. Use [get_key_bytes] to get the found key.
pub fn kv_max_bytes(db: DbId, key: &[u8]) -> Option<Vec<u8>> {
    let size = unsafe { native_raw::kvMax(db, key.as_ptr(), key.len() as u32) };
    get_optional_result_bytes(size)
}

/// Get the maximum key-value pair which has key as a prefix
///
/// If one is found, then returns the value. Use [get_key_bytes] to get the found key.
pub fn kv_max<K: ToKey, V: UnpackOwned>(db_id: DbId, key: &K) -> Option<V> {
    let bytes = kv_max_bytes(db_id, &key.to_key());
    bytes.map(|v| V::unpack(&v[..], &mut 0).unwrap())
}