Skip to content

Commit 4af952f

Browse files
committed
Make sure destination party exists in SimulationSync
1 parent 64bce21 commit 4af952f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

round-based/src/simulation/sim_sync.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ where
115115

116116
match party.proceed() {
117117
ProceedResult::SendMsg(msg) => {
118-
messages_queue.send_message(i, msg);
118+
messages_queue.send_message(i, msg)?;
119119
continue 'this_party;
120120
}
121121
ProceedResult::NeedsOneMoreMessage => {
@@ -154,20 +154,16 @@ where
154154
/// Error returned by [`SimulationSync::run`]
155155
#[derive(Debug, thiserror::Error)]
156156
#[error(transparent)]
157-
pub struct SimulationSyncError(Reason);
157+
pub struct SimulationSyncError(#[from] Reason);
158158

159159
#[derive(Debug, thiserror::Error)]
160160
enum Reason {
161161
#[error("save incoming message")]
162162
SaveIncomingMsg,
163163
#[error("execution error")]
164164
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 },
171167
}
172168

173169
struct MessagesQueue<M> {
@@ -183,7 +179,7 @@ impl<M: Clone> MessagesQueue<M> {
183179
}
184180
}
185181

186-
fn send_message(&mut self, sender: u16, msg: Outgoing<M>) {
182+
fn send_message(&mut self, sender: u16, msg: Outgoing<M>) -> Result<(), SimulationSyncError> {
187183
match msg.recipient {
188184
MessageDestination::AllParties => {
189185
let mut msg_ids = self.next_id..;
@@ -206,14 +202,22 @@ impl<M: Clone> MessagesQueue<M> {
206202
let next_id = self.next_id;
207203
self.next_id += 1;
208204

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+
})
215217
}
216218
}
219+
220+
Ok(())
217221
}
218222

219223
fn recv_next_msg(&mut self, recipient: u16) -> Option<Incoming<M>> {

0 commit comments

Comments
 (0)