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
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

#[derive(Clone, Debug, Default, PartialEq, RlpEncodable, RlpDecodable)]
pub struct NodeMerkleProof {
    pub delta_proof: Option<TrieProof>,
    pub intermediate_proof: Option<TrieProof>,
    pub snapshot_proof: Option<TrieProof>,
}

pub type StorageRootProof = NodeMerkleProof;

impl NodeMerkleProof {
    pub fn with_delta(
        &mut self, maybe_delta_proof: Option<TrieProof>,
    ) -> &mut Self {
        self.delta_proof = maybe_delta_proof;
        self
    }

    pub fn with_intermediate(
        &mut self, maybe_intermediate_proof: Option<TrieProof>,
    ) -> &mut Self {
        self.intermediate_proof = maybe_intermediate_proof;
        self
    }

    pub fn with_snapshot(
        &mut self, maybe_snapshot_proof: Option<TrieProof>,
    ) -> &mut Self {
        self.snapshot_proof = maybe_snapshot_proof;
        self
    }

    pub fn is_valid(
        &self, key: &Vec<u8>, storage_root: &StorageRoot,
        state_root: StateRoot,
        maybe_intermediate_padding: Option<DeltaMptKeyPadding>,
    ) -> bool {
        let delta_root = &state_root.delta_root;
        let intermediate_root = &state_root.intermediate_delta_root;
        let snapshot_root = &state_root.snapshot_root;

        let storage_key =
            match StorageKeyWithSpace::from_key_bytes::<CheckInput>(&key) {
                Ok(k) => k,
                Err(e) => {
                    warn!("Checking proof with invalid key: {:?}", e);
                    return false;
                }
            };

        match self.delta_proof {
            None => {
                // empty proof for non-empty trie is invalid
                if delta_root.ne(&MERKLE_NULL_NODE) {
                    return false;
                }

                // empty proof for non-empty storage root is invalid
                if storage_root.delta != MptValue::None {
                    return false;
                }
            }

            Some(ref proof) => {
                // convert storage key into delta mpt key
                let padding = StorageKeyWithSpace::delta_mpt_padding(
                    &snapshot_root,
                    &intermediate_root,
                );

                let key = storage_key.to_delta_mpt_key_bytes(&padding);

                // check if delta proof is valid
                if !proof.is_valid_node_merkle(
                    &key,
                    &storage_root.delta,
                    delta_root,
                ) {
                    return false;
                }
            }
        }

        match self.intermediate_proof {
            None => {
                // empty proof for non-empty trie is invalid
                if intermediate_root.ne(&MERKLE_NULL_NODE) {
                    return false;
                }

                // empty proof for non-empty storage root is invalid
                if storage_root.intermediate != MptValue::None {
                    return false;
                }
            }

            Some(ref proof) => {
                // convert storage key into delta mpt key
                let key = match maybe_intermediate_padding {
                    None => return false,
                    Some(p) => storage_key.to_delta_mpt_key_bytes(&p),
                };

                // check if intermediate proof is valid
                if !proof.is_valid_node_merkle(
                    &key,
                    &storage_root.intermediate,
                    intermediate_root,
                ) {
                    return false;
                }
            }
        }

        match self.snapshot_proof {
            None => {
                // empty proof for non-empty trie is invalid
                if snapshot_root.ne(&MERKLE_NULL_NODE) {
                    return false;
                }

                // empty proof for non-empty storage root is invalid
                if storage_root.snapshot != None {
                    return false;
                }
            }

            Some(ref proof) => {
                // check if snapshot proof is valid
                if !proof.is_valid_node_merkle(
                    &key,
                    &storage_root.snapshot.into(),
                    snapshot_root,
                ) {
                    return false;
                }
            }
        }

        true
    }
}

use super::merkle_patricia_trie::TrieProof;
use primitives::{
    CheckInput, DeltaMptKeyPadding, MptValue, StateRoot, StorageKeyWithSpace,
    StorageRoot, MERKLE_NULL_NODE,
};
use rlp_derive::{RlpDecodable, RlpEncodable};