@@ -59,16 +59,26 @@ async fn credential_storage_preparation(
5959 ecash_verifier : Arc < dyn EcashManager + Send + Sync > ,
6060 client_id : i64 ,
6161) -> Result < PersistedBandwidth , GatewayError > {
62- ecash_verifier
62+ // Check if bandwidth entry already exists (idempotent)
63+ let existing_bandwidth = ecash_verifier
6364 . storage ( )
64- . create_bandwidth_entry ( client_id)
65+ . get_available_bandwidth ( client_id)
6566 . await ?;
67+
68+ // Only create if it doesn't exist
69+ if existing_bandwidth. is_none ( ) {
70+ ecash_verifier
71+ . storage ( )
72+ . create_bandwidth_entry ( client_id)
73+ . await ?;
74+ }
75+
6676 let bandwidth = ecash_verifier
6777 . storage ( )
6878 . get_available_bandwidth ( client_id)
6979 . await ?
7080 . ok_or_else ( || {
71- GatewayError :: InternalError ( "bandwidth entry should have just been created " . to_string ( ) )
81+ GatewayError :: InternalError ( "bandwidth entry should exist " . to_string ( ) )
7282 } ) ?;
7383 Ok ( bandwidth)
7484}
@@ -96,18 +106,30 @@ async fn credential_verification(
96106 // Track credential verification attempts
97107 inc ! ( "lp_credential_verification_attempts" ) ;
98108
99- match verifier. verify ( ) . await {
100- Ok ( allocated) => {
101- inc ! ( "lp_credential_verification_success" ) ;
102- // Track allocated bandwidth
103- inc_by ! ( "lp_bandwidth_allocated_bytes_total" , allocated) ;
104- Ok ( allocated)
105- }
106- Err ( e) => {
107- inc ! ( "lp_credential_verification_failed" ) ;
108- Err ( e. into ( ) )
109+ // For mock ecash mode (local testing), skip cryptographic verification
110+ // and just return a dummy bandwidth value since we don't have blockchain access
111+ let allocated = if ecash_verifier. is_mock ( ) {
112+ // Return a reasonable test bandwidth value (e.g., 1GB in bytes)
113+ const MOCK_BANDWIDTH : i64 = 1024 * 1024 * 1024 ;
114+ inc ! ( "lp_credential_verification_success" ) ;
115+ inc_by ! ( "lp_bandwidth_allocated_bytes_total" , MOCK_BANDWIDTH ) ;
116+ Ok :: < i64 , GatewayError > ( MOCK_BANDWIDTH )
117+ } else {
118+ match verifier. verify ( ) . await {
119+ Ok ( allocated) => {
120+ inc ! ( "lp_credential_verification_success" ) ;
121+ // Track allocated bandwidth
122+ inc_by ! ( "lp_bandwidth_allocated_bytes_total" , allocated) ;
123+ Ok ( allocated)
124+ }
125+ Err ( e) => {
126+ inc ! ( "lp_credential_verification_failed" ) ;
127+ Err ( e. into ( ) )
128+ }
109129 }
110- }
130+ } ?;
131+
132+ Ok ( allocated)
111133}
112134
113135/// Process an LP registration request
@@ -320,7 +342,22 @@ async fn register_wg_peer(
320342 ] ;
321343 peer. persistent_keepalive_interval = Some ( 25 ) ;
322344
323- // Send to WireGuard peer controller and track latency
345+ // Store peer in database FIRST (before adding to controller)
346+ // This ensures bandwidth storage exists when controller's generate_bandwidth_manager() is called
347+ let client_id = state
348+ . storage
349+ . insert_wireguard_peer ( & peer, ticket_type. into ( ) )
350+ . await
351+ . map_err ( |e| {
352+ error ! ( "Failed to store WireGuard peer in database: {}" , e) ;
353+ GatewayError :: InternalError ( format ! ( "Failed to store peer: {}" , e) )
354+ } ) ?;
355+
356+ // Create bandwidth entry for the client
357+ // This must happen BEFORE AddPeer because generate_bandwidth_manager() expects it to exist
358+ credential_storage_preparation ( state. ecash_verifier . clone ( ) , client_id) . await ?;
359+
360+ // Now send to WireGuard peer controller and track latency
324361 let controller_start = std:: time:: Instant :: now ( ) ;
325362 let ( tx, rx) = oneshot:: channel ( ) ;
326363 wg_controller
@@ -348,16 +385,6 @@ async fn register_wg_peer(
348385
349386 result?;
350387
351- // Store bandwidth allocation and get client_id
352- let client_id = state
353- . storage
354- . insert_wireguard_peer ( & peer, ticket_type. into ( ) )
355- . await
356- . map_err ( |e| {
357- error ! ( "Failed to store WireGuard peer in database: {}" , e) ;
358- GatewayError :: InternalError ( format ! ( "Failed to store peer: {}" , e) )
359- } ) ?;
360-
361388 // Get gateway's actual WireGuard public key
362389 let gateway_pubkey = * wg_data. keypair ( ) . public_key ( ) ;
363390
0 commit comments