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
use crate::{Error, GetCompact};
use primitive_types::U256;
pub const TARGET_TIMESPAN: u64 = 14 * 24 * 60 * 60;
pub const TARGET_TIMESPAN_DIVISOR: u64 = 4;
pub const UNROUNDED_MAX_TARGET: U256 = U256([<u64>::MAX, <u64>::MAX, <u64>::MAX, 0x0000_0000_ffff_ffffu64]);
pub fn calculate_next_work_required(
previous_target: U256,
first_block_time: u64,
last_block_time: u64,
) -> Result<u32, Error> {
let mut actual_timespan = last_block_time.saturating_sub(first_block_time);
if actual_timespan < TARGET_TIMESPAN / TARGET_TIMESPAN_DIVISOR {
actual_timespan = TARGET_TIMESPAN / TARGET_TIMESPAN_DIVISOR;
}
if actual_timespan > TARGET_TIMESPAN * TARGET_TIMESPAN_DIVISOR {
actual_timespan = TARGET_TIMESPAN * TARGET_TIMESPAN_DIVISOR;
}
let target = previous_target * actual_timespan;
let target = target / TARGET_TIMESPAN;
if target > UNROUNDED_MAX_TARGET {
UNROUNDED_MAX_TARGET
} else {
target
}
.get_compact()
.ok_or(Error::InvalidCompact)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::SetCompact;
use frame_support::assert_ok;
fn target_set_compact(bits: u32) -> U256 {
U256::set_compact(bits).unwrap()
}
#[test]
fn get_next_work() {
let previous_target = target_set_compact(0x1d00ffff);
let first_block_time = 1261130161; let last_block_time = 1262152739; assert_ok!(
calculate_next_work_required(previous_target, first_block_time, last_block_time),
0x1d00d86a
);
}
#[test]
fn get_next_work_pow_limit() {
let previous_target = target_set_compact(0x1d00ffff);
let first_block_time = 1231006505; let last_block_time = 1233061996; assert_ok!(
calculate_next_work_required(previous_target, first_block_time, last_block_time),
0x1d00ffff
);
}
#[test]
fn get_next_work_lower_limit_actual() {
let previous_target = target_set_compact(0x1c05a3f4);
let first_block_time = 1279008237; let last_block_time = 1279297671; assert_ok!(
calculate_next_work_required(previous_target, first_block_time, last_block_time),
0x1c0168fd
);
}
#[test]
fn get_next_work_upper_limit_actual() {
let previous_target = target_set_compact(0x1c387f6f);
let first_block_time = 1263163443; let last_block_time = 1269211443; assert_ok!(
calculate_next_work_required(previous_target, first_block_time, last_block_time),
0x1d00e1fd
);
}
#[test]
fn get_next_work_recent() {
let previous_target = target_set_compact(0x170ed0eb);
let first_block_time = 1632234876; let last_block_time = 1633390031; assert_ok!(
calculate_next_work_required(previous_target, first_block_time, last_block_time),
0x170e2632 );
}
}