use crate::util::{Hash256, Result, Serializable};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io;
use std::io::{Read, Write};
pub const INV_VECT_ERROR: u32 = 0;
pub const INV_VECT_TX: u32 = 1;
pub const INV_VECT_BLOCK: u32 = 2;
pub const INV_VECT_FILTERED_BLOCK: u32 = 3;
pub const INV_VECT_COMPACT_BLOCK: u32 = 4;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct InvVect {
pub obj_type: u32,
pub hash: Hash256,
}
impl InvVect {
pub const SIZE: usize = 36;
pub fn size(&self) -> usize {
InvVect::SIZE
}
}
impl Serializable<InvVect> for InvVect {
fn read(reader: &mut dyn Read) -> Result<InvVect> {
let inv_vect = InvVect {
obj_type: reader.read_u32::<LittleEndian>()?,
hash: Hash256::read(reader)?,
};
Ok(inv_vect)
}
fn write(&self, writer: &mut dyn Write) -> io::Result<()> {
writer.write_u32::<LittleEndian>(self.obj_type as u32)?;
self.hash.write(writer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn write_read() {
let mut v = Vec::new();
let iv = InvVect {
obj_type: INV_VECT_TX,
hash: Hash256([8; 32]),
};
iv.write(&mut v).unwrap();
assert!(v.len() == iv.size());
assert!(InvVect::read(&mut Cursor::new(&v)).unwrap() == iv);
}
}