Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d4e231d

Browse files
committed
avail-dist: expressive errors
1 parent 39c756e commit d4e231d

File tree

2 files changed

+122
-52
lines changed
  • node
    • network/availability-distribution/src
    • subsystem/src

2 files changed

+122
-52
lines changed

node/network/availability-distribution/src/lib.rs

Lines changed: 119 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
//! peers. Verified in this context means, the erasure chunks contained merkle proof
2323
//! is checked.
2424
25+
#[deny(unused_extern_crates, unused_results, unused_qualifications)]
26+
2527
use codec::{Decode, Encode};
26-
use futures::{channel::oneshot, FutureExt};
28+
use futures::{channel::oneshot, FutureExt, TryFutureExt};
2729

2830
use sp_core::crypto::Public;
2931
use sp_keystore::{CryptoStore, SyncCryptoStorePtr};
@@ -53,27 +55,71 @@ use polkadot_node_network_protocol::{
5355
NetworkBridgeEvent,
5456
};
5557
use std::collections::{HashMap, HashSet};
56-
use std::io;
5758
use std::iter;
5859
use thiserror::Error;
5960
const TARGET: &'static str = "avad";
6061

6162
#[derive(Debug, Error)]
6263
enum Error {
63-
#[error(transparent)]
64-
Erasure(polkadot_erasure_coding::Error),
65-
66-
#[error(transparent)]
67-
Io(io::Error),
64+
#[error("Sending PendingAvailability query failed")]
65+
QueryPendingAvailabilitySendQuery(#[source] SubsystemError),
66+
#[error("Response channel to obtain PendingAvailability failed")]
67+
QueryPendingAvailabilityResponseChannel(#[source] oneshot::Canceled),
68+
#[error("RuntimeAPI to obtain PendingAvailability failed")]
69+
QueryPendingAvailability(#[source] RuntimeApiError),
70+
71+
#[error("Sending StoreChunk query failed")]
72+
StoreChunkSendQuery(#[source] SubsystemError),
73+
#[error("Response channel to obtain StoreChunk failed")]
74+
StoreChunkResponseChannel(#[source] oneshot::Canceled),
75+
76+
#[error("Sending QueryChunk query failed")]
77+
QueryChunkSendQuery(#[source] SubsystemError),
78+
#[error("Response channel to obtain QueryChunk failed")]
79+
QueryChunkResponseChannel(#[source] oneshot::Canceled),
80+
81+
#[error("Sending QueryAncestors query failed")]
82+
QueryAncestorsSendQuery(#[source] SubsystemError),
83+
#[error("Response channel to obtain QueryAncestors failed")]
84+
QueryAncestorsResponseChannel(#[source] oneshot::Canceled),
85+
#[error("RuntimeAPI to obtain QueryAncestors failed")]
86+
QueryAncestors(#[source] ChainApiError),
87+
88+
89+
#[error("Sending QuerySession query failed")]
90+
QuerySessionSendQuery(#[source] SubsystemError),
91+
#[error("Response channel to obtain QuerySession failed")]
92+
QuerySessionResponseChannel(#[source] oneshot::Canceled),
93+
#[error("RuntimeAPI to obtain QuerySession failed")]
94+
QuerySession(#[source] RuntimeApiError),
95+
96+
#[error("Sending QueryValidators query failed")]
97+
QueryValidatorsSendQuery(#[source] SubsystemError),
98+
#[error("Response channel to obtain QueryValidators failed")]
99+
QueryValidatorsResponseChannel(#[source] oneshot::Canceled),
100+
#[error("RuntimeAPI to obtain QueryValidators failed")]
101+
QueryValidators(#[source] RuntimeApiError),
102+
103+
#[error("Sending AvailabilityCores query failed")]
104+
AvailabilityCoresSendQuery(#[source] SubsystemError),
105+
#[error("Response channel to obtain AvailabilityCores failed")]
106+
AvailabilityCoresResponseChannel(#[source] oneshot::Canceled),
107+
#[error("RuntimeAPI to obtain AvailabilityCores failed")]
108+
AvailabilityCores(#[source] RuntimeApiError),
109+
110+
#[error("Sending AvailabilityCores query failed")]
111+
QueryAvailabilitySendQuery(#[source] SubsystemError),
112+
#[error("Response channel to obtain AvailabilityCores failed")]
113+
QueryAvailabilityResponseChannel(#[source] oneshot::Canceled),
114+
115+
#[error("Sending out a peer report message")]
116+
ReportPeerMessageSend(#[source] SubsystemError),
68117

69-
#[error(transparent)]
70-
Oneshot(oneshot::Canceled),
71-
72-
#[error(transparent)]
73-
RuntimeApi(RuntimeApiError),
118+
#[error("Sending a gossip message")]
119+
TrackedGossipMessage(#[source] SubsystemError),
74120

75-
#[error(transparent)]
76-
ChainApi(ChainApiError),
121+
#[error("Receive channel closed")]
122+
IncomingMessageChannel(#[source] SubsystemError),
77123
}
78124

79125
type Result<T> = std::result::Result<T, Error>;
@@ -508,7 +554,7 @@ where
508554
),
509555
))
510556
.await
511-
.map_err::<Error, _>(Into::into)?;
557+
.map_err(|e| Error::TrackedGossipMessage(e))?;
512558

513559
metrics.on_chunk_distributed();
514560
}
@@ -730,7 +776,10 @@ impl AvailabilityDistributionSubsystem {
730776
// work: process incoming messages from the overseer.
731777
let mut state = ProtocolState::default();
732778
loop {
733-
let message = ctx.recv().await.map_err::<Error, _>(Into::into)?;
779+
let message = ctx
780+
.recv()
781+
.await
782+
.map_err(|e| Error::IncomingMessageChannel(e))?;
734783
match message {
735784
FromOverseer::Communication {
736785
msg: AvailabilityDistributionMessage::NetworkBridgeUpdateV1(event),
@@ -770,9 +819,9 @@ where
770819
fn start(self, ctx: Context) -> SpawnedSubsystem {
771820

772821
let future = self.run(ctx)
773-
.map_err(|e| {
822+
.map_err(|e|
774823
SubsystemError::with_origin("availability-distribution", e)
775-
})
824+
)
776825
.map(|_| ()).boxed();
777826

778827
SpawnedSubsystem {
@@ -881,14 +930,16 @@ where
881930
{
882931
let (tx, rx) = oneshot::channel();
883932
ctx.send_message(AllMessages::RuntimeApi(RuntimeApiMessage::Request(
884-
relay_parent,
885-
RuntimeApiRequest::AvailabilityCores(tx),
886-
)))
887-
.await
888-
.map_err::<Error, _>(Into::into)?;
933+
relay_parent,
934+
RuntimeApiRequest::AvailabilityCores(tx),
935+
)))
936+
.await
937+
.map_err(|e| Error::AvailabilityCoresSendQuery(e))?;
889938

890939
let all_para_ids: Vec<_> = rx
891-
.await??;
940+
.await
941+
.map_err(|e| Error::AvailabilityCoresResponseChannel(e))?
942+
.map_err(|e| Error::AvailabilityCores(e))?;
892943

893944
let occupied_para_ids = all_para_ids
894945
.into_iter()
@@ -915,10 +966,10 @@ where
915966
peer
916967
);
917968
ctx.send_message(AllMessages::NetworkBridge(
918-
NetworkBridgeMessage::ReportPeer(peer, rep),
919-
))
920-
.await
921-
.map_err::<Error, _>(Into::into)
969+
NetworkBridgeMessage::ReportPeer(peer, rep),
970+
))
971+
.await
972+
.map_err(|e| Error::ReportPeerMessageSend(e))
922973
}
923974

924975
/// Query the proof of validity for a particular candidate hash.
@@ -931,10 +982,14 @@ where
931982
{
932983
let (tx, rx) = oneshot::channel();
933984
ctx.send_message(AllMessages::AvailabilityStore(
934-
AvailabilityStoreMessage::QueryDataAvailability(candidate_hash, tx),
935-
))
936-
.await?;
937-
rx.await.map_err::<Error, _>(Into::into)
985+
AvailabilityStoreMessage::QueryDataAvailability(candidate_hash, tx),
986+
))
987+
.await
988+
.map_err(|e| Error::QueryAvailabilitySendQuery(e))
989+
?;
990+
rx
991+
.await
992+
.map_err(|e| Error::QueryAvailabilityResponseChannel(e))
938993
}
939994

940995

@@ -950,8 +1005,10 @@ where
9501005
ctx.send_message(AllMessages::AvailabilityStore(
9511006
AvailabilityStoreMessage::QueryChunk(candidate_hash, validator_index, tx),
9521007
))
953-
.await?;
954-
rx.await.map_err::<Error, _>(Into::into)
1008+
.await
1009+
.map_err(|e| Error::QueryChunkSendQuery(e))?;
1010+
rx.await
1011+
.map_err(|e| Error::QueryChunkResponseChannel(e))
9551012
}
9561013

9571014

@@ -966,9 +1023,13 @@ where
9661023
{
9671024
let (tx, rx) = oneshot::channel();
9681025
ctx.send_message(AllMessages::AvailabilityStore(
969-
AvailabilityStoreMessage::StoreChunk(candidate_hash, validator_index, erasure_chunk, tx),
970-
)).await?;
971-
rx.await.map_err::<Error, _>(Into::into)
1026+
AvailabilityStoreMessage::StoreChunk(candidate_hash, validator_index, erasure_chunk, tx),
1027+
))
1028+
.await
1029+
.map_err(|e| Error::StoreChunkSendQuery(e))?;
1030+
rx
1031+
.await
1032+
.map_err(|e| Error::StoreChunkResponseChannel(e))
9721033
}
9731034

9741035
/// Request the head data for a particular para.
@@ -985,9 +1046,12 @@ where
9851046
relay_parent,
9861047
RuntimeApiRequest::CandidatePendingAvailability(para, tx),
9871048
)))
988-
.await?;
989-
rx.await?
990-
.map_err::<Error, _>(Into::into)
1049+
.await
1050+
.map_err(|e| Error::QueryPendingAvailabilitySendQuery(e))?;
1051+
1052+
rx.await
1053+
.map_err(|e| Error::QueryPendingAvailabilityResponseChannel(e))?
1054+
.map_err(|e| Error::QueryPendingAvailability(e))
9911055
}
9921056

9931057
/// Query the validator set.
@@ -1005,9 +1069,11 @@ where
10051069
));
10061070

10071071
ctx.send_message(query_validators)
1008-
.await?;
1009-
rx.await?
1010-
.map_err::<Error, _>(Into::into)
1072+
.await
1073+
.map_err(|e| Error::QueryValidatorsSendQuery(e))?;
1074+
rx.await
1075+
.map_err(|e| Error::QueryValidatorsResponseChannel(e))?
1076+
.map_err(|e| Error::QueryValidators(e))
10111077
}
10121078

10131079
/// Query the hash of the `K` ancestors
@@ -1027,9 +1093,11 @@ where
10271093
});
10281094

10291095
ctx.send_message(query_ancestors)
1030-
.await?;
1031-
rx.await?
1032-
.map_err::<Error, _>(Into::into)
1096+
.await
1097+
.map_err(|e| Error::QueryAncestorsSendQuery(e))?;
1098+
rx.await
1099+
.map_err(|e| Error::QueryAncestorsResponseChannel(e))?
1100+
.map_err(|e| Error::QueryAncestors(e))
10331101
}
10341102

10351103
/// Query the session index of a relay parent
@@ -1047,9 +1115,11 @@ where
10471115
));
10481116

10491117
ctx.send_message(query_session_idx_for_child)
1050-
.await?;
1051-
rx.await?
1052-
.map_err::<Error, _>(Into::into)
1118+
.await
1119+
.map_err(|e| Error::QuerySessionSendQuery(e))?;
1120+
rx.await
1121+
.map_err(|e| Error::QuerySessionResponseChannel(e))?
1122+
.map_err(|e| Error::QuerySession(e))
10531123
}
10541124

10551125
/// Queries up to k ancestors with the constraints of equiv session

node/subsystem/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ pub enum SubsystemError {
142142
/// An additional anotation tag for the origin of `source`.
143143
origin: &'static str,
144144
/// The wrapped error. Marked as source for tracking the error chain.
145-
#[source] source: Box<Self>
145+
#[source] source: Box<dyn std::error::Error + Send>
146146
},
147147
}
148148

149149
impl SubsystemError {
150150
/// Adds a `str` as `origin` to the given error `err`.
151-
pub fn with_origin(origin: &'static str, err: impl Into<Self>) -> Self {
152-
Self::FromOrigin{ origin, source: Box::new(err.into()) }
151+
pub fn with_origin<E: 'static + Send+ std::error::Error>(origin: &'static str, err: E) -> Self {
152+
Self::FromOrigin{ origin, source: Box::new(err) }
153153
}
154154
}
155155

0 commit comments

Comments
 (0)