Skip to content

Commit

Permalink
Use ecash credential type for bandwidth value (#4840)
Browse files Browse the repository at this point in the history
* Use ecash credential type for bandwidth value

* Fill explicit default value for args

* Use up-to-date values for tickets

* Fix sdk example default value

* Another default

* Fix sdk test

* Fix TicketTypeRepr default
  • Loading branch information
neacsu committed Sep 5, 2024
1 parent 3c4bd13 commit 18891e5
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion common/commands/src/coconut/issue_ticket_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 1 addition & 10 deletions common/credentials-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,11 @@ impl From<PayInfo> 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,
Expand Down
13 changes: 5 additions & 8 deletions common/network-defaults/src/ecash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@ 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,
V1WireguardExit = 3,
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);

Expand All @@ -385,6 +389,8 @@ where
api_clients: Option<RwLockReadGuard<'_, Vec<EcashApiClient>>>,
) -> Result<bool, EcashTicketError> {
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 => {
Expand Down Expand Up @@ -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?;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion sdk/rust/nym-sdk/examples/bandwidth.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion sdk/rust/nym-sdk/src/bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//!
//! ```no_run
//! use nym_sdk::mixnet::{self, MixnetMessageSender};
//! use nym_credentials_interface::TicketType;
//!
//! #[tokio::main]
//! async fn main() {
Expand All @@ -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();
Expand Down

0 comments on commit 18891e5

Please sign in to comment.