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

mod snapshot;
pub use snapshot::FakeSnapshotMptDb;

#[cfg(test)]
mod proofs;
#[cfg(test)]
mod sharded_iter_merger;
#[cfg(test)]
mod state;

#[cfg(test)]
const TEST_NUMBER_OF_KEYS: usize = 100000;

#[derive(Default)]
pub struct FakeDbForStateTest {}

// Compatible hack for KeyValueDB
impl MallocSizeOf for FakeDbForStateTest {
    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { 0 }
}

impl KeyValueDB for FakeDbForStateTest {
    fn get(&self, _col: u32, _key: &[u8]) -> std::io::Result<Option<DBValue>> {
        Ok(None)
    }

    fn get_by_prefix(&self, _col: u32, _prefix: &[u8]) -> Option<Box<[u8]>> {
        unreachable!()
    }

    /// No-op
    fn write_buffered(&self, _transaction: DBTransaction) {}

    /// No-op
    fn flush(&self) -> std::io::Result<()> { Ok(()) }

    fn iter<'a>(
        &'a self, _col: u32,
    ) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)>> {
        unreachable!()
    }

    fn iter_from_prefix<'a>(
        &'a self, _col: u32, _prefix: &'a [u8],
    ) -> Box<dyn Iterator<Item = (Box<[u8]>, Box<[u8]>)>> {
        unreachable!()
    }

    fn restore(&self, _new_db: &str) -> std::io::Result<()> { unreachable!() }
}

#[cfg(any(test, feature = "testonly_code"))]
pub struct FakeStateManager {
    data_dir: String,
    state_manager: Option<Arc<StateManager>>,
}

#[cfg(any(test, feature = "testonly_code"))]
impl FakeStateManager {
    fn new(
        conflux_data_dir: String, snapshot_epoch_count: u32,
    ) -> Result<Self> {
        // Use a random directory to prevent conflicts in concurrently running
        // tests.
        let unit_test_data_dir =
            conflux_data_dir + &random::<u64>().to_string();
        fs::create_dir_all(unit_test_data_dir.as_str())?;
        let mut storage_conf = StorageConfiguration::new_default(
            &unit_test_data_dir,
            snapshot_epoch_count,
            20000,
        );
        storage_conf.delta_mpts_cache_size = 20_000_000;
        storage_conf.delta_mpts_cache_start_size = 1_000_000;
        storage_conf.delta_mpts_node_map_vec_size = 20_000_000;
        storage_conf.delta_mpts_slab_idle_size = 200_000;

        Ok(FakeStateManager {
            data_dir: unit_test_data_dir,
            state_manager: Some(Arc::new(StateManager::new(storage_conf)?)),
        })
    }
}

#[cfg(any(test, feature = "testonly_code"))]
impl Drop for FakeStateManager {
    fn drop(&mut self) {
        self.state_manager.take();
        fs::remove_dir_all(self.data_dir.as_str()).ok();
        let maybe_parent_dir = Path::new(self.data_dir.as_str()).parent();
        if let Some(parent_dir) = maybe_parent_dir {
            fs::remove_dir(parent_dir).ok();
        }
    }
}

#[cfg(any(test, feature = "testonly_code"))]
impl Deref for FakeStateManager {
    type Target = Arc<StateManager>;

    fn deref(&self) -> &Self::Target { self.state_manager.as_ref().unwrap() }
}

#[cfg(any(test, feature = "testonly_code"))]
impl DerefMut for FakeStateManager {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.state_manager.as_mut().unwrap()
    }
}

#[cfg(any(test, feature = "testonly_code"))]
pub fn new_state_manager_for_unit_test_with_snapshot_epoch_count(
    snapshot_epoch_count: u32,
) -> FakeStateManager {
    const WITH_LOGGER: bool = false;
    if WITH_LOGGER {
        log4rs::init_config(
            log4rs::config::Config::builder()
                .appender(
                    log4rs::config::Appender::builder().build(
                        "stdout",
                        Box::new(
                            log4rs::append::console::ConsoleAppender::builder()
                                .build(),
                        ),
                    ),
                )
                .build(
                    log4rs::config::Root::builder()
                        .appender("stdout")
                        .build(log::LevelFilter::Debug),
                )
                .unwrap(),
        )
        .ok();
    }

    FakeStateManager::new(
        "./conflux_unit_test_data_dir".to_string(),
        snapshot_epoch_count,
    )
    .unwrap()
}

#[cfg(any(test, feature = "testonly_code"))]
pub fn new_state_manager_for_unit_test() -> FakeStateManager {
    let snapshot_epoch_count = 10;
    new_state_manager_for_unit_test_with_snapshot_epoch_count(
        snapshot_epoch_count,
    )
}

#[derive(Default)]
pub struct DumpedMptKvIterator {
    pub kv: Vec<MptKeyValue>,
}

pub struct DumpedMptKvFallibleIterator {
    pub kv: Vec<MptKeyValue>,
    pub index: usize,
}

impl DumpedMptKvIterator {
    pub fn iterate<'a, DeltaMptDumper: KVInserter<MptKeyValue>>(
        &self, dumper: &mut DeltaMptDumper,
    ) -> Result<()> {
        let mut sorted_kv = self.kv.clone();
        sorted_kv.sort();
        for kv_item in sorted_kv {
            dumper.push(kv_item)?;
        }
        Ok(())
    }
}

impl KVInserter<MptKeyValue> for DumpedMptKvIterator {
    fn push(&mut self, v: MptKeyValue) -> Result<()> {
        let (mpt_key, value) = v;
        let snapshot_key =
            StorageKeyWithSpace::from_delta_mpt_key(&mpt_key).to_key_bytes();

        self.kv.push((snapshot_key, value));
        Ok(())
    }
}

impl FallibleIterator for DumpedMptKvFallibleIterator {
    type Error = Error;
    type Item = MptKeyValue;

    fn next(&mut self) -> Result<Option<Self::Item>> {
        let result = Ok(self.kv.get(self.index).cloned());
        self.index += 1;
        result
    }
}

#[cfg(test)]
fn generate_keys(number_of_keys: usize) -> Vec<Vec<u8>> {
    let mut rng = get_rng_for_test();

    let mut keys_num: Vec<u64> = Default::default();

    for _i in 0..number_of_keys {
        keys_num.push(rng.gen());
    }

    keys_num.sort();

    let mut keys = vec![];
    let mut last_key = keys_num[0];
    for key in &keys_num[1..number_of_keys] {
        if *key != last_key {
            keys.push(Vec::from(
                &unsafe { std::mem::transmute::<u64, [u8; 8]>(key.clone()) }[..],
            ));
        }
        last_key = *key;
    }

    keys.shuffle(&mut rng);
    keys
}

#[cfg(test)]
fn generate_account_keys(number_of_keys: usize) -> Vec<Vec<u8>> {
    let mut rng = get_rng_for_test();
    (0..number_of_keys)
        .map(|_| rng.gen::<[u8; 20]>().to_vec())
        .collect()
}

#[cfg(test)]
fn get_rng_for_test() -> ChaChaRng { ChaChaRng::from_seed([123; 32]) }

// Kept for debugging.
#[allow(dead_code)]
pub fn print_mpt_key(key: &[u8]) {
    print!("key = (");
    for char in key {
        print!(
            "{}, {}, ",
            CompressedPathRaw::first_nibble(*char),
            CompressedPathRaw::second_nibble(*char)
        );
    }
    println!(")");
}

#[cfg(any(test, feature = "testonly_code"))]
use crate::{impls::state_manager::StateManager, StorageConfiguration};
use crate::{
    impls::{
        errors::*,
        merkle_patricia_trie::{CompressedPathRaw, MptKeyValue},
    },
    KVInserter,
};
use fallible_iterator::FallibleIterator;
use kvdb::{DBTransaction, DBValue, KeyValueDB};
use parity_util_mem::{MallocSizeOf, MallocSizeOfOps};
use primitives::StorageKeyWithSpace;
#[cfg(any(test, feature = "testonly_code"))]
use rand::random;
#[cfg(test)]
use rand::{seq::SliceRandom, Rng, SeedableRng};
#[cfg(test)]
use rand_chacha::ChaChaRng;
#[cfg(any(test, feature = "testonly_code"))]
use std::{
    fs,
    ops::{Deref, DerefMut},
    path::Path,
    sync::Arc,
};