From e767c73beea7bbade4ab0bee3ce9417f3cadf46b Mon Sep 17 00:00:00 2001
From: "aleksei.voronin" <aleksei.voronin@demlabs.net>
Date: Wed, 27 May 2020 12:32:19 +0300
Subject: [PATCH] [*] fixed events_lasts_unlinked to work correctly with
 threshold

---
 modules/type/dag/dap_chain_cs_dag.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c
index 698a24fe7a..7cc5c729a6 100644
--- a/modules/type/dag/dap_chain_cs_dag.c
+++ b/modules/type/dag/dap_chain_cs_dag.c
@@ -114,6 +114,7 @@ static dap_chain_datum_t* s_chain_callback_datum_iter_get_next( dap_chain_datum_
 */
 
 static int s_cli_dag(int argc, char ** argv, void *arg_func, char **str_reply);
+void s_dag_events_lasts_process_new_last_event(dap_chain_cs_dag_t * a_dag, dap_chain_cs_dag_event_item_t * a_event_item);
 
 static bool s_seed_mode = false;
 /**
@@ -362,6 +363,8 @@ static int s_chain_callback_atom_add(dap_chain_t * a_chain, dap_chain_atom_ptr_t
       }
       pthread_rwlock_wrlock(&PVT(l_dag)->events_rwlock);
       int res_ledger = dap_chain_add_to_ledger(l_dag, a_chain->ledger, l_event_item);
+      if(res_ledger >= 0)
+        s_dag_events_lasts_process_new_last_event(l_dag, l_event_item);
       pthread_rwlock_unlock(&PVT(l_dag)->events_rwlock);
 
       if(res_ledger < 0)
@@ -645,16 +648,26 @@ void s_dag_events_lasts_delete_linked_with_event(dap_chain_cs_dag_t * a_dag, dap
     for (size_t i = 0; i< a_event->header.hash_count; i++) {
         dap_chain_hash_fast_t * l_hash =  ((dap_chain_hash_fast_t *) a_event->hashes_n_datum_n_signs) + i;
         dap_chain_cs_dag_event_item_t * l_event_item = NULL;
-        pthread_rwlock_wrlock(&PVT(a_dag)->events_rwlock);
         HASH_FIND(hh, PVT(a_dag)->events_lasts_unlinked ,l_hash ,sizeof (*l_hash),  l_event_item);
         if ( l_event_item ){
             HASH_DEL(PVT(a_dag)->events_lasts_unlinked,l_event_item);
             DAP_DEL_Z(l_event_item);
         }
-        pthread_rwlock_wrlock(&PVT(a_dag)->events_rwlock);
     }
 }
 
+void s_dag_events_lasts_process_new_last_event(dap_chain_cs_dag_t * a_dag, dap_chain_cs_dag_event_item_t * a_event_item){
+    //delete linked with event
+    s_dag_events_lasts_delete_linked_with_event(a_dag, a_event_item->event);
+
+    //add self
+    dap_chain_cs_dag_event_item_t * l_event_last= DAP_NEW_Z(dap_chain_cs_dag_event_item_t);
+    l_event_last->ts_added = a_event_item->ts_added;
+    l_event_last->event = a_event_item->event;
+    dap_hash_fast(l_event_last->event, dap_chain_cs_dag_event_calc_size(l_event_last->event),&l_event_last->hash );
+    HASH_ADD(hh,PVT(a_dag)->events_lasts_unlinked,hash, sizeof(l_event_last->hash),l_event_last);
+}
+
 
 typedef enum{
   DAP_THRESHOLD_OK = 0,
@@ -721,11 +734,11 @@ bool dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a
             if(ret == DAP_THRESHOLD_OK){
                 HASH_ADD(hh, PVT(a_dag)->events, hash,sizeof (l_event_item->hash),  l_event_item);
                 int l_ledger_ret = dap_chain_add_to_ledger(a_dag, a_ledger, l_event_item);
+                s_dag_events_lasts_process_new_last_event(a_dag, l_event_item);
                 res = true;
             }else if(ret == DAP_THRESHOLD_CONFLICTING)
                 HASH_ADD(hh, PVT(a_dag)->events_treshold_conflicted, hash,sizeof (l_event_item->hash),  l_event_item);
 
-            s_dag_events_lasts_delete_linked_with_event(a_dag, l_event);
         }
     }
     return res;
-- 
GitLab