cache.rs 12.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

17
use std::collections::btree_map::BTreeMap;
18
19

use memory_lru::{MemoryLruCache, ResidentSize};
20
21
use parity_util_mem::{MallocSizeOf, MallocSizeOfExt};
use sp_consensus_babe::Epoch;
22

23
24
25
26
use polkadot_primitives::v1::{
	AuthorityDiscoveryId, BlockNumber, CandidateCommitments, CandidateEvent,
	CommittedCandidateReceipt, CoreState, GroupRotationInfo, Hash, Id as ParaId,
	InboundDownwardMessage, InboundHrmpMessage, OccupiedCoreAssumption, PersistedValidationData,
27
28
	ScrapedOnChainVotes, SessionIndex, SessionInfo, ValidationCode, ValidationCodeHash,
	ValidatorId, ValidatorIndex,
29
};
30

31
const AUTHORITIES_CACHE_SIZE: usize = 128 * 1024;
32
33
34
35
36
37
38
39
40
41
42
43
const VALIDATORS_CACHE_SIZE: usize = 64 * 1024;
const VALIDATOR_GROUPS_CACHE_SIZE: usize = 64 * 1024;
const AVAILABILITY_CORES_CACHE_SIZE: usize = 64 * 1024;
const PERSISTED_VALIDATION_DATA_CACHE_SIZE: usize = 64 * 1024;
const CHECK_VALIDATION_OUTPUTS_CACHE_SIZE: usize = 64 * 1024;
const SESSION_INDEX_FOR_CHILD_CACHE_SIZE: usize = 64 * 1024;
const VALIDATION_CODE_CACHE_SIZE: usize = 10 * 1024 * 1024;
const CANDIDATE_PENDING_AVAILABILITY_CACHE_SIZE: usize = 64 * 1024;
const CANDIDATE_EVENTS_CACHE_SIZE: usize = 64 * 1024;
const SESSION_INFO_CACHE_SIZE: usize = 64 * 1024;
const DMQ_CONTENTS_CACHE_SIZE: usize = 64 * 1024;
const INBOUND_HRMP_CHANNELS_CACHE_SIZE: usize = 64 * 1024;
44
const CURRENT_BABE_EPOCH_CACHE_SIZE: usize = 64 * 1024;
45
const ON_CHAIN_VOTES_CACHE_SIZE: usize = 3 * 1024;
46
47
48
49
50
51
52
53
54

struct ResidentSizeOf<T>(T);

impl<T: MallocSizeOf> ResidentSize for ResidentSizeOf<T> {
	fn resident_size(&self) -> usize {
		std::mem::size_of::<Self>() + self.0.malloc_size_of()
	}
}

55
56
57
58
59
60
61
62
struct DoesNotAllocate<T>(T);

impl<T> ResidentSize for DoesNotAllocate<T> {
	fn resident_size(&self) -> usize {
		std::mem::size_of::<Self>()
	}
}

63
64
65
66
67
68
69
70
71
72
// this is an ugly workaround for `AuthorityDiscoveryId`
// not implementing `MallocSizeOf`
struct VecOfDoesNotAllocate<T>(Vec<T>);

impl<T> ResidentSize for VecOfDoesNotAllocate<T> {
	fn resident_size(&self) -> usize {
		std::mem::size_of::<T>() * self.0.capacity()
	}
}

73
pub(crate) struct RequestResultCache {
74
	authorities: MemoryLruCache<Hash, VecOfDoesNotAllocate<AuthorityDiscoveryId>>,
75
	validators: MemoryLruCache<Hash, ResidentSizeOf<Vec<ValidatorId>>>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
76
77
	validator_groups:
		MemoryLruCache<Hash, ResidentSizeOf<(Vec<Vec<ValidatorIndex>>, GroupRotationInfo)>>,
78
	availability_cores: MemoryLruCache<Hash, ResidentSizeOf<Vec<CoreState>>>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
79
80
81
82
83
84
	persisted_validation_data: MemoryLruCache<
		(Hash, ParaId, OccupiedCoreAssumption),
		ResidentSizeOf<Option<PersistedValidationData>>,
	>,
	check_validation_outputs:
		MemoryLruCache<(Hash, ParaId, CandidateCommitments), ResidentSizeOf<bool>>,
85
	session_index_for_child: MemoryLruCache<Hash, ResidentSizeOf<SessionIndex>>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
86
87
88
89
90
91
92
93
	validation_code: MemoryLruCache<
		(Hash, ParaId, OccupiedCoreAssumption),
		ResidentSizeOf<Option<ValidationCode>>,
	>,
	validation_code_by_hash:
		MemoryLruCache<ValidationCodeHash, ResidentSizeOf<Option<ValidationCode>>>,
	candidate_pending_availability:
		MemoryLruCache<(Hash, ParaId), ResidentSizeOf<Option<CommittedCandidateReceipt>>>,
94
	candidate_events: MemoryLruCache<Hash, ResidentSizeOf<Vec<CandidateEvent>>>,
95
	session_info: MemoryLruCache<SessionIndex, ResidentSizeOf<Option<SessionInfo>>>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
96
97
98
99
100
101
	dmq_contents:
		MemoryLruCache<(Hash, ParaId), ResidentSizeOf<Vec<InboundDownwardMessage<BlockNumber>>>>,
	inbound_hrmp_channels_contents: MemoryLruCache<
		(Hash, ParaId),
		ResidentSizeOf<BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>>,
	>,
102
	current_babe_epoch: MemoryLruCache<Hash, DoesNotAllocate<Epoch>>,
103
	on_chain_votes: MemoryLruCache<Hash, ResidentSizeOf<Option<ScrapedOnChainVotes>>>,
104
105
106
107
108
}

impl Default for RequestResultCache {
	fn default() -> Self {
		Self {
109
			authorities: MemoryLruCache::new(AUTHORITIES_CACHE_SIZE),
110
111
112
113
114
115
116
			validators: MemoryLruCache::new(VALIDATORS_CACHE_SIZE),
			validator_groups: MemoryLruCache::new(VALIDATOR_GROUPS_CACHE_SIZE),
			availability_cores: MemoryLruCache::new(AVAILABILITY_CORES_CACHE_SIZE),
			persisted_validation_data: MemoryLruCache::new(PERSISTED_VALIDATION_DATA_CACHE_SIZE),
			check_validation_outputs: MemoryLruCache::new(CHECK_VALIDATION_OUTPUTS_CACHE_SIZE),
			session_index_for_child: MemoryLruCache::new(SESSION_INDEX_FOR_CHILD_CACHE_SIZE),
			validation_code: MemoryLruCache::new(VALIDATION_CODE_CACHE_SIZE),
117
			validation_code_by_hash: MemoryLruCache::new(VALIDATION_CODE_CACHE_SIZE),
Shawn Tabrizi's avatar
Shawn Tabrizi committed
118
119
120
			candidate_pending_availability: MemoryLruCache::new(
				CANDIDATE_PENDING_AVAILABILITY_CACHE_SIZE,
			),
121
122
123
124
			candidate_events: MemoryLruCache::new(CANDIDATE_EVENTS_CACHE_SIZE),
			session_info: MemoryLruCache::new(SESSION_INFO_CACHE_SIZE),
			dmq_contents: MemoryLruCache::new(DMQ_CONTENTS_CACHE_SIZE),
			inbound_hrmp_channels_contents: MemoryLruCache::new(INBOUND_HRMP_CHANNELS_CACHE_SIZE),
125
			current_babe_epoch: MemoryLruCache::new(CURRENT_BABE_EPOCH_CACHE_SIZE),
126
			on_chain_votes: MemoryLruCache::new(ON_CHAIN_VOTES_CACHE_SIZE),
127
128
129
130
131
		}
	}
}

impl RequestResultCache {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
132
133
134
135
	pub(crate) fn authorities(
		&mut self,
		relay_parent: &Hash,
	) -> Option<&Vec<AuthorityDiscoveryId>> {
136
137
138
		self.authorities.get(relay_parent).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
139
140
141
142
143
	pub(crate) fn cache_authorities(
		&mut self,
		relay_parent: Hash,
		authorities: Vec<AuthorityDiscoveryId>,
	) {
144
145
146
		self.authorities.insert(relay_parent, VecOfDoesNotAllocate(authorities));
	}

147
148
149
150
151
152
153
154
	pub(crate) fn validators(&mut self, relay_parent: &Hash) -> Option<&Vec<ValidatorId>> {
		self.validators.get(relay_parent).map(|v| &v.0)
	}

	pub(crate) fn cache_validators(&mut self, relay_parent: Hash, validators: Vec<ValidatorId>) {
		self.validators.insert(relay_parent, ResidentSizeOf(validators));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
155
156
157
158
	pub(crate) fn validator_groups(
		&mut self,
		relay_parent: &Hash,
	) -> Option<&(Vec<Vec<ValidatorIndex>>, GroupRotationInfo)> {
159
160
161
		self.validator_groups.get(relay_parent).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
162
163
164
165
166
	pub(crate) fn cache_validator_groups(
		&mut self,
		relay_parent: Hash,
		groups: (Vec<Vec<ValidatorIndex>>, GroupRotationInfo),
	) {
167
168
169
170
171
172
173
174
175
176
177
		self.validator_groups.insert(relay_parent, ResidentSizeOf(groups));
	}

	pub(crate) fn availability_cores(&mut self, relay_parent: &Hash) -> Option<&Vec<CoreState>> {
		self.availability_cores.get(relay_parent).map(|v| &v.0)
	}

	pub(crate) fn cache_availability_cores(&mut self, relay_parent: Hash, cores: Vec<CoreState>) {
		self.availability_cores.insert(relay_parent, ResidentSizeOf(cores));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
178
179
180
181
	pub(crate) fn persisted_validation_data(
		&mut self,
		key: (Hash, ParaId, OccupiedCoreAssumption),
	) -> Option<&Option<PersistedValidationData>> {
182
183
184
		self.persisted_validation_data.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
185
186
187
188
189
	pub(crate) fn cache_persisted_validation_data(
		&mut self,
		key: (Hash, ParaId, OccupiedCoreAssumption),
		data: Option<PersistedValidationData>,
	) {
190
191
192
		self.persisted_validation_data.insert(key, ResidentSizeOf(data));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
193
194
195
196
	pub(crate) fn check_validation_outputs(
		&mut self,
		key: (Hash, ParaId, CandidateCommitments),
	) -> Option<&bool> {
197
198
199
		self.check_validation_outputs.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
200
201
202
203
204
	pub(crate) fn cache_check_validation_outputs(
		&mut self,
		key: (Hash, ParaId, CandidateCommitments),
		value: bool,
	) {
205
206
207
208
209
210
211
		self.check_validation_outputs.insert(key, ResidentSizeOf(value));
	}

	pub(crate) fn session_index_for_child(&mut self, relay_parent: &Hash) -> Option<&SessionIndex> {
		self.session_index_for_child.get(relay_parent).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
212
213
214
215
216
	pub(crate) fn cache_session_index_for_child(
		&mut self,
		relay_parent: Hash,
		index: SessionIndex,
	) {
217
218
219
		self.session_index_for_child.insert(relay_parent, ResidentSizeOf(index));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
220
221
222
223
	pub(crate) fn validation_code(
		&mut self,
		key: (Hash, ParaId, OccupiedCoreAssumption),
	) -> Option<&Option<ValidationCode>> {
224
225
226
		self.validation_code.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
227
228
229
230
231
	pub(crate) fn cache_validation_code(
		&mut self,
		key: (Hash, ParaId, OccupiedCoreAssumption),
		value: Option<ValidationCode>,
	) {
232
233
234
		self.validation_code.insert(key, ResidentSizeOf(value));
	}

235
236
	// the actual key is `ValidationCodeHash` (`Hash` is ignored),
	// but we keep the interface that way to keep the macro simple
Shawn Tabrizi's avatar
Shawn Tabrizi committed
237
238
239
240
	pub(crate) fn validation_code_by_hash(
		&mut self,
		key: (Hash, ValidationCodeHash),
	) -> Option<&Option<ValidationCode>> {
241
		self.validation_code_by_hash.get(&key.1).map(|v| &v.0)
242
243
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
244
245
246
247
248
	pub(crate) fn cache_validation_code_by_hash(
		&mut self,
		key: ValidationCodeHash,
		value: Option<ValidationCode>,
	) {
249
		self.validation_code_by_hash.insert(key, ResidentSizeOf(value));
250
251
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
252
253
254
255
	pub(crate) fn candidate_pending_availability(
		&mut self,
		key: (Hash, ParaId),
	) -> Option<&Option<CommittedCandidateReceipt>> {
256
257
258
		self.candidate_pending_availability.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
259
260
261
262
263
	pub(crate) fn cache_candidate_pending_availability(
		&mut self,
		key: (Hash, ParaId),
		value: Option<CommittedCandidateReceipt>,
	) {
264
265
266
267
268
269
270
		self.candidate_pending_availability.insert(key, ResidentSizeOf(value));
	}

	pub(crate) fn candidate_events(&mut self, relay_parent: &Hash) -> Option<&Vec<CandidateEvent>> {
		self.candidate_events.get(relay_parent).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
271
272
273
274
275
	pub(crate) fn cache_candidate_events(
		&mut self,
		relay_parent: Hash,
		events: Vec<CandidateEvent>,
	) {
276
277
278
		self.candidate_events.insert(relay_parent, ResidentSizeOf(events));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
279
280
281
282
	pub(crate) fn session_info(
		&mut self,
		key: (Hash, SessionIndex),
	) -> Option<&Option<SessionInfo>> {
283
		self.session_info.get(&key.1).map(|v| &v.0)
284
285
	}

286
	pub(crate) fn cache_session_info(&mut self, key: SessionIndex, value: Option<SessionInfo>) {
287
288
289
		self.session_info.insert(key, ResidentSizeOf(value));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
290
291
292
293
	pub(crate) fn dmq_contents(
		&mut self,
		key: (Hash, ParaId),
	) -> Option<&Vec<InboundDownwardMessage<BlockNumber>>> {
294
295
296
		self.dmq_contents.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
297
298
299
300
301
	pub(crate) fn cache_dmq_contents(
		&mut self,
		key: (Hash, ParaId),
		value: Vec<InboundDownwardMessage<BlockNumber>>,
	) {
302
303
304
		self.dmq_contents.insert(key, ResidentSizeOf(value));
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
305
306
307
308
	pub(crate) fn inbound_hrmp_channels_contents(
		&mut self,
		key: (Hash, ParaId),
	) -> Option<&BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>> {
309
310
311
		self.inbound_hrmp_channels_contents.get(&key).map(|v| &v.0)
	}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
312
313
314
315
316
	pub(crate) fn cache_inbound_hrmp_channel_contents(
		&mut self,
		key: (Hash, ParaId),
		value: BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>,
	) {
317
318
		self.inbound_hrmp_channels_contents.insert(key, ResidentSizeOf(value));
	}
319
320
321
322
323
324
325
326

	pub(crate) fn current_babe_epoch(&mut self, relay_parent: &Hash) -> Option<&Epoch> {
		self.current_babe_epoch.get(relay_parent).map(|v| &v.0)
	}

	pub(crate) fn cache_current_babe_epoch(&mut self, relay_parent: Hash, epoch: Epoch) {
		self.current_babe_epoch.insert(relay_parent, DoesNotAllocate(epoch));
	}
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

	pub(crate) fn on_chain_votes(
		&mut self,
		relay_parent: &Hash,
	) -> Option<&Option<ScrapedOnChainVotes>> {
		self.on_chain_votes.get(relay_parent).map(|v| &v.0)
	}

	pub(crate) fn cache_on_chain_votes(
		&mut self,
		relay_parent: Hash,
		scraped: Option<ScrapedOnChainVotes>,
	) {
		self.on_chain_votes.insert(relay_parent, ResidentSizeOf(scraped));
	}
342
343
344
}

pub(crate) enum RequestResult {
345
	Authorities(Hash, Vec<AuthorityDiscoveryId>),
346
347
348
349
350
351
352
	Validators(Hash, Vec<ValidatorId>),
	ValidatorGroups(Hash, (Vec<Vec<ValidatorIndex>>, GroupRotationInfo)),
	AvailabilityCores(Hash, Vec<CoreState>),
	PersistedValidationData(Hash, ParaId, OccupiedCoreAssumption, Option<PersistedValidationData>),
	CheckValidationOutputs(Hash, ParaId, CandidateCommitments, bool),
	SessionIndexForChild(Hash, SessionIndex),
	ValidationCode(Hash, ParaId, OccupiedCoreAssumption, Option<ValidationCode>),
353
	ValidationCodeByHash(Hash, ValidationCodeHash, Option<ValidationCode>),
354
355
356
357
	CandidatePendingAvailability(Hash, ParaId, Option<CommittedCandidateReceipt>),
	CandidateEvents(Hash, Vec<CandidateEvent>),
	SessionInfo(Hash, SessionIndex, Option<SessionInfo>),
	DmqContents(Hash, ParaId, Vec<InboundDownwardMessage<BlockNumber>>),
Shawn Tabrizi's avatar
Shawn Tabrizi committed
358
359
360
361
362
	InboundHrmpChannelsContents(
		Hash,
		ParaId,
		BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>,
	),
363
	CurrentBabeEpoch(Hash, Epoch),
364
	FetchOnChainVotes(Hash, Option<ScrapedOnChainVotes>),
365
}