2
2
// SPDX-License-Identifier: GPL-3.0-only
3
3
4
4
use crate :: Error ;
5
+ use async_trait:: async_trait;
5
6
use credential_sender:: CredentialHandler ;
6
7
use credential_sender:: CredentialHandlerConfig ;
7
8
use error:: EcashTicketError ;
8
9
use futures:: channel:: mpsc:: { self , UnboundedSender } ;
9
10
use nym_credentials:: CredentialSpendingData ;
10
11
use nym_credentials_interface:: { ClientTicket , CompactEcashError , NymPayInfo , VerificationKeyAuth } ;
12
+ use nym_gateway_storage:: traits:: BandwidthGatewayStorage ;
11
13
use nym_gateway_storage:: GatewayStorage ;
12
14
use nym_validator_client:: nym_api:: EpochId ;
13
15
use nym_validator_client:: DirectSigningHttpRpcNyxdClient ;
@@ -20,6 +22,7 @@ pub mod credential_sender;
20
22
pub mod error;
21
23
mod helpers;
22
24
mod state;
25
+ pub mod traits;
23
26
24
27
pub const TIME_RANGE_SEC : i64 = 30 ;
25
28
@@ -31,44 +34,21 @@ pub struct EcashManager {
31
34
cred_sender : UnboundedSender < ClientTicket > ,
32
35
}
33
36
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 (
60
40
& self ,
61
41
epoch_id : EpochId ,
62
42
) -> Result < RwLockReadGuard < VerificationKeyAuth > , EcashTicketError > {
63
43
self . shared_state . verification_key ( epoch_id) . await
64
44
}
65
45
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 )
68
48
}
69
49
70
50
//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 (
72
52
& self ,
73
53
credential : & CredentialSpendingData ,
74
54
aggregated_verification_key : & VerificationKeyAuth ,
@@ -88,6 +68,40 @@ impl EcashManager {
88
68
. await
89
69
}
90
70
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
+
91
105
pub async fn verify_pay_info ( & self , pay_info : NymPayInfo ) -> Result < usize , EcashTicketError > {
92
106
//Public key check
93
107
if pay_info. pk ( ) != self . pk_bytes {
@@ -152,12 +166,86 @@ impl EcashManager {
152
166
inner. insert ( index, pay_info) ;
153
167
Ok ( ( ) )
154
168
}
169
+ }
155
170
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
+ }
162
226
}
163
227
}
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
+ }
0 commit comments