-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SubscriptionId consists of random part and offer/demand hash
- Loading branch information
1 parent
2701327
commit fd6c148
Showing
8 changed files
with
206 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
mod demand; | ||
mod offer; | ||
mod subscription; | ||
|
||
pub use demand::Demand; | ||
pub use offer::Offer; | ||
|
||
pub use subscription::{SubscriptionId, SubscriptionParseError}; |
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
145 changes: 145 additions & 0 deletions
145
core/market/decentralized/src/db/models/subscription.rs
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use derive_more::Display; | ||
use diesel::backend::Backend; | ||
use diesel::deserialize::{FromSql, Result as DeserializeResult}; | ||
use diesel::serialize::{Output, Result as SerializeResult, ToSql}; | ||
use diesel::sql_types::Text; | ||
use digest::Digest; | ||
use sha3::Sha3_256; | ||
use std::str::FromStr; | ||
use std::io::Write; | ||
use thiserror::Error; | ||
use uuid::Uuid; | ||
|
||
use ya_client::model::ErrorMessage; | ||
use digest::generic_array::GenericArray; | ||
|
||
|
||
#[derive(Error, Debug)] | ||
pub enum SubscriptionParseError { | ||
#[error("Subscription id [{}] has invalid format.", .0)] | ||
InvalidFormat(String), | ||
#[error("Subscription id [{}] contains non hexadecimal characters.", .0)] | ||
NotHexadecimal(String), | ||
#[error("Subscription id [{}] has invalid length.", .0)] | ||
InvalidLength(String), | ||
} | ||
|
||
|
||
#[derive(Display, Debug, Clone, AsExpression, FromSqlRow, Hash, PartialEq, Eq)] | ||
#[display(fmt = "{}-{}", random_id, hash)] | ||
#[sql_type = "Text"] | ||
pub struct SubscriptionId { | ||
random_id: String, | ||
hash: String, | ||
} | ||
|
||
/// TODO: Should be cryptographically strong. | ||
pub fn generate_random_id() -> String { | ||
Uuid::new_v4().to_simple().to_string() | ||
} | ||
|
||
impl SubscriptionId { | ||
pub fn generate_id(properties: &str, constraints: &str, node_id: &str) -> SubscriptionId { | ||
SubscriptionId { | ||
random_id: generate_random_id(), | ||
hash: hash(properties, constraints, node_id), | ||
} | ||
} | ||
} | ||
|
||
fn hash(properties: &str, constraints: &str, node_id: &str) -> String { | ||
let mut hasher = Sha3_256::new(); | ||
|
||
hasher.input(properties); | ||
hasher.input(constraints); | ||
hasher.input(node_id); | ||
|
||
format!("{:x}", hasher.result()) | ||
} | ||
|
||
|
||
impl FromStr for SubscriptionId { | ||
type Err = SubscriptionParseError; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
let elements: Vec<&str> = s | ||
.split('-') | ||
.collect(); | ||
|
||
if elements.len() != 2 { | ||
Err(SubscriptionParseError::InvalidFormat(s.to_string()))?; | ||
} | ||
|
||
if !elements | ||
.iter() | ||
.map(|slice| slice | ||
.chars() | ||
.all(|character| character.is_ascii_hexdigit())) | ||
.all(|result| result == true) { | ||
Err(SubscriptionParseError::NotHexadecimal(s.to_string()))?; | ||
} | ||
|
||
if elements[0].len() != 32 { | ||
Err(SubscriptionParseError::InvalidLength(s.to_string()))?; | ||
} | ||
|
||
if elements[1].len() != 64 { | ||
Err(SubscriptionParseError::InvalidLength(s.to_string()))?; | ||
} | ||
|
||
Ok(SubscriptionId { | ||
random_id: elements[0].to_string(), | ||
hash: elements[1].to_string(), | ||
}) | ||
} | ||
} | ||
|
||
impl<DB> ToSql<Text, DB> for SubscriptionId | ||
where | ||
DB: Backend, | ||
String: ToSql<Text, DB>, | ||
{ | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> SerializeResult { | ||
self.to_string().to_sql(out) | ||
} | ||
} | ||
|
||
impl<DB> FromSql<Text, DB> for SubscriptionId | ||
where | ||
DB: Backend, | ||
String: FromSql<Text, DB>, | ||
{ | ||
fn from_sql(bytes: Option<&DB::RawValue>) -> DeserializeResult<Self> { | ||
let string = String::from_sql(bytes)?; | ||
match SubscriptionId::from_str(&string) { | ||
Ok(subscription) => Ok(subscription), | ||
Err(error) => Err(error.into()), | ||
} | ||
} | ||
} | ||
|
||
impl From<SubscriptionParseError> for ErrorMessage { | ||
fn from(err: SubscriptionParseError) -> Self { | ||
ErrorMessage::new(err.to_string()) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_subscription_from_str() { | ||
let subscription_id = "c76161077d0343ab85ac986eb5f6ea38-edb0016d9f8bafb54540da34f05a8d510de8114488f23916276bdead05509a53"; | ||
|
||
let sub_id = SubscriptionId::from_str(subscription_id).unwrap(); | ||
assert_eq!(sub_id.hash.as_str(), "edb0016d9f8bafb54540da34f05a8d510de8114488f23916276bdead05509a53"); | ||
assert_eq!(sub_id.random_id.as_str(), "c76161077d0343ab85ac986eb5f6ea38"); | ||
|
||
assert_eq!(SubscriptionId::from_str("34324-241").is_ok(), false); | ||
assert_eq!(SubscriptionId::from_str("c76161077d0343ab85ac986eb5f6ea38edb0016d9f8bafb54540da34f05a8d510de8114488f23916276bdead05509a53").is_ok(), false); | ||
assert_eq!(SubscriptionId::from_str("gfht-ertry").is_ok(), false); | ||
} | ||
|
||
} |
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