diff --git a/src/storage_proofs.rs b/src/storage_proofs.rs index cb06ff6..f93c0c1 100644 --- a/src/storage_proofs.rs +++ b/src/storage_proofs.rs @@ -134,3 +134,89 @@ impl StorageProofs { Ok(()) } } + + +#[cfg(test)] +mod tests { + use ark_std::rand::{distributions::Alphanumeric, rngs::ThreadRng, Rng}; + use rs_poseidon::poseidon::hash; + use ruint::aliases::U256; + + use crate::circuit_tests::utils::{digest, treehash}; + + use rmpv::Value; + use rmpv::encode::write_value; + use rmpv::decode::read_value; + + #[test] + fn test_mpack() { + let mut buf = Vec::new(); + let val = Value::from("le message"); + + // example of serializing the random chunk data + // we build them up in mpack Value enums + let data = (0..4) + .map(|_| { + let rng = ThreadRng::default(); + let preimages: Vec = rng + .sample_iter(Alphanumeric) + .take(256) + .map(|c| U256::from(c)) + .collect(); + let hash = digest(&preimages, Some(16)); + (preimages, hash) + }) + .collect::, U256)>>(); + + let chunks = data.iter() + .map(|c| { + let x = c.0.iter() + .map(|c| Value::Ext(50, c.to_le_bytes_vec())) + .collect::>(); + Value::Array(x) + }) + .collect::>(); + let chunks = Value::Array(chunks); + let mut data = Value::Map(vec![(Value::String("chunks".into()), chunks.clone() )]); + + println!("Debug: chunks: {:?}", chunks[0][0]); + + // Serialize the value types to an array pointer + write_value(&mut buf, &data).unwrap(); + let mut rd = &buf[..]; + + let args = read_value(&mut rd).unwrap(); + + assert!(Value::is_map(&args)); + assert!(Value::is_array(&args["chunks"])); + assert!(Value::is_array(&args["chunks"][0])); + let mut arg_chunks: Vec> = Vec::new(); + + // deserialize the data back into u256's + // instead of this, we'll want to use `builder.push_input` + args["chunks"] + .as_array() + .unwrap() + .iter() + .for_each(|c| { + if let Some(x) = c.as_array() { + let mut vals: Vec = Vec::new(); + x.iter().for_each(|n| { + let b = n.as_ext().unwrap(); + // ensure it's a LE uin256 which we've set as ext 50 + assert_eq!(b.0, 50); + vals.push(U256::try_from_le_slice(b.1).unwrap()); + // TODO: change to use + // builder.push_input("hashes", *c) + }); + arg_chunks.push(vals); + } else { + panic!("unhandled type!"); + } + }); + + assert_eq!(arg_chunks.len(), 4); + assert_eq!(arg_chunks[0].len(), 256); + + } +}