add value to ecash wallets

This commit is contained in:
Simon Wicky
2023-11-23 09:11:52 +01:00
committed by durch
parent 2bb130c204
commit e0f36537db
10 changed files with 65 additions and 39 deletions
@@ -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
+3 -3
View File
@@ -52,7 +52,7 @@ impl<C, St: Storage> BandwidthController<C, St> {
{
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<C, St: Storage> BandwidthController<C, St> {
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<C, St: Storage> BandwidthController<C, St> {
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)))
}
@@ -578,7 +578,7 @@ impl<C, St> GatewayClient<C, St> {
self.bandwidth_controller
.as_ref()
.unwrap()
.update_ecash_credential(new_wallet, wallet_id)
.update_ecash_wallet(new_wallet, wallet_id)
.await?;
Ok(())
@@ -0,0 +1,15 @@
/*
* Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
* 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
);
@@ -1,14 +1,14 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// 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<RwLock<Vec<CoconutCredential>>>,
ecash: Arc<RwLock<Vec<EcashCredential>>>,
ecash: Arc<RwLock<Vec<EcashWallet>>>,
}
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<EcashCredential> {
pub async fn get_next_ecash_wallet(&self) -> Option<EcashWallet> {
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;
@@ -1,7 +1,7 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// 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<Option<EcashCredential>, sqlx::Error> {
pub async fn get_next_ecash_wallet(&self) -> Result<Option<EcashWallet>, 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
@@ -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<EcashCredential, StorageError> {
async fn get_next_ecash_wallet(&self) -> Result<EcashWallet, StorageError> {
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(())
}
+2 -1
View File
@@ -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,
}
@@ -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<EcashCredential, StorageError> {
async fn get_next_ecash_wallet(&self) -> Result<EcashWallet, StorageError> {
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(())
}
+6 -4
View File
@@ -1,7 +1,7 @@
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
// 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<EcashCredential, Self::StorageError>;
async fn get_next_ecash_wallet(&self) -> Result<EcashWallet, Self::StorageError>;
/// Tries to retrieve one of the stored, unused credentials.
async fn get_next_coconut_credential(&self) -> Result<CoconutCredential, Self::StorageError>;
@@ -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,