snarkvm_console_program/state_path/
bytes.rsuse super::*;
impl<N: Network> FromBytes for StatePath<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let version = u8::read_le(&mut reader)?;
if version != 1 {
return Err(error("Invalid state path version"));
}
let global_state_root = N::StateRoot::read_le(&mut reader)?;
let block_path = BlockPath::read_le(&mut reader)?;
let block_hash = N::BlockHash::read_le(&mut reader)?;
let previous_block_hash = N::BlockHash::read_le(&mut reader)?;
let header_root = Field::read_le(&mut reader)?;
let header_path = HeaderPath::read_le(&mut reader)?;
let header_leaf = HeaderLeaf::read_le(&mut reader)?;
let transactions_path = TransactionsPath::read_le(&mut reader)?;
let transaction_id = FromBytes::read_le(&mut reader)?;
let transaction_path = FromBytes::read_le(&mut reader)?;
let transaction_leaf = FromBytes::read_le(&mut reader)?;
let transition_root = Field::read_le(&mut reader)?;
let tcm = FromBytes::read_le(&mut reader)?;
let transition_path = FromBytes::read_le(&mut reader)?;
let transition_leaf = FromBytes::read_le(&mut reader)?;
Ok(Self::from(
global_state_root,
block_path,
block_hash,
previous_block_hash,
header_root,
header_path,
header_leaf,
transactions_path,
transaction_id,
transaction_path,
transaction_leaf,
transition_root,
tcm,
transition_path,
transition_leaf,
))
}
}
impl<N: Network> ToBytes for StatePath<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
1u8.write_le(&mut writer)?;
self.global_state_root.write_le(&mut writer)?;
self.block_path.write_le(&mut writer)?;
self.block_hash.write_le(&mut writer)?;
self.previous_block_hash.write_le(&mut writer)?;
self.header_root.write_le(&mut writer)?;
self.header_path.write_le(&mut writer)?;
self.header_leaf.write_le(&mut writer)?;
self.transactions_path.write_le(&mut writer)?;
self.transaction_id.write_le(&mut writer)?;
self.transaction_path.write_le(&mut writer)?;
self.transaction_leaf.write_le(&mut writer)?;
self.transition_root.write_le(&mut writer)?;
self.tcm.write_le(&mut writer)?;
self.transition_path.write_le(&mut writer)?;
self.transition_leaf.write_le(&mut writer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
const ITERATIONS: usize = 100;
#[test]
fn test_bytes() {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected =
crate::state_path::test_helpers::sample_global_state_path::<CurrentNetwork>(None, &mut rng).unwrap();
let expected_bytes = expected.to_bytes_le().unwrap();
assert_eq!(expected, StatePath::read_le(&expected_bytes[..]).unwrap());
assert!(StatePath::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
}
}