Skip to content

Commit

Permalink
Compact storage into ecash verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
neacsu committed Sep 17, 2024
1 parent 3fa4863 commit 9a9b0ed
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
1 change: 0 additions & 1 deletion common/authenticator-requests/src/v1/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use crate::error::Error;
use base64::{engine::general_purpose, Engine};
use nym_credentials_interface::CredentialSpendingData;
use nym_wireguard_types::PeerPublicKey;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down
6 changes: 3 additions & 3 deletions common/authenticator-requests/src/v2/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ impl From<v1::registration::InitMessage> for v2::registration::InitMessage {
}
}

impl From<v1::registration::GatewayClient> for v2::registration::FinalMessage {
impl From<v1::registration::GatewayClient> for Box<v2::registration::FinalMessage> {
fn from(gw_client: v1::registration::GatewayClient) -> Self {
Self {
Box::new(v2::registration::FinalMessage {
gateway_client: gw_client.into(),
credential: None,
}
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/authenticator-requests/src/v2/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl AuthenticatorRequest {
(
Self {
version: VERSION,
data: AuthenticatorRequestData::Final(final_message),
data: AuthenticatorRequestData::Final(Box::new(final_message)),
reply_to,
request_id,
},
Expand Down Expand Up @@ -80,6 +80,6 @@ impl AuthenticatorRequest {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum AuthenticatorRequestData {
Initial(InitMessage),
Final(FinalMessage),
Final(Box<FinalMessage>),
QueryBandwidth(PeerPublicKey),
}
4 changes: 4 additions & 0 deletions common/credential-verification/src/ecash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ where
self.shared_state.verification_key(epoch_id).await
}

pub fn storage(&self) -> &S {
&self.shared_state.storage
}

//Check for duplicate pay_info, then check the payment, then insert pay_info if everything succeeded
pub async fn check_payment(
&self,
Expand Down
1 change: 0 additions & 1 deletion gateway/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ impl<St> Gateway<St> {
used_private_network_ips,
peer_response_rx,
)
.with_storage(self.storage.clone())
.with_ecash_verifier(ecash_verifier)
.with_custom_gateway_transceiver(Box::new(transceiver))
.with_shutdown(shutdown.fork("authenticator"))
Expand Down
10 changes: 0 additions & 10 deletions service-providers/authenticator/src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct Authenticator<S> {
custom_topology_provider: Option<Box<dyn TopologyProvider + Send + Sync>>,
custom_gateway_transceiver: Option<Box<dyn GatewayTransceiver + Send + Sync>>,
wireguard_gateway_data: WireguardGatewayData,
storage: Option<S>,
ecash_verifier: Option<Arc<EcashManager<S>>>,
used_private_network_ips: Vec<IpAddr>,
response_rx: UnboundedReceiver<PeerControlResponse>,
Expand All @@ -53,7 +52,6 @@ impl<S: Storage + Clone + 'static> Authenticator<S> {
wait_for_gateway: false,
custom_topology_provider: None,
custom_gateway_transceiver: None,
storage: None,
ecash_verifier: None,
wireguard_gateway_data,
used_private_network_ips,
Expand All @@ -70,13 +68,6 @@ impl<S: Storage + Clone + 'static> Authenticator<S> {
self
}

#[must_use]
#[allow(unused)]
pub fn with_storage(mut self, storage: S) -> Self {
self.storage = Some(storage);
self
}

#[must_use]
#[allow(unused)]
pub fn with_shutdown(mut self, shutdown: TaskClient) -> Self {
Expand Down Expand Up @@ -170,7 +161,6 @@ impl<S: Storage + Clone + 'static> Authenticator<S> {
.collect();
let mixnet_listener = crate::mixnet_listener::MixnetListener::new(
self.config,
self.storage,
free_private_network_ips,
self.wireguard_gateway_data,
self.response_rx,
Expand Down
31 changes: 15 additions & 16 deletions service-providers/authenticator/src/mixnet_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ pub(crate) struct MixnetListener<S> {
// The configuration for the mixnet listener
pub(crate) config: Config,

pub(crate) storage: Option<S>,

// The mixnet client that we use to send and receive packets from the mixnet
pub(crate) mixnet_client: nym_sdk::mixnet::MixnetClient,

Expand All @@ -82,7 +80,6 @@ pub(crate) struct MixnetListener<S> {
impl<S: Storage + Clone + 'static> MixnetListener<S> {
pub fn new(
config: Config,
storage: Option<S>,
free_private_network_ips: PrivateIPs,
wireguard_gateway_data: WireguardGatewayData,
response_rx: UnboundedReceiver<PeerControlResponse>,
Expand All @@ -94,7 +91,6 @@ impl<S: Storage + Clone + 'static> MixnetListener<S> {
IntervalStream::new(tokio::time::interval(DEFAULT_REGISTRATION_TIMEOUT_CHECK));
MixnetListener {
config,
storage,
mixnet_client,
task_handle,
registred_and_free: RwLock::new(RegistredAndFree::new(free_private_network_ips)),
Expand Down Expand Up @@ -249,14 +245,13 @@ impl<S: Storage + Clone + 'static> MixnetListener<S> {
.registration_in_progres
.remove(&final_message.gateway_client.pub_key());

if let (Some(storage), Some(ecash_verifier), Some(credential), Some(client_id)) = (
self.storage.clone(),
if let (Some(ecash_verifier), Some(credential), Some(client_id)) = (
self.ecash_verifier.clone(),
final_message.credential.clone(),
client_id,
) {
if let Err(e) =
Self::credential_verification(storage, ecash_verifier, credential, client_id).await
Self::credential_verification(ecash_verifier, credential, client_id).await
{
self.peer_manager
.remove_peer(&final_message.gateway_client)
Expand All @@ -283,23 +278,27 @@ impl<S: Storage + Clone + 'static> MixnetListener<S> {
}

async fn credential_verification(
storage: S,
ecash_verifier: Arc<EcashManager<S>>,
credential: CredentialSpendingData,
client_id: i64,
) -> Result<i64> {
storage.create_bandwidth_entry(client_id).await?;
let bandwidth = storage.get_available_bandwidth(client_id).await?.ok_or(
AuthenticatorError::InternalError(
ecash_verifier
.storage()
.create_bandwidth_entry(client_id)
.await?;
let bandwidth = ecash_verifier
.storage()
.get_available_bandwidth(client_id)
.await?
.ok_or(AuthenticatorError::InternalError(
"bandwidth entry should have just been created".to_string(),
),
)?;
))?;
let client_bandwidth = ClientBandwidth::new(bandwidth.into());
let mut verifier = CredentialVerifier::new(
CredentialSpendingRequest::new(credential),
ecash_verifier,
ecash_verifier.clone(),
BandwidthStorageManager::new(
storage,
ecash_verifier.storage().clone(),
client_bandwidth,
client_id,
BandwidthFlushingBehaviourConfig {
Expand Down Expand Up @@ -348,7 +347,7 @@ impl<S: Storage + Clone + 'static> MixnetListener<S> {
.await
}
AuthenticatorRequestData::Final(final_msg) => {
self.on_final_request(final_msg, request.request_id, request.reply_to)
self.on_final_request(*final_msg, request.request_id, request.reply_to)
.await
}
AuthenticatorRequestData::QueryBandwidth(peer_public_key) => {
Expand Down

0 comments on commit 9a9b0ed

Please sign in to comment.