-
Notifications
You must be signed in to change notification settings - Fork 171
feat: time-based uniqueness #2564
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| mod capabilities; | ||
| mod generations; | ||
| mod nonces; | ||
| mod timestamp; | ||
| use std::collections::{BTreeMap, HashSet}; | ||
|
|
||
| use sov_modules_api::{ | ||
|
|
@@ -34,6 +35,14 @@ pub struct Uniqueness<S: Spec> { | |
| #[state] | ||
| pub(crate) nonces: StateMap<CredentialId, u64>, | ||
|
|
||
| /// Buckets of transactions with their expiry timestamp. The | ||
| /// buckets are taken from the first bytes of the TxHash. | ||
| #[state] | ||
| pub(crate) timestamps: StateMap<u16, Vec<(TxHash, u64)>>, | ||
|
Member
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. (non-blocking): Noting for other reviewers that this looks non-breaking since it's the last #[state] item in the module |
||
|
|
||
| #[module] | ||
| pub(crate) chain_state: sov_chain_state::ChainState<S>, | ||
|
|
||
| #[phantom] | ||
| phantom: std::marker::PhantomData<S>, | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| use sov_modules_api::{CredentialId, Spec, TimeStateAccessor, TxHash}; | ||
|
|
||
| use crate::Uniqueness; | ||
|
|
||
| impl<S: Spec> Uniqueness<S> { | ||
| pub(crate) fn check_timestamp_uniqueness( | ||
| &self, | ||
| credential_id: &CredentialId, | ||
| expires: u64, | ||
| transaction_hash: TxHash, | ||
| state: &mut impl TimeStateAccessor, | ||
| ) -> anyhow::Result<()> { | ||
| let current_time = self.chain_state.get_oracle_time_nanos(state)? / 1000; | ||
| anyhow::ensure!( | ||
| expires as u128 > current_time, | ||
| "Time-expired transaction for credential_id {credential_id}: hash {transaction_hash:}" | ||
| ); | ||
|
|
||
| anyhow::ensure!((expires as u128) < current_time + 60_000_000, | ||
|
Member
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. nit: let's extract this into a const |
||
| "Future transaction for credential_id {credential_id}: hash {transaction_hash:} needs to wait." | ||
|
Member
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. nit: it's probably helpful to include the two timestamps that lead to the rejection in this message |
||
| ); | ||
| anyhow::ensure!( | ||
| expires > self.nonces.get(credential_id, state)?.unwrap_or_default(), | ||
| "Nonce-expired transaction for credential_id {credential_id}: hash {transaction_hash:} is invalidated" | ||
|
Member
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. nit: it's probably helpful to include the two timestamps that lead to the rejection in this message |
||
| ); | ||
|
|
||
| let bucket = self | ||
| .timestamps | ||
| .get(&Self::hash_to_bucket(&transaction_hash), state)? | ||
| .unwrap_or_default(); | ||
|
|
||
| if bucket.iter().any(|(tx, _)| transaction_hash == *tx) { | ||
| return Err(anyhow::anyhow!("Duplicate transaction for credential_id {credential_id}: hash {transaction_hash:} has already been seen")); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn mark_timestamp_tx_attempted( | ||
| &mut self, | ||
| credential_id: Option<&CredentialId>, | ||
| expires: u64, | ||
| transaction_hash: TxHash, | ||
| state: &mut impl TimeStateAccessor, | ||
| ) -> anyhow::Result<()> { | ||
| let bucket = Self::hash_to_bucket(&transaction_hash); | ||
| let current_time = self.chain_state.get_oracle_time_nanos(state)? / 1000; | ||
|
|
||
| if let Some(credential_id) = credential_id { | ||
| self.nonces.set(credential_id, &expires, state)?; | ||
| } | ||
|
Comment on lines
+48
to
+50
Member
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. Anyone who uses this functionality is vulnerable to replay attacks. What's the design goal here?
Member
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. Oh, I see this is only intended to increment the nonce. this is probably fine then, but let's add documentation explaining why this is safe |
||
|
|
||
| let mut data = self.timestamps.get(&bucket, state)?.unwrap_or_default(); | ||
| data.retain(|(_, ts)| *ts as u128 > current_time); | ||
| data.push((transaction_hash, expires)); | ||
| Ok(self.timestamps.set(&bucket, &data, state)?) | ||
| } | ||
|
|
||
| /// Use the first two bytes as bucket value. | ||
| fn hash_to_bucket(transaction_hash: &TxHash) -> u16 { | ||
| let ptr: &[u8] = transaction_hash.as_ref(); | ||
| unsafe { core::ptr::read_unaligned(ptr.as_ptr() as *const u16) } | ||
|
Member
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. We have a strong norm of avoiding unsafe unless absolutely necessary. It looks to me like we can just |
||
| } | ||
| } | ||
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.
(non-blocking): flagging that this will be a chain hash breaking change