-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Set actual value for bandwidth Also put it as a public attribute, such that it can be actively used by the credential consumer * Switch from sending Attribute structs to sending the actual attribute bytes over the wire * Add atomic bandwidth value to gateway * Consume bandwidth based on the mix packet size * Use Bandwidth struct for specific functionality * Move bandwidth code outside the dependency path of wasm client * Use u64 instead of AtomicU64, as the handling is not parallel
- Loading branch information
Showing
12 changed files
with
196 additions
and
50 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
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
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net> | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::collections::HashMap; | ||
use std::convert::TryFrom; | ||
use std::sync::Arc; | ||
use tokio::sync::RwLock; | ||
|
||
use coconut_interface::Credential; | ||
use credentials::error::Error; | ||
use nymsphinx::DestinationAddressBytes; | ||
|
||
const BANDWIDTH_INDEX: usize = 0; | ||
|
||
pub type BandwidthDatabase = Arc<RwLock<HashMap<DestinationAddressBytes, u64>>>; | ||
|
||
pub fn empty_bandwidth_database() -> BandwidthDatabase { | ||
Arc::new(RwLock::new(HashMap::new())) | ||
} | ||
|
||
pub struct Bandwidth { | ||
value: u64, | ||
} | ||
|
||
impl Bandwidth { | ||
pub fn value(&self) -> u64 { | ||
self.value | ||
} | ||
|
||
pub async fn consume_bandwidth( | ||
bandwidths: &BandwidthDatabase, | ||
remote_address: &DestinationAddressBytes, | ||
consumed: u64, | ||
) -> Result<(), Error> { | ||
if let Some(bandwidth) = bandwidths.write().await.get_mut(remote_address) { | ||
if let Some(res) = bandwidth.checked_sub(consumed) { | ||
*bandwidth = res; | ||
Ok(()) | ||
} else { | ||
Err(Error::BandwidthOverflow(String::from( | ||
"Allocate more bandwidth for consumption", | ||
))) | ||
} | ||
} else { | ||
Err(Error::MissingBandwidth) | ||
} | ||
} | ||
|
||
pub async fn increase_bandwidth( | ||
bandwidths: &BandwidthDatabase, | ||
remote_address: &DestinationAddressBytes, | ||
increase: u64, | ||
) -> Result<(), Error> { | ||
let mut db = bandwidths.write().await; | ||
if let Some(bandwidth) = db.get_mut(remote_address) { | ||
if let Some(new_bandwidth) = bandwidth.checked_add(increase) { | ||
*bandwidth = new_bandwidth; | ||
} else { | ||
return Err(Error::BandwidthOverflow(String::from( | ||
"Use some of the already allocated bandwidth", | ||
))); | ||
} | ||
} else { | ||
db.insert(*remote_address, increase); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl TryFrom<Credential> for Bandwidth { | ||
type Error = Error; | ||
|
||
fn try_from(credential: Credential) -> Result<Self, Self::Error> { | ||
match credential.public_attributes().get(BANDWIDTH_INDEX) { | ||
None => Err(Error::NotEnoughPublicAttributes), | ||
Some(attr) => match <[u8; 8]>::try_from(attr.as_slice()) { | ||
Ok(bandwidth_bytes) => { | ||
let value = u64::from_be_bytes(bandwidth_bytes); | ||
Ok(Self { value }) | ||
} | ||
Err(_) => Err(Error::InvalidBandwidthSize), | ||
}, | ||
} | ||
} | ||
} |
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,6 @@ | ||
// Copyright 2020 - Nym Technologies SA <contact@nymtech.net> | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
mod bandwidth; | ||
pub(crate) mod clients_handler; | ||
pub(crate) mod websocket; |
Oops, something went wrong.