Use ecash credential type for bandwidth value (#4840)

* 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
This commit is contained in:
Bogdan-Ștefan Neacşu
2024-09-05 13:19:19 +02:00
committed by GitHub
parent 3c4bd13c2a
commit 18891e5f20
10 changed files with 34 additions and 29 deletions
@@ -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.
+1 -10
View File
@@ -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,
+5 -8
View File
@@ -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 {
@@ -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,
@@ -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?;
@@ -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<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 => {
@@ -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?;
}
@@ -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,
@@ -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);
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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();