Skip to content

Commit 64ed34e

Browse files
committed
chore: use RwLock instead of Mutex
1 parent 98a59ab commit 64ed34e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

bindings/node/src/lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, Mutex};
1+
use std::sync::{Arc, RwLock};
22

33
use napi::{
44
bindgen_prelude::{AsyncTask, BigInt, Env, Error, Uint8Array},
@@ -26,7 +26,7 @@ pub struct AsyncBlobToKzgCommitment {
2626
blob: Uint8Array,
2727

2828
// TODO: make this RwLock parking_lot crate
29-
prover_context: Arc<Mutex<ProverContext>>,
29+
prover_context: Arc<RwLock<ProverContext>>,
3030
}
3131

3232
#[napi]
@@ -38,7 +38,7 @@ impl Task for AsyncBlobToKzgCommitment {
3838
let blob = self.blob.as_ref();
3939
let prover_context = self
4040
.prover_context
41-
.lock()
41+
.read()
4242
.map_err(|_| napi::Error::from_reason("Failed to acquire lock"))?;
4343
let commitment = prover_context.blob_to_kzg_commitment(blob);
4444
Ok(commitment)
@@ -62,7 +62,7 @@ pub struct CellsAndProofs {
6262

6363
pub struct AsyncComputeCellsAndKzgProofs {
6464
blob: Uint8Array,
65-
prover_context: Arc<Mutex<ProverContext>>,
65+
prover_context: Arc<RwLock<ProverContext>>,
6666
}
6767

6868
#[napi]
@@ -74,7 +74,7 @@ impl Task for AsyncComputeCellsAndKzgProofs {
7474
let blob = self.blob.as_ref();
7575
let prover_context = self
7676
.prover_context
77-
.lock()
77+
.read()
7878
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
7979
let (cells, proofs) = prover_context.compute_cells_and_kzg_proofs(blob);
8080

@@ -98,7 +98,7 @@ impl Task for AsyncComputeCellsAndKzgProofs {
9898

9999
pub struct AsyncComputeCells {
100100
blob: Uint8Array,
101-
prover_context: Arc<Mutex<ProverContext>>,
101+
prover_context: Arc<RwLock<ProverContext>>,
102102
}
103103

104104
#[napi]
@@ -110,7 +110,7 @@ impl Task for AsyncComputeCells {
110110
let blob = self.blob.as_ref();
111111
let prover_context = self
112112
.prover_context
113-
.lock()
113+
.read()
114114
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
115115
let cells = prover_context.compute_cells(blob);
116116
Ok(cells)
@@ -127,15 +127,15 @@ impl Task for AsyncComputeCells {
127127

128128
#[napi]
129129
pub struct ProverContextJs {
130-
inner: Arc<Mutex<ProverContext>>,
130+
inner: Arc<RwLock<ProverContext>>,
131131
}
132132

133133
#[napi]
134134
impl ProverContextJs {
135135
#[napi(constructor)]
136136
pub fn new() -> Self {
137137
ProverContextJs {
138-
inner: Arc::new(Mutex::new(ProverContext::new())),
138+
inner: Arc::new(RwLock::new(ProverContext::new())),
139139
}
140140
}
141141

@@ -144,7 +144,7 @@ impl ProverContextJs {
144144
let blob = blob.as_ref();
145145
let prover_context = self
146146
.inner
147-
.lock()
147+
.read()
148148
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
149149
let commitment = prover_context.blob_to_kzg_commitment(blob);
150150
Ok(Uint8Array::from(&commitment))
@@ -166,7 +166,7 @@ impl ProverContextJs {
166166
let blob = blob.as_ref();
167167
let prover_context = self
168168
.inner
169-
.lock()
169+
.read()
170170
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
171171
let (cells, proofs) = prover_context.compute_cells_and_kzg_proofs(blob);
172172

@@ -201,7 +201,7 @@ impl ProverContextJs {
201201
let blob = blob.as_ref();
202202
let prover_context = self
203203
.inner
204-
.lock()
204+
.read()
205205
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
206206
let cells = prover_context.compute_cells(blob);
207207

@@ -227,7 +227,7 @@ pub struct AsyncVerifyCellKzgProof {
227227
cell_id: u64,
228228
cell: Vec<u8>,
229229
proof: Vec<u8>,
230-
verifier_context: Arc<Mutex<VerifierContext>>,
230+
verifier_context: Arc<RwLock<VerifierContext>>,
231231
}
232232

233233
#[napi]
@@ -241,7 +241,7 @@ impl Task for AsyncVerifyCellKzgProof {
241241
let proof = self.proof.as_ref();
242242
let verifier_context = self
243243
.verifier_context
244-
.lock()
244+
.read()
245245
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
246246
Ok(verifier_context.verify_cell_kzg_proof(commitment, self.cell_id, cell, proof))
247247
}
@@ -253,15 +253,15 @@ impl Task for AsyncVerifyCellKzgProof {
253253

254254
#[napi]
255255
pub struct VerifierContextJs {
256-
inner: Arc<Mutex<VerifierContext>>,
256+
inner: Arc<RwLock<VerifierContext>>,
257257
}
258258

259259
#[napi]
260260
impl VerifierContextJs {
261261
#[napi(constructor)]
262262
pub fn new() -> Self {
263263
VerifierContextJs {
264-
inner: Arc::new(Mutex::new(VerifierContext::new())),
264+
inner: Arc::new(RwLock::new(VerifierContext::new())),
265265
}
266266
}
267267

@@ -282,7 +282,7 @@ impl VerifierContextJs {
282282
assert!(signed == false, "cell id should be an unsigned integer");
283283
let verifier_context = self
284284
.inner
285-
.lock()
285+
.read()
286286
.map_err(|_| Error::from_reason("Failed to acquire lock"))?;
287287

288288
let cell_id_u64 = cell_id_value as u64;

0 commit comments

Comments
 (0)