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 1 commit
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
Prev Previous commit
Next Next commit
Startup dispute sending.
  • Loading branch information
eskimor committed Jun 17, 2021
commit bd8bb95ba9fe91e6f1fe40ab4f79b3792b6eb03c
6 changes: 5 additions & 1 deletion node/network/dispute-distribution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.


use std::collections::HashSet;

/// Sending and receiving of `DisputeRequest`s.
use futures::channel::mpsc;
use futures::channel::{mpsc, oneshot};
use futures::{future::Either, FutureExt, StreamExt, TryFutureExt, select};

use polkadot_primitives::v1::CandidateHash;
use polkadot_subsystem::messages::{AllMessages, DisputeCoordinatorMessage};
use sp_keystore::SyncCryptoStorePtr;

use polkadot_subsystem::{
Expand Down
11 changes: 11 additions & 0 deletions node/network/dispute-distribution/src/sender/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,20 @@ pub enum NonFatal {
#[error("Oneshot for asking dispute coordinator for active disputes got canceled.")]
AskActiveDisputesCanceled,

/// This error likely indicates a bug in the coordinator.
#[error("Oneshot for asking dispute coordinator for candidate votes got canceled.")]
AskCandidateVotesCanceled,

/// This error does indicate a bug in the coordinator.
///
/// We did not receive votes on both sides for `CandidateVotes` received from the coordinator.
#[error("Invalid dispute encountered.")]
InvalidDisputeFromCoordinator,

/// Errors coming from runtime::Runtime.
#[error("Error while accessing runtime information")]
Runtime(#[from] #[source] runtime::NonFatal),
}

pub type Result<T> = std::result::Result<T, Error>;
pub type NonFatalResult<T> = std::result::Result<T, NonFatal>;
177 changes: 158 additions & 19 deletions node/network/dispute-distribution/src/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ use polkadot_node_network_protocol::request_response::OutgoingResult;
use polkadot_node_network_protocol::request_response::Recipient;
use polkadot_node_network_protocol::request_response::Requests;
use polkadot_node_network_protocol::request_response::v1::DisputeResponse;
use polkadot_node_primitives::CandidateVotes;
use polkadot_node_primitives::DisputeMessage;
use polkadot_node_primitives::SignedDisputeStatement;
use polkadot_primitives::v1::DisputeStatement;
use polkadot_primitives::v1::{SessionIndex, AuthorityDiscoveryId};
use polkadot_subsystem::ActiveLeavesUpdate;
use polkadot_subsystem::messages::AllMessages;
Expand All @@ -40,6 +43,7 @@ use polkadot_node_network_protocol::request_response::v1::DisputeRequest;
use polkadot_primitives::v1::CandidateHash;
use polkadot_primitives::v1::Hash;
use polkadot_subsystem::SubsystemContext;
use polkadot_node_subsystem_util::runtime::RuntimeInfo;


/// For each ongoing dispute we have a `SendTask` which takes care of it.
Expand All @@ -54,9 +58,8 @@ pub use send_task::FromSendingTask;
mod error;
pub use error::{Result, Error, Fatal, NonFatal};

use polkadot_node_subsystem_util::runtime::RuntimeInfo;

use crate::LOG_TARGET;
use self::error::NonFatalResult;

/// Sending of disputes to all relevant validator nodes.
pub struct DisputeSender {
Expand All @@ -80,6 +83,7 @@ impl DisputeSender
pub fn new(tx: mpsc::Sender<FromSendingTask>) -> Self {
Self {
active_heads: Vec::new(),
active_sessions: HashMap::new(),
sendings: HashMap::new(),
tx,
}
Expand All @@ -95,10 +99,10 @@ impl DisputeSender
let req: DisputeRequest = msg.into();
match self.sendings.entry(req.0.candidate_hash) {
Entry::Occupied(_) => {
tracing::warn!(
tracing::trace!(
target: LOG_TARGET,
candidate_hash = ?req.0.candidate_hash,
"Double dispute participation - not supposed to happen."
"Dispute sending already active."
);
return Ok(())
}
Expand All @@ -121,6 +125,7 @@ impl DisputeSender
/// - Initiate a retry of sends disputes are still active.
/// - Get new authorities to send messages to.
/// - Get rid of obsolete tasks and disputes.
/// - Get dispute sending started in case we missed one for some reason (e.g. on node startup)
pub async fn update_leaves<Context: SubsystemContext>(
&mut self,
ctx: &mut Context,
Expand All @@ -134,13 +139,30 @@ impl DisputeSender

let have_new_sessions = self.refresh_sessions(ctx, runtime).await?;

self.cleanup_dead_disputes(ctx).await?;
let active_disputes = get_active_disputes(ctx).await?;
let unknown_disputes = {
let mut disputes = active_disputes.clone();
disputes.retain(|(_, c)| !self.sendings.contains_key(c));
disputes
};

let active_disputes: HashSet<_> = active_disputes.into_iter().map(|(_, c)| c).collect();

// Cleanup obsolete senders:
self.sendings.retain(
|candidate_hash, _| active_disputes.contains(candidate_hash)
);

for send in self.sendings.values_mut() {
if have_new_sessions || send.has_failed_sends() {
send.refresh_sends(ctx, runtime, &self.active_sessions).await?;
}
}

// This should only be non-empty on startup, but if not - we got you covered:
for dispute in unknown_disputes {
self.start_send_for_dispute(ctx, runtime, dispute).await?
}
Ok(())
}

Expand All @@ -165,6 +187,118 @@ impl DisputeSender
}
}

/// Call `start_sending` on all passed in disputes.
///
/// Recover necessary votes for building up `DisputeMessage` and start sending for all of them.
async fn start_send_for_dispute<Context: SubsystemContext>(
&mut self,
ctx: &mut Context,
runtime: &mut RuntimeInfo,
dispute: (SessionIndex, CandidateHash),
) -> Result<()> {
let (session_index, candidate_hash) = dispute;
// We need some relay chain head for context for receiving session info information:
let ref_head = self.active_sessions.values().next().ok_or(NonFatal::NoActiveHeads)?;
let info = runtime.get_session_info_by_index(ctx, *ref_head, session_index).await?;
let our_index = match info.validator_info.our_index {
None => {
tracing::trace!(
target: LOG_TARGET,
"Not a validator in that session - not starting dispute sending."
);
return Ok(())
}
Some(index) => index,
};

let votes = match get_candidate_votes(ctx, session_index, candidate_hash).await? {
None => {
tracing::debug!(
target: LOG_TARGET,
?session_index,
?candidate_hash,
"No votes for active dispute?! - possible, due to race."
);
return Ok(())
}
Some(votes) => votes,
};

let our_valid_vote = votes
.valid
.iter()
.find(|(_, i, _)| *i == our_index);

let our_invalid_vote = votes
.invalid
.iter()
.find(|(_, i, _)| *i == our_index);

let (valid_vote, invalid_vote) =
if let Some(our_valid_vote) = our_valid_vote {
// Get some invalid vote as well:
let invalid_vote = votes
.invalid
.get(0)
.ok_or(NonFatal::InvalidDisputeFromCoordinator)?;
(our_valid_vote, invalid_vote)
} else if let Some(our_invalid_vote) = our_invalid_vote {
// Get some valid vote as well:
let valid_vote = votes
.valid
.get(0)
.ok_or(NonFatal::InvalidDisputeFromCoordinator)?;
(valid_vote, our_invalid_vote)
} else {
return Err(From::from(NonFatal::InvalidDisputeFromCoordinator))
}
;
let (kind, valid_index, signature) = valid_vote;
let valid_public = info
.session_info
.validators
.get(valid_index.0 as usize)
.ok_or(NonFatal::InvalidDisputeFromCoordinator)?;
let valid_signed = SignedDisputeStatement::new_checked(
DisputeStatement::Valid(kind.clone()),
candidate_hash,
session_index,
valid_public.clone(),
signature.clone(),
).map_err(|()| NonFatal::InvalidDisputeFromCoordinator)?;

let (kind, invalid_index, signature) = invalid_vote;
let invalid_public = info
.session_info
.validators
.get(invalid_index.0 as usize)
.ok_or(NonFatal::InvalidDisputeFromCoordinator)?;
let invalid_signed = SignedDisputeStatement::new_checked(
DisputeStatement::Invalid(kind.clone()),
candidate_hash,
session_index,
invalid_public.clone(),
signature.clone(),
).map_err(|()| NonFatal::InvalidDisputeFromCoordinator)?;

// Reconstructing the checked signed dispute statements is hardly useful here and wasteful,
// but I don't want to enable a bypass for the below smart constructor and this code path
// is supposed to be only hit on startup basically.
//
// Revisit this decision when the `from_signed_statements` is unneded for the normal code
// path as well.
let message = DisputeMessage::from_signed_statements(
valid_signed,
*valid_index,
invalid_signed,
*invalid_index,
&info.session_info
).ok_or(NonFatal::InvalidDisputeFromCoordinator)?;

// Finally, get the party started:
self.start_sending(ctx, runtime, message).await
}

/// Make active sessions correspond to currently active heads.
///
/// Returns: true if sessions changed.
Expand All @@ -181,17 +315,6 @@ impl DisputeSender
self.active_sessions = new_sessions;
Ok(updated)
}

/// Query dispute coordinator for currently active disputes and get rid of all obsolete
/// senders.
async fn cleanup_dead_disputes<Context: SubsystemContext>(
&mut self,
ctx: &mut Context,
) -> Result<()> {
let active_disputes = get_active_disputes(ctx).await?;
self.sendings.retain(|candidate_hash, _| active_disputes.contains(candidate_hash));
Ok(())
}
}

/// Retrieve the currently active sessions.
Expand All @@ -212,11 +335,27 @@ async fn get_active_session_indeces<Context: SubsystemContext>(

/// Retrieve Set of active disputes from the dispute coordinator.
async fn get_active_disputes<Context: SubsystemContext>(ctx: &mut Context)
-> Result<HashSet<CandidateHash>> {
-> NonFatalResult<Vec<(SessionIndex, CandidateHash)>> {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::DisputeCoordinator(
DisputeCoordinatorMessage::ActiveDisputes(tx)
)).await;
let disputes = rx.await.map_err(|_| NonFatal::AskActiveDisputesCanceled)?;
Ok(disputes.into_iter().map(|(_, c)| c).collect())
rx.await.map_err(|_| NonFatal::AskActiveDisputesCanceled)
}

/// Get all locally available dispute votes for a given dispute.
async fn get_candidate_votes<Context: SubsystemContext>(
ctx: &mut Context,
session_index: SessionIndex,
candidate_hash: CandidateHash,
) -> NonFatalResult<Option<CandidateVotes>> {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::DisputeCoordinator(
DisputeCoordinatorMessage::QueryCandidateVotes(
session_index,
candidate_hash,
tx
)
)).await;
rx.await.map_err(|_| NonFatal::AskCandidateVotesCanceled)
}