cs_mwc_bch/messages/
out_point.rs

1use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
2use std::io;
3use std::io::{Read, Write};
4use util::{Hash256, Result, Serializable};
5
6/// The coinbase transaction input will have this hash
7pub const COINBASE_OUTPOINT_HASH: Hash256 = Hash256([0; 32]);
8/// The coinbase transaction input will have this index
9pub const COINBASE_OUTPOINT_INDEX: u32 = 0xffffffff;
10
11/// Reference to a transaction output
12#[derive(Debug, Default, PartialEq, Eq, Hash, Clone)]
13pub struct OutPoint {
14    /// Hash of the referenced transaction
15    pub hash: Hash256,
16    /// Index of the output in the transaction, zero-indexed
17    pub index: u32,
18}
19
20impl OutPoint {
21    /// Size of the out point in bytes
22    pub const SIZE: usize = 36;
23
24    /// Returns the size of the out point in bytes
25    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}