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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
crate::ix!();
/**
| Extended statistics about a CAddress
|
*/
#[derive(Serialize,Deserialize,Clone)]
pub struct AddrInfo {
pub address: Address,
/**
| last try whatsoever by us (memory only)
|
*/
#[serde(skip)]
pub n_last_try: i64,
/**
| last counted attempt (memory only)
|
*/
#[serde(skip)]
pub n_last_count_attempt: i64,
/**
| where knowledge about this address
| first came from
|
*/
pub source: NetAddr,
/**
| last successful connection by us
|
*/
pub n_last_success: i64,
/**
| connection attempts since last successful
| attempt
|
*/
pub n_attempts: i32,
/**
| reference count in new sets (memory
| only)
|
*/
#[serde(skip)]
pub n_ref_count: i32,
/**
| in tried set? (memory only)
|
*/
#[serde(skip)]
pub in_tried: bool,
/**
| position in vRandom
|
*/
#[serde(skip)]
pub n_random_pos: RefCell<i32>,
}
impl Default for AddrInfo {
fn default() -> Self {
Self {
address: Address::default(),
n_last_try: 0,
n_last_count_attempt: 0,
source: NetAddr::default(),
n_last_success: 0,
n_attempts: 0,
n_ref_count: 0,
in_tried: false,
n_random_pos: RefCell::new(-1),
}
}
}
impl AddrInfo {
pub fn new(
addr_in: Address,
addr_source: NetAddr) -> Self {
Self {
address: addr_in,
n_last_try: 0,
n_last_count_attempt: 0,
source: addr_source,
n_last_success: 0,
n_attempts: 0,
n_ref_count: 0,
in_tried: false,
n_random_pos: RefCell::new(-1),
}
}
fn get_writer() -> HashWriter {
HashWriter::new(SER_GETHASH.try_into().unwrap(), 0)
}
/**
| Calculate in which "new" bucket this
| entry belongs, using its default source
|
*/
pub fn get_new_bucket(&self,
n_key: &u256,
asmap: &Vec<bool>) -> i32 {
self.get_new_bucket_given_source(
n_key,
&self.source,
asmap
)
}
/**
| Calculate in which "tried" bucket this
| entry belongs
|
*/
pub fn get_tried_bucket(&self,
n_key: &u256,
asmap: &Vec<bool>) -> i32 {
let hash1: u64 = {
let w = Self::get_writer();
let svc_key = self.address.service.get_key();
(w << n_key << &svc_key).get_cheap_hash()
};
let hash2: u64 = {
let w = Self::get_writer();
let group = self.source.get_group(asmap);
let h1_mod = hash1 % (ADDRMAN_TRIED_BUCKETS_PER_GROUP as u64);
(w << n_key << &group << &h1_mod).get_cheap_hash()
};
(hash2 % (ADDRMAN_TRIED_BUCKET_COUNT as u64)).try_into().unwrap()
}
/**
| Calculate in which "new" bucket this
| entry belongs, given a certain source
|
*/
pub fn get_new_bucket_given_source(&self,
n_key: &u256,
src: &NetAddr,
asmap: &Vec<bool>) -> i32 {
let vch_source_group_key: Vec<u8> = src.get_group(asmap);
let hash1: u64 = {
let w = Self::get_writer();
let g = self.source.get_group(asmap);
(w << n_key << &g << &vch_source_group_key).get_cheap_hash()
};
let hash2: u64 = {
let w = Self::get_writer();
let h1_mod = (hash1 % (ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP as u64));
(w << n_key << &vch_source_group_key << &h1_mod).get_cheap_hash()
};
(hash2 % (ADDRMAN_NEW_BUCKET_COUNT as u64)).try_into().unwrap()
}
/**
| Calculate in which position of a bucket
| to store this entry.
|
*/
pub fn get_bucket_position(&self,
n_key: &u256,
f_new: bool,
n_bucket: usize) -> i32 {
let hash1: u64 = {
let w = Self::get_writer();
let tag = if f_new { 'N' as u8 } else { 'K' as u8 };
let svc_key = self.address.service.get_key();
(w << n_key << &tag << &n_bucket << &svc_key).get_cheap_hash()
};
(hash1 % (ADDRMAN_BUCKET_SIZE as u64)).try_into().unwrap()
}
/**
| Determine whether the statistics about
| this entry are bad enough so that it can
| just be deleted
|
*/
pub fn is_terrible(&self, n_now: Option<i64>) -> bool {
let n_now: i64 = n_now.unwrap_or(get_adjusted_time());
let n_time: i64 = self.address.n_time as i64;
if self.n_last_try != 0 && self.n_last_try >= n_now - 60 {
// never remove things tried in the last minute
return false;
}
if n_time > n_now + 10 * 60 {
// came in a flying DeLorean
return true;
}
if n_time == 0
|| n_now - n_time > (ADDRMAN_HORIZON_DAYS * 24 * 60 * 60).try_into().unwrap()
{
// not seen in recent history
return true;
}
if self.n_last_success == 0
&& self.n_attempts >= (ADDRMAN_RETRIES.try_into().unwrap())
{
// tried N times and never a success
return true;
}
if n_now - self.n_last_success > (ADDRMAN_MIN_FAIL_DAYS * 24 * 60 * 60) as i64
&& self.n_attempts >= (ADDRMAN_MAX_FAILURES as i32)
{
// N successive failures in the last week
return true;
}
false
}
/**
| Calculate the relative chance this
| entry should be given when selecting
| nodes to connect to
|
*/
pub fn get_chance(&self, n_now: Option<i64>) -> f64 {
let n_now: i64 = n_now.unwrap_or(get_adjusted_time());
let mut chance: f64 = 1.0;
let n_since_last_try: i64 = std::cmp::max(n_now - self.n_last_try, 0);
//deprioritize very recent attempts away
if n_since_last_try < 60 * 10 {
chance *= 0.01;
}
// deprioritize 66% after each failed
// attempt, but at most 1/28th to avoid
// the search taking forever or overly
// penalizing outages.
chance *= 0.66_f64.powf(std::cmp::min(self.n_attempts, 8) as f64);
chance
}
}