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
use microkv::namespace::NamespaceMicroKV;
use microkv::MicroKV;

pub fn migrate(microkv: &MicroKV) -> color_eyre::Result<()> {
    let n_microkv = microkv.namespace("task-pangolin-ropsten");
    migrate_tracker_ropsten(&n_microkv)?;
    migrate_tracker_pangolin(&n_microkv)?;
    migrate_affirm(&n_microkv)?;
    Ok(())
}

fn migrate_tracker_ropsten(microkv: &NamespaceMicroKV) -> color_eyre::Result<()> {
    if let Some(value) = microkv.get("scan.ropsten.finish")? {
        if value.is_number() {
            let last_block = value.as_u64().unwrap();
            microkv.put("scan.ropsten.redeem.current", &last_block)?;
            microkv.put("scan.ropsten.check.current", &last_block)?;
            microkv.put("scan.ropsten.affirm.current", &last_block)?;
        }
    }
    if let Some(value) = microkv.get("scan.ropsten.running")? {
        let mut is_running = false;
        if value.is_boolean() {
            is_running = value.as_bool().unwrap_or(false);
        }
        if value.is_string() {
            is_running = value.as_str().map_or(false, |v| v == "true");
        }
        if is_running {
            microkv.put("scan.ropsten.redeem.running", &true)?;
            microkv.put("scan.ropsten.check.running", &true)?;
            microkv.put("scan.ropsten.affirm.running", &true)?;
        }
    }
    for key in &[
        "scan.ropsten.running",
        "scan.ropsten.finish",
        "scan.ropsten.current",
        "scan.ropsten.next",
        "scan.ropsten.skipped",
        "scan.ropsten.fast_mode",
    ] {
        microkv.delete(key)?;
    }
    Ok(())
}

fn migrate_tracker_pangolin(microkv: &NamespaceMicroKV) -> color_eyre::Result<()> {
    if let Some(value) = microkv.get("scan.pangolin.finish")? {
        if value.is_number() {
            microkv.put("scan.pangolin.current", &value.as_u64().unwrap_or(0))?;
        }
    }
    for key in &[
        "scan.pangolin.finish",
        "scan.pangolin.next",
        "scan.pangolin.skipped",
        "scan.pangolin.fast_mode",
    ] {
        microkv.delete(key)?;
    }
    Ok(())
}

fn migrate_affirm(microkv: &NamespaceMicroKV) -> color_eyre::Result<()> {
    if let Some(value) = microkv.get("target")? {
        if value.is_number() {
            microkv.put("affirm.target", &value.as_u64().unwrap_or(0))?;
        }
        microkv.delete("target")?;
    }
    if let Some(value) = microkv.get("relayed")? {
        if value.is_number() {
            microkv.put("affirm.relayed", &value.as_u64().unwrap_or(0))?;
        }
        microkv.delete("relayed")?;
    }
    Ok(())
}