From e0f36537dbd065c90db6df2d024bc0a96b0a3f80 Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Thu, 23 Nov 2023 09:11:52 +0100 Subject: [PATCH] add value to ecash wallets --- .../bandwidth-controller/src/acquire/mod.rs | 3 ++- common/bandwidth-controller/src/lib.rs | 6 +++--- .../client-libs/gateway-client/src/client.rs | 2 +- ...0231123090000_add_value_to_ecash_table.sql | 15 ++++++++++++++ .../credential-storage/src/backends/memory.rs | 15 ++++++++------ .../credential-storage/src/backends/sqlite.rs | 20 ++++++++++--------- .../src/ephemeral_storage.rs | 15 +++++++------- common/credential-storage/src/models.rs | 3 ++- .../src/persistent_storage.rs | 15 +++++++------- common/credential-storage/src/storage.rs | 10 ++++++---- 10 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 common/credential-storage/migrations/20231123090000_add_value_to_ecash_table.sql diff --git a/common/bandwidth-controller/src/acquire/mod.rs b/common/bandwidth-controller/src/acquire/mod.rs index 952e30b803..fba8ef05ac 100644 --- a/common/bandwidth-controller/src/acquire/mod.rs +++ b/common/bandwidth-controller/src/acquire/mod.rs @@ -84,9 +84,10 @@ where obtain_aggregate_signature(&state.params, &state.voucher, &ecash_api_clients, threshold) .await?; storage - .insert_ecash_credential( + .insert_ecash_wallet( ECASH_INFO.to_string(), wallet.to_bs58(), + state.voucher.get_voucher_value(), epoch_id.to_string(), ) .await diff --git a/common/bandwidth-controller/src/lib.rs b/common/bandwidth-controller/src/lib.rs index 83aa4d7f9a..28e7541aa7 100644 --- a/common/bandwidth-controller/src/lib.rs +++ b/common/bandwidth-controller/src/lib.rs @@ -52,7 +52,7 @@ impl BandwidthController { { let ecash_credential = self .storage - .get_next_ecash_credential() + .get_next_ecash_wallet() .await .map_err(|err| BandwidthControllerError::CredentialStorageError(Box::new(err)))?; //let voucher_info = ecash_credential.voucher_info.clone(); @@ -84,7 +84,7 @@ impl BandwidthController { Ok((credential, wallet, ecash_credential.id)) } - pub async fn update_ecash_credential( + pub async fn update_ecash_wallet( &self, wallet: Wallet, id: i64, @@ -98,7 +98,7 @@ impl BandwidthController { let wallet_string = wallet.to_bs58(); self.storage - .update_ecash_credential(wallet_string, id, consumed) + .update_ecash_wallet(wallet_string, id, consumed) .await .map_err(|err| BandwidthControllerError::CredentialStorageError(Box::new(err))) } diff --git a/common/client-libs/gateway-client/src/client.rs b/common/client-libs/gateway-client/src/client.rs index 251e9c10c1..51b4ffbb07 100644 --- a/common/client-libs/gateway-client/src/client.rs +++ b/common/client-libs/gateway-client/src/client.rs @@ -578,7 +578,7 @@ impl GatewayClient { self.bandwidth_controller .as_ref() .unwrap() - .update_ecash_credential(new_wallet, wallet_id) + .update_ecash_wallet(new_wallet, wallet_id) .await?; Ok(()) diff --git a/common/credential-storage/migrations/20231123090000_add_value_to_ecash_table.sql b/common/credential-storage/migrations/20231123090000_add_value_to_ecash_table.sql new file mode 100644 index 0000000000..ee2594b36c --- /dev/null +++ b/common/credential-storage/migrations/20231123090000_add_value_to_ecash_table.sql @@ -0,0 +1,15 @@ +/* + * Copyright 2023 - Nym Technologies SA + * SPDX-License-Identifier: Apache-2.0 + */ + +DROP TABLE ecash_credentials; +CREATE TABLE ecash_wallets +( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + voucher_info TEXT NOT NULL, + wallet TEXT NOT NULL UNIQUE, + value TEXT NOT NULL, + epoch_id TEXT NOT NULL, + consumed BOOLEAN NOT NULL +); \ No newline at end of file diff --git a/common/credential-storage/src/backends/memory.rs b/common/credential-storage/src/backends/memory.rs index e04af7645f..ee070a5137 100644 --- a/common/credential-storage/src/backends/memory.rs +++ b/common/credential-storage/src/backends/memory.rs @@ -1,14 +1,14 @@ // Copyright 2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::models::{CoconutCredential, EcashCredential}; +use crate::models::{CoconutCredential, EcashWallet}; use std::sync::Arc; use tokio::sync::RwLock; #[derive(Clone)] pub struct CoconutCredentialManager { inner: Arc>>, - ecash: Arc>>, + ecash: Arc>>, } impl CoconutCredentialManager { @@ -58,26 +58,29 @@ impl CoconutCredentialManager { /// /// * `voucher_info`: What type of credential it is. /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `value` : The value of the ecash wallet /// * `epoch_id`: The epoch when it was signed. - pub async fn insert_ecash_credential( + pub async fn insert_ecash_wallet( &self, voucher_info: String, wallet: String, + value: String, epoch_id: String, ) { let mut creds = self.ecash.write().await; let id = creds.len() as i64; - creds.push(EcashCredential { + creds.push(EcashWallet { id, voucher_info, wallet, + value, epoch_id, consumed: false, }); } /// Tries to retrieve one of the stored, unused credentials. - pub async fn get_next_ecash_credential(&self) -> Option { + pub async fn get_next_ecash_wallet(&self) -> Option { let creds = self.ecash.read().await; creds.iter().find(|c| !c.consumed).cloned() } @@ -100,7 +103,7 @@ impl CoconutCredentialManager { } } - pub async fn update_ecash_credential(&self, wallet: String, id: i64, consumed: bool) { + pub async fn update_ecash_wallet(&self, wallet: String, id: i64, consumed: bool) { let mut creds = self.ecash.write().await; if let Some(cred) = creds.get_mut(id as usize) { cred.wallet = wallet; diff --git a/common/credential-storage/src/backends/sqlite.rs b/common/credential-storage/src/backends/sqlite.rs index f9473d77dc..97199596c7 100644 --- a/common/credential-storage/src/backends/sqlite.rs +++ b/common/credential-storage/src/backends/sqlite.rs @@ -1,7 +1,7 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::models::{CoconutCredential, EcashCredential}; +use crate::models::{CoconutCredential, EcashWallet}; #[derive(Clone)] pub struct CoconutCredentialManager { @@ -51,17 +51,19 @@ impl CoconutCredentialManager { /// /// * `voucher_info`: What type of credential it is. /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `value` : The value of the ecash wallet /// * `epoch_id`: The epoch when it was signed. - pub async fn insert_ecash_credential( + pub async fn insert_ecash_wallet( &self, voucher_info: String, wallet: String, + value: String, epoch_id: String, ) -> Result<(), sqlx::Error> { sqlx::query!( - "INSERT INTO ecash_credentials(voucher_info, wallet, epoch_id, consumed) VALUES (?, ?, ?, ?)", - voucher_info, wallet, epoch_id, false + "INSERT INTO ecash_wallets(voucher_info, wallet, value, epoch_id, consumed) VALUES (?, ?, ?, ?, ?)", + voucher_info, wallet, value, epoch_id, false ) .execute(&self.connection_pool) .await?; @@ -69,10 +71,10 @@ impl CoconutCredentialManager { } /// Tries to retrieve one of the stored, unused credentials. - pub async fn get_next_ecash_credential(&self) -> Result, sqlx::Error> { + pub async fn get_next_ecash_wallet(&self) -> Result, sqlx::Error> { sqlx::query_as!( - EcashCredential, - "SELECT * FROM ecash_credentials WHERE NOT consumed" + EcashWallet, + "SELECT * FROM ecash_wallets WHERE NOT consumed" ) .fetch_optional(&self.connection_pool) .await @@ -113,14 +115,14 @@ impl CoconutCredentialManager { /// * `id`: Database id. /// * `consumed` : If the wallet is entirely consumed /// - pub async fn update_ecash_credential( + pub async fn update_ecash_wallet( &self, wallet: String, id: i64, consumed: bool, ) -> Result<(), sqlx::Error> { sqlx::query!( - "UPDATE ecash_credentials SET wallet = ?, consumed = ? WHERE id = ?", + "UPDATE ecash_wallets SET wallet = ?, consumed = ? WHERE id = ?", wallet, consumed, id diff --git a/common/credential-storage/src/ephemeral_storage.rs b/common/credential-storage/src/ephemeral_storage.rs index ae927c29d0..d5ecda9ccc 100644 --- a/common/credential-storage/src/ephemeral_storage.rs +++ b/common/credential-storage/src/ephemeral_storage.rs @@ -3,7 +3,7 @@ use crate::backends::memory::CoconutCredentialManager; use crate::error::StorageError; -use crate::models::{CoconutCredential, EcashCredential}; +use crate::models::{CoconutCredential, EcashWallet}; use crate::storage::Storage; use async_trait::async_trait; @@ -50,23 +50,24 @@ impl Storage for EphemeralStorage { Ok(()) } - async fn insert_ecash_credential( + async fn insert_ecash_wallet( &self, voucher_info: String, wallet: String, + value: String, epoch_id: String, ) -> Result<(), StorageError> { self.coconut_credential_manager - .insert_ecash_credential(voucher_info, wallet, epoch_id) + .insert_ecash_wallet(voucher_info, wallet, value, epoch_id) .await; Ok(()) } - async fn get_next_ecash_credential(&self) -> Result { + async fn get_next_ecash_wallet(&self) -> Result { let credential = self .coconut_credential_manager - .get_next_ecash_credential() + .get_next_ecash_wallet() .await .ok_or(StorageError::NoCredential)?; @@ -91,14 +92,14 @@ impl Storage for EphemeralStorage { Ok(()) } - async fn update_ecash_credential( + async fn update_ecash_wallet( &self, wallet: String, id: i64, consumed: bool, ) -> Result<(), StorageError> { self.coconut_credential_manager - .update_ecash_credential(wallet, id, consumed) + .update_ecash_wallet(wallet, id, consumed) .await; Ok(()) } diff --git a/common/credential-storage/src/models.rs b/common/credential-storage/src/models.rs index 08a92014ab..1c304d0e1a 100644 --- a/common/credential-storage/src/models.rs +++ b/common/credential-storage/src/models.rs @@ -15,11 +15,12 @@ pub struct CoconutCredential { } #[derive(Clone)] -pub struct EcashCredential { +pub struct EcashWallet { #[allow(dead_code)] pub id: i64, pub voucher_info: String, pub wallet: String, + pub value: String, pub epoch_id: String, pub consumed: bool, } diff --git a/common/credential-storage/src/persistent_storage.rs b/common/credential-storage/src/persistent_storage.rs index 3b96775786..6224f8e936 100644 --- a/common/credential-storage/src/persistent_storage.rs +++ b/common/credential-storage/src/persistent_storage.rs @@ -3,7 +3,7 @@ use crate::error::StorageError; use crate::storage::Storage; -use crate::{backends::sqlite::CoconutCredentialManager, models::EcashCredential}; +use crate::{backends::sqlite::CoconutCredentialManager, models::EcashWallet}; use crate::models::CoconutCredential; use async_trait::async_trait; @@ -81,23 +81,24 @@ impl Storage for PersistentStorage { Ok(()) } - async fn insert_ecash_credential( + async fn insert_ecash_wallet( &self, voucher_info: String, wallet: String, + value: String, epoch_id: String, ) -> Result<(), StorageError> { self.coconut_credential_manager - .insert_ecash_credential(voucher_info, wallet, epoch_id) + .insert_ecash_wallet(voucher_info, wallet, value, epoch_id) .await?; Ok(()) } - async fn get_next_ecash_credential(&self) -> Result { + async fn get_next_ecash_wallet(&self) -> Result { let credential = self .coconut_credential_manager - .get_next_ecash_credential() + .get_next_ecash_wallet() .await? .ok_or(StorageError::NoCredential)?; @@ -122,14 +123,14 @@ impl Storage for PersistentStorage { Ok(()) } - async fn update_ecash_credential( + async fn update_ecash_wallet( &self, wallet: String, id: i64, consumed: bool, ) -> Result<(), StorageError> { self.coconut_credential_manager - .update_ecash_credential(wallet, id, consumed) + .update_ecash_wallet(wallet, id, consumed) .await?; Ok(()) } diff --git a/common/credential-storage/src/storage.rs b/common/credential-storage/src/storage.rs index 0be286232b..981465d631 100644 --- a/common/credential-storage/src/storage.rs +++ b/common/credential-storage/src/storage.rs @@ -1,7 +1,7 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use crate::models::{CoconutCredential, EcashCredential}; +use crate::models::{CoconutCredential, EcashWallet}; use async_trait::async_trait; use std::error::Error; @@ -35,16 +35,18 @@ pub trait Storage: Send + Sync { /// /// * `voucher_info`: What type of credential it is. /// * `signature`: Ecash wallet credential in the form of a wallet. + /// * `value` : The value of the ecash wallet /// * `epoch_id`: The epoch when it was signed. - async fn insert_ecash_credential( + async fn insert_ecash_wallet( &self, voucher_info: String, signature: String, + value: String, epoch_id: String, ) -> Result<(), Self::StorageError>; /// Tries to retrieve one of the stored, unused credentials. - async fn get_next_ecash_credential(&self) -> Result; + async fn get_next_ecash_wallet(&self) -> Result; /// Tries to retrieve one of the stored, unused credentials. async fn get_next_coconut_credential(&self) -> Result; @@ -64,7 +66,7 @@ pub trait Storage: Send + Sync { /// * `id`: Id of the credential to be updated. /// * `consumed`: if the credential is consumed or not /// - async fn update_ecash_credential( + async fn update_ecash_wallet( &self, wallet: String, id: i64,