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
275
276
277
278
use super::Substate;

#[cfg(test)]
use super::StorageLayout;
use cfx_parameters::{
    internal_contract_addresses::SYSTEM_STORAGE_ADDRESS,
    staking::COLLATERAL_UNITS_PER_STORAGE_KEY,
};
use cfx_statedb::{Result as DbResult, StateDbExt, StateDbGeneric};
use cfx_types::{Address, Space, U256};

use primitives::{
    SkipInputCheck, StorageKey, StorageKeyWithSpace, StorageValue,
};
use std::collections::hash_map::Entry::*;

use super::OverlayAccount;

#[cfg(test)]
use super::super::checkpoints::CheckpointEntry;

impl OverlayAccount {
    pub fn set_storage(
        &mut self, key: Vec<u8>, value: U256, old_value: StorageValue,
        owner: Address, substate: &mut Substate,
    ) -> DbResult<()> {
        // Refund the collateral of old value
        if let Some(old_owner) = old_value.owner {
            substate.record_storage_release(
                &old_owner,
                COLLATERAL_UNITS_PER_STORAGE_KEY,
            );
        }

        // Settle the collateral of new value
        let new_owner = if self.should_have_owner(&key) && !value.is_zero() {
            substate.record_storage_occupy(
                &owner,
                COLLATERAL_UNITS_PER_STORAGE_KEY,
            );
            Some(owner)
        } else {
            None
        };

        self.insert_storage_write_cache(
            key,
            StorageValue {
                owner: new_owner,
                value,
            },
        );
        Ok(())
    }

    #[cfg(test)]
    pub fn set_storage_simple(&mut self, key: Vec<u8>, value: U256) {
        self.insert_storage_write_cache(
            key,
            StorageValue { owner: None, value },
        );
    }

    pub fn delete_storage_range(
        &mut self, db_deletion_log: impl Iterator<Item = (Vec<u8>, Box<[u8]>)>,
        key_prefix: &[u8], substate: &mut Substate,
    ) -> DbResult<()> {
        assert_eq!(self.address.space, Space::Native);
        let delete_all = key_prefix.is_empty();

        // Its strong count should be 1 and will not cause memory copy,
        // unless in test and gas estimation.
        assert!(self.storage_write_checkpoint.is_none());
        let write_cache = &mut self.storage_write_cache.write();
        // Must have no checkpoint in range deletion
        for (k, v) in write_cache.iter_mut() {
            if k.starts_with(key_prefix) && !v.value.is_zero() {
                if let Some(old_owner) = v.owner {
                    substate.record_storage_release(
                        &old_owner,
                        COLLATERAL_UNITS_PER_STORAGE_KEY,
                    );
                };
                *v = StorageValue::default();
            }
        }

        let read_cache = self.storage_read_cache.read();
        for (key, raw_value) in db_deletion_log
            .into_iter()
            .filter_map(|(k, v)| Some((decode_storage_key(&k)?, v)))
        {
            match write_cache.entry(key.clone()) {
                Vacant(entry) => {
                    // Propogate the db changes to cache
                    // However, if all keys are removed, we don't update
                    // cache since it will be cleared later.
                    if !delete_all {
                        entry.insert(StorageValue::default());
                    }

                    if !delete_all && !read_cache.contains_key(&key) {
                        // Backward compatible with an existing bug
                        // When remove whitelist entries, if the entry does not
                        // appear in the cache, the collateral is not refunded
                        // correctly.
                        continue;
                    }
                }
                Occupied(_) => {
                    // The key has been modified in cache, and the db holds
                    // a deprecated version.
                    // So we do nothing here.
                    continue;
                }
            }
            // Decode owner
            let StorageValue { owner, value } =
                rlp::decode::<StorageValue>(&raw_value)?;
            assert!(!value.is_zero());
            let owner = owner.unwrap_or(self.address.address);
            substate.record_storage_release(
                &owner,
                COLLATERAL_UNITS_PER_STORAGE_KEY,
            );
        }
        std::mem::drop(read_cache);

        if delete_all {
            write_cache.clear();
            self.storage_read_cache.write().clear();
            self.pending_db_clear = true;
        }
        Ok(())
    }

    fn cached_entry_at(&self, key: &[u8]) -> Option<StorageValue> {
        if let Some(entry) = self.storage_write_cache.read().get(key) {
            return Some(*entry);
        }
        if let Some(entry) = self.storage_read_cache.read().get(key) {
            return Some(*entry);
        }
        None
    }

    #[cfg(test)]
    pub fn cached_value_at_cache(&self, key: &[u8]) -> Option<U256> {
        self.cached_entry_at(key).map(|e| e.value)
    }

    #[cfg(test)]
    fn cached_entry_at_checkpoint(
        &self, key: &[u8], state_checkpoint_id: usize,
    ) -> Option<CheckpointEntry<StorageValue>> {
        if self.storage_write_checkpoint.is_none() {
            return None;
        }
        if self
            .storage_write_checkpoint
            .as_ref()
            .unwrap()
            .get_state_cp_id()
            < state_checkpoint_id
        {
            return None;
        }
        self.storage_write_checkpoint.as_ref().unwrap().get(key)
    }

    #[cfg(test)]
    pub fn cached_value_at_checkpoint(
        &self, key: &[u8], state_checkpoint_id: usize,
    ) -> Option<CheckpointEntry<U256>> {
        self.cached_entry_at_checkpoint(key, state_checkpoint_id)
            .map(|e: CheckpointEntry<StorageValue>| match e {
                CheckpointEntry::Unchanged => CheckpointEntry::Unchanged,
                CheckpointEntry::Recorded(sv) => {
                    CheckpointEntry::Recorded(sv.value)
                }
            })
    }

    // If a contract is removed, and then some one transfer balance to it,
    // `storage_at` will return incorrect value. But this case should never
    // happens.
    pub fn storage_at(
        &self, db: &StateDbGeneric, key: &[u8],
    ) -> DbResult<U256> {
        Ok(self.storage_entry_at(db, key)?.value)
    }

    // If a contract is removed, and then some one transfer balance to it,
    // `storage_at` will return incorrect value. But this case should never
    // happens.
    pub fn storage_entry_at(
        &self, db: &StateDbGeneric, key: &[u8],
    ) -> DbResult<StorageValue> {
        Ok(if let Some(value) = self.cached_entry_at(key) {
            value
        } else if self.fresh_storage() {
            StorageValue::default()
        } else {
            self.get_and_cache_storage(db, key)?
        })
    }

    pub fn transient_storage_at(&self, key: &[u8]) -> U256 {
        self.transient_storage_cache
            .read()
            .get(key)
            .cloned()
            .unwrap_or_default()
    }

    fn get_and_cache_storage(
        &self, db: &StateDbGeneric, key: &[u8],
    ) -> DbResult<StorageValue> {
        let storage_key =
            StorageKey::new_storage_key(&self.address.address, key.as_ref())
                .with_space(self.address.space);
        let StorageValue { mut owner, value } =
            db.get::<StorageValue>(storage_key)?.unwrap_or_default();
        if !value.is_zero() && owner.is_none() && self.should_have_owner(key) {
            owner = Some(self.address.address)
        }
        let storage_value = StorageValue { owner, value };
        self.storage_read_cache
            .write()
            .insert(key.to_vec(), storage_value.clone());
        Ok(storage_value)
    }

    pub fn transient_set_storage(&mut self, key: Vec<u8>, value: U256) {
        self.insert_transient_write_cache(key, value);
    }

    pub(super) fn should_have_owner(&self, _key: &[u8]) -> bool {
        self.address.space == Space::Native
            && self.address.address != SYSTEM_STORAGE_ADDRESS
    }

    pub fn change_storage_value(
        &mut self, db: &StateDbGeneric, key: &[u8], value: U256,
    ) -> DbResult<()> {
        let mut entry = self.storage_entry_at(db, key)?;
        if !entry.value.is_zero() {
            entry.value = value;
            self.insert_storage_write_cache(key.to_vec(), entry);
        } else {
            warn!("Change storage value outside transaction fails: current value is zero, tx {:?}, key {:?}", self.address, key);
        }
        Ok(())
    }

    #[cfg(test)]
    pub fn storage_layout_change(&self) -> Option<&StorageLayout> {
        self.storage_layout_change.as_ref()
    }

    #[cfg(test)]
    pub fn set_storage_layout(&mut self, layout: StorageLayout) {
        self.storage_layout_change = Some(layout);
    }
}

fn decode_storage_key(key: &Vec<u8>) -> Option<Vec<u8>> {
    if let StorageKeyWithSpace {
        key: StorageKey::StorageKey { storage_key, .. },
        ..
    } = StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&key[..])
    {
        Some(storage_key.to_vec())
    } else {
        // Should be unreachable
        None
    }
}