Skip to main content

ownable_std/abi/
mod.rs

1use crate::IdbStateDump;
2use cosmwasm_std::Response;
3use serde::{Deserialize, Serialize};
4use std::fmt::Display;
5use std::panic::UnwindSafe;
6
7pub const HOST_ABI_VERSION: &str = "1";
8pub const HOST_ABI_MANIFEST_FIELD: &str = "ownablesAbi";
9pub const HOST_ABI_WIRE_FORMAT: &str = "cbor";
10pub const HOST_ABI_WIRE_FORMAT_MANIFEST_FIELD: &str = "wireFormat";
11pub const ERR_INVALID_POINTER: &str = "INVALID_POINTER";
12pub const ERR_INVALID_CBOR: &str = "INVALID_CBOR";
13pub const ERR_SERIALIZATION_FAILED: &str = "SERIALIZATION_FAILED";
14pub const ERR_HANDLER_PANIC: &str = "HANDLER_PANIC";
15
16/// Error object returned by the host ABI envelope.
17#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
18pub struct HostAbiError {
19    pub code: Option<String>,
20    pub message: String,
21}
22
23impl HostAbiError {
24    /// Creates an error without a machine-readable code.
25    pub fn new(message: impl Into<String>) -> Self {
26        Self {
27            code: None,
28            message: message.into(),
29        }
30    }
31
32    /// Creates an error with both code and message.
33    pub fn with_code(code: impl Into<String>, message: impl Into<String>) -> Self {
34        Self {
35            code: Some(code.into()),
36            message: message.into(),
37        }
38    }
39
40    /// Creates an error from any displayable value.
41    pub fn from_display(err: impl Display) -> Self {
42        Self::new(err.to_string())
43    }
44}
45
46impl From<String> for HostAbiError {
47    fn from(value: String) -> Self {
48        Self::new(value)
49    }
50}
51
52impl From<&str> for HostAbiError {
53    fn from(value: &str) -> Self {
54        Self::new(value)
55    }
56}
57
58#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
59/// Top-level response envelope used by host ABI exports.
60pub struct HostAbiResponse {
61    pub success: bool,
62    #[serde(default, skip_serializing_if = "Vec::is_empty", with = "serde_bytes")]
63    pub payload: Vec<u8>,
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub error_code: Option<String>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub error_message: Option<String>,
68}
69
70impl HostAbiResponse {
71    /// Builds a successful response with encoded payload bytes.
72    pub fn ok(payload: Vec<u8>) -> Self {
73        Self {
74            success: true,
75            payload,
76            error_code: None,
77            error_message: None,
78        }
79    }
80
81    /// Builds an error response from any value convertible to [`HostAbiError`].
82    pub fn err(error: impl Into<HostAbiError>) -> Self {
83        let error = error.into();
84        Self {
85            success: false,
86            payload: Vec::new(),
87            error_code: error.code,
88            error_message: Some(error.message),
89        }
90    }
91}
92
93/// Packs `(ptr, len)` into a single `u64` where the high 32 bits hold length.
94pub fn pack_ptr_len(ptr: u32, len: u32) -> u64 {
95    ((len as u64) << 32) | (ptr as u64)
96}
97
98/// Unpacks a pointer/length pair produced by [`pack_ptr_len`].
99pub fn unpack_ptr_len(packed: u64) -> (u32, u32) {
100    let ptr = packed as u32;
101    let len = (packed >> 32) as u32;
102    (ptr, len)
103}
104
105/// Allocates `len` bytes in wasm linear memory and returns pointer.
106pub fn alloc(len: u32) -> u32 {
107    if len == 0 {
108        return 0;
109    }
110
111    let mut buffer = Vec::<u8>::with_capacity(len as usize);
112    let ptr = buffer.as_mut_ptr();
113    std::mem::forget(buffer);
114    ptr as u32
115}
116
117/// Frees memory previously allocated via `alloc`.
118///
119/// # Safety
120/// The `(ptr, len)` pair must come from `alloc` and must not be freed twice.
121pub unsafe fn free(ptr: u32, len: u32) {
122    if ptr == 0 || len == 0 {
123        return;
124    }
125
126    // SAFETY: Caller guarantees the pointer/length pair originated from `alloc`.
127    unsafe {
128        drop(Vec::from_raw_parts(
129            ptr as *mut u8,
130            len as usize,
131            len as usize,
132        ));
133    }
134}
135
136/// Reads a byte slice from wasm linear memory.
137pub fn read_memory(ptr: u32, len: u32) -> Result<Vec<u8>, HostAbiError> {
138    if len == 0 {
139        return Ok(Vec::new());
140    }
141    if ptr == 0 {
142        return Err(HostAbiError::with_code(
143            ERR_INVALID_POINTER,
144            "received null pointer for non-empty input",
145        ));
146    }
147
148    // SAFETY: The host is expected to pass a valid input buffer in wasm memory.
149    let bytes = unsafe { std::slice::from_raw_parts(ptr as *const u8, len as usize) };
150    Ok(bytes.to_vec())
151}
152
153/// Writes bytes into wasm linear memory and returns a packed pointer/length pair.
154pub fn write_memory(data: &[u8]) -> u64 {
155    let len = data.len() as u32;
156    if len == 0 {
157        return pack_ptr_len(0, 0);
158    }
159
160    let ptr = alloc(len);
161    if ptr == 0 {
162        return pack_ptr_len(0, 0);
163    }
164
165    // SAFETY: `ptr` points to `len` bytes allocated via `alloc`.
166    unsafe {
167        std::ptr::copy_nonoverlapping(data.as_ptr(), ptr as *mut u8, len as usize);
168    }
169    pack_ptr_len(ptr, len)
170}
171
172/// Deserialize CBOR bytes into a value.
173pub fn cbor_from_slice<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T, HostAbiError> {
174    ciborium::de::from_reader(bytes)
175        .map_err(|e| HostAbiError::with_code(ERR_INVALID_CBOR, e.to_string()))
176}
177
178/// Serialize a value to CBOR bytes.
179pub fn cbor_to_vec<T: serde::Serialize>(value: &T) -> Result<Vec<u8>, HostAbiError> {
180    let mut buf = Vec::new();
181    ciborium::ser::into_writer(value, &mut buf)
182        .map_err(|e| HostAbiError::with_code(ERR_SERIALIZATION_FAILED, e.to_string()))?;
183    Ok(buf)
184}
185
186/// Serializes [`HostAbiResponse`] to CBOR and writes it into wasm memory.
187pub fn encode_response(response: &HostAbiResponse) -> u64 {
188    let encoded = cbor_to_vec(response).unwrap_or_else(|error| {
189        let fallback = HostAbiResponse::err(HostAbiError::with_code(
190            ERR_SERIALIZATION_FAILED,
191            error.message,
192        ));
193        cbor_to_vec(&fallback).unwrap_or_default()
194    });
195    write_memory(&encoded)
196}
197
198/// Reads input bytes, invokes a handler, and returns an encoded ABI response.
199pub fn dispatch<E, F>(ptr: u32, len: u32, handler: F) -> u64
200where
201    E: Into<HostAbiError>,
202    F: FnOnce(&[u8]) -> Result<Vec<u8>, E> + UnwindSafe,
203{
204    let response = dispatch_response(read_memory(ptr, len), handler);
205    encode_response(&response)
206}
207
208/// Converts handler output (or failure) into [`HostAbiResponse`].
209pub fn dispatch_response<E, F>(input: Result<Vec<u8>, HostAbiError>, handler: F) -> HostAbiResponse
210where
211    E: Into<HostAbiError>,
212    F: FnOnce(&[u8]) -> Result<Vec<u8>, E> + UnwindSafe,
213{
214    match input {
215        Ok(input) => match std::panic::catch_unwind(|| handler(&input)) {
216            Ok(handler_result) => match handler_result {
217                Ok(payload) => HostAbiResponse::ok(payload),
218                Err(error) => HostAbiResponse::err(error.into()),
219            },
220            Err(_) => HostAbiResponse::err(HostAbiError::with_code(
221                ERR_HANDLER_PANIC,
222                "handler panicked",
223            )),
224        },
225        Err(error) => HostAbiResponse::err(error),
226    }
227}
228
229#[macro_export]
230macro_rules! ownable_host_abi_v1 {
231    (
232        instantiate = $instantiate:path,
233        execute = $execute:path,
234        query = $query:path,
235        register = $register:path,
236        ingest = $ingest:path $(,)?
237    ) => {
238        #[unsafe(no_mangle)]
239        pub extern "C" fn ownable_alloc(len: u32) -> u32 {
240            $crate::abi::alloc(len)
241        }
242
243        #[unsafe(no_mangle)]
244        pub extern "C" fn ownable_free(ptr: u32, len: u32) {
245            // SAFETY: Host must only pass pointers obtained through `ownable_alloc`.
246            unsafe { $crate::abi::free(ptr, len) }
247        }
248
249        #[unsafe(no_mangle)]
250        pub extern "C" fn ownable_instantiate(ptr: u32, len: u32) -> u64 {
251            $crate::abi::dispatch(ptr, len, $instantiate)
252        }
253
254        #[unsafe(no_mangle)]
255        pub extern "C" fn ownable_execute(ptr: u32, len: u32) -> u64 {
256            $crate::abi::dispatch(ptr, len, $execute)
257        }
258
259        #[unsafe(no_mangle)]
260        pub extern "C" fn ownable_query(ptr: u32, len: u32) -> u64 {
261            $crate::abi::dispatch(ptr, len, $query)
262        }
263
264        #[unsafe(no_mangle)]
265        pub extern "C" fn ownable_register(ptr: u32, len: u32) -> u64 {
266            $crate::abi::dispatch(ptr, len, $register)
267        }
268
269        #[unsafe(no_mangle)]
270        pub extern "C" fn ownable_ingest(ptr: u32, len: u32) -> u64 {
271            $crate::abi::dispatch(ptr, len, $ingest)
272        }
273    };
274}
275
276/// A single key-value attribute from a contract response.
277#[derive(Serialize, Deserialize, Clone, Debug)]
278pub struct AbiAttribute {
279    pub key: String,
280    pub value: String,
281}
282
283/// An event emitted by a contract response.
284#[derive(Serialize, Deserialize, Clone, Debug)]
285pub struct AbiEvent {
286    #[serde(rename = "type")]
287    pub kind: String,
288    pub attributes: Vec<AbiAttribute>,
289}
290
291/// CBOR-native representation of a cosmwasm execute/instantiate/register/ingest Response.
292/// Only carries the fields the host actually needs; skips messages and sub-messages.
293#[derive(Serialize, Deserialize, Clone, Debug)]
294pub struct AbiResponse {
295    pub attributes: Vec<AbiAttribute>,
296    #[serde(default, skip_serializing_if = "Vec::is_empty")]
297    pub events: Vec<AbiEvent>,
298}
299
300impl From<Response> for AbiResponse {
301    fn from(r: Response) -> Self {
302        AbiResponse {
303            attributes: r
304                .attributes
305                .into_iter()
306                .map(|a| AbiAttribute {
307                    key: a.key,
308                    value: a.value,
309                })
310                .collect(),
311            events: r
312                .events
313                .into_iter()
314                .map(|e| AbiEvent {
315                    kind: e.ty,
316                    attributes: e
317                        .attributes
318                        .into_iter()
319                        .map(|a| AbiAttribute {
320                            key: a.key,
321                            value: a.value,
322                        })
323                        .collect(),
324                })
325                .collect(),
326        }
327    }
328}
329
330/// The inner payload returned by every ABI handler, CBOR-encoded inside `HostAbiResponse.payload`.
331///
332/// - `result` carries raw bytes:
333///   - for execute/instantiate/register/ingest: a CBOR-encoded `AbiResponse`
334///   - for query: the raw bytes from cosmwasm `Binary` (JSON-encoded by `to_json_binary`)
335/// - `mem` is present for state-mutating calls; absent for queries.
336#[derive(Serialize, Deserialize, Clone, Debug)]
337pub struct AbiResultPayload {
338    #[serde(with = "serde_bytes")]
339    pub result: Vec<u8>,
340    #[serde(skip_serializing_if = "Option::is_none")]
341    pub mem: Option<IdbStateDump>,
342}
343
344#[cfg(test)]
345mod tests {
346    use super::*;
347
348    mod macro_exports {
349        use super::super::HostAbiError;
350
351        fn passthrough_handler(input: &[u8]) -> Result<Vec<u8>, HostAbiError> {
352            Ok(input.to_vec())
353        }
354
355        crate::ownable_host_abi_v1!(
356            instantiate = passthrough_handler,
357            execute = passthrough_handler,
358            query = passthrough_handler,
359            register = passthrough_handler,
360            ingest = passthrough_handler,
361        );
362
363        #[test]
364        fn register_export_is_generated() {
365            let export: extern "C" fn(u32, u32) -> u64 = ownable_register;
366            let _ = export;
367        }
368
369        #[test]
370        fn ingest_export_is_generated() {
371            let export: extern "C" fn(u32, u32) -> u64 = ownable_ingest;
372            let _ = export;
373        }
374    }
375
376    #[test]
377    fn dispatch_response_supports_register_style_handlers() {
378        let response = dispatch_response::<HostAbiError, _>(Ok(b"register".to_vec()), |input| {
379            Ok(input.to_vec())
380        });
381        assert!(response.success);
382        assert_eq!(response.payload, b"register");
383    }
384
385    #[test]
386    fn dispatch_response_supports_ingest_style_handlers() {
387        let response = dispatch_response::<HostAbiError, _>(Ok(b"ingest".to_vec()), |input| {
388            Ok(input.to_vec())
389        });
390        assert!(response.success);
391        assert_eq!(response.payload, b"ingest");
392    }
393
394    #[test]
395    fn pack_unpack_round_trip() {
396        let packed = pack_ptr_len(42, 128);
397        assert_eq!(unpack_ptr_len(packed), (42, 128));
398    }
399
400    #[test]
401    fn response_serializes_error_fields() {
402        let response = HostAbiResponse::err(HostAbiError::with_code("E", "failed"));
403        let encoded = cbor_to_vec(&response).expect("serialize");
404        let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
405        assert!(!decoded.success);
406        assert_eq!(decoded.error_code.as_deref(), Some("E"));
407        assert_eq!(decoded.error_message.as_deref(), Some("failed"));
408    }
409
410    #[test]
411    fn response_omits_empty_payload() {
412        let response = HostAbiResponse::err("boom");
413        let encoded = cbor_to_vec(&response).expect("serialize");
414        let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
415        assert!(decoded.payload.is_empty());
416    }
417
418    #[test]
419    fn payload_round_trips_as_cbor_bytes_not_array() {
420        // Ensure Vec<u8> payload is encoded as CBOR byte string (major type 2),
421        // not a CBOR array of integers (major type 4). cbor-x on the JS side
422        // decodes byte strings to Uint8Array; it cannot decode a CBOR array as
423        // input to a second decode() call.
424        let inner = vec![0x01u8, 0x02, 0x03];
425        let response = HostAbiResponse::ok(inner.clone());
426        let encoded = cbor_to_vec(&response).expect("serialize");
427
428        // The "payload" value in the CBOR map must be a byte string (major type 2),
429        // NOT an array (major type 4).
430        let value: ciborium::Value =
431            ciborium::de::from_reader(&encoded[..]).expect("parse as Value");
432        if let ciborium::Value::Map(entries) = value {
433            let payload_val = entries
434                .into_iter()
435                .find(|(k, _)| k == &ciborium::Value::Text("payload".into()))
436                .map(|(_, v)| v)
437                .expect("payload key present");
438            assert!(
439                matches!(payload_val, ciborium::Value::Bytes(_)),
440                "payload must be CBOR bytes, got {:?}",
441                payload_val
442            );
443        } else {
444            panic!("expected CBOR map");
445        }
446
447        // Also verify round-trip correctness.
448        let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
449        assert_eq!(decoded.payload, inner);
450    }
451
452    #[test]
453    fn cbor_parse_error_maps_to_invalid_cbor_code() {
454        let result: Result<HostAbiResponse, _> = cbor_from_slice(b"\xff");
455        let abi_err = result.expect_err("should fail on invalid CBOR");
456        assert_eq!(abi_err.code.as_deref(), Some(ERR_INVALID_CBOR));
457    }
458
459    #[test]
460    fn dispatch_converts_panic_into_structured_error() {
461        let response = dispatch_response::<HostAbiError, _>(
462            Ok(Vec::new()),
463            |_| -> Result<Vec<u8>, HostAbiError> {
464                panic!("boom");
465            },
466        );
467        assert!(!response.success);
468        assert_eq!(response.error_code.as_deref(), Some(ERR_HANDLER_PANIC));
469        assert_eq!(response.error_message.as_deref(), Some("handler panicked"));
470    }
471}