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
// Copyright (C) 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/>.

use futures::{future::Either, FutureExt, StreamExt, TryFutureExt};

use sp_keystore::KeystorePtr;

use polkadot_node_network_protocol::request_response::{
	v1, v2, IncomingRequestReceiver, ReqProtocolNames,
};
use polkadot_node_subsystem::{
	jaeger, messages::AvailabilityDistributionMessage, overseer, FromOrchestra, OverseerSignal,
	SpawnedSubsystem, SubsystemError,
};
use polkadot_primitives::{BlockNumber, Hash};
use std::collections::HashMap;

/// Error and [`Result`] type for this subsystem.
mod error;
use error::{log_error, FatalError, Result};

use polkadot_node_subsystem_util::runtime::RuntimeInfo;

/// `Requester` taking care of requesting chunks for candidates pending availability.
mod requester;
use requester::Requester;

/// Handing requests for PoVs during backing.
mod pov_requester;

/// Responding to erasure chunk requests:
mod responder;
use responder::{run_chunk_receivers, run_pov_receiver};

mod metrics;
/// Prometheus `Metrics` for availability distribution.
pub use metrics::Metrics;

#[cfg(test)]
mod tests;

const LOG_TARGET: &'static str = "parachain::availability-distribution";

/// The availability distribution subsystem.
pub struct AvailabilityDistributionSubsystem {
	/// Easy and efficient runtime access for this subsystem.
	runtime: RuntimeInfo,
	/// Receivers to receive messages from.
	recvs: IncomingRequestReceivers,
	/// Mapping of the req-response protocols to the full protocol names.
	req_protocol_names: ReqProtocolNames,
	/// Prometheus metrics.
	metrics: Metrics,
}

/// Receivers to be passed into availability distribution.
pub struct IncomingRequestReceivers {
	/// Receiver for incoming PoV requests.
	pub pov_req_receiver: IncomingRequestReceiver<v1::PoVFetchingRequest>,
	/// Receiver for incoming v1 availability chunk requests.
	pub chunk_req_v1_receiver: IncomingRequestReceiver<v1::ChunkFetchingRequest>,
	/// Receiver for incoming v2 availability chunk requests.
	pub chunk_req_v2_receiver: IncomingRequestReceiver<v2::ChunkFetchingRequest>,
}

#[overseer::subsystem(AvailabilityDistribution, error=SubsystemError, prefix=self::overseer)]
impl<Context> AvailabilityDistributionSubsystem {
	fn start(self, ctx: Context) -> SpawnedSubsystem {
		let future = self
			.run(ctx)
			.map_err(|e| SubsystemError::with_origin("availability-distribution", e))
			.boxed();

		SpawnedSubsystem { name: "availability-distribution-subsystem", future }
	}
}

#[overseer::contextbounds(AvailabilityDistribution, prefix = self::overseer)]
impl AvailabilityDistributionSubsystem {
	/// Create a new instance of the availability distribution.
	pub fn new(
		keystore: KeystorePtr,
		recvs: IncomingRequestReceivers,
		req_protocol_names: ReqProtocolNames,
		metrics: Metrics,
	) -> Self {
		let runtime = RuntimeInfo::new(Some(keystore));
		Self { runtime, recvs, req_protocol_names, metrics }
	}

	/// Start processing work as passed on from the Overseer.
	async fn run<Context>(self, mut ctx: Context) -> std::result::Result<(), FatalError> {
		let Self { mut runtime, recvs, metrics, req_protocol_names } = self;
		let mut spans: HashMap<Hash, (BlockNumber, jaeger::PerLeafSpan)> = HashMap::new();

		let IncomingRequestReceivers {
			pov_req_receiver,
			chunk_req_v1_receiver,
			chunk_req_v2_receiver,
		} = recvs;
		let mut requester = Requester::new(req_protocol_names, metrics.clone()).fuse();
		let mut warn_freq = gum::Freq::new();

		{
			let sender = ctx.sender().clone();
			ctx.spawn(
				"pov-receiver",
				run_pov_receiver(sender.clone(), pov_req_receiver, metrics.clone()).boxed(),
			)
			.map_err(FatalError::SpawnTask)?;

			ctx.spawn(
				"chunk-receiver",
				run_chunk_receivers(
					sender,
					chunk_req_v1_receiver,
					chunk_req_v2_receiver,
					metrics.clone(),
				)
				.boxed(),
			)
			.map_err(FatalError::SpawnTask)?;
		}

		loop {
			let action = {
				let mut subsystem_next = ctx.recv().fuse();
				futures::select! {
					subsystem_msg = subsystem_next => Either::Left(subsystem_msg),
					from_task = requester.next() => Either::Right(from_task),
				}
			};

			// Handle task messages sending:
			let message = match action {
				Either::Left(subsystem_msg) =>
					subsystem_msg.map_err(|e| FatalError::IncomingMessageChannel(e))?,
				Either::Right(from_task) => {
					let from_task = from_task.ok_or(FatalError::RequesterExhausted)?;
					ctx.send_message(from_task).await;
					continue
				},
			};
			match message {
				FromOrchestra::Signal(OverseerSignal::ActiveLeaves(update)) => {
					let cloned_leaf = match update.activated.clone() {
						Some(activated) => activated,
						None => continue,
					};
					let span =
						jaeger::PerLeafSpan::new(cloned_leaf.span, "availability-distribution");
					spans.insert(cloned_leaf.hash, (cloned_leaf.number, span));
					log_error(
						requester
							.get_mut()
							.update_fetching_heads(&mut ctx, &mut runtime, update, &spans)
							.await,
						"Error in Requester::update_fetching_heads",
						&mut warn_freq,
					)?;
				},
				FromOrchestra::Signal(OverseerSignal::BlockFinalized(_hash, finalized_number)) => {
					spans.retain(|_hash, (block_number, _span)| *block_number > finalized_number);
				},
				FromOrchestra::Signal(OverseerSignal::Conclude) => return Ok(()),
				FromOrchestra::Communication {
					msg:
						AvailabilityDistributionMessage::FetchPoV {
							relay_parent,
							from_validator,
							para_id,
							candidate_hash,
							pov_hash,
							tx,
						},
				} => {
					let span = spans
						.get(&relay_parent)
						.map(|(_, span)| span.child("fetch-pov"))
						.unwrap_or_else(|| jaeger::Span::new(&relay_parent, "fetch-pov"))
						.with_trace_id(candidate_hash)
						.with_candidate(candidate_hash)
						.with_relay_parent(relay_parent)
						.with_stage(jaeger::Stage::AvailabilityDistribution);

					log_error(
						pov_requester::fetch_pov(
							&mut ctx,
							&mut runtime,
							relay_parent,
							from_validator,
							para_id,
							candidate_hash,
							pov_hash,
							tx,
							metrics.clone(),
							&span,
						)
						.await,
						"pov_requester::fetch_pov",
						&mut warn_freq,
					)?;
				},
			}
		}
	}
}