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
use std::io;

use crate::{ids, packer::Packer, raw_tx};

/// Vertex represents a set of transactions for Avalanche X-chain.
/// ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex#Build
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Vertex {
    pub codec_version: u16,
    pub chain_id: ids::Id,
    pub height: u64,
    pub epoch: u32,
    pub parent_ids: Vec<ids::Id>,
    pub txs: Vec<Vec<u8>>,
}

impl Packer {
    /// Encodes vertex fields with codec version and packer.
    /// ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex#Build
    pub fn pack_vertex(&self, vtx: &mut Vertex) -> io::Result<()> {
        // sort "parent_ids"
        // ref. "ids.SortIDs"
        vtx.parent_ids.sort();

        // sort "txs" by SHA256 hashes
        // ref. "SortHashOf"
        vtx.txs.sort_by(|a, b| {
            (raw_tx::Data::from_slice(a.as_ref())).cmp(&raw_tx::Data::from_slice(b.as_ref()))
        });

        self.pack_u16(vtx.codec_version)?;
        self.pack_bytes(vtx.chain_id.as_ref())?;
        self.pack_u64(vtx.height)?;
        self.pack_u32(vtx.epoch)?;

        self.pack_u32(vtx.parent_ids.len() as u32)?;
        for id in vtx.parent_ids.iter() {
            self.pack_bytes(id.as_ref())?;
        }

        self.pack_u32(vtx.txs.len() as u32)?;
        for tx in vtx.txs.iter() {
            self.pack_bytes_with_header(tx.as_ref())?;
        }

        Ok(())
    }

    /// Unpacks the vertex.
    /// ref. https://pkg.go.dev/github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex#Build
    pub fn unpack_vertex(&self) -> io::Result<Vertex> {
        let codec_version = self.unpack_u16()?;

        let chain_id = self.unpack_bytes(ids::ID_LEN)?;
        let chain_id = ids::Id::from_slice(chain_id.as_ref());

        let height = self.unpack_u64()?;
        let epoch = self.unpack_u32()?;

        let parent_ids_size = self.unpack_u32()?;
        let mut parent_ids: Vec<ids::Id> = Vec::new();
        for _ in 0..parent_ids_size {
            let parent_id = self.unpack_bytes(ids::ID_LEN)?;
            let parent_id = ids::Id::from_slice(parent_id.as_ref());
            parent_ids.push(parent_id);
        }

        let txs_size = self.unpack_u32()?;
        let mut txs: Vec<Vec<u8>> = Vec::new();
        for _ in 0..txs_size {
            let tx_size = self.unpack_u32()?;
            let tx = self.unpack_bytes(tx_size as usize)?;
            txs.push(tx);
        }

        Ok(Vertex {
            codec_version,
            chain_id,
            height,
            epoch,
            parent_ids,
            txs,
        })
    }
}

/// RUST_LOG=debug cargo test --package avalanche-types --lib -- avm::vertex::test_pack_and_unpack --exact --show-output
#[test]
fn test_pack_and_unpack() {
    use bytes::BytesMut;

    let mut vtx = Vertex {
        codec_version: 0_u16,
        chain_id: ids::Id::from_slice(&<Vec<u8>>::from([
            0x3d, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
            0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
            0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
            0x59, 0xa7,
        ])),
        height: 1234567_u64,
        epoch: 0,

        // to be sorted
        parent_ids: vec![
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x03, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x02, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x01, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
        ],

        // to be sorted
        txs: vec![
            <Vec<u8>>::from([0x01]),
            <Vec<u8>>::from([0x02]),
            <Vec<u8>>::from([0x03]),
        ],
    };

    let packer = Packer::new(1024, 0);
    packer.pack_vertex(&mut vtx).unwrap();

    let vtx_sorted = Vertex {
        codec_version: 0_u16,
        chain_id: ids::Id::from_slice(&<Vec<u8>>::from([
            0x3d, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
            0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
            0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
            0x59, 0xa7,
        ])),
        height: 1234567_u64,
        epoch: 0,

        // sorted
        parent_ids: vec![
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x01, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x02, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
            ids::Id::from_slice(&<Vec<u8>>::from([
                0x03, 0x0a, 0xd1, 0x2b, 0x8e, 0xe8, 0x92, 0x8e, 0xdf, 0x24, //
                0x8c, 0xa9, 0x1c, 0xa5, 0x56, 0x00, 0xfb, 0x38, 0x3f, 0x07, //
                0xc3, 0x2b, 0xff, 0x1d, 0x6d, 0xec, 0x47, 0x2b, 0x25, 0xcf, //
                0x59, 0xa7,
            ])),
        ],

        // sorted
        txs: vec![
            <Vec<u8>>::from([0x03]),
            <Vec<u8>>::from([0x01]),
            <Vec<u8>>::from([0x02]),
        ],
    };
    assert!(vtx == vtx_sorted);

    let b = packer.take_bytes();
    let b = BytesMut::from(&b[..]);

    let packer = Packer::new(0, 0);
    packer.set_bytes(&b);

    let vtx_unpacked = packer.unpack_vertex().unwrap();
    assert!(vtx == vtx_unpacked);
}