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
use std::io::{Error, ErrorKind, Result};

use log::debug;

use crate::message::{self, compress, Compressor};

impl Compressor for message::push_query::Message {
    fn compress_with_header(&self) -> Result<bytes::Bytes> {
        let type_id = message::TYPES
            .get("push_query")
            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "unknown type name"))?;

        // first build uncompressed data
        let packer = message::default_packer();
        packer.pack_bytes(self.chain_id.as_ref())?;
        packer.pack_u32(self.request_id)?;
        packer.pack_u64(self.deadline.as_nanos() as u64)?;
        packer.pack_bytes(self.container_id.as_ref())?;
        packer.pack_bytes_with_header(self.container_bytes.as_ref())?;

        // compress the data
        let bytes_uncompressed = packer.bytes_len();
        let compressed = compress::pack_gzip(&packer.take_bytes())?;
        let bytes_compressed = compressed.len();

        // serialize with compressed data
        let packer = message::default_packer_with_header();
        packer.pack_byte(*type_id)?;
        packer.pack_bool(true)?; // compressible
        packer.pack_bytes(compressed.as_ref())?;

        if bytes_uncompressed > bytes_compressed {
            debug!(
                "push_query compression saved {} bytes",
                bytes_uncompressed - bytes_compressed
            );
        } else {
            debug!(
                "push_query compression added {} byte(s)",
                bytes_compressed - bytes_uncompressed
            );
        }
        Ok(packer.take_bytes())
    }
}

/// RUST_LOG=debug cargo test --package avalanche-types --lib -- message::push_query_gzip::test_message --exact --show-output
#[test]
fn test_message() {
    use crate::ids;
    use avalanche_utils::cmp;

    let msg = message::push_query::Message::create(
        ids::Id::empty(),
        7,
        std::time::Duration::from_secs(10),
        ids::Id::from_slice(&[
            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, //
            0x01, 0x01, //
        ]),
        vec![0x01, 0x02, 0x03],
    );
    let data_with_header = msg.compress_with_header().unwrap();
    // for c in &data_with_header {
    //     print!("{:#02x},", *c);
    // }

    // 48 in Golang but it's ok -- Golang can still decompress!
    // Golang compressed
    // 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
    // 0x62, 0x20, 0x0c, 0xd8, 0x19, 0x18, 0x18, 0x98, 0x42, 0xb8,
    // 0x9f, 0x10, 0x50, 0xc6, 0xc8, 0xc8, 0xc0, 0xc0, 0xc0, 0xcc,
    // 0xc8, 0xc4, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x19, 0x60,
    // 0x0e, 0xc5, 0x53, 0x00, 0x00, 0x00,
    let expected_data: &[u8] = &[
        0x00, 0x00, 0x00, 0x33, // message length
        0x0e, // type_id
        0x01, // compressible
        0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x85, 0x8a, 0xb1, 0x0d, 0x00,
        0x00, 0x08, 0xc2, 0x04, 0x06, 0x07, 0xdf, 0xf3, 0xff, 0xc9, 0x43, 0xe4, 0x03, 0x9a, 0xb4,
        0x53, 0xab, 0x22, 0x6d, 0xb9, 0x73, 0x61, 0x03, 0x1c, 0x81, 0x7a, 0x19, 0x60, 0x0e, 0xc5,
        0x53, 0x00, 0x00, 0x00, // compressed data
    ];
    assert!(cmp::eq_vectors(expected_data, &data_with_header));
}