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
use clap::Arg;
use log::{debug, error};
use o1vm::{
    cannon::PreimageKey,
    cannon_cli::{main_cli, read_configuration},
    preimage_oracle::{PreImageOracle, PreImageOracleT},
};
use std::{
    io::{self},
    path::{Path, PathBuf},
    process::ExitCode,
    str::FromStr,
};

fn main() -> ExitCode {
    use rand::Rng;

    env_logger::init();

    // Add command-line parameter to read the Optimism op-program DB directory
    let cli = main_cli().arg(
        Arg::new("preimage-db-dir")
            .long("preimage-db-dir")
            .value_name("PREIMAGE_DB_DIR"),
    );

    // Now read matches with the additional argument(s)
    let matches = cli.get_matches();

    let configuration = read_configuration(&matches);

    // Get DB directory and abort if unset
    let preimage_db_dir = matches.get_one::<String>("preimage-db-dir");

    if let Some(preimage_key_dir) = preimage_db_dir {
        let mut po = PreImageOracle::create(&configuration.host);
        let _child = po.start();
        debug!("Let server start");
        std::thread::sleep(std::time::Duration::from_secs(5));

        debug!("Reading from {}", preimage_key_dir);
        // Get all files under the preimage db dir
        let paths = std::fs::read_dir(preimage_key_dir)
            .unwrap()
            .map(|res| res.map(|e| e.path()))
            .collect::<Result<Vec<_>, io::Error>>()
            .unwrap();

        // Just take 10 elements at random to test
        let how_many = 10_usize;
        let max = paths.len();
        let mut rng = rand::thread_rng();

        let mut selected: Vec<PathBuf> = vec![PathBuf::new(); how_many];
        for pb in selected.iter_mut() {
            let idx = rng.gen_range(0..max);
            *pb = paths[idx].to_path_buf();
        }

        for (idx, path) in selected.into_iter().enumerate() {
            let preimage_key = Path::new(&path)
                .file_name()
                .unwrap()
                .to_str()
                .unwrap()
                .split('.')
                .collect::<Vec<_>>()[0];

            let hash = PreimageKey::from_str(preimage_key).unwrap();
            let mut key = hash.0;
            key[0] = 2; // Keccak

            debug!(
                "Generating OP Keccak key for {} at index {}",
                preimage_key, idx
            );

            let expected = std::fs::read_to_string(path).unwrap();

            debug!("Asking for preimage");
            let preimage = po.get_preimage(key);
            let got = hex::encode(preimage.get()).to_string();

            assert_eq!(expected, got);
        }
        ExitCode::SUCCESS
    } else {
        error!("Unset command-line argument --preimage-db-dir. Cannot run test. Please set parameter and rerun.");
        ExitCode::FAILURE
    }
}