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
pub const DIFFICULTY_BOMB_DIVISOR: f64 = 1024f64;
pub fn naive_retarget(
block_timestamp: i64,
parent_timestamp: i64,
target_block_time: i64,
parent_difficulty: u64,
) -> u64 {
let time_elapsed = block_timestamp - parent_timestamp;
if time_elapsed == target_block_time || time_elapsed == 0 {
parent_difficulty
} else {
let parent_diff = parent_difficulty as f64;
let mut x: f64;
x = (target_block_time - time_elapsed) as f64;
x /= target_block_time as f64;
x *= parent_diff;
x = parent_diff - x;
println!("old difficulty {:#x}", parent_difficulty);
println!("new difficulty {:#x}", x as u64);
x as u64
}
}
pub fn bitcoin_retarget(
block_timestamp: i64,
parent_timestamp: i64,
target_block_time: i64,
parent_difficulty: u64,
) -> u64 {
let mut time_elapsed = block_timestamp - parent_timestamp;
if time_elapsed < target_block_time / 2 {
time_elapsed = target_block_time / 2
} else if time_elapsed > target_block_time * 2 {
time_elapsed = target_block_time * 2
}
let mut x: u64;
x = match parent_difficulty.checked_mul(time_elapsed as u64) {
Some(x) => x,
None => u64::max_value(),
};
x /= target_block_time as u64;
x
}
pub fn custom_retarget(
_block_timestamp: i64,
_parent_timestamp: i64,
_target_block_time: i64,
_parent_difficulty: u64,
) -> u64 {
unimplemented!()
}
pub fn ethereum_retarget(block_timestamp: i64, parent_timestamp: i64, parent_difficulty: u64) -> u64 {
let parent_diff = parent_difficulty as f64;
let mut x: f64;
let y: f64;
x = (block_timestamp - parent_timestamp) as f64;
x /= 10f64;
x = 1f64 - x;
x = f64::max(x, -99f64);
y = parent_diff / DIFFICULTY_BOMB_DIVISOR;
x *= y;
x += parent_diff;
println!("old difficulty {:#x}", parent_difficulty);
println!("new difficulty {:#x}", x as u64);
x as u64
}