diff --git a/common/commands/src/coconut/issue_ticket_book.rs b/common/commands/src/coconut/issue_ticket_book.rs index c6b0997ba5..79eb67ac0c 100644 --- a/common/commands/src/coconut/issue_ticket_book.rs +++ b/common/commands/src/coconut/issue_ticket_book.rs @@ -14,7 +14,7 @@ use std::path::PathBuf; #[derive(Debug, Parser)] pub struct Args { /// Specify which type of ticketbook should be issued - #[clap(long, default_value_t = TicketType::default())] + #[clap(long, default_value_t = TicketType::V1MixnetEntry)] pub(crate) ticketbook_type: TicketType, /// Config file of the client that is supposed to use the credential. diff --git a/common/credentials-interface/src/lib.rs b/common/credentials-interface/src/lib.rs index 85938c3c61..270cb5f658 100644 --- a/common/credentials-interface/src/lib.rs +++ b/common/credentials-interface/src/lib.rs @@ -221,20 +221,11 @@ impl From for NymPayInfo { } #[derive( - Default, - Copy, - Clone, - Debug, - PartialEq, - Serialize, - Deserialize, - strum::Display, - strum::EnumString, + Copy, Clone, Debug, PartialEq, Serialize, Deserialize, strum::Display, strum::EnumString, )] #[serde(rename_all = "kebab-case")] #[strum(serialize_all = "kebab-case")] pub enum TicketType { - #[default] V1MixnetEntry, V1MixnetExit, V1WireguardEntry, diff --git a/common/network-defaults/src/ecash.rs b/common/network-defaults/src/ecash.rs index 21d1ddfe85..ee5e7b5c61 100644 --- a/common/network-defaults/src/ecash.rs +++ b/common/network-defaults/src/ecash.rs @@ -9,10 +9,9 @@ pub const TICKETBOOK_SIZE: u64 = 50; /// This type is defined mostly for the purposes of having constants (like sizes) associated with given variants /// It's not meant to be serialised or have any fancy traits defined on it (in this crate) -#[derive(Default, Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq)] #[repr(u8)] pub enum TicketTypeRepr { - #[default] V1MixnetEntry = 0, V1MixnetExit = 1, V1WireguardEntry = 2, @@ -20,12 +19,10 @@ pub enum TicketTypeRepr { } impl TicketTypeRepr { - pub const WIREGUARD_ENTRY_TICKET_SIZE: u64 = 500 * 1024 * 1024; // 500 MB - - // TBD: - pub const WIREGUARD_EXIT_TICKET_SIZE: u64 = Self::WIREGUARD_ENTRY_TICKET_SIZE; - pub const MIXNET_ENTRY_TICKET_SIZE: u64 = Self::WIREGUARD_ENTRY_TICKET_SIZE; - pub const MIXNET_EXIT_TICKET_SIZE: u64 = Self::WIREGUARD_ENTRY_TICKET_SIZE; + pub const WIREGUARD_ENTRY_TICKET_SIZE: u64 = 512 * 1024 * 1024; // 512 MB + pub const WIREGUARD_EXIT_TICKET_SIZE: u64 = 512 * 1024 * 1024; // 512 MB + pub const MIXNET_ENTRY_TICKET_SIZE: u64 = 128 * 1024 * 1024; // 128 MB + pub const MIXNET_EXIT_TICKET_SIZE: u64 = 128 * 1024 * 1024; // 128 MB /// How much bandwidth (in bytes) one ticket can grant pub const fn bandwidth_value(&self) -> u64 { diff --git a/common/nym_offline_compact_ecash/benches/benchmarks_ecash_e2e.rs b/common/nym_offline_compact_ecash/benches/benchmarks_ecash_e2e.rs index f7ea0cf9f3..e57eaafa03 100644 --- a/common/nym_offline_compact_ecash/benches/benchmarks_ecash_e2e.rs +++ b/common/nym_offline_compact_ecash/benches/benchmarks_ecash_e2e.rs @@ -32,7 +32,7 @@ fn bench_compact_ecash(c: &mut Criterion) { let spend_date = 1701907200; // Dec 07 2023 00:00:00 let expiration_date = 1702166400; // Dec 10 2023 00:00:00 - let encoded_ticket_type = TicketTypeRepr::default() as u8; + let encoded_ticket_type = TicketTypeRepr::V1MixnetEntry as u8; let case = BenchCase { num_authorities: 100, diff --git a/gateway/src/node/client_handling/websocket/connection_handler/authenticated.rs b/gateway/src/node/client_handling/websocket/connection_handler/authenticated.rs index 51117b2879..d0f039fcca 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler/authenticated.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler/authenticated.rs @@ -18,7 +18,7 @@ use futures::{ FutureExt, StreamExt, }; use nym_credentials::ecash::utils::{ecash_today, EcashTime}; -use nym_credentials_interface::{ClientTicket, CredentialSpendingData}; +use nym_credentials_interface::{ClientTicket, CredentialSpendingData, TicketType}; use nym_gateway_requests::models::CredentialSpendingRequest; use nym_gateway_requests::{ types::{BinaryRequest, ServerResponse}, @@ -104,6 +104,9 @@ pub enum RequestHandlingError { "the received payment contained more than a single ticket. that's currently not supported" )] MultipleTickets, + + #[error("{0}")] + UnknownTicketType(#[from] nym_credentials_interface::UnknownTicketType), } impl RequestHandlingError { @@ -414,6 +417,7 @@ where // check if the credential hasn't been spent before let serial_number = credential.data.encoded_serial_number(); + let credential_type = TicketType::try_from_encoded(credential.data.payment.t_type)?; if credential.data.payment.spend_value != 1 { return Err(RequestHandlingError::MultipleTickets); @@ -433,7 +437,7 @@ where // TODO: double storing? // self.store_spent_credential(serial_number_bs58).await?; - let bandwidth = Bandwidth::ticket_amount(Default::default()); + let bandwidth = Bandwidth::ticket_amount(credential_type.into()); self.increase_bandwidth(bandwidth, spend_date).await?; diff --git a/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs b/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs index 16670fe132..7cc478cc9a 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler/ecash/credential_sender.rs @@ -12,7 +12,7 @@ use futures::channel::mpsc::UnboundedReceiver; use futures::{Stream, StreamExt}; use nym_api_requests::constants::MIN_BATCH_REDEMPTION_DELAY; use nym_api_requests::ecash::models::{BatchRedeemTicketsBody, VerifyEcashTicketBody}; -use nym_credentials_interface::ClientTicket; +use nym_credentials_interface::{ClientTicket, TicketType}; use nym_gateway_storage::Storage; use nym_validator_client::nym_api::EpochId; use nym_validator_client::nyxd::contract_traits::{ @@ -362,10 +362,14 @@ where } #[instrument(skip(self))] - async fn revoke_ticket_bandwidth(&self, ticket_id: i64) -> Result<(), EcashTicketError> { + async fn revoke_ticket_bandwidth( + &self, + ticket_id: i64, + ticket_type: TicketType, + ) -> Result<(), EcashTicketError> { warn!("revoking bandwidth associated with ticket {ticket_id} since it failed verification"); - let bytes_to_revoke = Bandwidth::ticket_amount(Default::default()).value() as f32 + let bytes_to_revoke = Bandwidth::ticket_amount(ticket_type.into()).value() as f32 * self.config.revocation_bandwidth_penalty; let to_revoke_bi2 = bibytes2(bytes_to_revoke as f64); @@ -385,6 +389,8 @@ where api_clients: Option>>, ) -> Result { let ticket_id = pending.ticket.ticket_id; + let ticket_type = + TicketType::try_from_encoded(pending.ticket.spending_data.payment.t_type)?; let api_clients = match api_clients { Some(clients) => clients, None => { @@ -444,7 +450,7 @@ where .storage .update_rejected_ticket(pending.ticket.ticket_id) .await?; - self.revoke_ticket_bandwidth(pending.ticket.ticket_id) + self.revoke_ticket_bandwidth(pending.ticket.ticket_id, ticket_type) .await?; } diff --git a/gateway/src/node/client_handling/websocket/connection_handler/ecash/error.rs b/gateway/src/node/client_handling/websocket/connection_handler/ecash/error.rs index c39ad89720..9e025316a7 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler/ecash/error.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler/ecash/error.rs @@ -28,6 +28,9 @@ pub enum EcashTicketError { #[error("provided payinfo's timestamp is invalid")] InvalidPayInfoTimestamp, + #[error("{0}")] + InvalidTicketType(#[from] nym_credentials_interface::UnknownTicketType), + #[error("received payinfo is a duplicate")] DuplicatePayInfo, diff --git a/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs b/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs index b4a2befa02..41bb39a6f8 100644 --- a/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs +++ b/nym-validator-rewarder/src/rewarder/credential_issuance/monitor.rs @@ -331,6 +331,7 @@ mod tests { use super::*; use nym_compact_ecash::ttp_keygen; use nym_credentials::IssuanceTicketBook; + use nym_credentials_interface::TicketType; use nym_crypto::asymmetric::{ed25519, identity}; use rand_chacha::rand_core::{RngCore, SeedableRng}; @@ -349,7 +350,8 @@ mod tests { identity::PrivateKey::from_bytes(&identity_keypair.private_key().to_bytes()).unwrap(); let identifier = [44u8; 32]; - let issuance = IssuanceTicketBook::new(deposit_id, identifier, id_priv, Default::default()); + let issuance = + IssuanceTicketBook::new(deposit_id, identifier, id_priv, TicketType::V1MixnetEntry); let signing_data = issuance.prepare_for_signing(); let request = issuance.create_blind_sign_request_body(&signing_data); diff --git a/sdk/rust/nym-sdk/examples/bandwidth.rs b/sdk/rust/nym-sdk/examples/bandwidth.rs index b104ee51ea..bc30d1f65e 100644 --- a/sdk/rust/nym-sdk/examples/bandwidth.rs +++ b/sdk/rust/nym-sdk/examples/bandwidth.rs @@ -1,4 +1,5 @@ use futures::StreamExt; +use nym_credentials_interface::TicketType; use nym_network_defaults::setup_env; use nym_sdk::mixnet; use nym_sdk::mixnet::MixnetMessageSender; @@ -19,7 +20,7 @@ async fn main() -> anyhow::Result<()> { .build()?; let bandwidth_client = mixnet_client - .create_bandwidth_client(mnemonic, Default::default()) + .create_bandwidth_client(mnemonic, TicketType::V1MixnetEntry) .await?; // Get a bandwidth credential for the mixnet_client diff --git a/sdk/rust/nym-sdk/src/bandwidth.rs b/sdk/rust/nym-sdk/src/bandwidth.rs index 2152319f97..ef619429de 100644 --- a/sdk/rust/nym-sdk/src/bandwidth.rs +++ b/sdk/rust/nym-sdk/src/bandwidth.rs @@ -7,6 +7,7 @@ //! //! ```no_run //! use nym_sdk::mixnet::{self, MixnetMessageSender}; +//! use nym_credentials_interface::TicketType; //! //! #[tokio::main] //! async fn main() { @@ -15,7 +16,7 @@ //! .build() //! .unwrap(); //! -//! let bandwidth_client = mixnet_client.create_bandwidth_client(String::from("my super secret mnemonic"), Default::default()).await.unwrap(); +//! let bandwidth_client = mixnet_client.create_bandwidth_client(String::from("my super secret mnemonic"), TicketType::V1MixnetEntry).await.unwrap(); //! //! // Get a bandwidth credential for the mixnet_client //! bandwidth_client.acquire().await.unwrap();