From 9678030f762aebef49a4e40bbd1cab6b6a4605e0 Mon Sep 17 00:00:00 2001
From: "daniil.frolov" <daniil.frolov@demlabs.net>
Date: Wed, 12 Mar 2025 12:24:52 +0300
Subject: [PATCH] ..

---
 .../chain-net-srv/dap_stream_ch_chain_net_srv.c    | 14 ++++++++------
 modules/service/vpn/dap_chain_net_srv_vpn.c        |  8 +++++---
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
index e13b1d1dfd..a930241aa2 100644
--- a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
+++ b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
@@ -600,7 +600,7 @@ static bool s_grace_period_finish(dap_chain_net_srv_grace_usage_t *a_grace_item)
     dap_chain_net_srv_t *l_srv = dap_chain_net_srv_get(a_grace_item->grace->usage->service->uid);
     pthread_mutex_lock(&l_srv->grace_mutex);
     HASH_DEL(l_srv->grace_hash_tab, a_grace_item);
-    pthread_mutex_lock(&l_srv->grace_mutex);
+    pthread_mutex_unlock(&l_srv->grace_mutex);
 
     s_service_substate_pay_service(a_grace_item->grace->usage);
     DAP_DEL_Z(a_grace_item->grace);
@@ -1351,7 +1351,7 @@ static void s_service_substate_pay_service(dap_chain_net_srv_usage_t *a_usage)
         a_usage->receipt_timeout_timer_start_callback(a_usage);    
             
     }else {
-        if (!a_usage->tx_cond) a_usage->tx_cond = dap_ledger_tx_find_by_hash(a_usage->net->pub.ledger, &a_usage->tx_cond_hash);
+        a_usage->tx_cond = dap_ledger_tx_find_by_hash(a_usage->net->pub.ledger, &a_usage->tx_cond_hash);
 
         if (a_usage->tx_cond){
             int ret = s_check_tx_params(a_usage);
@@ -1389,6 +1389,8 @@ static void s_service_substate_pay_service(dap_chain_net_srv_usage_t *a_usage)
                     log_it(L_ERROR, "Can't find tx cond. Start grace!");
                     s_service_substate_go_to_waiting_prev_tx(a_usage);
                     return;
+                } else if (a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_IDLE) {
+                    s_service_state_go_to_grace(a_usage);
                 } else {
                     if (a_usage->service_substate == DAP_CHAIN_NET_SRV_USAGE_SERVICE_SUBSTATE_WAITING_NEW_TX_FROM_CLIENT){
                         log_it(L_INFO, "No new tx cond!");
@@ -1407,10 +1409,10 @@ static void s_service_substate_pay_service(dap_chain_net_srv_usage_t *a_usage)
                     a_usage->service_substate != DAP_CHAIN_NET_SRV_USAGE_SERVICE_SUBSTATE_WAITING_NEW_TX_FROM_CLIENT){
                     
                     log_it(L_ERROR, "Tx cond have not enough funds");
-                    if (a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_IDLE) {
-                        a_usage->service_state = DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_GRACE;
-                    } 
-                    s_service_substate_go_to_waiting_new_tx(a_usage);
+                    if (a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_IDLE)
+                        s_service_state_go_to_grace(a_usage);
+                    else
+                        s_service_substate_go_to_waiting_new_tx(a_usage);
                     return;
                 } else  {
                     log_it(L_ERROR, "New tx cond have not enough funds");
diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c
index ab0c47a999..b8f41fe1bc 100644
--- a/modules/service/vpn/dap_chain_net_srv_vpn.c
+++ b/modules/service/vpn/dap_chain_net_srv_vpn.c
@@ -1367,7 +1367,9 @@ static void s_update_limits(dap_stream_ch_t * a_ch ,
     bool l_issue_new_receipt = false;
     // Check if there are time limits
 
-    if (!a_usage || a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_FREE || !a_usage->is_active)
+    if (!a_usage || !a_usage->is_active || 
+        a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_FREE ||
+        a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_ERROR )
         return;
 
     if (a_usage->service_state == DAP_CHAIN_NET_SRV_USAGE_SERVICE_STATE_GRACE){
@@ -1377,7 +1379,7 @@ static void s_update_limits(dap_stream_ch_t * a_ch ,
         return;
     }
 
-    if (a_usage->receipt->receipt_info.units_type.enm == SERV_UNIT_SEC){
+    if (a_usage->receipt && a_usage->receipt->receipt_info.units_type.enm == SERV_UNIT_SEC){
         time_t l_current_limit_ts = 0;
 
         switch( a_usage->receipt->receipt_info.units_type.enm){
@@ -1438,7 +1440,7 @@ static void s_update_limits(dap_stream_ch_t * a_ch ,
                 dap_stream_ch_set_ready_to_read_unsafe(a_ch,false);
             }
         }
-    }else if ( a_usage->receipt->receipt_info.units_type.enm == SERV_UNIT_B){
+    }else if (a_usage->receipt && a_usage->receipt->receipt_info.units_type.enm == SERV_UNIT_B){
         intmax_t current_limit_bytes = 0;
         if ( a_usage->receipt){// if we have active receipt and a_srv_session->last_update_ts == 0 then we counts units by traffic
             switch( a_usage->receipt->receipt_info.units_type.enm){
-- 
GitLab