From bc1ee21456d1215ec768e4ee2f7ce1f9be02c87c Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Fri, 8 Sep 2023 10:27:41 +0000
Subject: [PATCH] hotfix-9490+

---
 .../consensus/esbocs/dap_chain_cs_esbocs.c    | 51 ++++++++++++-------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
index 19e03b1712..c05c113b49 100644
--- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c
+++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
@@ -325,6 +325,16 @@ static void s_session_db_serialize(dap_global_db_context_t *a_context, void *a_a
     }
     dap_store_obj_free(l_objs, l_objs_count);
 
+    if (l_pkt)
+        dap_hash_fast(l_pkt->data, l_pkt->data_size, &l_session->db_hash);
+    else
+        l_session->db_hash = (dap_hash_fast_t){};
+    if (PVT(l_session->esbocs)->debug) {
+        char l_sync_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE];
+        dap_chain_hash_fast_to_str(&l_session->db_hash, l_sync_hash_str, DAP_CHAIN_HASH_FAST_STR_SIZE);
+        log_it(L_MSG, "DB changes applied, new DB resync hash is %s", l_sync_hash_str);
+    }
+
     char *l_del_sync_group = dap_strdup_printf("%s.del", l_sync_group);
     l_objs_count = 0;
     l_objs = dap_global_db_get_all_raw_unsafe(a_context, l_del_sync_group, 0, &l_objs_count);
@@ -347,15 +357,7 @@ static void s_session_db_serialize(dap_global_db_context_t *a_context, void *a_a
     DAP_DELETE(l_sync_group);
     DAP_DEL_Z(l_session->db_serial);
     l_session->db_serial = l_pkt;
-    if (l_pkt)
-        dap_hash_fast(l_pkt->data, l_pkt->data_size, &l_session->db_hash);
-    else
-        l_session->db_hash = (dap_hash_fast_t){};
-    if (PVT(l_session->esbocs)->debug) {
-        char l_sync_hash_str[DAP_CHAIN_HASH_FAST_STR_SIZE];
-        dap_chain_hash_fast_to_str(&l_session->db_hash, l_sync_hash_str, DAP_CHAIN_HASH_FAST_STR_SIZE);
-        log_it(L_MSG, "DB changes applied, new DB resync hash is %s", l_sync_hash_str);
-    }
+
     dap_proc_queue_add_callback(dap_events_worker_get_auto(), s_change_db_broadcast, l_session);
 }
 
@@ -902,7 +904,7 @@ static uint64_t s_session_calc_current_round_id(dap_chain_esbocs_session_t *a_se
         uint64_t l_id_candidate = 0;
         for (dap_chain_esbocs_message_item_t *l_item = a_session->cur_round.message_items; l_item; l_item = l_item->hh.next) {
             if (l_item->message->hdr.type == DAP_CHAIN_ESBOCS_MSG_TYPE_START_SYNC &&
-                    l_item->message->hdr.attempt_num == a_session->cur_round.sync_attempt &&
+                    ((struct sync_params *)l_item->message->msg_n_sign)->attempt == a_session->cur_round.sync_attempt &&
                     dap_chain_addr_compare(&l_item->signing_addr, &l_validator->signing_addr)) {
                 l_id_candidate = l_item->message->hdr.round_id;
                 break;
@@ -1000,14 +1002,9 @@ dap_chain_esbocs_directive_t *s_session_directive_ready(dap_chain_esbocs_session
 
 static void s_session_state_change(dap_chain_esbocs_session_t *a_session, enum s_esbocs_session_state a_new_state, dap_time_t a_time)
 {
-    if (a_new_state != DAP_CHAIN_ESBOCS_SESSION_STATE_PREVIOUS) {
-        if (a_session->state == DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_VOTING) {
-            // Do not change this state, state changing will be applied after return to PREVIOUS state
-            a_session->old_state = a_new_state;
-            return;
-        }
+    if (a_new_state != DAP_CHAIN_ESBOCS_SESSION_STATE_PREVIOUS)
         a_session->old_state = a_session->state;
-    }
+
     a_session->state = a_new_state;
     a_session->ts_stage_entry = a_time;
 
@@ -1071,6 +1068,24 @@ static void s_session_state_change(dap_chain_esbocs_session_t *a_session, enum s
             }
         }
     } break;
+    case DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_VOTING: {
+        if (a_session->old_state == DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_PROC) {
+            // Clear mark of chosen to submit validator
+            dap_list_t *l_list = s_validator_check(
+                        &a_session->cur_round.attempt_submit_validator,
+                        a_session->cur_round.validators_list
+                        );
+            dap_chain_esbocs_validator_t *l_validator = l_list ? l_list->data : NULL;
+            if (!l_validator || !l_validator->is_chosen) {
+                char *l_addr = dap_chain_addr_to_str(&a_session->cur_round.attempt_submit_validator);
+                log_it(L_MSG, "Error: can't find current attmempt submit validator %s in signers list", l_addr);
+                DAP_DELETE(l_addr);
+            }
+            l_validator->is_chosen = false;
+        } else
+            a_session->old_state = DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_PROC;
+    } break;
+
     case DAP_CHAIN_ESBOCS_SESSION_STATE_WAIT_FINISH: {
         dap_chain_esbocs_store_t *l_store;
         HASH_FIND(hh, a_session->cur_round.store_items, &a_session->cur_round.attempt_candidate_hash, sizeof(dap_hash_fast_t), l_store);
@@ -2327,7 +2342,7 @@ static void s_session_packet_in(void *a_arg, dap_chain_node_addr_t *a_sender_nod
 
     case DAP_CHAIN_ESBOCS_MSG_TYPE_DIRECTIVE: {
         if (l_session->cur_round.directive) {
-            log_it(L_WARNING, "Only one directive can be processed at a time");
+            log_it(L_WARNING, "Only one directive can be processed by round");
             break;
         }
         dap_chain_esbocs_directive_t *l_directive = l_message_data;
-- 
GitLab