Skip to content

Commit b975d08

Browse files
authored
Remove old free credential handle (#5864)
* Set cached storage counters to 0 (#5812) * Set cached storage counters to 0 * u64 to i64 log possible error * Check addition too Debug commit Remove more data from wg storage peer Put actual ticket type in storage Simplify add peer Finish rebase Pass defguard Peer Cache less data for consumption GatewayStorage traits Wg API trait Mock test structures Unit test for peer controller EcashManager trait Init test of Authenticator Remove peer test * Fix windows different API * Use make_bincode_serializer like in other places * Add log_slow_statements to gateway storage * Use correct LevelFilter * Fix clippy * More win fix * Win clippy * Use two error variants more * Use only one Arc<RwLock<T>> instead of many more * Remove commented test * Specific trait import
1 parent 8e44f9f commit b975d08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1598
-1554
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ digest = "0.10.7"
234234
dirs = "5.0"
235235
doc-comment = "0.3"
236236
dotenvy = "0.15.6"
237+
dyn-clone = "1.0.19"
237238
ecdsa = "0.16"
238239
ed25519-dalek = "2.1"
239240
encoding_rs = "0.8.35"

common/authenticator-requests/src/v5/registration.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub type HmacSha256 = Hmac<Sha256>;
2828
pub type Nonce = u64;
2929
pub type Taken = Option<SystemTime>;
3030

31-
pub const BANDWIDTH_CAP_PER_DAY: u64 = 250 * 1024 * 1024 * 1024; // 250 GB
32-
3331
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
3432
pub struct IpPair {
3533
pub ipv4: Ipv4Addr,

common/credential-verification/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ rust-version.workspace = true
1111
readme.workspace = true
1212

1313
[dependencies]
14+
async-trait = { workspace = true }
1415
bs58 = { workspace = true }
1516
cosmwasm-std = { workspace = true }
1617
cw-utils = { workspace = true }
18+
dyn-clone = { workspace = true }
1719
futures = { workspace = true }
1820
rand = { workspace = true }
1921
si-scale = { workspace = true }

common/credential-verification/src/bandwidth_storage_manager.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@ use crate::ClientBandwidth;
77
use nym_credentials::ecash::utils::ecash_today;
88
use nym_credentials_interface::Bandwidth;
99
use nym_gateway_requests::ServerResponse;
10-
use nym_gateway_storage::GatewayStorage;
10+
use nym_gateway_storage::traits::BandwidthGatewayStorage;
1111
use si_scale::helpers::bibytes2;
1212
use time::OffsetDateTime;
1313
use tracing::*;
1414

1515
const FREE_TESTNET_BANDWIDTH_VALUE: Bandwidth = Bandwidth::new_unchecked(64 * 1024 * 1024 * 1024); // 64GB
1616

17-
#[derive(Clone)]
1817
pub struct BandwidthStorageManager {
19-
pub(crate) storage: GatewayStorage,
18+
pub(crate) storage: Box<dyn BandwidthGatewayStorage + Send + Sync>,
2019
pub(crate) client_bandwidth: ClientBandwidth,
2120
pub(crate) client_id: i64,
2221
pub(crate) bandwidth_cfg: BandwidthFlushingBehaviourConfig,
2322
pub(crate) only_coconut_credentials: bool,
2423
}
2524

25+
impl Clone for BandwidthStorageManager {
26+
fn clone(&self) -> Self {
27+
Self {
28+
storage: dyn_clone::clone_box(&*self.storage),
29+
client_bandwidth: self.client_bandwidth.clone(),
30+
client_id: self.client_id,
31+
bandwidth_cfg: self.bandwidth_cfg,
32+
only_coconut_credentials: self.only_coconut_credentials,
33+
}
34+
}
35+
}
36+
2637
impl BandwidthStorageManager {
2738
pub fn new(
28-
storage: GatewayStorage,
39+
storage: Box<dyn BandwidthGatewayStorage + Send + Sync>,
2940
client_bandwidth: ClientBandwidth,
3041
client_id: i64,
3142
bandwidth_cfg: BandwidthFlushingBehaviourConfig,

common/credential-verification/src/ecash/mod.rs

Lines changed: 123 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// SPDX-License-Identifier: GPL-3.0-only
33

44
use crate::Error;
5+
use async_trait::async_trait;
56
use credential_sender::CredentialHandler;
67
use credential_sender::CredentialHandlerConfig;
78
use error::EcashTicketError;
89
use futures::channel::mpsc::{self, UnboundedSender};
910
use nym_credentials::CredentialSpendingData;
1011
use nym_credentials_interface::{ClientTicket, CompactEcashError, NymPayInfo, VerificationKeyAuth};
12+
use nym_gateway_storage::traits::BandwidthGatewayStorage;
1113
use nym_gateway_storage::GatewayStorage;
1214
use nym_validator_client::nym_api::EpochId;
1315
use nym_validator_client::DirectSigningHttpRpcNyxdClient;
@@ -20,6 +22,7 @@ pub mod credential_sender;
2022
pub mod error;
2123
mod helpers;
2224
mod state;
25+
pub mod traits;
2326

2427
pub const TIME_RANGE_SEC: i64 = 30;
2528

@@ -31,44 +34,21 @@ pub struct EcashManager {
3134
cred_sender: UnboundedSender<ClientTicket>,
3235
}
3336

34-
impl EcashManager {
35-
pub async fn new(
36-
credential_handler_cfg: CredentialHandlerConfig,
37-
nyxd_client: DirectSigningHttpRpcNyxdClient,
38-
pk_bytes: [u8; 32],
39-
shutdown: nym_task::TaskClient,
40-
storage: GatewayStorage,
41-
) -> Result<Self, Error> {
42-
let shared_state = SharedState::new(nyxd_client, storage).await?;
43-
44-
let (cred_sender, cred_receiver) = mpsc::unbounded();
45-
46-
let cs =
47-
CredentialHandler::new(credential_handler_cfg, cred_receiver, shared_state.clone())
48-
.await?;
49-
cs.start(shutdown);
50-
51-
Ok(EcashManager {
52-
shared_state,
53-
pk_bytes,
54-
pay_infos: Default::default(),
55-
cred_sender,
56-
})
57-
}
58-
59-
pub async fn verification_key(
37+
#[async_trait]
38+
impl traits::EcashManager for EcashManager {
39+
async fn verification_key(
6040
&self,
6141
epoch_id: EpochId,
6242
) -> Result<RwLockReadGuard<VerificationKeyAuth>, EcashTicketError> {
6343
self.shared_state.verification_key(epoch_id).await
6444
}
6545

66-
pub fn storage(&self) -> &GatewayStorage {
67-
&self.shared_state.storage
46+
fn storage(&self) -> Box<dyn BandwidthGatewayStorage + Send + Sync> {
47+
dyn_clone::clone_box(&*self.shared_state.storage)
6848
}
6949

7050
//Check for duplicate pay_info, then check the payment, then insert pay_info if everything succeeded
71-
pub async fn check_payment(
51+
async fn check_payment(
7252
&self,
7353
credential: &CredentialSpendingData,
7454
aggregated_verification_key: &VerificationKeyAuth,
@@ -88,6 +68,40 @@ impl EcashManager {
8868
.await
8969
}
9070

71+
fn async_verify(&self, ticket: ClientTicket) {
72+
// TODO: I guess do something for shutdowns
73+
let _ = self
74+
.cred_sender
75+
.unbounded_send(ticket)
76+
.inspect_err(|_| error!("failed to send the client ticket for verification task"));
77+
}
78+
}
79+
80+
impl EcashManager {
81+
pub async fn new(
82+
credential_handler_cfg: CredentialHandlerConfig,
83+
nyxd_client: DirectSigningHttpRpcNyxdClient,
84+
pk_bytes: [u8; 32],
85+
shutdown: nym_task::TaskClient,
86+
storage: GatewayStorage,
87+
) -> Result<Self, Error> {
88+
let shared_state = SharedState::new(nyxd_client, Box::new(storage)).await?;
89+
90+
let (cred_sender, cred_receiver) = mpsc::unbounded();
91+
92+
let cs =
93+
CredentialHandler::new(credential_handler_cfg, cred_receiver, shared_state.clone())
94+
.await?;
95+
cs.start(shutdown);
96+
97+
Ok(EcashManager {
98+
shared_state,
99+
pk_bytes,
100+
pay_infos: Default::default(),
101+
cred_sender,
102+
})
103+
}
104+
91105
pub async fn verify_pay_info(&self, pay_info: NymPayInfo) -> Result<usize, EcashTicketError> {
92106
//Public key check
93107
if pay_info.pk() != self.pk_bytes {
@@ -152,12 +166,86 @@ impl EcashManager {
152166
inner.insert(index, pay_info);
153167
Ok(())
154168
}
169+
}
155170

156-
pub fn async_verify(&self, ticket: ClientTicket) {
157-
// TODO: I guess do something for shutdowns
158-
let _ = self
159-
.cred_sender
160-
.unbounded_send(ticket)
161-
.inspect_err(|_| error!("failed to send the client ticket for verification task"));
171+
pub struct MockEcashManager {
172+
verfication_key: tokio::sync::RwLock<VerificationKeyAuth>,
173+
storage: Box<dyn BandwidthGatewayStorage + Send + Sync>,
174+
}
175+
176+
impl MockEcashManager {
177+
pub fn new(storage: Box<dyn BandwidthGatewayStorage + Send + Sync>) -> Self {
178+
Self {
179+
verfication_key: tokio::sync::RwLock::new(
180+
VerificationKeyAuth::from_bytes(&[
181+
129, 187, 76, 12, 1, 51, 46, 26, 132, 205, 148, 109, 140, 131, 50, 119, 45,
182+
128, 51, 218, 106, 70, 181, 74, 244, 38, 162, 62, 42, 12, 5, 100, 7, 136, 32,
183+
155, 18, 219, 195, 182, 3, 56, 168, 16, 93, 154, 249, 230, 16, 202, 90, 134,
184+
246, 25, 98, 6, 175, 215, 188, 239, 71, 84, 66, 1, 43, 66, 197, 180, 216, 80,
185+
55, 185, 140, 216, 14, 48, 244, 214, 20, 68, 106, 41, 48, 252, 188, 181, 231,
186+
170, 23, 211, 215, 12, 91, 147, 47, 7, 4, 0, 0, 0, 0, 0, 0, 0, 174, 31, 237,
187+
215, 159, 183, 71, 125, 90, 147, 84, 78, 49, 216, 66, 232, 92, 206, 41, 230,
188+
239, 209, 211, 166, 131, 190, 148, 36, 225, 194, 146, 6, 120, 34, 194, 5, 154,
189+
155, 234, 41, 191, 119, 227, 51, 91, 128, 151, 240, 129, 208, 253, 171, 234,
190+
170, 71, 139, 251, 78, 49, 35, 218, 16, 77, 150, 177, 204, 83, 210, 67, 147,
191+
66, 162, 58, 25, 96, 168, 61, 180, 92, 21, 18, 78, 194, 98, 176, 123, 122, 176,
192+
81, 150, 187, 20, 64, 69, 0, 134, 142, 3, 84, 108, 3, 55, 107, 111, 73, 31, 46,
193+
51, 225, 248, 202, 173, 194, 24, 104, 96, 31, 61, 24, 140, 220, 31, 176, 200,
194+
30, 217, 66, 58, 11, 181, 158, 196, 179, 199, 177, 7, 210, 4, 119, 142, 149,
195+
59, 3, 186, 145, 27, 230, 125, 230, 246, 197, 196, 119, 70, 239, 115, 99, 215,
196+
63, 205, 63, 74, 108, 201, 42, 226, 150, 137, 3, 157, 45, 25, 163, 54, 107,
197+
153, 61, 141, 64, 207, 139, 41, 203, 39, 36, 97, 181, 72, 206, 235, 221, 178,
198+
171, 60, 4, 6, 170, 181, 213, 10, 216, 53, 28, 32, 33, 41, 224, 60, 247, 206,
199+
137, 108, 251, 229, 234, 112, 65, 145, 124, 212, 125, 116, 154, 114, 2, 125,
200+
202, 24, 25, 196, 219, 104, 200, 131, 133, 180, 39, 21, 144, 204, 8, 151, 218,
201+
99, 64, 209, 47, 5, 42, 13, 214, 139, 54, 112, 224, 53, 238, 250, 56, 42, 105,
202+
15, 21, 238, 99, 225, 79, 121, 104, 155, 230, 243, 133, 47, 39, 147, 98, 45,
203+
113, 137, 200, 102, 151, 122, 174, 9, 250, 17, 138, 191, 129, 202, 244, 107,
204+
75, 48, 141, 136, 89, 168, 124, 88, 174, 251, 17, 35, 146, 88, 76, 134, 102,
205+
105, 204, 16, 176, 214, 63, 13, 170, 225, 250, 112, 7, 237, 161, 160, 15, 71,
206+
10, 130, 137, 69, 186, 64, 223, 188, 5, 5, 228, 57, 214, 134, 247, 20, 171,
207+
140, 43, 230, 57, 29, 127, 136, 169, 80, 14, 137, 130, 200, 205, 222, 81, 143,
208+
40, 77, 68, 197, 91, 142, 91, 84, 164, 15, 133, 242, 149, 255, 173, 201, 108,
209+
208, 23, 188, 230, 158, 146, 54, 198, 52, 148, 123, 202, 52, 222, 50, 4, 62,
210+
211, 208, 176, 61, 104, 151, 227, 192, 224, 200, 132, 53, 187, 240, 254, 150,
211+
60, 30, 140, 11, 63, 71, 12, 30, 233, 255, 144, 250, 16, 81, 38, 33, 9, 185,
212+
195, 214, 0, 119, 117, 94, 100, 103, 144, 10, 189, 65, 113, 114, 192, 11, 177,
213+
214, 223, 218, 36, 139, 183, 2, 206, 247, 245, 88, 62, 231, 183, 50, 46, 95,
214+
202, 152, 82, 244, 80, 173, 192, 147, 51, 248, 46, 181, 194, 205, 233, 67, 144,
215+
155, 250, 142, 124, 71, 9, 136, 142, 88, 29, 99, 222, 43, 181, 172, 120, 187,
216+
179, 172, 240, 231, 57, 236, 195, 158, 182, 203, 19, 49, 220, 180, 212, 101,
217+
105, 239, 58, 215, 0, 50, 100, 172, 29, 236, 170, 108, 129, 150, 5, 64, 238,
218+
59, 50, 4, 21, 131, 197, 142, 191, 76, 101, 140, 133, 112, 38, 235, 113, 203,
219+
22, 161, 204, 84, 73, 125, 219, 70, 62, 67, 119, 52, 130, 208, 180, 231, 78,
220+
141, 181, 13, 207, 196, 126, 159, 70, 34, 195, 70,
221+
])
222+
.unwrap(),
223+
),
224+
storage: dyn_clone::clone_box(&*storage),
225+
}
162226
}
163227
}
228+
229+
#[async_trait]
230+
impl traits::EcashManager for MockEcashManager {
231+
async fn verification_key(
232+
&self,
233+
_epoch_id: EpochId,
234+
) -> Result<RwLockReadGuard<VerificationKeyAuth>, EcashTicketError> {
235+
Ok(self.verfication_key.read().await)
236+
}
237+
238+
fn storage(&self) -> Box<dyn BandwidthGatewayStorage + Send + Sync> {
239+
dyn_clone::clone_box(&*self.storage)
240+
}
241+
242+
async fn check_payment(
243+
&self,
244+
_credential: &CredentialSpendingData,
245+
_aggregated_verification_key: &VerificationKeyAuth,
246+
) -> Result<(), EcashTicketError> {
247+
Ok(())
248+
}
249+
250+
fn async_verify(&self, _ticket: ClientTicket) {}
251+
}

common/credential-verification/src/ecash/state.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::Error;
66
use cosmwasm_std::{from_json, CosmosMsg, WasmMsg};
77
use nym_credentials_interface::VerificationKeyAuth;
88
use nym_ecash_contract_common::msg::ExecuteMsg;
9-
use nym_gateway_storage::GatewayStorage;
9+
use nym_gateway_storage::traits::BandwidthGatewayStorage;
1010
use nym_validator_client::coconut::all_ecash_api_clients;
1111
use nym_validator_client::nym_api::EpochId;
1212
use nym_validator_client::nyxd::contract_traits::{
@@ -22,18 +22,28 @@ use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
2222
use tracing::{error, trace, warn};
2323

2424
// state shared by different subtasks dealing with credentials
25-
#[derive(Clone)]
2625
pub(crate) struct SharedState {
2726
pub(crate) nyxd_client: Arc<RwLock<DirectSigningHttpRpcNyxdClient>>,
2827
pub(crate) address: AccountId,
2928
pub(crate) epoch_data: Arc<RwLock<BTreeMap<EpochId, EpochState>>>,
30-
pub(crate) storage: GatewayStorage,
29+
pub(crate) storage: Box<dyn BandwidthGatewayStorage + Send + Sync>,
30+
}
31+
32+
impl Clone for SharedState {
33+
fn clone(&self) -> Self {
34+
Self {
35+
nyxd_client: self.nyxd_client.clone(),
36+
address: self.address.clone(),
37+
epoch_data: self.epoch_data.clone(),
38+
storage: dyn_clone::clone_box(&*self.storage),
39+
}
40+
}
3141
}
3242

3343
impl SharedState {
3444
pub(crate) async fn new(
3545
nyxd_client: DirectSigningHttpRpcNyxdClient,
36-
storage: GatewayStorage,
46+
storage: Box<dyn BandwidthGatewayStorage + Send + Sync>,
3747
) -> Result<Self, Error> {
3848
let address = nyxd_client.address();
3949

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use async_trait::async_trait;
2+
use nym_credentials::CredentialSpendingData;
3+
use nym_credentials_interface::{ClientTicket, VerificationKeyAuth};
4+
use nym_gateway_storage::traits::BandwidthGatewayStorage;
5+
use nym_validator_client::nym_api::EpochId;
6+
use tokio::sync::RwLockReadGuard;
7+
8+
use crate::ecash::error::EcashTicketError;
9+
10+
#[async_trait]
11+
pub trait EcashManager {
12+
async fn verification_key(
13+
&self,
14+
epoch_id: EpochId,
15+
) -> Result<RwLockReadGuard<VerificationKeyAuth>, EcashTicketError>;
16+
fn storage(&self) -> Box<dyn BandwidthGatewayStorage + Send + Sync>;
17+
async fn check_payment(
18+
&self,
19+
credential: &CredentialSpendingData,
20+
aggregated_verification_key: &VerificationKeyAuth,
21+
) -> Result<(), EcashTicketError>;
22+
fn async_verify(&self, ticket: ClientTicket);
23+
}

0 commit comments

Comments
 (0)