Approve multiple candidates with a single signature (#1191)

Initial implementation for the plan discussed here: https://github.com/paritytech/polkadot-sdk/issues/701
Built on top of https://github.com/paritytech/polkadot-sdk/pull/1178
v0: https://github.com/paritytech/polkadot/pull/7554,

## Overall idea

When approval-voting checks a candidate and is ready to advertise the
approval, defer it in a per-relay chain block until we either have
MAX_APPROVAL_COALESCE_COUNT candidates to sign or a candidate has stayed
MAX_APPROVALS_COALESCE_TICKS in the queue, in both cases we sign what
candidates we have available.

This should allow us to reduce the number of approvals messages we have
to create/send/verify. The parameters are configurable, so we should
find some values that balance:

- Security of the network: Delaying broadcasting of an approval
shouldn't but the finality at risk and to make sure that never happens
we won't delay sending a vote if we are past 2/3 from the no-show time.
- Scalability of the network: MAX_APPROVAL_COALESCE_COUNT = 1 &
MAX_APPROVALS_COALESCE_TICKS =0, is what we have now and we know from
the measurements we did on versi, it bottlenecks
approval-distribution/approval-voting when increase significantly the
number of validators and parachains
- Block storage: In case of disputes we have to import this votes on
chain and that increase the necessary storage with
MAX_APPROVAL_COALESCE_COUNT * CandidateHash per vote. Given that
disputes are not the normal way of the network functioning and we will
limit MAX_APPROVAL_COALESCE_COUNT in the single digits numbers, this
should be good enough. Alternatively, we could try to create a better
way to store this on-chain through indirection, if that's needed.

## Other fixes:
- Fixed the fact that we were sending random assignments to
non-validators, that was wrong because those won't do anything with it
and they won't gossip it either because they do not have a grid topology
set, so we would waste the random assignments.
- Added metrics to be able to debug potential no-shows and
mis-processing of approvals/assignments.

## TODO:
- [x] Get feedback, that this is moving in the right direction. @ordian
@sandreim @eskimor @burdges, let me know what you think.
- [x] More and more testing.
- [x]  Test in versi.
- [x] Make MAX_APPROVAL_COALESCE_COUNT &
MAX_APPROVAL_COALESCE_WAIT_MILLIS a parachain host configuration.
- [x] Make sure the backwards compatibility works correctly
- [x] Make sure this direction is compatible with other streams of work:
https://github.com/paritytech/polkadot-sdk/issues/635 &
https://github.com/paritytech/polkadot-sdk/issues/742
- [x] Final versi burn-in before merging

---------

Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
This commit is contained in:
Alexandru Gheorghe
2023-12-13 08:43:15 +02:00
committed by GitHub
parent d18a682bf7
commit a84dd0dba5
82 changed files with 5883 additions and 1483 deletions
@@ -22,8 +22,8 @@ use polkadot_node_network_protocol::{
grid_topology::{GridNeighbors, RequiredRouting, SessionBoundGridTopologyStorage},
peer_set::{IsAuthority, PeerSet, ValidationVersion},
v1::{self as protocol_v1, StatementMetadata},
v2 as protocol_v2, vstaging as protocol_vstaging, IfDisconnected, PeerId,
UnifiedReputationChange as Rep, Versioned, View,
v2 as protocol_v2, v3 as protocol_v3, IfDisconnected, PeerId, UnifiedReputationChange as Rep,
Versioned, View,
};
use polkadot_node_primitives::{
SignedFullStatement, Statement, StatementWithPVD, UncheckedSignedFullStatement,
@@ -1075,7 +1075,7 @@ async fn circulate_statement<'a, Context>(
})
.partition::<Vec<_>, _>(|(_, _, version)| match version {
ValidationVersion::V1 => true,
ValidationVersion::V2 | ValidationVersion::VStaging => false,
ValidationVersion::V2 | ValidationVersion::V3 => false,
}); // partition is handy here but not if we add more protocol versions
let payload = v1_statement_message(relay_parent, stored.statement.clone(), metrics);
@@ -1108,8 +1108,7 @@ async fn circulate_statement<'a, Context>(
.collect();
let v2_peers_to_send = filter_by_peer_version(&peers_to_send, ValidationVersion::V2.into());
let vstaging_to_send =
filter_by_peer_version(&peers_to_send, ValidationVersion::VStaging.into());
let v3_to_send = filter_by_peer_version(&peers_to_send, ValidationVersion::V3.into());
if !v2_peers_to_send.is_empty() {
gum::trace!(
@@ -1126,17 +1125,17 @@ async fn circulate_statement<'a, Context>(
.await;
}
if !vstaging_to_send.is_empty() {
if !v3_to_send.is_empty() {
gum::trace!(
target: LOG_TARGET,
?vstaging_to_send,
?v3_to_send,
?relay_parent,
statement = ?stored.statement,
"Sending statement to vstaging peers",
"Sending statement to v3 peers",
);
ctx.send_message(NetworkBridgeTxMessage::SendValidationMessage(
vstaging_to_send,
compatible_v1_message(ValidationVersion::VStaging, payload.clone()).into(),
v3_to_send,
compatible_v1_message(ValidationVersion::V3, payload.clone()).into(),
))
.await;
}
@@ -1472,10 +1471,8 @@ async fn handle_incoming_message<'a, Context>(
let message = match message {
Versioned::V1(m) => m,
Versioned::V2(protocol_v2::StatementDistributionMessage::V1Compatibility(m)) |
Versioned::VStaging(protocol_vstaging::StatementDistributionMessage::V1Compatibility(
m,
)) => m,
Versioned::V2(_) | Versioned::VStaging(_) => {
Versioned::V3(protocol_v3::StatementDistributionMessage::V1Compatibility(m)) => m,
Versioned::V2(_) | Versioned::V3(_) => {
// The higher-level subsystem code is supposed to filter out
// all non v1 messages.
gum::debug!(
@@ -2201,8 +2198,7 @@ fn compatible_v1_message(
ValidationVersion::V1 => Versioned::V1(message),
ValidationVersion::V2 =>
Versioned::V2(protocol_v2::StatementDistributionMessage::V1Compatibility(message)),
ValidationVersion::VStaging => Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::V1Compatibility(message),
),
ValidationVersion::V3 =>
Versioned::V3(protocol_v3::StatementDistributionMessage::V1Compatibility(message)),
}
}
@@ -27,7 +27,7 @@ use std::time::Duration;
use polkadot_node_network_protocol::{
request_response::{v1 as request_v1, v2::AttestedCandidateRequest, IncomingRequestReceiver},
v2 as protocol_v2, vstaging as protocol_vstaging, Versioned,
v2 as protocol_v2, v3 as protocol_v3, Versioned,
};
use polkadot_node_primitives::StatementWithPVD;
use polkadot_node_subsystem::{
@@ -400,11 +400,11 @@ impl<R: rand::Rng> StatementDistributionSubsystem<R> {
Versioned::V2(
protocol_v2::StatementDistributionMessage::V1Compatibility(_),
) |
Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::V1Compatibility(_),
Versioned::V3(
protocol_v3::StatementDistributionMessage::V1Compatibility(_),
) => VersionTarget::Legacy,
Versioned::V1(_) => VersionTarget::Legacy,
Versioned::V2(_) | Versioned::VStaging(_) => VersionTarget::Current,
Versioned::V2(_) | Versioned::V3(_) => VersionTarget::Current,
},
_ => VersionTarget::Both,
};
@@ -29,8 +29,7 @@ use polkadot_node_network_protocol::{
MAX_PARALLEL_ATTESTED_CANDIDATE_REQUESTS,
},
v2::{self as protocol_v2, StatementFilter},
vstaging as protocol_vstaging, IfDisconnected, PeerId, UnifiedReputationChange as Rep,
Versioned, View,
v3 as protocol_v3, IfDisconnected, PeerId, UnifiedReputationChange as Rep, Versioned, View,
};
use polkadot_node_primitives::{
SignedFullStatementWithPVD, StatementWithPVD as FullStatementWithPVD,
@@ -366,7 +365,7 @@ pub(crate) async fn handle_network_update<Context>(
gum::trace!(target: LOG_TARGET, ?peer_id, ?role, ?protocol_version, "Peer connected");
let versioned_protocol = if protocol_version != ValidationVersion::V2.into() &&
protocol_version != ValidationVersion::VStaging.into()
protocol_version != ValidationVersion::V3.into()
{
return
} else {
@@ -432,28 +431,28 @@ pub(crate) async fn handle_network_update<Context>(
net_protocol::StatementDistributionMessage::V2(
protocol_v2::StatementDistributionMessage::V1Compatibility(_),
) |
net_protocol::StatementDistributionMessage::VStaging(
protocol_vstaging::StatementDistributionMessage::V1Compatibility(_),
net_protocol::StatementDistributionMessage::V3(
protocol_v3::StatementDistributionMessage::V1Compatibility(_),
) => return,
net_protocol::StatementDistributionMessage::V2(
protocol_v2::StatementDistributionMessage::Statement(relay_parent, statement),
) |
net_protocol::StatementDistributionMessage::VStaging(
protocol_vstaging::StatementDistributionMessage::Statement(relay_parent, statement),
net_protocol::StatementDistributionMessage::V3(
protocol_v3::StatementDistributionMessage::Statement(relay_parent, statement),
) =>
handle_incoming_statement(ctx, state, peer_id, relay_parent, statement, reputation)
.await,
net_protocol::StatementDistributionMessage::V2(
protocol_v2::StatementDistributionMessage::BackedCandidateManifest(inner),
) |
net_protocol::StatementDistributionMessage::VStaging(
protocol_vstaging::StatementDistributionMessage::BackedCandidateManifest(inner),
net_protocol::StatementDistributionMessage::V3(
protocol_v3::StatementDistributionMessage::BackedCandidateManifest(inner),
) => handle_incoming_manifest(ctx, state, peer_id, inner, reputation).await,
net_protocol::StatementDistributionMessage::V2(
protocol_v2::StatementDistributionMessage::BackedCandidateKnown(inner),
) |
net_protocol::StatementDistributionMessage::VStaging(
protocol_vstaging::StatementDistributionMessage::BackedCandidateKnown(inner),
net_protocol::StatementDistributionMessage::V3(
protocol_v3::StatementDistributionMessage::BackedCandidateKnown(inner),
) => handle_incoming_acknowledgement(ctx, state, peer_id, inner, reputation).await,
},
NetworkBridgeEvent::PeerViewChange(peer_id, view) =>
@@ -806,13 +805,13 @@ fn pending_statement_network_message(
protocol_v2::StatementDistributionMessage::Statement(relay_parent, signed)
})
.map(|msg| (vec![peer.0], Versioned::V2(msg).into())),
ValidationVersion::VStaging => statement_store
ValidationVersion::V3 => statement_store
.validator_statement(originator, compact)
.map(|s| s.as_unchecked().clone())
.map(|signed| {
protocol_vstaging::StatementDistributionMessage::Statement(relay_parent, signed)
protocol_v3::StatementDistributionMessage::Statement(relay_parent, signed)
})
.map(|msg| (vec![peer.0], Versioned::VStaging(msg).into())),
.map(|msg| (vec![peer.0], Versioned::V3(msg).into())),
ValidationVersion::V1 => {
gum::error!(
target: LOG_TARGET,
@@ -945,10 +944,10 @@ async fn send_pending_grid_messages<Context>(
)
.into(),
)),
ValidationVersion::VStaging => messages.push((
ValidationVersion::V3 => messages.push((
vec![peer_id.0],
Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::BackedCandidateManifest(
Versioned::V3(
protocol_v3::StatementDistributionMessage::BackedCandidateManifest(
manifest,
),
)
@@ -960,7 +959,7 @@ async fn send_pending_grid_messages<Context>(
"Bug ValidationVersion::V1 should not be used in statement-distribution v2,
legacy should have handled this"
);
}
},
};
},
grid::ManifestKind::Acknowledgement => {
@@ -1308,8 +1307,8 @@ async fn circulate_statement<Context>(
let statement_to_v2_peers =
filter_by_peer_version(&statement_to_peers, ValidationVersion::V2.into());
let statement_to_vstaging_peers =
filter_by_peer_version(&statement_to_peers, ValidationVersion::VStaging.into());
let statement_to_v3_peers =
filter_by_peer_version(&statement_to_peers, ValidationVersion::V3.into());
// ship off the network messages to the network bridge.
if !statement_to_v2_peers.is_empty() {
@@ -1331,17 +1330,17 @@ async fn circulate_statement<Context>(
.await;
}
if !statement_to_vstaging_peers.is_empty() {
if !statement_to_v3_peers.is_empty() {
gum::debug!(
target: LOG_TARGET,
?compact_statement,
n_peers = ?statement_to_peers.len(),
"Sending statement to vstaging peers",
"Sending statement to v3 peers",
);
ctx.send_message(NetworkBridgeTxMessage::SendValidationMessage(
statement_to_vstaging_peers,
Versioned::VStaging(protocol_vstaging::StatementDistributionMessage::Statement(
statement_to_v3_peers,
Versioned::V3(protocol_v3::StatementDistributionMessage::Statement(
relay_parent,
statement.as_unchecked().clone(),
))
@@ -1887,8 +1886,7 @@ async fn provide_candidate_to_grid<Context>(
}
let manifest_peers_v2 = filter_by_peer_version(&manifest_peers, ValidationVersion::V2.into());
let manifest_peers_vstaging =
filter_by_peer_version(&manifest_peers, ValidationVersion::VStaging.into());
let manifest_peers_v3 = filter_by_peer_version(&manifest_peers, ValidationVersion::V3.into());
if !manifest_peers_v2.is_empty() {
gum::debug!(
target: LOG_TARGET,
@@ -1908,27 +1906,27 @@ async fn provide_candidate_to_grid<Context>(
.await;
}
if !manifest_peers_vstaging.is_empty() {
if !manifest_peers_v3.is_empty() {
gum::debug!(
target: LOG_TARGET,
?candidate_hash,
local_validator = ?per_session.local_validator,
n_peers = manifest_peers_vstaging.len(),
"Sending manifest to vstaging peers"
n_peers = manifest_peers_v3.len(),
"Sending manifest to v3 peers"
);
ctx.send_message(NetworkBridgeTxMessage::SendValidationMessage(
manifest_peers_vstaging,
Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::BackedCandidateManifest(manifest),
)
manifest_peers_v3,
Versioned::V3(protocol_v3::StatementDistributionMessage::BackedCandidateManifest(
manifest,
))
.into(),
))
.await;
}
let ack_peers_v2 = filter_by_peer_version(&ack_peers, ValidationVersion::V2.into());
let ack_peers_vstaging = filter_by_peer_version(&ack_peers, ValidationVersion::VStaging.into());
let ack_peers_v3 = filter_by_peer_version(&ack_peers, ValidationVersion::V3.into());
if !ack_peers_v2.is_empty() {
gum::debug!(
target: LOG_TARGET,
@@ -1948,22 +1946,20 @@ async fn provide_candidate_to_grid<Context>(
.await;
}
if !ack_peers_vstaging.is_empty() {
if !ack_peers_v3.is_empty() {
gum::debug!(
target: LOG_TARGET,
?candidate_hash,
local_validator = ?per_session.local_validator,
n_peers = ack_peers_vstaging.len(),
"Sending acknowledgement to vstaging peers"
n_peers = ack_peers_v3.len(),
"Sending acknowledgement to v3 peers"
);
ctx.send_message(NetworkBridgeTxMessage::SendValidationMessage(
ack_peers_vstaging,
Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::BackedCandidateKnown(
acknowledgement,
),
)
ack_peers_v3,
Versioned::V3(protocol_v3::StatementDistributionMessage::BackedCandidateKnown(
acknowledgement,
))
.into(),
))
.await;
@@ -2293,8 +2289,8 @@ fn post_acknowledgement_statement_messages(
)
.into(),
)),
ValidationVersion::VStaging => messages.push(Versioned::VStaging(
protocol_vstaging::StatementDistributionMessage::Statement(
ValidationVersion::V3 => messages.push(Versioned::V3(
protocol_v3::StatementDistributionMessage::Statement(
relay_parent,
statement.as_unchecked().clone(),
)
@@ -2441,9 +2437,9 @@ fn acknowledgement_and_statement_messages(
let mut messages = match peer.1 {
ValidationVersion::V2 => vec![(vec![peer.0], msg_v2.into())],
ValidationVersion::VStaging => vec![(
ValidationVersion::V3 => vec![(
vec![peer.0],
Versioned::VStaging(protocol_v2::StatementDistributionMessage::BackedCandidateKnown(
Versioned::V3(protocol_v2::StatementDistributionMessage::BackedCandidateKnown(
acknowledgement,
))
.into(),
@@ -2830,7 +2830,7 @@ fn inactive_local_participates_in_grid() {
send_peer_message(
&mut overseer,
peer_a.clone(),
protocol_vstaging::StatementDistributionMessage::BackedCandidateManifest(manifest),
protocol_v3::StatementDistributionMessage::BackedCandidateManifest(manifest),
)
.await;