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
use rust_base58::FromBase58;
use serde_json::from_str;
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::ops::Deref;

use crate::common::block_status::BlockStatus;
use crate::common::exodus_block::ExodusBlock;
use crate::common::genesis_header::GenesisHeader;
use crate::common::header::Header;
use crate::common::meta::Meta;
use crate::traits::{Decode, Exception};

pub fn init_exodus_block() -> Result<ExodusBlock, Box<Error>> {
    let mut path = env::current_dir()?;
    path.push("data/exodusBlock.dat");
    let mut exodus_file = File::open(path)?;
    let mut exodus_buf = Vec::new();
    exodus_file.read_to_end(&mut exodus_buf)?;
    ExodusBlock::decode(&exodus_buf)
}

pub fn init_exodus_meta() -> Result<(Meta<Header>, Vec<u8>), Box<Error>> {
    let exodus_hash = "6yt4X2giLv73Jh2b2iGN1Ns7fBUQYUdAS7LpQxdtGQJq"
        .from_base58()
        .map_err(|e| Exception::new(&format!("Error: {:?}", e)))?;
    let mut path = env::current_dir()?;
    path.push("data/exodus_meta.json");
    let mut file = File::open(path)?;
    let mut buff = String::new();
    file.read_to_string(&mut buff)?;
    let json: serde_json::Value = from_str(&buff)?;
    let height: u32;
    match &json["height"] {
        serde_json::Value::Number(n) => {
            if let Some(block_height) = n.as_u64() {
                height = block_height as u32;
            } else {
                return Err(Box::new(Exception::new("Exodus Height not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus Height not Found")));
        }
    }
    let t_ema: f64;
    match &json["tEMA"] {
        serde_json::Value::Number(n) => {
            if let Some(block_tema) = n.as_f64() {
                t_ema = block_tema;
            } else {
                return Err(Box::new(Exception::new("Exodus TEMA not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus TEMA not Found")));
        }
    }
    let p_ema: f64;
    match &json["pEMA"] {
        serde_json::Value::Number(n) => {
            if let Some(block_pema) = n.as_f64() {
                p_ema = block_pema;
            } else {
                return Err(Box::new(Exception::new("Exodus pEMA not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus pEMA not Found")));
        }
    }
    let next_difficulty: f64;
    match &json["nextDifficulty"] {
        serde_json::Value::Number(n) => {
            if let Some(next_diff) = n.as_f64() {
                next_difficulty = next_diff;
            } else {
                return Err(Box::new(Exception::new("Exodus nextDifficulty not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus nextDifficulty not Found")));
        }
    }
    let total_work: f64;
    match &json["totalWork"] {
        serde_json::Value::Number(n) => {
            if let Some(tot_work) = n.as_f64() {
                total_work = tot_work;
            } else {
                return Err(Box::new(Exception::new("Exodus totalWork not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus totalWork not Found")));
        }
    }
    let mut merkle_root: Vec<u8> = Vec::with_capacity(32);
    let mut state_root: Vec<u8> = Vec::with_capacity(32);
    match &json["header"]["merkleRoot"] {
        serde_json::Value::Array(vec) => {
            for num in vec {
                match num {
                    serde_json::Value::Number(n) => {
                        if let Some(val) = n.as_u64() {
                            merkle_root.push(val as u8);
                        } else {
                            return Err(Box::new(Exception::new("Exodus merkleRoot not found")));
                        }
                    }
                    _ => {
                        return Err(Box::new(Exception::new("Exodus merkleRoot not found")));
                    }
                }
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus merkleRoot not found")));
        }
    }
    match &json["header"]["stateRoot"] {
        serde_json::Value::Array(vec) => {
            for num in vec {
                match num {
                    serde_json::Value::Number(n) => {
                        if let Some(val) = n.as_u64() {
                            state_root.push(val as u8);
                        } else {
                            return Err(Box::new(Exception::new("Exodus stateRoot not found")));
                        }
                    }
                    _ => {
                        return Err(Box::new(Exception::new("Exodus stateRoot not found")));
                    }
                }
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus stateRoot not found")));
        }
    }
    let time_stamp;
    match &json["header"]["timeStamp"] {
        serde_json::Value::Number(n) => {
            if let Some(exodus_time_stamp) = n.as_u64() {
                time_stamp = exodus_time_stamp;
            } else {
                return Err(Box::new(Exception::new("Exodus TimeStampt not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus Timestamp not Found")));
        }
    }
    let difficulty;
    match &json["header"]["difficulty"] {
        serde_json::Value::Number(n) => {
            if let Some(diff) = n.as_f64() {
                difficulty = diff;
            } else {
                return Err(Box::new(Exception::new("Exodus pEMA not Found")));
            }
        }
        _ => {
            return Err(Box::new(Exception::new("Exodus pEMA not Found")));
        }
    }
    let meta = Meta::new(
        height,
        GenesisHeader::new(merkle_root, time_stamp, difficulty, state_root)
            .deref()
            .clone(),
        t_ema,
        p_ema,
        next_difficulty,
        total_work,
        None,
        None,
        None,
        BlockStatus::MainChain,
    );
    Ok((meta, exodus_hash))
}

#[cfg(test)]
pub mod tests {
    use super::*;

    #[test]
    fn it_reads_exodus_meta() {
        assert!(init_exodus_meta().is_ok());
    }
    #[test]
    fn it_reads_exodus_block() {
        assert!(init_exodus_block().is_ok());
    }
}