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
use std::collections::HashSet;
use lru::LruCache;
use rand::{seq::SliceRandom, thread_rng};
use polkadot_node_subsystem::overseer;
use polkadot_node_subsystem_util::runtime::RuntimeInfo;
use polkadot_primitives::v2::{
AuthorityDiscoveryId, GroupIndex, Hash, SessionIndex, ValidatorIndex,
};
use crate::{
error::{Error, Result},
LOG_TARGET,
};
pub struct SessionCache {
session_info_cache: LruCache<SessionIndex, SessionInfo>,
}
#[derive(Clone)]
pub struct SessionInfo {
pub session_index: SessionIndex,
pub validator_groups: Vec<Vec<AuthorityDiscoveryId>>,
pub our_index: ValidatorIndex,
pub our_group: Option<GroupIndex>,
}
pub struct BadValidators {
pub session_index: SessionIndex,
pub group_index: GroupIndex,
pub bad_validators: Vec<AuthorityDiscoveryId>,
}
#[overseer::contextbounds(AvailabilityDistribution, prefix = self::overseer)]
impl SessionCache {
pub fn new() -> Self {
SessionCache {
session_info_cache: LruCache::new(2),
}
}
pub async fn with_session_info<Context, F, R>(
&mut self,
ctx: &mut Context,
runtime: &mut RuntimeInfo,
parent: Hash,
session_index: SessionIndex,
with_info: F,
) -> Result<Option<R>>
where
F: FnOnce(&SessionInfo) -> R,
{
if let Some(o_info) = self.session_info_cache.get(&session_index) {
gum::trace!(target: LOG_TARGET, session_index, "Got session from lru");
return Ok(Some(with_info(o_info)))
}
if let Some(info) =
self.query_info_from_runtime(ctx, runtime, parent, session_index).await?
{
gum::trace!(target: LOG_TARGET, session_index, "Calling `with_info`");
let r = with_info(&info);
gum::trace!(target: LOG_TARGET, session_index, "Storing session info in lru!");
self.session_info_cache.put(session_index, info);
Ok(Some(r))
} else {
Ok(None)
}
}
pub fn report_bad_log(&mut self, report: BadValidators) {
if let Err(err) = self.report_bad(report) {
gum::warn!(
target: LOG_TARGET,
err = ?err,
"Reporting bad validators failed with error"
);
}
}
pub fn report_bad(&mut self, report: BadValidators) -> Result<()> {
let available_sessions = self.session_info_cache.iter().map(|(k, _)| *k).collect();
let session = self.session_info_cache.get_mut(&report.session_index).ok_or(
Error::NoSuchCachedSession {
available_sessions,
missing_session: report.session_index,
},
)?;
let group = session.validator_groups.get_mut(report.group_index.0 as usize).expect(
"A bad validator report must contain a valid group for the reported session. qed.",
);
let bad_set = report.bad_validators.iter().collect::<HashSet<_>>();
group.retain(|v| !bad_set.contains(v));
let mut new_group = report.bad_validators;
new_group.append(group);
*group = new_group;
Ok(())
}
async fn query_info_from_runtime<Context>(
&self,
ctx: &mut Context,
runtime: &mut RuntimeInfo,
relay_parent: Hash,
session_index: SessionIndex,
) -> Result<Option<SessionInfo>> {
let info = runtime
.get_session_info_by_index(ctx.sender(), relay_parent, session_index)
.await?;
let discovery_keys = info.session_info.discovery_keys.clone();
let mut validator_groups = info.session_info.validator_groups.clone();
if let Some(our_index) = info.validator_info.our_index {
let our_group = info.validator_info.our_group;
let mut rng = thread_rng();
for g in validator_groups.iter_mut() {
g.shuffle(&mut rng)
}
let validator_groups: Vec<Vec<_>> = validator_groups
.into_iter()
.map(|group| {
group
.into_iter()
.map(|index| {
discovery_keys.get(index.0 as usize)
.expect("There should be a discovery key for each validator of each validator group. qed.")
.clone()
})
.collect()
})
.collect();
let info = SessionInfo { validator_groups, our_index, session_index, our_group };
return Ok(Some(info))
}
return Ok(None)
}
}