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#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
18pub struct HostAbiError {
19 pub code: Option<String>,
20 pub message: String,
21}
22
23impl HostAbiError {
24 pub fn new(message: impl Into<String>) -> Self {
26 Self {
27 code: None,
28 message: message.into(),
29 }
30 }
31
32 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 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)]
59pub 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 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 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
93pub fn pack_ptr_len(ptr: u32, len: u32) -> u64 {
95 ((len as u64) << 32) | (ptr as u64)
96}
97
98pub 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
105pub 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
117pub unsafe fn free(ptr: u32, len: u32) {
122 if ptr == 0 || len == 0 {
123 return;
124 }
125
126 unsafe {
128 drop(Vec::from_raw_parts(
129 ptr as *mut u8,
130 len as usize,
131 len as usize,
132 ));
133 }
134}
135
136pub 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 let bytes = unsafe { std::slice::from_raw_parts(ptr as *const u8, len as usize) };
150 Ok(bytes.to_vec())
151}
152
153pub 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 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
172pub 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
178pub 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
186pub 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
198pub 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
208pub 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 external_event = $external_event:path $(,)?
236 ) => {
237 #[unsafe(no_mangle)]
238 pub extern "C" fn ownable_alloc(len: u32) -> u32 {
239 $crate::abi::alloc(len)
240 }
241
242 #[unsafe(no_mangle)]
243 pub extern "C" fn ownable_free(ptr: u32, len: u32) {
244 unsafe { $crate::abi::free(ptr, len) }
246 }
247
248 #[unsafe(no_mangle)]
249 pub extern "C" fn ownable_instantiate(ptr: u32, len: u32) -> u64 {
250 $crate::abi::dispatch(ptr, len, $instantiate)
251 }
252
253 #[unsafe(no_mangle)]
254 pub extern "C" fn ownable_execute(ptr: u32, len: u32) -> u64 {
255 $crate::abi::dispatch(ptr, len, $execute)
256 }
257
258 #[unsafe(no_mangle)]
259 pub extern "C" fn ownable_query(ptr: u32, len: u32) -> u64 {
260 $crate::abi::dispatch(ptr, len, $query)
261 }
262
263 #[unsafe(no_mangle)]
264 pub extern "C" fn ownable_external_event(ptr: u32, len: u32) -> u64 {
265 $crate::abi::dispatch(ptr, len, $external_event)
266 }
267 };
268}
269
270#[derive(Serialize, Deserialize, Clone, Debug)]
272pub struct AbiAttribute {
273 pub key: String,
274 pub value: String,
275}
276
277#[derive(Serialize, Deserialize, Clone, Debug)]
279pub struct AbiEvent {
280 #[serde(rename = "type")]
281 pub kind: String,
282 pub attributes: Vec<AbiAttribute>,
283}
284
285#[derive(Serialize, Deserialize, Clone, Debug)]
288pub struct AbiResponse {
289 pub attributes: Vec<AbiAttribute>,
290 #[serde(default, skip_serializing_if = "Vec::is_empty")]
291 pub events: Vec<AbiEvent>,
292}
293
294impl From<Response> for AbiResponse {
295 fn from(r: Response) -> Self {
296 AbiResponse {
297 attributes: r
298 .attributes
299 .into_iter()
300 .map(|a| AbiAttribute {
301 key: a.key,
302 value: a.value,
303 })
304 .collect(),
305 events: r
306 .events
307 .into_iter()
308 .map(|e| AbiEvent {
309 kind: e.ty,
310 attributes: e
311 .attributes
312 .into_iter()
313 .map(|a| AbiAttribute {
314 key: a.key,
315 value: a.value,
316 })
317 .collect(),
318 })
319 .collect(),
320 }
321 }
322}
323
324#[derive(Serialize, Deserialize, Clone, Debug)]
331pub struct AbiResultPayload {
332 #[serde(with = "serde_bytes")]
333 pub result: Vec<u8>,
334 #[serde(skip_serializing_if = "Option::is_none")]
335 pub mem: Option<IdbStateDump>,
336}
337
338#[cfg(test)]
339mod tests {
340 use super::*;
341
342 #[test]
343 fn pack_unpack_round_trip() {
344 let packed = pack_ptr_len(42, 128);
345 assert_eq!(unpack_ptr_len(packed), (42, 128));
346 }
347
348 #[test]
349 fn response_serializes_error_fields() {
350 let response = HostAbiResponse::err(HostAbiError::with_code("E", "failed"));
351 let encoded = cbor_to_vec(&response).expect("serialize");
352 let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
353 assert!(!decoded.success);
354 assert_eq!(decoded.error_code.as_deref(), Some("E"));
355 assert_eq!(decoded.error_message.as_deref(), Some("failed"));
356 }
357
358 #[test]
359 fn response_omits_empty_payload() {
360 let response = HostAbiResponse::err("boom");
361 let encoded = cbor_to_vec(&response).expect("serialize");
362 let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
363 assert!(decoded.payload.is_empty());
364 }
365
366 #[test]
367 fn payload_round_trips_as_cbor_bytes_not_array() {
368 let inner = vec![0x01u8, 0x02, 0x03];
373 let response = HostAbiResponse::ok(inner.clone());
374 let encoded = cbor_to_vec(&response).expect("serialize");
375
376 let value: ciborium::Value =
379 ciborium::de::from_reader(&encoded[..]).expect("parse as Value");
380 if let ciborium::Value::Map(entries) = value {
381 let payload_val = entries
382 .into_iter()
383 .find(|(k, _)| k == &ciborium::Value::Text("payload".into()))
384 .map(|(_, v)| v)
385 .expect("payload key present");
386 assert!(
387 matches!(payload_val, ciborium::Value::Bytes(_)),
388 "payload must be CBOR bytes, got {:?}",
389 payload_val
390 );
391 } else {
392 panic!("expected CBOR map");
393 }
394
395 let decoded: HostAbiResponse = cbor_from_slice(&encoded).expect("deserialize");
397 assert_eq!(decoded.payload, inner);
398 }
399
400 #[test]
401 fn cbor_parse_error_maps_to_invalid_cbor_code() {
402 let result: Result<HostAbiResponse, _> = cbor_from_slice(b"\xff");
403 let abi_err = result.expect_err("should fail on invalid CBOR");
404 assert_eq!(abi_err.code.as_deref(), Some(ERR_INVALID_CBOR));
405 }
406
407 #[test]
408 fn dispatch_converts_panic_into_structured_error() {
409 let response = dispatch_response::<HostAbiError, _>(
410 Ok(Vec::new()),
411 |_| -> Result<Vec<u8>, HostAbiError> {
412 panic!("boom");
413 },
414 );
415 assert!(!response.success);
416 assert_eq!(response.error_code.as_deref(), Some(ERR_HANDLER_PANIC));
417 assert_eq!(response.error_message.as_deref(), Some("handler panicked"));
418 }
419}