diff --git a/chain-signatures/node/src/protocol/presignature.rs b/chain-signatures/node/src/protocol/presignature.rs index 233c5498..16aca0a2 100644 --- a/chain-signatures/node/src/protocol/presignature.rs +++ b/chain-signatures/node/src/protocol/presignature.rs @@ -218,7 +218,7 @@ impl PresignatureManager { /// Returns true if the presignature with the given id is already generated pub async fn contains(&self, id: &PresignatureId) -> bool { self.presignature_storage - .write() + .read() .await .contains(id) .await @@ -231,7 +231,7 @@ impl PresignatureManager { /// Returns true if the mine presignature with the given id is already generated pub async fn contains_mine(&self, id: &PresignatureId) -> bool { self.presignature_storage - .write() + .read() .await .contains_mine(id) .await @@ -291,7 +291,7 @@ impl PresignatureManager { /// Returns the number of unspent presignatures available in the manager. pub async fn len_generated(&self) -> usize { self.presignature_storage - .write() + .read() .await .len_generated() .await @@ -304,7 +304,7 @@ impl PresignatureManager { /// Returns the number of unspent presignatures assigned to this node. pub async fn len_mine(&self) -> usize { self.presignature_storage - .write() + .read() .await .len_mine() .await diff --git a/chain-signatures/node/src/protocol/triple.rs b/chain-signatures/node/src/protocol/triple.rs index d7399bce..327d543f 100644 --- a/chain-signatures/node/src/protocol/triple.rs +++ b/chain-signatures/node/src/protocol/triple.rs @@ -162,7 +162,7 @@ impl TripleManager { pub async fn contains(&self, id: &TripleId) -> bool { self.triple_storage - .write() + .read() .await .contains(id) .await @@ -172,7 +172,7 @@ impl TripleManager { pub async fn contains_mine(&self, id: &TripleId) -> bool { self.triple_storage - .write() + .read() .await .contains_mine(id) .await @@ -189,7 +189,8 @@ impl TripleManager { id0: TripleId, id1: TripleId, ) -> Result<(Triple, Triple), GenerationError> { - let triple_0 = match self.triple_storage.write().await.take(&id0).await { + let mut triples = self.triple_storage.write().await; + let triple_0 = match triples.take(&id0).await { Ok(Some(triple)) => triple, Ok(None) => { if self.generators.contains_key(&id0) { @@ -209,10 +210,10 @@ impl TripleManager { } }; - let triple_1 = match self.triple_storage.write().await.take(&id1).await { + let triple_1 = match triples.take(&id1).await { Ok(Some(triple)) => triple, Ok(None) => { - if let Err(e) = self.triple_storage.write().await.insert(triple_0).await { + if let Err(e) = triples.insert(triple_0).await { tracing::warn!(id0, ?e, "failed to insert triple back"); } if self.generators.contains_key(&id1) { @@ -228,7 +229,7 @@ impl TripleManager { } Err(e) => { tracing::warn!(id1, ?e, "failed to take triple"); - if let Err(e) = self.triple_storage.write().await.insert(triple_0).await { + if let Err(e) = triples.insert(triple_0).await { tracing::warn!(id0, ?e, "failed to insert triple back"); } return Err(GenerationError::TripleIsMissing(id1)); @@ -247,11 +248,12 @@ impl TripleManager { /// It is very important to NOT reuse the same triple twice for two different /// protocols. pub async fn take_two_mine(&mut self) -> Option<(Triple, Triple)> { - if self.len_mine().await < 2 { + let mut triples = self.triple_storage.write().await; + if triples.len_mine().await.unwrap_or(0) < 2 { tracing::warn!("not enough mine triples"); return None; } - let triple_0 = match self.triple_storage.write().await.take_mine().await { + let triple_0 = match triples.take_mine().await { Ok(Some(triple)) => triple, Ok(None) => { tracing::warn!("no mine triple left"); @@ -263,13 +265,10 @@ impl TripleManager { } }; - let triple_1 = match self.triple_storage.write().await.take_mine().await { + let triple_1 = match triples.take_mine().await { Ok(Some(triple)) => triple, Ok(None) => { - if let Err(e) = self - .triple_storage - .write() - .await + if let Err(e) = triples .insert_mine(triple_0) .await { @@ -280,10 +279,7 @@ impl TripleManager { } Err(e) => { tracing::warn!(?e, "failed to take mine triple"); - if let Err(e) = self - .triple_storage - .write() - .await + if let Err(e) = triples .insert_mine(triple_0) .await { @@ -304,7 +300,7 @@ impl TripleManager { /// Returns the number of unspent triples available in the manager. pub async fn len_generated(&self) -> usize { self.triple_storage - .write() + .read() .await .len_generated() .await @@ -314,7 +310,7 @@ impl TripleManager { /// Returns the number of unspent triples assigned to this node. pub async fn len_mine(&self) -> usize { self.triple_storage - .write() + .read() .await .len_mine() .await diff --git a/chain-signatures/node/src/storage/presignature_storage.rs b/chain-signatures/node/src/storage/presignature_storage.rs index f8b24684..7720976b 100644 --- a/chain-signatures/node/src/storage/presignature_storage.rs +++ b/chain-signatures/node/src/storage/presignature_storage.rs @@ -48,13 +48,13 @@ impl PresignatureRedisStorage { Ok(()) } - pub async fn contains(&mut self, id: &PresignatureId) -> PresigResult { + pub async fn contains(&self, id: &PresignatureId) -> PresigResult { let mut connection = self.redis_pool.get().await?; let result: bool = connection.hexists(self.presig_key(), id).await?; Ok(result) } - pub async fn contains_mine(&mut self, id: &PresignatureId) -> PresigResult { + pub async fn contains_mine(&self, id: &PresignatureId) -> PresigResult { let mut connection = self.redis_pool.get().await?; let result: bool = connection.sismember(self.mine_key(), id).await?; Ok(result) @@ -87,13 +87,13 @@ impl PresignatureRedisStorage { } } - pub async fn len_generated(&mut self) -> PresigResult { + pub async fn len_generated(&self) -> PresigResult { let mut connection = self.redis_pool.get().await?; let result: usize = connection.hlen(self.presig_key()).await?; Ok(result) } - pub async fn len_mine(&mut self) -> PresigResult { + pub async fn len_mine(&self) -> PresigResult { let mut connection = self.redis_pool.get().await?; let result: usize = connection.scard(self.mine_key()).await?; Ok(result) diff --git a/chain-signatures/node/src/storage/triple_storage.rs b/chain-signatures/node/src/storage/triple_storage.rs index 9d128b21..89c02151 100644 --- a/chain-signatures/node/src/storage/triple_storage.rs +++ b/chain-signatures/node/src/storage/triple_storage.rs @@ -41,13 +41,13 @@ impl TripleRedisStorage { Ok(()) } - pub async fn contains(&mut self, id: &TripleId) -> TripleResult { + pub async fn contains(&self, id: &TripleId) -> TripleResult { let mut conn = self.redis_pool.get().await?; let result: bool = conn.hexists(self.triple_key(), id).await?; Ok(result) } - pub async fn contains_mine(&mut self, id: &TripleId) -> TripleResult { + pub async fn contains_mine(&self, id: &TripleId) -> TripleResult { let mut conn = self.redis_pool.get().await?; let result: bool = conn.sismember(self.mine_key(), id).await?; Ok(result) @@ -79,13 +79,13 @@ impl TripleRedisStorage { } } - pub async fn len_generated(&mut self) -> TripleResult { + pub async fn len_generated(&self) -> TripleResult { let mut conn = self.redis_pool.get().await?; let result: usize = conn.hlen(self.triple_key()).await?; Ok(result) } - pub async fn len_mine(&mut self) -> TripleResult { + pub async fn len_mine(&self) -> TripleResult { let mut conn = self.redis_pool.get().await?; let result: usize = conn.scard(self.mine_key()).await?; Ok(result)