Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure triples and presignatures are not reused #931

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions chain-signatures/node/src/protocol/presignature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,6 @@ impl PresignatureManager {
participants = ?presig_participants.keys_vec(),
"running: the intersection of participants is less than the threshold"
);

// Insert back the triples to be used later since this active set of
// participants were not able to make use of these triples.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of wasting the triples, we can actually change the order of ```
if let Some((triple0, triple1)) = triple_manager.take_two_mine().await

and 

if presig_participants.len() < self.threshold```
basically check participants.len() first, if that passes, then take_mine

triple_manager.insert_mine(triple0).await;
triple_manager.insert_mine(triple1).await;
} else {
self.generate(
&presig_participants,
Expand Down
37 changes: 37 additions & 0 deletions chain-signatures/node/src/protocol/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ impl TripleManager {

pub async fn insert(&mut self, triple: Triple) {
tracing::debug!(id = triple.id, "inserting triple");
if self.contains(&triple.id).await {
tracing::error!(id = triple.id, "triple already inserted");
return;
}
if self.contains_used(&triple.id).await {
tracing::error!(id = triple.id, "tried to insert used triple");
return;
}
self.gc.remove(&triple.id);
if let Err(e) = self.triple_storage.insert(triple).await {
tracing::warn!(?e, "failed to insert triple");
Expand All @@ -154,12 +162,27 @@ impl TripleManager {

pub async fn insert_mine(&mut self, triple: Triple) {
tracing::debug!(id = triple.id, "inserting mine triple");
if self.contains(&triple.id).await {
tracing::error!(id = triple.id, "mine triple already inserted");
return;
}
if self.contains_used(&triple.id).await {
tracing::error!(id = triple.id, "tried to insert used mine triple");
return;
}
self.gc.remove(&triple.id);
if let Err(e) = self.triple_storage.insert_mine(triple).await {
tracing::warn!(?e, "failed to insert mine triple");
}
}

pub async fn insert_used(&mut self, id: TripleId) {
tracing::debug!(id, "inserting triple to used");
if let Err(e) = self.triple_storage.insert_used(id).await {
tracing::warn!(?e, "failed to insert tripel to used");
}
}

pub async fn contains(&self, id: &TripleId) -> bool {
self.triple_storage
.contains(id)
Expand All @@ -176,6 +199,14 @@ impl TripleManager {
.unwrap_or(false)
}

pub async fn contains_used(&self, id: &TripleId) -> bool {
self.triple_storage
.contains_used(id)
.await
.map_err(|e| tracing::warn!(?e, "failed to check if triple was used"))
.unwrap_or(false)
}

/// Take two unspent triple by theirs id with no way to return it. Only takes
/// if both of them are present.
/// It is very important to NOT reuse the same triple twice for two different
Expand Down Expand Up @@ -232,6 +263,9 @@ impl TripleManager {
}
};

self.insert_used(triple_0.id).await;
self.insert_used(triple_1.id).await;

self.gc.insert(id0, Instant::now());
self.gc.insert(id1, Instant::now());

Expand Down Expand Up @@ -279,6 +313,9 @@ impl TripleManager {
}
};

self.insert_used(triple_0.id).await;
self.insert_used(triple_1.id).await;

self.gc.insert(triple_0.id, Instant::now());
self.gc.insert(triple_1.id, Instant::now());

Expand Down
29 changes: 28 additions & 1 deletion chain-signatures/node/src/storage/triple_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use near_account_id::AccountId;
type TripleResult<T> = std::result::Result<T, anyhow::Error>;

// Can be used to "clear" redis storage in case of a breaking change
const TRIPLE_STORAGE_VERSION: &str = "v1";
const TRIPLE_STORAGE_VERSION: &str = "v2";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incrementing the storage version here to start from scratch.


pub fn init(pool: &Pool, account_id: &AccountId) -> TripleRedisStorage {
TripleRedisStorage {
Expand Down Expand Up @@ -39,6 +39,13 @@ impl TripleRedisStorage {
Ok(())
}

pub async fn insert_used(&self, id: TripleId) -> TripleResult<()> {
let mut conn = self.redis_pool.get().await?;
conn.sadd::<&str, TripleId, ()>(&self.used_key(), id)
.await?;
Ok(())
}

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?;
Expand All @@ -51,6 +58,12 @@ impl TripleRedisStorage {
Ok(result)
}

pub async fn contains_used(&self, id: &TripleId) -> TripleResult<bool> {
let mut conn = self.redis_pool.get().await?;
let result: bool = conn.sismember(self.used_key(), id).await?;
Ok(result)
}

pub async fn take(&self, id: &TripleId) -> TripleResult<Option<Triple>> {
let mut conn = self.redis_pool.get().await?;
if self.contains_mine(id).await? {
Expand Down Expand Up @@ -89,10 +102,17 @@ impl TripleRedisStorage {
Ok(result)
}

pub async fn len_used(&self) -> TripleResult<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same problem as the used presignature. this will grow indefinitely

let mut conn = self.redis_pool.get().await?;
let result: usize = conn.scard(self.used_key()).await?;
Ok(result)
}

pub async fn clear(&self) -> TripleResult<()> {
let mut conn = self.redis_pool.get().await?;
conn.del::<&str, ()>(&self.triple_key()).await?;
conn.del::<&str, ()>(&self.mine_key()).await?;
conn.del::<&str, ()>(&self.used_key()).await?;
Ok(())
}

Expand All @@ -109,6 +129,13 @@ impl TripleRedisStorage {
TRIPLE_STORAGE_VERSION, self.node_account_id
)
}

fn used_key(&self) -> String {
format!(
"triples_used:{}:{}",
TRIPLE_STORAGE_VERSION, self.node_account_id
)
}
}

impl ToRedisArgs for Triple {
Expand Down
Loading