|
| 1 | +//! I/O utilities for the host |
| 2 | +//! |
| 3 | +//! Handles serialization/deserialization of inputs and outputs |
| 4 | +//! for communication with the guest and external systems. |
| 5 | +
|
| 6 | +use anyhow::{Context, Result}; |
| 7 | +use shadow_evm_core::prelude::*; |
| 8 | +use std::fs; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +/// Serialize ExecutionInput to bytes |
| 12 | +pub fn serialize_input(input: &ExecutionInput) -> Result<Vec<u8>> { |
| 13 | + bincode::serialize(input).context("Failed to serialize ExecutionInput") |
| 14 | +} |
| 15 | + |
| 16 | +/// Deserialize ExecutionInput from bytes |
| 17 | +pub fn deserialize_input(data: &[u8]) -> Result<ExecutionInput> { |
| 18 | + bincode::deserialize(data).context("Failed to deserialize ExecutionInput") |
| 19 | +} |
| 20 | + |
| 21 | +/// Serialize ExecutionCommitment to bytes |
| 22 | +pub fn serialize_commitment(commitment: &ExecutionCommitment) -> Result<Vec<u8>> { |
| 23 | + bincode::serialize(commitment).context("Failed to serialize ExecutionCommitment") |
| 24 | +} |
| 25 | + |
| 26 | +/// Deserialize ExecutionCommitment from bytes |
| 27 | +pub fn deserialize_commitment(data: &[u8]) -> Result<ExecutionCommitment> { |
| 28 | + bincode::deserialize(data).context("Failed to deserialize ExecutionCommitment") |
| 29 | +} |
| 30 | + |
| 31 | +/// Save ExecutionInput to a JSON file |
| 32 | +pub fn save_input_json<P: AsRef<Path>>(input: &ExecutionInput, path: P) -> Result<()> { |
| 33 | + let json = serde_json::to_string_pretty(input)?; |
| 34 | + fs::write(path, json)?; |
| 35 | + Ok(()) |
| 36 | +} |
| 37 | + |
| 38 | +/// Load ExecutionInput from a JSON file |
| 39 | +pub fn load_input_json<P: AsRef<Path>>(path: P) -> Result<ExecutionInput> { |
| 40 | + let json = fs::read_to_string(path)?; |
| 41 | + let input: ExecutionInput = serde_json::from_str(&json)?; |
| 42 | + Ok(input) |
| 43 | +} |
| 44 | + |
| 45 | +/// Save binary data to a file |
| 46 | +pub fn save_bytes<P: AsRef<Path>>(data: &[u8], path: P) -> Result<()> { |
| 47 | + fs::write(path, data)?; |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +/// Load binary data from a file |
| 52 | +pub fn load_bytes<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> { |
| 53 | + let data = fs::read(path)?; |
| 54 | + Ok(data) |
| 55 | +} |
| 56 | + |
| 57 | +/// Format a hash as a hex string |
| 58 | +pub fn format_hash(hash: &Hash) -> String { |
| 59 | + format!("0x{}", hex::encode(hash.as_slice())) |
| 60 | +} |
| 61 | + |
| 62 | +/// Format commitment for display |
| 63 | +pub fn format_commitment(commitment: &ExecutionCommitment) -> String { |
| 64 | + format!( |
| 65 | + "ExecutionCommitment {{\n \ |
| 66 | + input_hash: {},\n \ |
| 67 | + output_hash: {},\n \ |
| 68 | + pre_state_root: {},\n \ |
| 69 | + post_state_root: {},\n \ |
| 70 | + commitment: {}\n\ |
| 71 | + }}", |
| 72 | + format_hash(&commitment.input_hash), |
| 73 | + format_hash(&commitment.output_hash), |
| 74 | + format_hash(&commitment.pre_state_root), |
| 75 | + format_hash(&commitment.post_state_root), |
| 76 | + format_hash(&commitment.commitment), |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_serialize_roundtrip() { |
| 86 | + let input = ExecutionInput::new(BlockEnv::default(), TxInput::default(), InMemoryDB::new()); |
| 87 | + |
| 88 | + let bytes = serialize_input(&input).unwrap(); |
| 89 | + let decoded = deserialize_input(&bytes).unwrap(); |
| 90 | + |
| 91 | + assert_eq!(input.hash(), decoded.hash()); |
| 92 | + } |
| 93 | +} |
0 commit comments