From f1e3c2880288e9d3cc27469dfbcac7c187df4d9c Mon Sep 17 00:00:00 2001
From: "roman.khlopkov" <roman.khlopkov@demlabs.net>
Date: Mon, 1 Jul 2024 16:42:02 +0300
Subject: [PATCH] [*] Block last / event last commands segfaults fix

---
 modules/type/blocks/dap_chain_cs_blocks.c | 11 ++++++-----
 modules/type/dag/dap_chain_cs_dag.c       | 17 +++++++++--------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c
index e4b26dc01d..04e518cb29 100644
--- a/modules/type/blocks/dap_chain_cs_blocks.c
+++ b/modules/type/blocks/dap_chain_cs_blocks.c
@@ -1017,12 +1017,13 @@ static int s_cli_blocks(int a_argc, char ** a_argv, void **a_str_reply)
             json_object* json_obj_out = json_object_new_object();
             dap_chain_block_cache_t *l_last_block = HASH_LAST(PVT(l_blocks)->blocks);
             char l_buf[DAP_TIME_STR_SIZE];
-            dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_last_block->ts_created);
-            json_object_object_add(json_obj_out, "Last block num",json_object_new_uint64(l_last_block->block_number));
-            json_object_object_add(json_obj_out, "Last block hash",json_object_new_string(l_last_block->block_hash_str));
-            json_object_object_add(json_obj_out, "ts_create",json_object_new_string(l_buf));
+            if (l_last_block)
+                dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_last_block->ts_created);
+            json_object_object_add(json_obj_out, "Last block num", json_object_new_uint64(l_last_block ? l_last_block->block_number : 0));
+            json_object_object_add(json_obj_out, "Last block hash", json_object_new_string(l_last_block ? l_last_block->block_hash_str : "empty"));
+            json_object_object_add(json_obj_out, "ts_created", json_object_new_string(l_last_block ? l_buf : "never"));
 
-            sprintf(l_tmp_buff,"%s.%s has blocks - ",l_net->pub.name,l_chain->name);
+            sprintf(l_tmp_buff,"%s.%s has blocks", l_net->pub.name, l_chain->name);
             json_object_object_add(json_obj_out, l_tmp_buff, json_object_new_uint64(PVT(l_blocks)->blocks_count));
             json_object_array_add(*json_arr_reply, json_obj_out);
         } break;
diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c
index b2a2130702..1deb540361 100644
--- a/modules/type/dag/dap_chain_cs_dag.c
+++ b/modules/type/dag/dap_chain_cs_dag.c
@@ -1850,19 +1850,20 @@ static int s_cli_dag(int argc, char ** argv, void **a_str_reply)
             } break;
             case SUBCMD_EVENT_LAST:{
                 json_object * json_obj_out = json_object_new_object();
-                char l_tmp_buff[70]={0};
-                dap_string_t *l_ret_str = dap_string_new(NULL);
+                char l_tmp_buff[70] = {0};
                 pthread_mutex_lock(&PVT(l_dag)->events_mutex);
-                dap_chain_cs_dag_event_item_t *l_last_event = HASH_LAST(PVT(l_dag)->events);
+                dap_chain_cs_dag_event_item_t *l_last_item = HASH_LAST(PVT(l_dag)->events);
                 pthread_mutex_unlock(&PVT(l_dag)->events_mutex);
                 char l_buf[DAP_TIME_STR_SIZE];
-                dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_last_event->event->header.ts_created);
-
-                json_object_object_add(json_obj_out, "Last atom hash in events",json_object_new_string(dap_hash_fast_to_str_static(&l_last_event->hash)));
-                json_object_object_add(json_obj_out, "ts_create",json_object_new_string(l_buf));
+                if (l_last_item)
+                    dap_time_to_str_rfc822(l_buf, DAP_TIME_STR_SIZE, l_last_item->event->header.ts_created);
+                 json_object_object_add(json_obj_out, "Last event num", json_object_new_uint64(l_last_item ? l_last_item->event_number : 0));
+                json_object_object_add(json_obj_out, "Last event hash", json_object_new_string(l_last_item ?
+                                                                                dap_hash_fast_to_str_static(&l_last_item->hash) : "empty"));
+                json_object_object_add(json_obj_out, "ts_create", l_last_item ? json_object_new_string(l_buf) : "never");
 
                 size_t l_event_count = HASH_COUNT(PVT(l_dag)->events);
-                sprintf(l_tmp_buff,"%s.%s has events - ",l_net->pub.name,l_chain->name);
+                sprintf(l_tmp_buff,"%s.%s has events", l_net->pub.name, l_chain->name);
                 json_object_object_add(json_obj_out, l_tmp_buff, json_object_new_uint64(l_event_count));
                 json_object_array_add(*json_arr_reply, json_obj_out);
             } break;
-- 
GitLab