Skip to content

Commit

Permalink
Use RwLock rather than mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Oct 28, 2024
1 parent c329ae5 commit 6a7d3d8
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/protocol/src/execute_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use num::bigint::BigUint;
use rand_core::{CryptoRngCore, OsRng};
use sp_core::{sr25519, Pair};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, RwLock};
use subxt::utils::AccountId32;
use synedrion::{
ecdsa::VerifyingKey,
Expand Down Expand Up @@ -108,11 +108,11 @@ pub async fn execute_protocol_generic<Res: synedrion::ProtocolResult + 'static>(
// Channel for receiving results of processing messages
let (process_tx, mut process_rx) = mpsc::unbounded_channel();
let current_round = session.current_round();
let session_arc = Arc::new(Mutex::new(session));
let session_arc = Arc::new(RwLock::new(session));

loop {
{
let session = session_arc.lock().unwrap();
let session = session_arc.read().unwrap();
if session.can_finalize(&accum)? {
break;
}
Expand All @@ -127,7 +127,7 @@ pub async fn execute_protocol_generic<Res: synedrion::ProtocolResult + 'static>(
if let ProtocolMessagePayload::MessageBundle(payload) = message.payload.clone() {
if payload.session_id() == &session_id {
let preprocessed = {
let session = session_arc.lock().unwrap();
let session = session_arc.read().unwrap();
// Perform quick checks before proceeding with the verification.
session.preprocess_message(&mut accum, &message.from, *payload)?
};
Expand All @@ -136,7 +136,7 @@ pub async fn execute_protocol_generic<Res: synedrion::ProtocolResult + 'static>(
let session_clone = session_arc.clone();
let tx_clone = process_tx.clone();
tokio::spawn(async move {
let session = session_clone.lock().unwrap();
let session = session_clone.read().unwrap();
let result = session.process_message(&mut OsRng, preprocessed).unwrap();
tx_clone.send(result).unwrap();
});
Expand Down

0 comments on commit 6a7d3d8

Please sign in to comment.