1
1
use crate :: block_status:: BlockStatus ;
2
- use crate :: synchronizer:: Synchronizer ;
3
2
use crate :: types:: { ActiveChain , BlockNumberAndHash , HeaderIndex , HeaderIndexView , IBDState } ;
3
+ use crate :: SyncShared ;
4
4
use ckb_constant:: sync:: {
5
5
BLOCK_DOWNLOAD_WINDOW , CHECK_POINT_WINDOW , INIT_BLOCKS_IN_TRANSIT_PER_PEER ,
6
6
} ;
@@ -9,34 +9,38 @@ use ckb_network::PeerIndex;
9
9
use ckb_systemtime:: unix_time_as_millis;
10
10
use ckb_types:: packed;
11
11
use std:: cmp:: min;
12
+ use std:: sync:: Arc ;
12
13
13
- pub struct BlockFetcher < ' a > {
14
- synchronizer : & ' a Synchronizer ,
14
+ pub struct BlockFetcher {
15
+ sync_shared : Arc < SyncShared > ,
15
16
peer : PeerIndex ,
16
17
active_chain : ActiveChain ,
17
18
ibd : IBDState ,
18
19
}
19
20
20
- impl < ' a > BlockFetcher < ' a > {
21
- pub fn new ( synchronizer : & ' a Synchronizer , peer : PeerIndex , ibd : IBDState ) -> Self {
22
- let active_chain = synchronizer . shared . active_chain ( ) ;
21
+ impl BlockFetcher {
22
+ pub fn new ( sync_shared : Arc < SyncShared > , peer : PeerIndex , ibd : IBDState ) -> Self {
23
+ let active_chain = sync_shared . active_chain ( ) ;
23
24
BlockFetcher {
25
+ sync_shared,
24
26
peer,
25
- synchronizer,
26
27
active_chain,
27
28
ibd,
28
29
}
29
30
}
30
31
31
32
pub fn reached_inflight_limit ( & self ) -> bool {
32
- let inflight = self . synchronizer . shared ( ) . state ( ) . read_inflight_blocks ( ) ;
33
+ let inflight = self . sync_shared . state ( ) . read_inflight_blocks ( ) ;
33
34
34
35
// Can't download any more from this peer
35
36
inflight. peer_can_fetch_count ( self . peer ) == 0
36
37
}
37
38
38
39
pub fn peer_best_known_header ( & self ) -> Option < HeaderIndex > {
39
- self . synchronizer . peers ( ) . get_best_known_header ( self . peer )
40
+ self . sync_shared
41
+ . state ( )
42
+ . peers ( )
43
+ . get_best_known_header ( self . peer )
40
44
}
41
45
42
46
pub fn update_last_common_header (
@@ -45,23 +49,28 @@ impl<'a> BlockFetcher<'a> {
45
49
) -> Option < BlockNumberAndHash > {
46
50
// Bootstrap quickly by guessing an ancestor of our best tip is forking point.
47
51
// Guessing wrong in either direction is not a problem.
48
- let mut last_common =
49
- if let Some ( header) = self . synchronizer . peers ( ) . get_last_common_header ( self . peer ) {
50
- header
51
- } else {
52
- let tip_header = self . active_chain . tip_header ( ) ;
53
- let guess_number = min ( tip_header. number ( ) , best_known. number ( ) ) ;
54
- let guess_hash = self . active_chain . get_block_hash ( guess_number) ?;
55
- ( guess_number, guess_hash) . into ( )
56
- } ;
52
+ let mut last_common = if let Some ( header) = self
53
+ . sync_shared
54
+ . state ( )
55
+ . peers ( )
56
+ . get_last_common_header ( self . peer )
57
+ {
58
+ header
59
+ } else {
60
+ let tip_header = self . active_chain . tip_header ( ) ;
61
+ let guess_number = min ( tip_header. number ( ) , best_known. number ( ) ) ;
62
+ let guess_hash = self . active_chain . get_block_hash ( guess_number) ?;
63
+ ( guess_number, guess_hash) . into ( )
64
+ } ;
57
65
58
66
// If the peer reorganized, our previous last_common_header may not be an ancestor
59
67
// of its current tip anymore. Go back enough to fix that.
60
68
last_common = self
61
69
. active_chain
62
70
. last_common_ancestor ( & last_common, best_known) ?;
63
71
64
- self . synchronizer
72
+ self . sync_shared
73
+ . state ( )
65
74
. peers ( )
66
75
. set_last_common_header ( self . peer , last_common. clone ( ) ) ;
67
76
@@ -80,13 +89,13 @@ impl<'a> BlockFetcher<'a> {
80
89
// Update `best_known_header` based on `unknown_header_list`. It must be involved before
81
90
// our acquiring the newest `best_known_header`.
82
91
if let IBDState :: In = self . ibd {
83
- let state = self . synchronizer . shared . state ( ) ;
92
+ let state = self . sync_shared . state ( ) ;
84
93
// unknown list is an ordered list, sorted from highest to lowest,
85
94
// when header hash unknown, break loop is ok
86
95
while let Some ( hash) = state. peers ( ) . take_unknown_last ( self . peer ) {
87
96
// Here we need to first try search from headermap, if not, fallback to search from the db.
88
97
// if not search from db, it can stuck here when the headermap may have been removed just as the block was downloaded
89
- if let Some ( header) = self . synchronizer . shared . get_header_index_view ( & hash, false ) {
98
+ if let Some ( header) = self . sync_shared . get_header_index_view ( & hash, false ) {
90
99
state
91
100
. peers ( )
92
101
. may_set_best_known_header ( self . peer , header. as_header_index ( ) ) ;
@@ -114,7 +123,8 @@ impl<'a> BlockFetcher<'a> {
114
123
// specially advance this peer's last_common_header at the case of both us on the same
115
124
// active chain.
116
125
if self . active_chain . is_main_chain ( & best_known. hash ( ) ) {
117
- self . synchronizer
126
+ self . sync_shared
127
+ . state ( )
118
128
. peers ( )
119
129
. set_last_common_header ( self . peer , best_known. number_and_hash ( ) ) ;
120
130
}
@@ -128,7 +138,7 @@ impl<'a> BlockFetcher<'a> {
128
138
return None ;
129
139
}
130
140
131
- let state = self . synchronizer . shared ( ) . state ( ) ;
141
+ let state = self . sync_shared . state ( ) ;
132
142
let mut inflight = state. write_inflight_blocks ( ) ;
133
143
let mut start = last_common. number ( ) + 1 ;
134
144
let mut end = min ( best_known. number ( ) , start + BLOCK_DOWNLOAD_WINDOW ) ;
@@ -156,7 +166,8 @@ impl<'a> BlockFetcher<'a> {
156
166
if status. contains ( BlockStatus :: BLOCK_STORED ) {
157
167
// If the block is stored, its ancestor must on store
158
168
// So we can skip the search of this space directly
159
- self . synchronizer
169
+ self . sync_shared
170
+ . state ( )
160
171
. peers ( )
161
172
. set_last_common_header ( self . peer , header. number_and_hash ( ) ) ;
162
173
end = min ( best_known. number ( ) , header. number ( ) + BLOCK_DOWNLOAD_WINDOW ) ;
@@ -172,8 +183,7 @@ impl<'a> BlockFetcher<'a> {
172
183
173
184
status = self . active_chain . get_block_status ( & parent_hash) ;
174
185
header = self
175
- . synchronizer
176
- . shared
186
+ . sync_shared
177
187
. get_header_index_view ( & parent_hash, false ) ?;
178
188
}
179
189
0 commit comments