use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::io;
use std::io::{Read, Write};
use util::{Hash256, Result, Serializable};
pub const COINBASE_OUTPOINT_HASH: Hash256 = Hash256([0; 32]);
pub const COINBASE_OUTPOINT_INDEX: u32 = 0xffffffff;
#[derive(Debug, Default, PartialEq, Eq, Hash, Clone)]
pub struct OutPoint {
pub hash: Hash256,
pub index: u32,
}
impl OutPoint {
pub const SIZE: usize = 36;
pub fn size(&self) -> usize {
OutPoint::SIZE
}
}
impl Serializable<OutPoint> for OutPoint {
fn read(reader: &mut dyn Read) -> Result<OutPoint> {
let hash = Hash256::read(reader)?;
let index = reader.read_u32::<LittleEndian>()?;
Ok(OutPoint { hash, index })
}
fn write(&self, writer: &mut dyn Write) -> io::Result<()> {
self.hash.write(writer)?;
writer.write_u32::<LittleEndian>(self.index)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn write_read() {
let mut v = Vec::new();
let t = OutPoint {
hash: Hash256::decode(
"123412345678567890ab90abcdefcdef123412345678567890ab90abcdefcdef",
).unwrap(),
index: 0,
};
t.write(&mut v).unwrap();
assert!(v.len() == t.size());
assert!(OutPoint::read(&mut Cursor::new(&v)).unwrap() == t);
}
}