Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
8c83835
Dispute protocol.
eskimor Jun 14, 2021
125dd32
Dispute distribution protocol.
eskimor Jun 15, 2021
2c395e5
Get network requests routed.
eskimor Jun 15, 2021
7c53c69
WIP: Basic dispute sender logic.
eskimor Jun 15, 2021
0bd8713
Basic validator determination logic.
eskimor Jun 15, 2021
608f84c
WIP: Getting things to typecheck.
eskimor Jun 15, 2021
a78fbc9
Slightly larger timeout.
eskimor Jun 15, 2021
53f0c15
More typechecking stuff.
eskimor Jun 16, 2021
6abf1f3
Cleanup.
eskimor Jun 16, 2021
b541062
Finished most of the sending logic.
eskimor Jun 17, 2021
5883c08
Handle active leaves updates
eskimor Jun 17, 2021
4c4e3a7
Pass sessions in already.
eskimor Jun 17, 2021
bd8bb95
Startup dispute sending.
eskimor Jun 17, 2021
4ebe127
Provide incoming decoding facilities
eskimor Jun 18, 2021
95351bd
Relaxed runtime util requirements.
eskimor Jun 18, 2021
863b1d9
Better usability of incoming requests.
eskimor Jun 18, 2021
35d2cad
Add basic receiver functionality.
eskimor Jun 18, 2021
da8abac
Cleanup + fixes for sender.
eskimor Jun 18, 2021
c997aed
One more sender fix.
eskimor Jun 18, 2021
06770db
Start receiver.
eskimor Jun 18, 2021
2378c7e
Make sure to send responses back.
eskimor Jun 19, 2021
3910cf4
WIP: Exposed authority discovery
eskimor Jun 21, 2021
840a046
Merge branch 'master' into rk-dispute-distribution-impl
eskimor Jun 21, 2021
5b10c78
Make tests pass.
eskimor Jun 21, 2021
d2aa4ff
Fully featured receiver.
eskimor Jun 21, 2021
da4955d
Decrease cost of `NotAValidator`.
eskimor Jun 22, 2021
1fc9740
Make `RuntimeInfo` LRU cache size configurable.
eskimor Jun 22, 2021
9397e35
Cache more sessions.
eskimor Jun 22, 2021
ccbab3f
Fix collator protocol.
eskimor Jun 22, 2021
5db60d3
Disable metrics for now.
eskimor Jun 22, 2021
da20774
Make dispute-distribution a proper subsystem.
eskimor Jun 22, 2021
f9da3ae
Fix naming.
eskimor Jun 22, 2021
2231dc3
Code style fixes.
eskimor Jun 23, 2021
41c2801
Factored out 4x copied mock function.
eskimor Jun 23, 2021
3e91427
WIP: Tests.
eskimor Jun 23, 2021
8a3da18
Whitespace cleanup.
eskimor Jun 24, 2021
0b188cd
Accessor functions.
eskimor Jun 24, 2021
2637c1b
More testing.
eskimor Jun 24, 2021
3d09d48
More Debug instances.
eskimor Jun 25, 2021
def8772
Fix busy loop.
eskimor Jun 25, 2021
c7cdca9
Working tests.
eskimor Jun 29, 2021
b9f20c2
More tests.
eskimor Jun 30, 2021
6f7da40
Merge branch 'master' into rk-dispute-distribution-impl
eskimor Jun 30, 2021
b3c7427
Cleanup.
eskimor Jun 30, 2021
c01d8d1
Fix build.
eskimor Jun 30, 2021
4f616d6
Basic receiving test.
eskimor Jun 30, 2021
a3ff6ae
Non validator message gets dropped.
eskimor Jun 30, 2021
217bd7e
More receiving tests.
eskimor Jun 30, 2021
26b8b00
Test nested and subsequent imports.
eskimor Jun 30, 2021
5dd7c84
Fix spaces.
eskimor Jun 30, 2021
488fb47
Better formatted imports.
eskimor Jun 30, 2021
92ff4be
Import cleanup.
eskimor Jul 1, 2021
7d5f416
Metrics.
eskimor Jul 2, 2021
04f91e9
Message -> MuxedMessage
eskimor Jul 2, 2021
d43fb5f
Message -> MuxedMessage
eskimor Jul 2, 2021
dc6d0f1
Merge branch 'master' into rk-dispute-distribution-impl
eskimor Jul 2, 2021
fc6a612
More review remarks.
eskimor Jul 2, 2021
2907a53
Add missing metrics.rs.
eskimor Jul 2, 2021
1f7ce88
Fix flaky test.
eskimor Jul 2, 2021
cd66550
Dispute coordinator - deliver confirmations.
eskimor Jul 6, 2021
fd920a1
Send out `DisputeMessage` on issue local statement.
eskimor Jul 6, 2021
90b46f4
Merge branch 'master' into rk-dispute-distribution-impl
eskimor Jul 8, 2021
210b853
Unwire dispute distribution.
eskimor Jul 8, 2021
58b1bee
Review remarks.
eskimor Jul 8, 2021
59c76c8
Review remarks.
eskimor Jul 8, 2021
3cd91cb
Better docs.
eskimor Jul 8, 2021
dedbaa8
Merge branch 'master' into rk-dispute-distribution-impl
eskimor Jul 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ members = [
"node/network/availability-recovery",
"node/network/collator-protocol",
"node/network/gossip-support",
"node/network/dispute-distribution",
"node/overseer",
"node/overseer/overseer-gen",
"node/overseer/overseer-gen/proc-macro",
Expand Down
180 changes: 139 additions & 41 deletions node/core/dispute-coordinator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@
use std::collections::HashSet;
use std::sync::Arc;

use polkadot_node_primitives::{CandidateVotes, SignedDisputeStatement};
use polkadot_node_primitives::{CandidateVotes, DISPUTE_WINDOW, DisputeMessage, SignedDisputeStatement, DisputeMessageCheckError};
use polkadot_node_subsystem::{
overseer,
messages::{
DisputeCoordinatorMessage, ChainApiMessage, DisputeParticipationMessage,
},
SubsystemContext, FromOverseer, OverseerSignal, SpawnedSubsystem,
SubsystemError,
overseer, SubsystemContext, FromOverseer, OverseerSignal, SpawnedSubsystem, SubsystemError,
errors::{ChainApiError, RuntimeApiError},
messages::{
ChainApiMessage, DisputeCoordinatorMessage, DisputeDistributionMessage,
DisputeParticipationMessage, ImportStatementsResult
}
};
use polkadot_node_subsystem_util::rolling_session_window::{
RollingSessionWindow, SessionWindowUpdate,
};
use polkadot_primitives::v1::{
SessionIndex, CandidateHash, Hash, CandidateReceipt, DisputeStatement, ValidatorIndex,
ValidatorSignature, BlockNumber, ValidatorPair,
BlockNumber, CandidateHash, CandidateReceipt, DisputeStatement, Hash,
SessionIndex, SessionInfo, ValidatorIndex, ValidatorPair, ValidatorSignature
};

use futures::prelude::*;
Expand All @@ -61,10 +60,6 @@ mod tests;

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

// It would be nice to draw this from the chain state, but we have no tools for it right now.
// On Polkadot this is 1 day, and on Kusama it's 6 hours.
const DISPUTE_WINDOW: SessionIndex = 6;

struct State {
keystore: Arc<LocalKeystore>,
highest_session: Option<SessionIndex>,
Expand Down Expand Up @@ -134,6 +129,9 @@ pub enum Error {
#[error(transparent)]
Oneshot(#[from] oneshot::Canceled),

#[error("Oneshot send failed")]
OneshotSend,

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

Expand Down Expand Up @@ -308,6 +306,7 @@ async fn handle_incoming(
candidate_receipt,
session,
statements,
pending_confirmation,
} => {
handle_import_statements(
ctx,
Expand All @@ -318,6 +317,7 @@ async fn handle_incoming(
candidate_receipt,
session,
statements,
pending_confirmation,
).await?;
}
DisputeCoordinatorMessage::ActiveDisputes(rx) => {
Expand Down Expand Up @@ -400,8 +400,13 @@ async fn handle_import_statements(
candidate_receipt: CandidateReceipt,
session: SessionIndex,
statements: Vec<(SignedDisputeStatement, ValidatorIndex)>,
pending_confirmation: oneshot::Sender<ImportStatementsResult>,
) -> Result<(), Error> {
if state.highest_session.map_or(true, |h| session + DISPUTE_WINDOW < h) {

// It is not valid to participate in an ancient dispute (spam?).
pending_confirmation.send(ImportStatementsResult::InvalidImport).map_err(|_| Error::OneshotSend)?;

return Ok(());
}

Expand Down Expand Up @@ -479,37 +484,54 @@ async fn handle_import_statements(
let already_disputed = is_disputed && !was_undisputed;
let concluded_valid = votes.valid.len() >= supermajority_threshold;

let mut tx = db::v1::Transaction::default();
{ // Scope so we will only confirm valid import after the import got actually persisted.
let mut tx = db::v1::Transaction::default();

if freshly_disputed && !concluded_valid {
// add to active disputes and begin local participation.
update_active_disputes(
store,
config,
&mut tx,
|active| active.insert(session, candidate_hash),
)?;
if freshly_disputed && !concluded_valid {

ctx.send_message(DisputeParticipationMessage::Participate {
candidate_hash,
candidate_receipt,
session,
n_validators: n_validators as u32,
}).await;
}
let (report_availability, receive_availability) = oneshot::channel();
ctx.send_message(DisputeParticipationMessage::Participate {
candidate_hash,
candidate_receipt,
session,
n_validators: n_validators as u32,
report_availability,
}).await;

if concluded_valid && already_disputed {
// remove from active disputes.
update_active_disputes(
store,
config,
&mut tx,
|active| active.delete(session, candidate_hash),
)?;
if !receive_availability.await.map_err(Error::Oneshot)? {
pending_confirmation.send(ImportStatementsResult::InvalidImport).map_err(|_| Error::OneshotSend)?;
tracing::debug!(
target: LOG_TARGET,
"Recovering availability failed - invalid import."
);
return Ok(())
}

// add to active disputes and begin local participation.
update_active_disputes(
store,
config,
&mut tx,
|active| active.insert(session, candidate_hash),
)?;

}

if concluded_valid && already_disputed {
// remove from active disputes.
update_active_disputes(
store,
config,
&mut tx,
|active| active.delete(session, candidate_hash),
)?;
}

tx.put_candidate_votes(session, candidate_hash, votes.into());
tx.write(store, &config.column_config())?;
}

tx.put_candidate_votes(session, candidate_hash, votes.into());
tx.write(store, &config.column_config())?;
pending_confirmation.send(ImportStatementsResult::ValidImport).map_err(|_| Error::OneshotSend)?;

Ok(())
}
Expand Down Expand Up @@ -541,7 +563,7 @@ async fn issue_local_statement(
valid: bool,
) -> Result<(), Error> {
// Load session info.
let validators = match state.rolling_session_window.session_info(session) {
let info = match state.rolling_session_window.session_info(session) {
None => {
tracing::warn!(
target: LOG_TARGET,
Expand All @@ -551,9 +573,11 @@ async fn issue_local_statement(

return Ok(())
}
Some(info) => info.validators.clone(),
Some(info) => info,
};

let validators = info.validators.clone();

let votes = db::v1::load_candidate_votes(
store,
&config.column_config(),
Expand Down Expand Up @@ -604,8 +628,27 @@ async fn issue_local_statement(
}
}

// Get our message out:
for (statement, index) in &statements {
let dispute_message = match make_dispute_message(info, &votes, statement.clone(), *index) {
Err(err) => {
tracing::debug!(
target: LOG_TARGET,
?err,
"Creating dispute message failed."
);
continue
}
Ok(dispute_message) => dispute_message,
};

ctx.send_message(DisputeDistributionMessage::SendDispute(dispute_message)).await;
}


// Do import
if !statements.is_empty() {
let (pending_confirmation, _rx) = oneshot::channel();
handle_import_statements(
ctx,
store,
Expand All @@ -615,12 +658,67 @@ async fn issue_local_statement(
candidate_receipt,
session,
statements,
pending_confirmation,
).await?;
}

Ok(())
}

#[derive(Debug, thiserror::Error)]
enum MakeDisputeMessageError {
#[error("There was no opposite vote available")]
NoOppositeVote,
#[error("Found vote had an invalid validator index that could not be found")]
InvalidValidatorIndex,
#[error("Statement found in votes had invalid signature.")]
InvalidStoredStatement,
#[error(transparent)]
InvalidStatementCombination(DisputeMessageCheckError),
}

fn make_dispute_message(
info: &SessionInfo,
votes: &CandidateVotes,
our_vote: SignedDisputeStatement,
our_index: ValidatorIndex
) -> Result<DisputeMessage, MakeDisputeMessageError> {

let validators = &info.validators;

let (valid_statement, valid_index, invalid_statement, invalid_index) =
if let DisputeStatement::Valid(_) = our_vote.statement() {
let (statement_kind, validator_index, validator_signature)
= votes.invalid.get(0).ok_or(MakeDisputeMessageError::NoOppositeVote)?.clone();
let other_vote = SignedDisputeStatement::new_checked(
DisputeStatement::Invalid(statement_kind),
our_vote.candidate_hash().clone(),
our_vote.session_index(),
validators.get(validator_index.0 as usize).ok_or(MakeDisputeMessageError::InvalidValidatorIndex)?.clone(),
validator_signature,
).map_err(|()| MakeDisputeMessageError::InvalidStoredStatement)?;
(our_vote, our_index, other_vote, validator_index)
} else {
let (statement_kind, validator_index, validator_signature)
= votes.valid.get(0).ok_or(MakeDisputeMessageError::NoOppositeVote)?.clone();
let other_vote = SignedDisputeStatement::new_checked(
DisputeStatement::Valid(statement_kind),
our_vote.candidate_hash().clone(),
our_vote.session_index(),
validators.get(validator_index.0 as usize).ok_or(MakeDisputeMessageError::InvalidValidatorIndex)?.clone(),
validator_signature,
).map_err(|()| MakeDisputeMessageError::InvalidStoredStatement)?;
(other_vote, validator_index, our_vote, our_index)
};

DisputeMessage::from_signed_statements(
valid_statement, valid_index,
invalid_statement, invalid_index,
votes.candidate_receipt.clone(),
info,
).map_err(MakeDisputeMessageError::InvalidStatementCombination)
}

fn determine_undisputed_chain(
store: &dyn KeyValueDB,
config: &Config,
Expand Down
Loading