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
use crate::blocks::Resistable;
use scsys::prelude::{hasher, Hashable, H256};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct BlockDifficulty {
pub pos: H256,
pub pow: H256,
}
impl BlockDifficulty {
pub fn new(pos: H256, pow: H256) -> Self {
Self { pos, pow }
}
}
impl Hashable for BlockDifficulty {
fn hash(&self) -> H256 {
hasher(self).as_slice().to_owned().into()
}
}
impl Resistable for BlockDifficulty {
fn pos_difficulty(&self) -> H256 {
self.pos
}
fn pow_difficulty(&self) -> H256 {
self.pow
}
}
impl Default for BlockDifficulty {
fn default() -> Self {
let pos = <H256>::from(crate::INITIAL_POS_DIFFICULTY);
let pow = <H256>::from(crate::INITIAL_POW_DIFFICULTY);
Self::new(pos, pow)
}
}
impl std::fmt::Display for BlockDifficulty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(&self).unwrap())
}
}
impl std::convert::From<(H256, H256)> for BlockDifficulty {
fn from(data: (H256, H256)) -> Self {
Self::new(data.0, data.1)
}
}
impl std::convert::From<(&H256, &H256)> for BlockDifficulty {
fn from(data: (&H256, &H256)) -> Self {
Self::new(*data.0, *data.1)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_difficulty() {
let a = BlockDifficulty::default();
let b = BlockDifficulty::new(
<H256>::from(crate::INITIAL_POS_DIFFICULTY),
<H256>::from(crate::INITIAL_POW_DIFFICULTY),
);
assert_eq!(&a, &b)
}
}