From 26f645c0078519c83f75e7e5bef8054403d53b5b Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Tue, 4 Apr 2023 13:55:14 +0300
Subject: [PATCH] [*] Validators count decree segfault fix

---
 .../consensus/esbocs/dap_chain_cs_esbocs.c    | 74 +++++++++++--------
 modules/net/dap_chain_net_decree.c            | 11 ++-
 .../dap_chain_net_srv_stake_pos_delegate.c    |  8 +-
 3 files changed, 58 insertions(+), 35 deletions(-)

diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
index f2215f00da..abc6d8d3fc 100644
--- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c
+++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
@@ -101,8 +101,10 @@ int dap_chain_cs_esbocs_init()
     dap_stream_ch_chain_voting_init();
     dap_chain_cs_add("esbocs", s_callback_new);
     dap_cli_server_cmd_add ("esbocs", s_cli_esbocs, "ESBOCS commands",
-        "esbocs min_validators_count -net <net_name> -chain <chain_name> -cert <poa_cert_name> -val_count <value>"
-            "\tSets minimum validators count for ESBOCS consensus\n\n");
+        "esbocs min_validators_count set -net <net_name> -chain <chain_name> -cert <poa_cert_name> -val_count <value>"
+            "\tSets minimum validators count for ESBOCS consensus\n"
+        "esbocs min_validators_count print -net <net_name> -chain <chain_name>"
+            "\tShow minimum validators count for ESBOCS consensus\n\n");
     return 0;
 }
 
@@ -1695,37 +1697,45 @@ static int s_cli_esbocs(int a_argc, char ** a_argv, char **a_str_reply)
         return -3;
     }
 
-    dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-cert", &l_cert_str);
-    if (!l_cert_str) {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'min_validators_count' requires parameter -cert");
-        return -3;
-    }
-    dap_cert_t *l_poa_cert = dap_cert_find_by_name(l_cert_str);
-    if (!l_poa_cert) {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified certificate not found");
-        return -25;
-    }
-
-    dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-val_count", &l_value_str);
-    if (!l_value_str) {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'min_validators_count' requires parameter -val_count");
-        return -9;
-    }
-    uint256_t l_value = dap_chain_balance_scan(l_value_str);
-    if (IS_ZERO_256(l_value)) {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Unrecognized number in '-val_count' param");
-        return -10;
-    }
+    if (dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, l_arg_index + 1, "set", NULL)) {
+        dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-cert", &l_cert_str);
+        if (!l_cert_str) {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'min_validators_count' requires parameter -cert");
+            return -3;
+        }
+        dap_cert_t *l_poa_cert = dap_cert_find_by_name(l_cert_str);
+        if (!l_poa_cert) {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Specified certificate not found");
+            return -25;
+        }
 
-    dap_chain_datum_decree_t *l_decree = s_esbocs_decree_set_min_validators_count(l_chain_net, l_value, l_poa_cert);
-    if (l_decree && s_esbocs_decree_put(l_decree, l_chain_net)) {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Minimum validators count is set");
-        DAP_DELETE(l_decree);
-    } else {
-        dap_cli_server_cmd_set_reply_text(a_str_reply, "Minimum validators count setting failed");
-        DAP_DELETE(l_decree);
-        return -21;
-    }
+        dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "-val_count", &l_value_str);
+        if (!l_value_str) {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Command 'min_validators_count' requires parameter -val_count");
+            return -9;
+        }
+        uint256_t l_value = dap_chain_balance_scan(l_value_str);
+        if (IS_ZERO_256(l_value)) {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Unrecognized number in '-val_count' param");
+            return -10;
+        }
 
+        dap_chain_datum_decree_t *l_decree = s_esbocs_decree_set_min_validators_count(l_chain_net, l_value, l_poa_cert);
+        if (l_decree && s_esbocs_decree_put(l_decree, l_chain_net)) {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Minimum validators count has been set");
+            DAP_DELETE(l_decree);
+        } else {
+            dap_cli_server_cmd_set_reply_text(a_str_reply, "Minimum validators count setting failed");
+            DAP_DELETE(l_decree);
+            return -21;
+        }
+    } else if (dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, l_arg_index + 1, "print", NULL)) {
+        dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(l_chain);
+        dap_chain_esbocs_t *l_esbocs = DAP_CHAIN_ESBOCS(l_blocks);
+        dap_chain_esbocs_pvt_t *l_esbocs_pvt = PVT(l_esbocs);
+        dap_cli_server_cmd_set_reply_text(a_str_reply, "Minimum validators count is %d",
+                                          l_esbocs_pvt->min_validators_count);
+    } else
+        dap_cli_server_cmd_set_reply_text(a_str_reply, "Unrecognized subcommand '%s'", a_argv[l_arg_index]);
     return ret;
 }
diff --git a/modules/net/dap_chain_net_decree.c b/modules/net/dap_chain_net_decree.c
index 8e966f095f..8a3713f803 100644
--- a/modules/net/dap_chain_net_decree.c
+++ b/modules/net/dap_chain_net_decree.c
@@ -496,13 +496,20 @@ static int s_common_decree_handler(dap_chain_datum_decree_t * a_decree, dap_chai
                 log_it(L_WARNING,"Can't get min stake value from decree.");
                 return -105;
             }
-            if (!a_chain->callback_set_min_validators_count) {
+            dap_chain_t *l_chain = a_chain;
+            if (!a_chain)
+                l_chain = dap_chain_find_by_id(a_net->pub.id, a_decree->header.common_decree_params.chain_id);
+            if (!l_chain) {
+                log_it(L_WARNING, "Specified chain not found");
+                return -106;
+            }
+            if (!l_chain->callback_set_min_validators_count) {
                 log_it(L_WARNING, "Can't apply this decree to specified chain");
                 return -115;
             }
             if (!a_apply)
                 break;
-            a_chain->callback_set_min_validators_count(a_chain, (uint16_t)dap_chain_uint256_to(l_uint256_buffer));
+            l_chain->callback_set_min_validators_count(a_chain, (uint16_t)dap_chain_uint256_to(l_uint256_buffer));
             break;
         default: return -1;
     }
diff --git a/modules/service/stake_pos_delegate/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake_pos_delegate/dap_chain_net_srv_stake_pos_delegate.c
index 3d8e45d8c8..e4fc001289 100644
--- a/modules/service/stake_pos_delegate/dap_chain_net_srv_stake_pos_delegate.c
+++ b/modules/service/stake_pos_delegate/dap_chain_net_srv_stake_pos_delegate.c
@@ -1353,8 +1353,14 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply)
                         s_srv_stake_print(l_stake, l_reply_str);
                     }
                 if (!HASH_CNT(hh, s_srv_stake->itemlist)) {
-                    dap_string_append(l_reply_str, "No keys found");
+                    dap_string_append(l_reply_str, "No keys found\n");
                 }
+                char *l_delegate_min_str = dap_chain_balance_to_coins(s_srv_stake->delegate_allowed_min);
+                char l_delegated_ticker[DAP_CHAIN_TICKER_SIZE_MAX];
+                dap_chain_datum_token_get_delegated_ticker(l_delegated_ticker, l_net->pub.native_ticker);
+                dap_string_append_printf(l_reply_str, "Minimum value for key delegating: %s %s",
+                                         l_delegate_min_str, l_delegated_ticker);
+                DAP_DELETE(l_delegate_min_str);
                 *a_str_reply = dap_string_free(l_reply_str, false);
             } else if (dap_cli_server_cmd_find_option_val(a_argv, l_arg_index, a_argc, "tx", NULL)) {
                 const char *l_net_str = NULL;
-- 
GitLab