-
Notifications
You must be signed in to change notification settings - Fork 17
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
volovyks
wants to merge
5
commits into
develop
Choose a base branch
from
serhii/prevent-triple-and-presignature-reusage
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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?; | ||
|
@@ -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? { | ||
|
@@ -89,10 +102,17 @@ impl TripleRedisStorage { | |
Ok(result) | ||
} | ||
|
||
pub async fn len_used(&self) -> TripleResult<usize> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||
} | ||
|
||
|
@@ -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 { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
if presig_participants.len() < self.threshold```
basically check participants.len() first, if that passes, then take_mine