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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
crate::ix!();
pub fn get_block_proof_equivalent_time(
to: Option<Arc<BlockIndex>>,
from: Option<Arc<BlockIndex>>,
tip: Option<Arc<BlockIndex>>,
params: &ChainConsensusParams) -> Duration {
todo!();
}
pub type CppIter<T: Iterator> = Peekable<Enumerate<Box<T>>>;
pub trait BlockRequested {
fn block_requested(
self: Arc<Self>,
nodeid: NodeId,
block: Option<Arc<BlockIndex>>,
pit: Amo<QueuedBlockIter>) -> bool;
}
impl BlockRequested for PeerManager {
#[EXCLUSIVE_LOCKS_REQUIRED(CS_MAIN)]
fn block_requested(
self: Arc<Self>,
nodeid: NodeId,
block: Option<Arc<BlockIndex>>,
pit: Amo<QueuedBlockIter>) -> bool {
assert!(block.is_some());
let hash: &u256 = &block.as_ref().unwrap().get_block_hash();
let state: Amo<NodeState> = create_state(nodeid);
assert!(state.is_some());
let mut inner = self.inner.lock();
let mut mbif = inner.map_blocks_in_flight.lock();
let mut it_in_flight = mbif.get(hash);
if it_in_flight.is_some()
&& it_in_flight.unwrap().0 == nodeid {
if pit.is_some() {
pit.replace(&it_in_flight.unwrap().1);
}
return false;
}
inner.remove_block_request(hash);
let partial_block = match pit.is_some() {
true => Amo::from(PartiallyDownloadedBlock::new(amo_none())),
false => amo_none(),
};
state.get_mut().blocks_in_flight.push(
QueuedBlock {
pindex: block.clone(),
partial_block
}
);
let len = state.get().blocks_in_flight.len();
let mut it = state.get_mut().blocks_in_flight_iter();;
it.advance_by(len - 1);
state.get_mut().n_blocks_in_flight.fetch_add(1,atomic::Ordering::Relaxed);
if state.get().n_blocks_in_flight.load(atomic::Ordering::Relaxed) == 1 {
state.get_mut().downloading_since = get_datetime();
inner.peers_downloading_from.fetch_add(1, atomic::Ordering::Relaxed);
}
let it_in_flight = mbif.insert(hash.clone(), (nodeid,it));
if pit.is_some() {
*pit.get_mut() = it_in_flight.unwrap().1;
}
true
}
}