cs_mwc_bch/messages/
out_point.rs1use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
2use std::io;
3use std::io::{Read, Write};
4use util::{Hash256, Result, Serializable};
5
6pub const COINBASE_OUTPOINT_HASH: Hash256 = Hash256([0; 32]);
8pub const COINBASE_OUTPOINT_INDEX: u32 = 0xffffffff;
10
11#[derive(Debug, Default, PartialEq, Eq, Hash, Clone)]
13pub struct OutPoint {
14 pub hash: Hash256,
16 pub index: u32,
18}
19
20impl OutPoint {
21 pub const SIZE: usize = 36;
23
24 pub fn size(&self) -> usize {
26 OutPoint::SIZE
27 }
28}
29
30impl Serializable<OutPoint> for OutPoint {
31 fn read(reader: &mut dyn Read) -> Result<OutPoint> {
32 let hash = Hash256::read(reader)?;
33 let index = reader.read_u32::<LittleEndian>()?;
34 Ok(OutPoint { hash, index })
35 }
36
37 fn write(&self, writer: &mut dyn Write) -> io::Result<()> {
38 self.hash.write(writer)?;
39 writer.write_u32::<LittleEndian>(self.index)?;
40 Ok(())
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 use std::io::Cursor;
48
49 #[test]
50 fn write_read() {
51 let mut v = Vec::new();
52 let t = OutPoint {
53 hash: Hash256::decode(
54 "123412345678567890ab90abcdefcdef123412345678567890ab90abcdefcdef",
55 ).unwrap(),
56 index: 0,
57 };
58 t.write(&mut v).unwrap();
59 assert!(v.len() == t.size());
60 assert!(OutPoint::read(&mut Cursor::new(&v)).unwrap() == t);
61 }
62}