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
use std::io::{Error, ErrorKind, Result};
use crate::ids;
use crate::message::{self, Compressor, Outbound};
#[derive(
std::clone::Clone,
std::cmp::Eq,
std::cmp::Ord,
std::cmp::PartialEq,
std::cmp::PartialOrd,
std::fmt::Debug,
std::hash::Hash,
)]
pub struct Message {
pub chain_id: ids::Id,
pub app_bytes: Vec<u8>,
}
impl Message {
pub fn create(chain_id: ids::Id, app_bytes: Vec<u8>) -> impl Outbound + Compressor {
Self {
chain_id,
app_bytes,
}
}
}
impl std::fmt::Display for Message {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "msg app_gossip")
}
}
impl Outbound for Message {
fn serialize_with_header(&self) -> Result<bytes::Bytes> {
let type_id = message::TYPES
.get("app_gossip")
.ok_or_else(|| Error::new(ErrorKind::InvalidInput, "unknown type name"))?;
let packer = message::default_packer_with_header();
packer.pack_byte(*type_id)?;
packer.pack_bool(false)?; packer.pack_bytes(self.chain_id.as_ref())?;
packer.pack_bytes_with_header(self.app_bytes.as_ref())?;
Ok(packer.take_bytes())
}
}
#[test]
fn test_message() {
let msg = Message::create(ids::Id::empty(), vec![0x01, 0x02, 0x03, 0x04]);
let data_with_header = msg.serialize_with_header().unwrap();
let expected_data: &[u8] = &[
0x00, 0x00, 0x00, 0x2a, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04, ];
assert!(cmp_manager::eq_vectors(&expected_data, &data_with_header));
}