use super::*;
impl<N: Network> FromBytes for Header<N> {
#[inline]
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let version = u8::read_le(&mut reader)?;
if version != 1 {
return Err(error("Invalid header version"));
}
let previous_state_root = N::StateRoot::read_le(&mut reader)?;
let transactions_root = Field::<N>::read_le(&mut reader)?;
let finalize_root = Field::<N>::read_le(&mut reader)?;
let ratifications_root = Field::<N>::read_le(&mut reader)?;
let solutions_root = Field::<N>::read_le(&mut reader)?;
let subdag_root = Field::<N>::read_le(&mut reader)?;
let metadata = Metadata::read_le(&mut reader)?;
Self::from(
previous_state_root,
transactions_root,
finalize_root,
ratifications_root,
solutions_root,
subdag_root,
metadata,
)
.map_err(|e| error(e.to_string()))
}
}
impl<N: Network> ToBytes for Header<N> {
#[inline]
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
1u8.write_le(&mut writer)?;
self.previous_state_root.write_le(&mut writer)?;
self.transactions_root.write_le(&mut writer)?;
self.finalize_root.write_le(&mut writer)?;
self.ratifications_root.write_le(&mut writer)?;
self.solutions_root.write_le(&mut writer)?;
self.subdag_root.write_le(&mut writer)?;
self.metadata.write_le(&mut writer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_bytes() -> Result<()> {
let rng = &mut TestRng::default();
for expected in [crate::header::test_helpers::sample_block_header(rng)].into_iter() {
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Header::read_le(&expected_bytes[..])?);
assert!(Header::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
}
Ok(())
}
}