@@ -115,7 +115,7 @@ where
115
115
116
116
match party. proceed ( ) {
117
117
ProceedResult :: SendMsg ( msg) => {
118
- messages_queue. send_message ( i, msg) ;
118
+ messages_queue. send_message ( i, msg) ? ;
119
119
continue ' this_party;
120
120
}
121
121
ProceedResult :: NeedsOneMoreMessage => {
@@ -154,20 +154,16 @@ where
154
154
/// Error returned by [`SimulationSync::run`]
155
155
#[ derive( Debug , thiserror:: Error ) ]
156
156
#[ error( transparent) ]
157
- pub struct SimulationSyncError ( Reason ) ;
157
+ pub struct SimulationSyncError ( # [ from ] Reason ) ;
158
158
159
159
#[ derive( Debug , thiserror:: Error ) ]
160
160
enum Reason {
161
161
#[ error( "save incoming message" ) ]
162
162
SaveIncomingMsg ,
163
163
#[ error( "execution error" ) ]
164
164
ExecutionError ( #[ source] crate :: state_machine:: ExecutionError ) ,
165
- }
166
-
167
- impl From < Reason > for SimulationSyncError {
168
- fn from ( err : Reason ) -> Self {
169
- Self ( err)
170
- }
165
+ #[ error( "party #{sender} tried to send a message to non existing party #{recipient}" ) ]
166
+ UnknownRecipient { sender : u16 , recipient : u16 } ,
171
167
}
172
168
173
169
struct MessagesQueue < M > {
@@ -183,7 +179,7 @@ impl<M: Clone> MessagesQueue<M> {
183
179
}
184
180
}
185
181
186
- fn send_message ( & mut self , sender : u16 , msg : Outgoing < M > ) {
182
+ fn send_message ( & mut self , sender : u16 , msg : Outgoing < M > ) -> Result < ( ) , SimulationSyncError > {
187
183
match msg. recipient {
188
184
MessageDestination :: AllParties => {
189
185
let mut msg_ids = self . next_id ..;
@@ -206,14 +202,22 @@ impl<M: Clone> MessagesQueue<M> {
206
202
let next_id = self . next_id ;
207
203
self . next_id += 1 ;
208
204
209
- self . queue [ usize:: from ( destination) ] . push_back ( Incoming {
210
- id : next_id,
211
- sender,
212
- msg_type : MessageType :: P2P ,
213
- msg : msg. msg ,
214
- } )
205
+ self . queue
206
+ . get_mut ( usize:: from ( destination) )
207
+ . ok_or ( Reason :: UnknownRecipient {
208
+ sender,
209
+ recipient : destination,
210
+ } ) ?
211
+ . push_back ( Incoming {
212
+ id : next_id,
213
+ sender,
214
+ msg_type : MessageType :: P2P ,
215
+ msg : msg. msg ,
216
+ } )
215
217
}
216
218
}
219
+
220
+ Ok ( ( ) )
217
221
}
218
222
219
223
fn recv_next_msg ( & mut self , recipient : u16 ) -> Option < Incoming < M > > {
0 commit comments