util.rs (952B)
1 use crate::{memory::MemoryManipulation, runtime::Env}; 2 3 /// Serialize payload to format accepted by the runtime entrypoint 4 pub fn serialize_payload(payload: &[u8]) -> Vec<u8> { 5 let mut out = vec![]; 6 7 let len = payload.len() as u64; 8 out.extend_from_slice(&len.to_le_bytes()); 9 out.extend_from_slice(payload); 10 11 out 12 } 13 14 /// Host function for logging strings. This is injected into the runtime. 15 pub(crate) fn drk_log(env: &Env, ptr: u32, len: u32) { 16 if let Some(bytes) = env.memory.get_ref().unwrap().read(ptr, len as usize) { 17 // Piece the string together 18 let msg = match String::from_utf8(bytes.to_vec()) { 19 Ok(v) => v, 20 Err(e) => { 21 println!("Invalid UTF-8 string: {:?}", e); 22 return 23 } 24 }; 25 26 let mut logs = env.logs.lock().unwrap(); 27 logs.push(msg); 28 return 29 } 30 31 println!("Failed to read any bytes from VM memory"); 32 }