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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2021 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/>.

//! Implements the dispute participation subsystem.
//!
//! This subsystem is responsible for actually participating in disputes: when
//! notified of a dispute, we recover the candidate data, validate the
//! candidate, and cast our vote in the dispute.

use futures::{channel::oneshot, prelude::*};

use polkadot_node_primitives::{ValidationResult, APPROVAL_EXECUTION_TIMEOUT};
use polkadot_node_subsystem::{
	errors::{RecoveryError, RuntimeApiError},
	messages::{
		AvailabilityRecoveryMessage, AvailabilityStoreMessage, CandidateValidationMessage,
		DisputeCoordinatorMessage, DisputeParticipationMessage, RuntimeApiMessage,
		RuntimeApiRequest,
	},
	overseer, ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem, SubsystemContext,
	SubsystemError,
};
use polkadot_primitives::v1::{BlockNumber, CandidateHash, CandidateReceipt, Hash, SessionIndex};

#[cfg(test)]
mod tests;

const LOG_TARGET: &str = "parachain::dispute-participation";

struct State {
	recent_block: Option<(BlockNumber, Hash)>,
}

/// An implementation of the dispute participation subsystem.
pub struct DisputeParticipationSubsystem;

impl DisputeParticipationSubsystem {
	/// Create a new instance of the subsystem.
	pub fn new() -> Self {
		DisputeParticipationSubsystem
	}
}

impl<Context> overseer::Subsystem<Context, SubsystemError> for DisputeParticipationSubsystem
where
	Context: SubsystemContext<Message = DisputeParticipationMessage>,
	Context: overseer::SubsystemContext<Message = DisputeParticipationMessage>,
{
	fn start(self, ctx: Context) -> SpawnedSubsystem {
		let future = run(ctx).map(|_| Ok(())).boxed();

		SpawnedSubsystem { name: "dispute-participation-subsystem", future }
	}
}

#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
	#[error(transparent)]
	RuntimeApi(#[from] RuntimeApiError),

	#[error(transparent)]
	Subsystem(#[from] SubsystemError),

	#[error(transparent)]
	Oneshot(#[from] oneshot::Canceled),

	#[error("Oneshot receiver died")]
	OneshotSendFailed,

	#[error(transparent)]
	Participation(#[from] ParticipationError),
}

#[derive(Debug, thiserror::Error)]
pub enum ParticipationError {
	#[error("Missing recent block state to participate in dispute")]
	MissingRecentBlockState,
	#[error("Failed to recover available data for candidate {0}")]
	MissingAvailableData(CandidateHash),
	#[error("Failed to recover validation code for candidate {0}")]
	MissingValidationCode(CandidateHash),
}

impl Error {
	fn trace(&self) {
		match self {
			// don't spam the log with spurious errors
			Self::RuntimeApi(_) | Self::Oneshot(_) => {
				tracing::debug!(target: LOG_TARGET, err = ?self)
			},
			// it's worth reporting otherwise
			_ => tracing::warn!(target: LOG_TARGET, err = ?self),
		}
	}
}

async fn run<Context>(mut ctx: Context)
where
	Context: SubsystemContext<Message = DisputeParticipationMessage>,
	Context: overseer::SubsystemContext<Message = DisputeParticipationMessage>,
{
	let mut state = State { recent_block: None };

	loop {
		match ctx.recv().await {
			Err(_) => return,
			Ok(FromOverseer::Signal(OverseerSignal::Conclude)) => {
				tracing::info!(target: LOG_TARGET, "Received `Conclude` signal, exiting");
				return
			},
			Ok(FromOverseer::Signal(OverseerSignal::BlockFinalized(_, _))) => {},
			Ok(FromOverseer::Signal(OverseerSignal::ActiveLeaves(update))) => {
				update_state(&mut state, update);
			},
			Ok(FromOverseer::Communication { msg }) => {
				if let Err(err) = handle_incoming(&mut ctx, &mut state, msg).await {
					err.trace();
					if let Error::Subsystem(SubsystemError::Context(_)) = err {
						return
					}
				}
			},
		}
	}
}

fn update_state(state: &mut State, update: ActiveLeavesUpdate) {
	for active in update.activated {
		if state.recent_block.map_or(true, |s| active.number > s.0) {
			state.recent_block = Some((active.number, active.hash));
		}
	}
}

async fn handle_incoming(
	ctx: &mut impl SubsystemContext,
	state: &mut State,
	message: DisputeParticipationMessage,
) -> Result<(), Error> {
	match message {
		DisputeParticipationMessage::Participate {
			candidate_hash,
			candidate_receipt,
			session,
			n_validators,
			report_availability,
		} =>
			if let Some((_, block_hash)) = state.recent_block {
				participate(
					ctx,
					block_hash,
					candidate_hash,
					candidate_receipt,
					session,
					n_validators,
					report_availability,
				)
				.await
			} else {
				return Err(ParticipationError::MissingRecentBlockState.into())
			},
	}
}

async fn participate(
	ctx: &mut impl SubsystemContext,
	block_hash: Hash,
	candidate_hash: CandidateHash,
	candidate_receipt: CandidateReceipt,
	session: SessionIndex,
	n_validators: u32,
	report_availability: oneshot::Sender<bool>,
) -> Result<(), Error> {
	let (recover_available_data_tx, recover_available_data_rx) = oneshot::channel();
	let (code_tx, code_rx) = oneshot::channel();
	let (store_available_data_tx, store_available_data_rx) = oneshot::channel();
	let (validation_tx, validation_rx) = oneshot::channel();

	// in order to validate a candidate we need to start by recovering the
	// available data
	ctx.send_message(AvailabilityRecoveryMessage::RecoverAvailableData(
		candidate_receipt.clone(),
		session,
		None,
		recover_available_data_tx,
	))
	.await;

	let available_data = match recover_available_data_rx.await? {
		Ok(data) => {
			report_availability.send(true).map_err(|_| Error::OneshotSendFailed)?;
			data
		},
		Err(RecoveryError::Invalid) => {
			report_availability.send(true).map_err(|_| Error::OneshotSendFailed)?;

			// the available data was recovered but it is invalid, therefore we'll
			// vote negatively for the candidate dispute
			cast_invalid_vote(ctx, candidate_hash, candidate_receipt, session).await;
			return Ok(())
		},
		Err(RecoveryError::Unavailable) => {
			report_availability.send(false).map_err(|_| Error::OneshotSendFailed)?;

			return Err(ParticipationError::MissingAvailableData(candidate_hash).into())
		},
	};

	// we also need to fetch the validation code which we can reference by its
	// hash as taken from the candidate descriptor
	ctx.send_message(RuntimeApiMessage::Request(
		block_hash,
		RuntimeApiRequest::ValidationCodeByHash(
			candidate_receipt.descriptor.validation_code_hash,
			code_tx,
		),
	))
	.await;

	let validation_code = match code_rx.await?? {
		Some(code) => code,
		None => {
			tracing::warn!(
				target: LOG_TARGET,
				"Validation code unavailable for code hash {:?} in the state of block {:?}",
				candidate_receipt.descriptor.validation_code_hash,
				block_hash,
			);

			return Err(ParticipationError::MissingValidationCode(candidate_hash).into())
		},
	};

	// we dispatch a request to store the available data for the candidate. we
	// want to maximize data availability for other potential checkers involved
	// in the dispute
	ctx.send_message(AvailabilityStoreMessage::StoreAvailableData {
		candidate_hash,
		n_validators,
		available_data: available_data.clone(),
		tx: store_available_data_tx,
	})
	.await;

	match store_available_data_rx.await? {
		Err(_) => {
			tracing::warn!(
				target: LOG_TARGET,
				"Failed to store available data for candidate {:?}",
				candidate_hash,
			);
		},
		Ok(()) => {},
	}

	// we issue a request to validate the candidate with the provided exhaustive
	// parameters
	//
	// We use the approval execution timeout because this is intended to
	// be run outside of backing and therefore should be subject to the
	// same level of leeway.
	ctx.send_message(CandidateValidationMessage::ValidateFromExhaustive(
		available_data.validation_data,
		validation_code,
		candidate_receipt.descriptor.clone(),
		available_data.pov,
		APPROVAL_EXECUTION_TIMEOUT,
		validation_tx,
	))
	.await;

	// we cast votes (either positive or negative) depending on the outcome of
	// the validation and if valid, whether the commitments hash matches
	match validation_rx.await? {
		Err(err) => {
			tracing::warn!(
				target: LOG_TARGET,
				"Candidate {:?} validation failed with: {:?}",
				candidate_receipt.hash(),
				err,
			);

			cast_invalid_vote(ctx, candidate_hash, candidate_receipt, session).await;
		},
		Ok(ValidationResult::Invalid(invalid)) => {
			tracing::warn!(
				target: LOG_TARGET,
				"Candidate {:?} considered invalid: {:?}",
				candidate_hash,
				invalid,
			);

			cast_invalid_vote(ctx, candidate_hash, candidate_receipt, session).await;
		},
		Ok(ValidationResult::Valid(commitments, _)) => {
			if commitments.hash() != candidate_receipt.commitments_hash {
				tracing::warn!(
					target: LOG_TARGET,
					expected = ?candidate_receipt.commitments_hash,
					got = ?commitments.hash(),
					"Candidate is valid but commitments hash doesn't match",
				);

				cast_invalid_vote(ctx, candidate_hash, candidate_receipt, session).await;
			} else {
				cast_valid_vote(ctx, candidate_hash, candidate_receipt, session).await;
			}
		},
	}

	Ok(())
}

async fn cast_valid_vote(
	ctx: &mut impl SubsystemContext,
	candidate_hash: CandidateHash,
	candidate_receipt: CandidateReceipt,
	session: SessionIndex,
) {
	tracing::info!(
		target: LOG_TARGET,
		"Casting valid vote in dispute for candidate {:?}",
		candidate_hash,
	);

	issue_local_statement(ctx, candidate_hash, candidate_receipt, session, true).await;
}

async fn cast_invalid_vote(
	ctx: &mut impl SubsystemContext,
	candidate_hash: CandidateHash,
	candidate_receipt: CandidateReceipt,
	session: SessionIndex,
) {
	tracing::info!(
		target: LOG_TARGET,
		"Casting invalid vote in dispute for candidate {:?}",
		candidate_hash,
	);

	issue_local_statement(ctx, candidate_hash, candidate_receipt, session, false).await;
}

async fn issue_local_statement(
	ctx: &mut impl SubsystemContext,
	candidate_hash: CandidateHash,
	candidate_receipt: CandidateReceipt,
	session: SessionIndex,
	valid: bool,
) {
	ctx.send_message(DisputeCoordinatorMessage::IssueLocalStatement(
		session,
		candidate_hash,
		candidate_receipt,
		valid,
	))
	.await
}