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
#![allow(dead_code)]
use crate::{
    chain::array::{H128, H512},
    hex,
};
use scale::{Decode, Encode};

/// Ethash proof
#[derive(Encode, Decode, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
pub struct EthashProof {
    /// Dag nodes
    pub dag_nodes: [H512; 2],
    /// Merkle Proofs
    pub proof: Vec<H128>,
}

impl EthashProof {
    /// Generate EthashProof from hex array
    pub fn from_tuple(dag_nodes: [&str; 2], proof: [&str; 23]) -> EthashProof {
        EthashProof {
            dag_nodes: [
                H512(bytes!(dag_nodes[0], 64)),
                H512(bytes!(dag_nodes[1], 64)),
            ],
            proof: proof
                .iter()
                .map(|s| H128(bytes!(*s, 16)))
                .collect::<Vec<H128>>(),
        }
    }
}

/// Json string format of `EthashProof`
#[derive(Serialize, Encode)]
pub struct EthashProofJson {
    dag_nodes: Vec<String>,
    proof: Vec<String>,
}

impl From<&EthashProof> for EthashProofJson {
    fn from(e: &EthashProof) -> EthashProofJson {
        EthashProofJson {
            dag_nodes: e
                .dag_nodes
                .as_ref()
                .iter()
                .map(|n| format!("0x{}", hex!(n.0.to_vec())))
                .collect(),
            proof: e
                .proof
                .iter()
                .map(|p| format!("0x{}", hex!(p.0.to_vec())))
                .collect(),
        }
    }
}