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
@@ -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,