Skip to content

Commit

Permalink
Fix locks being acquired incorrectly (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest authored Oct 30, 2024
1 parent df919c8 commit 566a134
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
8 changes: 4 additions & 4 deletions chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
34 changes: 15 additions & 19 deletions chain-signatures/node/src/protocol/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl TripleManager {

pub async fn contains(&self, id: &TripleId) -> bool {
self.triple_storage
.write()
.read()
.await
.contains(id)
.await
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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));
Expand All @@ -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");
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions chain-signatures/node/src/storage/presignature_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ impl PresignatureRedisStorage {
Ok(())
}

pub async fn contains(&mut self, id: &PresignatureId) -> PresigResult<bool> {
pub async fn contains(&self, id: &PresignatureId) -> PresigResult<bool> {
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<bool> {
pub async fn contains_mine(&self, id: &PresignatureId) -> PresigResult<bool> {
let mut connection = self.redis_pool.get().await?;
let result: bool = connection.sismember(self.mine_key(), id).await?;
Ok(result)
Expand Down Expand Up @@ -87,13 +87,13 @@ impl PresignatureRedisStorage {
}
}

pub async fn len_generated(&mut self) -> PresigResult<usize> {
pub async fn len_generated(&self) -> PresigResult<usize> {
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<usize> {
pub async fn len_mine(&self) -> PresigResult<usize> {
let mut connection = self.redis_pool.get().await?;
let result: usize = connection.scard(self.mine_key()).await?;
Ok(result)
Expand Down
8 changes: 4 additions & 4 deletions chain-signatures/node/src/storage/triple_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ impl TripleRedisStorage {
Ok(())
}

pub async fn contains(&mut self, id: &TripleId) -> TripleResult<bool> {
pub async fn contains(&self, id: &TripleId) -> TripleResult<bool> {
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<bool> {
pub async fn contains_mine(&self, id: &TripleId) -> TripleResult<bool> {
let mut conn = self.redis_pool.get().await?;
let result: bool = conn.sismember(self.mine_key(), id).await?;
Ok(result)
Expand Down Expand Up @@ -79,13 +79,13 @@ impl TripleRedisStorage {
}
}

pub async fn len_generated(&mut self) -> TripleResult<usize> {
pub async fn len_generated(&self) -> TripleResult<usize> {
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<usize> {
pub async fn len_mine(&self) -> TripleResult<usize> {
let mut conn = self.redis_pool.get().await?;
let result: usize = conn.scard(self.mine_key()).await?;
Ok(result)
Expand Down

0 comments on commit 566a134

Please sign in to comment.