Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
RuntimeApiRequest::Validators(tx),
));
ctx.send_message(query_validators)
.await?;
rx.await?
.map_err::<Error, _>(Into::into)
}
/// Query the hash of the `K` ancestors
async fn query_k_ancestors<Context>(
ctx: &mut Context,
relay_parent: Hash,
k: usize,
) -> Result<Vec<Hash>>
where
Context: SubsystemContext<Message = AvailabilityDistributionMessage>,
{
let (tx, rx) = oneshot::channel();
let query_ancestors = AllMessages::ChainApi(ChainApiMessage::Ancestors {
hash: relay_parent,
k,
response_channel: tx,
});
ctx.send_message(query_ancestors)
.await?;
rx.await?
.map_err::<Error, _>(Into::into)
}
/// Query the session index of a relay parent
async fn query_session_index_for_child<Context>(
ctx: &mut Context,
relay_parent: Hash,
) -> Result<SessionIndex>
where
Context: SubsystemContext<Message = AvailabilityDistributionMessage>,
{
let (tx, rx) = oneshot::channel();
let query_session_idx_for_child = AllMessages::RuntimeApi(RuntimeApiMessage::Request(
relay_parent,
RuntimeApiRequest::SessionIndexForChild(tx),
));
ctx.send_message(query_session_idx_for_child)
.await?;
rx.await?
.map_err::<Error, _>(Into::into)
}
/// Queries up to k ancestors with the constraints of equiv session
async fn query_up_to_k_ancestors_in_same_session<Context>(
ctx: &mut Context,
relay_parent: Hash,
k: usize,
) -> Result<Vec<Hash>>
where
Context: SubsystemContext<Message = AvailabilityDistributionMessage>,
{
// k + 1 since we always query the child's session index
// ordering is [parent, grandparent, greatgrandparent, greatgreatgrandparent, ...]
let ancestors = query_k_ancestors(ctx, relay_parent, k + 1).await?;
let desired_session = query_session_index_for_child(ctx, relay_parent).await?;
// we would only need `ancestors.len() - 1`, but the one extra could avoid a re-alloc
// if the consumer wants to push the `relay_parent` onto it too and does not hurt otherwise
let mut acc = Vec::with_capacity(ancestors.len());
// iterate from youngest to oldest
let mut iter = ancestors.into_iter().peekable();
while let Some(ancestor) = iter.next() {
if let Some(ancestor_parent) = iter.peek() {
let session = query_session_index_for_child(ctx, *ancestor_parent).await?;
if session != desired_session {
break;
}
acc.push(ancestor);
} else {
// either ended up at genesis or the blocks were
// already pruned
break;
}
}
debug_assert!(acc.len() <= k);
Ok(acc)
}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
#[derive(Clone)]
struct MetricsInner {
gossipped_availability_chunks: prometheus::Counter<prometheus::U64>,
}
/// Availability Distribution metrics.
#[derive(Default, Clone)]
pub struct Metrics(Option<MetricsInner>);
impl Metrics {
fn on_chunk_distributed(&self) {
if let Some(metrics) = &self.0 {
metrics.gossipped_availability_chunks.inc();
}
}
}
impl metrics::Metrics for Metrics {
fn try_register(registry: &prometheus::Registry) -> std::result::Result<Self, prometheus::PrometheusError> {
let metrics = MetricsInner {
gossipped_availability_chunks: prometheus::register(
prometheus::Counter::new(
"parachain_gossipped_availability_chunks_total",
"Number of availability chunks gossipped to other peers."
)?,
registry,
)?,
};
Ok(Metrics(Some(metrics)))
}
}