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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2020 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

fn min_repr_bytes(number_of_keys: usize) -> usize {
    let mut largest_value = match number_of_keys {
        n if n == 0 => return 0,
        n if n == 1 => return 1,
        n => n - 1,
    };

    let mut min_repr_bytes = 0;

    while largest_value != 0 {
        min_repr_bytes += 1;
        largest_value >>= 8;
    }

    min_repr_bytes
}

fn to_index_bytes(mut index: usize, len: usize) -> Vec<u8> {
    let mut bytes = vec![0u8; len];

    for i in (0..len).rev() {
        bytes[i] = index as u8;
        index >>= 8;
    }

    bytes
}

/// Given an integer-indexed `SimpleMpt` with `num_keys` elements
/// stored in it, convert `key` into the corresponding key format.
pub fn into_simple_mpt_key(key: usize, num_keys: usize) -> Vec<u8> {
    let key_length = min_repr_bytes(num_keys);
    to_index_bytes(key, key_length)
}

pub fn make_simple_mpt(mut values: Vec<Box<[u8]>>) -> SimpleMpt {
    let mut mpt = SimpleMpt::default();
    let keys = values.len();
    let mut mpt_kvs = Vec::with_capacity(keys);

    let index_byte_len = min_repr_bytes(keys);

    for (index, value) in values.drain(..).enumerate() {
        mpt_kvs.push((to_index_bytes(index, index_byte_len), value));
    }

    MptMerger::new(None, &mut mpt)
        .merge(&DumpedMptKvIterator { kv: mpt_kvs })
        .expect("SimpleMpt does not fail.");

    mpt
}

pub fn simple_mpt_merkle_root(simple_mpt: &mut SimpleMpt) -> MerkleHash {
    let maybe_root_node = simple_mpt
        .load_node(&CompressedPathRaw::default())
        .expect("SimpleMpt does not fail.");
    match maybe_root_node {
        None => MERKLE_NULL_NODE,
        Some(root_node) => {
            // if all keys share the same prefix (e.g. 0x00, ..., 0x0f share
            // the first nibble) then they will all be under the first child of
            // the root. in this case, we will use this first child as the root.
            if root_node.get_children_count() == 1 {
                trace!(
                    "debug receipts calculation: root node {:?}",
                    simple_mpt.load_node(
                        &CompressedPathRaw::new_and_apply_mask(
                            &[0],
                            CompressedPathRaw::second_nibble_mask()
                        )
                    )
                );

                root_node.get_child(0).expect("Child 0 must exist").merkle
            } else {
                trace!("debug receipts calculation: root node {:?}", root_node);
                *root_node.get_merkle()
            }
        }
    }
}

pub fn simple_mpt_proof(
    simple_mpt: &mut SimpleMpt, access_key: &[u8],
) -> TrieProof {
    let mut cursor = MptCursor::<
        &mut dyn SnapshotMptTraitRead,
        BasicPathNode<&mut dyn SnapshotMptTraitRead>,
    >::new(simple_mpt);

    cursor.load_root().expect("load_root should succeed");

    // see comment in `simple_mpt_merkle_root`
    let remove_root = cursor.current_node_mut().get_children_count() == 1;

    cursor
        .open_path_for_key::<access_mode::Read>(access_key)
        .expect("open_path_for_key should succeed");

    let mut proof = cursor.to_proof();
    cursor.finish().expect("finish should succeed");

    if remove_root {
        proof = TrieProof::new(proof.get_proof_nodes()[1..].to_vec())
            .expect("Proof with root removed is still connected");
    }

    proof
}

use crate::{
    impls::merkle_patricia_trie::{
        mpt_cursor::{BasicPathNode, MptCursor},
        trie_node::TrieNodeTrait,
        walk::GetChildTrait,
        CompressedPathRaw, MptMerger, TrieProof,
    },
    storage_db::SnapshotMptTraitRead,
    tests::DumpedMptKvIterator,
    utils::access_mode,
};
use primitives::{MerkleHash, MERKLE_NULL_NODE};

pub use crate::tests::FakeSnapshotMptDb as SimpleMpt;

#[cfg(test)]
mod tests {
    use super::{
        into_simple_mpt_key, make_simple_mpt, min_repr_bytes,
        simple_mpt_merkle_root, simple_mpt_proof, MerkleHash,
    };

    #[test]
    fn test_min_repr_bytes() {
        assert_eq!(min_repr_bytes(0x00_00_00_00), 0); // 0

        assert_eq!(min_repr_bytes(0x00_00_00_01), 1); // 1
        assert_eq!(min_repr_bytes(0x00_00_01_00), 1); // 256

        assert_eq!(min_repr_bytes(0x00_00_01_01), 2); // 257
        assert_eq!(min_repr_bytes(0x00_01_00_00), 2); // 65536

        assert_eq!(min_repr_bytes(0x00_01_00_01), 3); // 65537
        assert_eq!(min_repr_bytes(0x01_00_00_00), 3); // 16777216
    }

    #[test]
    fn test_into_simple_mpt_key() {
        assert_eq!(into_simple_mpt_key(0x01, 1), vec![0x01]);
        assert_eq!(into_simple_mpt_key(0x01, 256), vec![0x01]);
        assert_eq!(into_simple_mpt_key(0x01, 257), vec![0x00, 0x01]);
        assert_eq!(into_simple_mpt_key(0x01, 65536), vec![0x00, 0x01]);
        assert_eq!(into_simple_mpt_key(0x01, 65537), vec![0x00, 0x00, 0x01]);
    }

    #[test]
    fn test_empty_simple_mpt() {
        let mut mpt = make_simple_mpt(vec![]);
        let root = simple_mpt_merkle_root(&mut mpt);

        assert_eq!(
            root,
            "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
                .parse::<MerkleHash>()
                .unwrap()
        );
    }

    fn check_proofs(num_items: usize, proof_size: Vec<usize>) {
        // create k-v pairs:
        // (0x00, 0x00)
        // (0x01, 0x01)
        // ...

        let value_from_key = |key| into_simple_mpt_key(key, num_items);

        let values: Vec<Box<[u8]>> = (0..num_items)
            .map(|k| value_from_key(k).into_boxed_slice())
            .collect();

        let mut mpt = make_simple_mpt(values);
        let root = simple_mpt_merkle_root(&mut mpt);

        for k in 0..num_items {
            let key = into_simple_mpt_key(k, num_items);
            let proof = simple_mpt_proof(&mut mpt, &key);

            assert!(proof_size.contains(&proof.number_nodes()));

            // proof should be able to verify correct k-v
            assert!(proof.is_valid_kv(
                &into_simple_mpt_key(k, num_items),
                Some(&value_from_key(k)[..]),
                &root
            ));

            // proof with incorrect value should fail
            assert!(!proof.is_valid_kv(
                &into_simple_mpt_key(k, num_items),
                Some(&value_from_key(k + 1)[..]),
                &root
            ));

            // proof should not be able to verify other values in the trie
            for other in 0..num_items {
                if k == other {
                    continue;
                }

                assert!(!proof.is_valid_kv(
                    &into_simple_mpt_key(other, num_items),
                    Some(&value_from_key(other)[..]),
                    &root
                ));
            }
        }
    }

    #[test]
    #[rustfmt::skip]
    fn test_simple_mpt_proof() {
        // number of items: 0x01
        // keys: 0x00
        // proof size: 1 (root)
        check_proofs(0x01, vec![1]);

        // number of items: 0x02
        // keys: 0x00, 0x01
        //          ^     ^
        // proof size: 2 (root + 2nd nibble)
        check_proofs(0x02, vec![2]);

        // number of items: 0x10
        // keys: 0x00, 0x01, ..., 0x0f
        //          ^     ^          ^
        // proof size: 2 (root + 2nd nibble)
        check_proofs(0x10, vec![2]);

        // number of items: 0x11
        // keys: 0x00, 0x01, ..., 0x0f, 0x10
        // proof size:
        //   0x00, 0x01, ..., 0x0f -> 3 (root + 1st nibble + 2nd nibble)
        //     ^^    ^^         ^^
        //   0x10                  -> 2 (root + 1st nibble)
        //     ^
        check_proofs(0x11, vec![2, 3]);

        // number of items: 0x12
        // keys: 0x00, 0x01, ..., 0x0f, 0x10, 0x11
        //         ^^    ^^         ^^    ^^    ^^
        // proof size: 3 (root + 1st nibble + 2nd nibble)
        check_proofs(0x12, vec![3]);

        // number of items: 0x0100
        // keys: 0x00, 0x01, ..., 0xff
        //         ^^    ^^         ^^
        // proof size: 3 (root + 1st nibble + 2nd nibble)
        check_proofs(0x0100, vec![3]);

        // number of items: 0x101
        // keys: [0x00, 0x00], [0x00, 0x01], [0x00, 0x02], ..., [0x01, 0x00]
        // proof size:
        //   [0x00, 0x00], ..., [0x00, 0xff] -> 4 (root + 2nd nibble + 3rd nibble + 4th nibble)
        //       ^    ^^            ^    ^^
        //   [0x01, 0x00]      -> 2 (root + 2nd nibble)
        //       ^
        check_proofs(0x0101, vec![2, 4]);
    }
}