1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{
    error::{Error, Result},
    sequence_message::SequenceMessage,
};
use bitcoin::{
    consensus::{deserialize, serialize},
    constants::MAX_BLOCK_WEIGHT,
    hashes::Hash,
    Block, BlockHash, Transaction, Txid,
};
use core::fmt;

pub const TOPIC_MAX_LEN: usize = 9;
pub const DATA_MAX_LEN: usize = MAX_BLOCK_WEIGHT as usize;
pub const SEQUENCE_LEN: usize = 4;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Message {
    HashBlock(BlockHash, u32),
    HashTx(Txid, u32),
    Block(Block, u32),
    Tx(Transaction, u32),
    Sequence(SequenceMessage, u32),
}

impl Message {
    /// Returns the topic of this [`Message`] as a byte slice.
    #[inline]
    pub fn topic(&self) -> &'static [u8] {
        self.topic_str().as_bytes()
    }

    /// Returns the topic of this [`Message`] as a string slice.
    #[inline]
    pub fn topic_str(&self) -> &'static str {
        let topic = match self {
            Self::HashBlock(..) => "hashblock",
            Self::HashTx(..) => "hashtx",
            Self::Block(..) => "block",
            Self::Tx(..) => "tx",
            Self::Sequence(..) => "sequence",
        };

        debug_assert!(topic.len() <= TOPIC_MAX_LEN);

        topic
    }

    /// Serializes the middle part of this [`Message`] (no topic and sequence).
    #[inline]
    pub fn serialize_data_to_vec(&self) -> Vec<u8> {
        match self {
            Self::HashBlock(_, _) | Self::HashTx(_, _) => {
                let mut arr = match self {
                    Self::HashBlock(blockhash, _) => blockhash.to_byte_array(),
                    Self::HashTx(txid, _) => txid.to_byte_array(),
                    _ => unreachable!(),
                };
                arr.reverse();
                arr.to_vec()
            }
            Self::Block(block, _) => serialize(&block),
            Self::Tx(tx, _) => serialize(&tx),
            Self::Sequence(sm, _) => sm.serialize_to_vec(),
        }
    }

    /// Serializes this [`Message`] to 3 [`Vec<u8>`]s.
    #[inline]
    pub fn serialize_to_vecs(&self) -> [Vec<u8>; 3] {
        [
            self.topic().to_vec(),
            self.serialize_data_to_vec(),
            self.sequence().to_le_bytes().to_vec(),
        ]
    }

    /// Returns the sequence of this [`Message`], a number that starts at 0 and goes up every time
    /// Bitcoin Core sends a ZMQ message per publisher.
    #[inline]
    pub fn sequence(&self) -> u32 {
        match self {
            Self::HashBlock(_, seq)
            | Self::HashTx(_, seq)
            | Self::Block(_, seq)
            | Self::Tx(_, seq)
            | Self::Sequence(_, seq) => *seq,
        }
    }

    /// Attempts to deserialize a multipart (multiple byte slices) to a [`Message`].
    #[inline]
    pub fn from_multipart<T: AsRef<[u8]>>(mp: &[T]) -> Result<Self> {
        Self::from_fixed_size_multipart(
            mp.try_into()
                .map_err(|_| Error::InvalidMutlipartLength(mp.len()))?,
        )
    }

    #[inline]
    pub fn from_fixed_size_multipart<T: AsRef<[u8]>>(mp: &[T; 3]) -> Result<Self> {
        let [topic, data, seq] = mp;

        let topic = topic.as_ref();
        let data = data.as_ref();
        let seq = seq.as_ref();

        let seq = seq
            .try_into()
            .map_err(|_| Error::InvalidSequenceLength(seq.len()))?;

        Self::from_parts(topic, data, seq)
    }

    #[inline]
    pub fn from_parts(topic: &[u8], data: &[u8], seq: [u8; 4]) -> Result<Self> {
        let seq = u32::from_le_bytes(seq);

        Ok(match topic {
            b"hashblock" | b"hashtx" => {
                let mut data: [u8; 32] = data
                    .try_into()
                    .map_err(|_| Error::Invalid256BitHashLength(data.len()))?;
                data.reverse();

                match topic {
                    b"hashblock" => Self::HashBlock(BlockHash::from_byte_array(data), seq),
                    _ /* b"hashtx" */ => Self::HashTx(Txid::from_byte_array(data), seq),
                }
            }
            b"rawblock" => Self::Block(deserialize(data)?, seq),
            b"rawtx" => Self::Tx(deserialize(data)?, seq),
            b"sequence" => Self::Sequence(SequenceMessage::from_byte_slice(data)?, seq),
            _ => {
                let mut buf = [0; TOPIC_MAX_LEN];

                buf[0..topic.len()].copy_from_slice(topic);

                return Err(Error::InvalidTopic(topic.len(), buf));
            }
        })
    }
}

impl<T: AsRef<[u8]>> TryFrom<&[T]> for Message {
    type Error = Error;

    #[inline]
    fn try_from(value: &[T]) -> Result<Self> {
        Self::from_multipart(value)
    }
}

impl<T: AsRef<[u8]>> TryFrom<[T; 3]> for Message {
    type Error = Error;

    #[inline]
    fn try_from(value: [T; 3]) -> Result<Self> {
        Self::from_fixed_size_multipart(&value)
    }
}

impl From<Message> for [Vec<u8>; 3] {
    #[inline]
    fn from(msg: Message) -> Self {
        msg.serialize_to_vecs()
    }
}

impl From<Message> for Vec<Vec<u8>> {
    #[inline]
    fn from(msg: Message) -> Self {
        msg.serialize_to_vecs().to_vec()
    }
}

impl fmt::Display for Message {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::HashBlock(blockhash, seq) => write!(f, "HashBlock({blockhash}, sequence={seq})"),
            Self::HashTx(txid, seq) => write!(f, "HashTx({txid}, sequence={seq})"),
            Self::Block(block, seq) => write!(f, "Block({}, sequence={seq})", block.block_hash()),
            Self::Tx(tx, seq) => write!(f, "Tx({}, sequence={seq})", tx.txid()),
            Self::Sequence(sm, seq) => write!(f, "Sequence({sm}, sequence={seq})"),
        }
    }
}