@@ -9,11 +9,12 @@ use sp_std::{cmp, result};
9
9
use xrml_support:: { debug, ensure_with_errorlog, error, info} ;
10
10
11
11
// light-bitcoin
12
- use btc_chain:: BlockHeader ;
12
+ use btc_chain:: BlockHeader as BTCHeader ;
13
+ use btc_keys:: Network ;
13
14
use btc_primitives:: { Compact , H256 , U256 } ;
14
15
15
16
use super :: ChainErr ;
16
- use crate :: types:: Params ;
17
+ use crate :: types:: BTCParams ;
17
18
use crate :: { Error , Module , Trait } ;
18
19
19
20
pub struct HeaderVerifier < ' a > {
@@ -23,7 +24,7 @@ pub struct HeaderVerifier<'a> {
23
24
}
24
25
25
26
impl < ' a > HeaderVerifier < ' a > {
26
- pub fn new < T : Trait > ( header : & ' a BlockHeader , height : u32 ) -> result:: Result < Self , ChainErr > {
27
+ pub fn new < T : Trait > ( header : & ' a BTCHeader , height : u32 ) -> result:: Result < Self , ChainErr > {
27
28
let now: T :: Moment = pallet_timestamp:: Module :: < T > :: now ( ) ;
28
29
// bitcoin use u32 to log time, we think the timestamp would not more then u32
29
30
let current_time: u32 = now. saturated_into :: < u32 > ( ) ;
@@ -36,9 +37,9 @@ impl<'a> HeaderVerifier<'a> {
36
37
}
37
38
38
39
pub fn check < T : Trait > ( & self ) -> DispatchResult {
39
- let params: Params = Module :: < T > :: params_info ( ) ;
40
- let network_id: u32 = Module :: < T > :: network_id ( ) ;
41
- if network_id == 0 {
40
+ let params: BTCParams = Module :: < T > :: params_info ( ) ;
41
+ let network_id: Network = Module :: < T > :: network_id ( ) ;
42
+ if let Network :: Mainnet = network_id {
42
43
self . work . check :: < T > ( & params) ?;
43
44
}
44
45
self . proof_of_work . check :: < T > ( & params) ?;
@@ -48,16 +49,16 @@ impl<'a> HeaderVerifier<'a> {
48
49
}
49
50
50
51
pub struct HeaderWork < ' a > {
51
- header : & ' a BlockHeader ,
52
+ header : & ' a BTCHeader ,
52
53
height : u32 ,
53
54
}
54
55
55
56
impl < ' a > HeaderWork < ' a > {
56
- fn new ( header : & ' a BlockHeader , height : u32 ) -> Self {
57
+ fn new ( header : & ' a BTCHeader , height : u32 ) -> Self {
57
58
HeaderWork { header, height }
58
59
}
59
60
60
- fn check < T : Trait > ( & self , p : & Params ) -> DispatchResult {
61
+ fn check < T : Trait > ( & self , p : & BTCParams ) -> DispatchResult {
61
62
let previous_header_hash = self . header . previous_header_hash . clone ( ) ;
62
63
let work = work_required :: < T > ( previous_header_hash, self . height , p) ;
63
64
ensure_with_errorlog ! (
@@ -72,13 +73,13 @@ impl<'a> HeaderWork<'a> {
72
73
}
73
74
}
74
75
75
- pub fn work_required < T : Trait > ( parent_hash : H256 , height : u32 , params : & Params ) -> Compact {
76
+ pub fn work_required < T : Trait > ( parent_hash : H256 , height : u32 , params : & BTCParams ) -> Compact {
76
77
let max_bits = params. max_bits ( ) . into ( ) ;
77
78
if height == 0 {
78
79
return max_bits;
79
80
}
80
81
81
- let parent_header: BlockHeader = Module :: < T > :: block_header_for ( & parent_hash) . unwrap ( ) . header ;
82
+ let parent_header: BTCHeader = Module :: < T > :: btc_header_for ( & parent_hash) . unwrap ( ) . header ;
82
83
83
84
if is_retarget_height ( height, params) {
84
85
let new_work = work_required_retarget :: < T > ( parent_header, height, params) ;
@@ -92,15 +93,15 @@ pub fn work_required<T: Trait>(parent_hash: H256, height: u32, params: &Params)
92
93
parent_header. bits
93
94
}
94
95
95
- pub fn is_retarget_height ( height : u32 , p : & Params ) -> bool {
96
+ pub fn is_retarget_height ( height : u32 , p : & BTCParams ) -> bool {
96
97
height % p. retargeting_interval ( ) == 0
97
98
}
98
99
99
100
/// Algorithm used for retargeting work every 2 weeks
100
101
pub fn work_required_retarget < T : Trait > (
101
- parent_header : BlockHeader ,
102
+ parent_header : BTCHeader ,
102
103
height : u32 ,
103
- params : & Params ,
104
+ params : & BTCParams ,
104
105
) -> Compact {
105
106
let retarget_num = height - params. retargeting_interval ( ) ;
106
107
@@ -112,7 +113,7 @@ pub fn work_required_retarget<T: Trait>(
112
113
let hash_list = Module :: < T > :: block_hash_for ( & retarget_num) ;
113
114
for h in hash_list {
114
115
// look up in main chain
115
- if let Some ( info) = Module :: < T > :: block_header_for ( h) {
116
+ if let Some ( info) = Module :: < T > :: btc_header_for ( h) {
116
117
if info. confirmed == true {
117
118
retarget_header = info. header ;
118
119
break ;
@@ -152,7 +153,7 @@ pub fn work_required_retarget<T: Trait>(
152
153
}
153
154
154
155
/// Returns constrained number of seconds since last retarget
155
- pub fn retarget_timespan ( retarget_timestamp : u32 , last_timestamp : u32 , p : & Params ) -> u32 {
156
+ pub fn retarget_timespan ( retarget_timestamp : u32 , last_timestamp : u32 , p : & BTCParams ) -> u32 {
156
157
// TODO i64??
157
158
// subtract unsigned 32 bit numbers in signed 64 bit space in
158
159
// order to prevent underflow before applying the range constraint.
@@ -169,15 +170,15 @@ fn range_constrain(value: i64, min: i64, max: i64) -> i64 {
169
170
}
170
171
171
172
pub struct HeaderProofOfWork < ' a > {
172
- header : & ' a BlockHeader ,
173
+ header : & ' a BTCHeader ,
173
174
}
174
175
175
176
impl < ' a > HeaderProofOfWork < ' a > {
176
- fn new ( header : & ' a BlockHeader ) -> Self {
177
+ fn new ( header : & ' a BTCHeader ) -> Self {
177
178
HeaderProofOfWork { header }
178
179
}
179
180
180
- fn check < T : Trait > ( & self , p : & Params ) -> DispatchResult {
181
+ fn check < T : Trait > ( & self , p : & BTCParams ) -> DispatchResult {
181
182
if is_valid_proof_of_work ( p. max_bits ( ) , self . header . bits , & self . header . hash ( ) ) {
182
183
Ok ( ( ) )
183
184
} else {
@@ -209,19 +210,19 @@ pub fn is_valid_proof_of_work(max_work_bits: Compact, bits: Compact, hash: &H256
209
210
}
210
211
211
212
pub struct HeaderTimestamp < ' a > {
212
- header : & ' a BlockHeader ,
213
+ header : & ' a BTCHeader ,
213
214
current_time : u32 ,
214
215
}
215
216
216
217
impl < ' a > HeaderTimestamp < ' a > {
217
- fn new ( header : & ' a BlockHeader , current_time : u32 ) -> Self {
218
+ fn new ( header : & ' a BTCHeader , current_time : u32 ) -> Self {
218
219
HeaderTimestamp {
219
220
header,
220
221
current_time,
221
222
}
222
223
}
223
224
224
- fn check < T : Trait > ( & self , p : & Params ) -> DispatchResult {
225
+ fn check < T : Trait > ( & self , p : & BTCParams ) -> DispatchResult {
225
226
if self . header . time > self . current_time + p. block_max_future ( ) {
226
227
error ! ( "[HeaderTimestamp check]|Futuristic timestamp|header time{:}|current time:{:}|max_future{:?}" , self . header. time, self . current_time, p. block_max_future( ) ) ;
227
228
Err ( Error :: < T > :: HeaderFuturisticTimestamp ) ?
0 commit comments