From 9981c8d3b224838c2d5d08ffc25b3a38d68511f4 Mon Sep 17 00:00:00 2001
From: "daniil.frolov" <daniil.frolov@demlabs.net>
Date: Mon, 17 Jun 2024 15:05:26 +0000
Subject: [PATCH] hotfix-12012

---
 modules/consensus/esbocs/dap_chain_cs_esbocs.c        |  1 +
 modules/net/dap_chain_ledger.c                        |  7 +++----
 modules/net/dap_chain_net.c                           |  3 +++
 modules/net/dap_chain_net_anchor.c                    | 10 +++++++++-
 .../stake/dap_chain_net_srv_stake_pos_delegate.c      |  2 +-
 modules/type/blocks/dap_chain_cs_blocks.c             | 11 +++++++----
 6 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
index 407e7e41b8..89defbc347 100644
--- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c
+++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c
@@ -1757,6 +1757,7 @@ static void s_session_round_finish(dap_chain_esbocs_session_t *a_session, dap_ch
                             a_session->cur_round.attempt_num, l_finish_candidate_hash_str, l_finish_block_hash_str);
         DAP_DEL_MULTY(l_finish_candidate_hash_str, l_finish_block_hash_str);
     }
+
     s_session_candidate_to_chain(a_session, &l_store->precommit_candidate_hash, l_store->candidate, l_store->candidate_size);
 }
 
diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c
index 320d0e811f..fe3ba367a3 100644
--- a/modules/net/dap_chain_ledger.c
+++ b/modules/net/dap_chain_ledger.c
@@ -5265,8 +5265,7 @@ int dap_ledger_tx_remove(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap
             if(l_item){
                 HASH_DEL(l_ledger_pvt->rewards, l_item);
                 DAP_DEL_Z(l_item);
-            }
-                
+            } 
             pthread_rwlock_unlock(&l_ledger_pvt->rewards_rwlock);
         }
         l_outs_used--; // Do not calc this output with tx used items
@@ -5312,7 +5311,7 @@ int dap_ledger_tx_remove(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap
             break;
         }
 
-        // add a used output memset(&(l_bound_item->stake_lock_item->tx_used_out), 0, sizeof(dap_hash_fast_t));
+        // add a used output 
         dap_ledger_tx_item_t *l_prev_item_out = l_bound_item->prev_item;
         memset(&(l_prev_item_out->cache_data.tx_hash_spent_fast[l_bound_item->prev_out_idx]), 0, sizeof(dap_hash_fast_t));
         l_prev_item_out->cache_data.n_outs_used--;
@@ -5397,7 +5396,7 @@ int dap_ledger_tx_remove(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap
         char *l_wallet_balance_key = dap_strjoin(" ", l_addr_str, l_cur_token_ticker, (char*)NULL);
         if(s_debug_more) {
             char *l_balance = dap_chain_balance_print(l_value);
-            log_it(L_DEBUG, "GOT %s to addr: %s", l_balance, l_wallet_balance_key);
+            log_it(L_DEBUG, "UNDO %s from addr: %s", l_balance, l_wallet_balance_key);
             DAP_DELETE(l_balance);
         }
         pthread_rwlock_rdlock(&l_ledger_pvt->balance_accounts_rwlock);
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 5e64e26c2e..0cc600b66a 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -3260,6 +3260,9 @@ int dap_chain_datum_remove(dap_chain_t *a_chain, dap_chain_datum_t *a_datum, siz
         }
         case DAP_CHAIN_DATUM_ANCHOR: {
             dap_chain_datum_anchor_t *l_anchor = (dap_chain_datum_anchor_t *)a_datum->data;
+
+            
+
             size_t l_anchor_size = dap_chain_datum_anchor_get_size(l_anchor);
             if (l_anchor_size != l_datum_data_size) {
                 log_it(L_WARNING, "Corrupted anchor, datum size %zd is not equal to size of anchor %zd", l_datum_data_size, l_anchor_size);
diff --git a/modules/net/dap_chain_net_anchor.c b/modules/net/dap_chain_net_anchor.c
index 5c1d5a6ca2..2fab9b0119 100644
--- a/modules/net/dap_chain_net_anchor.c
+++ b/modules/net/dap_chain_net_anchor.c
@@ -266,7 +266,9 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
         return -108;
     }
 
-    if ((ret_val = dap_chain_net_anchor_verify(l_net, a_anchor, dap_chain_datum_anchor_get_size(a_anchor))) != 0)
+    ret_val = s_anchor_verify(l_net, a_anchor, dap_chain_datum_anchor_get_size(a_anchor), true);
+
+    if (ret_val != 0)
     {
         log_it(L_WARNING,"Decree is not pass verification!");
         return ret_val;
@@ -284,6 +286,7 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
         switch (l_decree->header.sub_type)
         {
             case DAP_CHAIN_DATUM_DECREE_COMMON_SUBTYPE_FEE:{
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
                 dap_chain_datum_anchor_t * l_new_anchor = s_find_previous_anchor(a_anchor, a_chain);
                 if (l_new_anchor){// if previous anchor is founded apply it
                     dap_chain_hash_fast_t l_hash = {0};
@@ -313,10 +316,12 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
                     return -105;
                 }
                 dap_chain_net_srv_stake_key_invalidate(&l_signing_addr);
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
             }
             break;
             case DAP_CHAIN_DATUM_DECREE_COMMON_SUBTYPE_STAKE_INVALIDATE:{
                 // Find previous anchor with this stake approve and apply it 
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
                 dap_chain_datum_anchor_t * l_new_anchor = s_find_previous_anchor(a_anchor, a_chain);
                 if (l_new_anchor){// if previous anchor is founded apply it
                     dap_chain_hash_fast_t l_hash = {0};
@@ -332,6 +337,7 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
             }
             break;
             case DAP_CHAIN_DATUM_DECREE_COMMON_SUBTYPE_STAKE_MIN_VALUE:{
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
                 dap_chain_datum_anchor_t * l_new_anchor = s_find_previous_anchor(a_anchor, a_chain);
                 if (l_new_anchor){// if previous anchor is founded apply it
                     dap_chain_hash_fast_t l_hash = {0};
@@ -350,6 +356,7 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
             }
             break;
             case DAP_CHAIN_DATUM_DECREE_COMMON_SUBTYPE_STAKE_MIN_VALIDATORS_COUNT:{
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
                 dap_chain_datum_anchor_t * l_new_anchor = s_find_previous_anchor(a_anchor, a_chain);
                 if (l_new_anchor){// if previous anchor is founded apply it
                     dap_chain_hash_fast_t l_hash = {0};
@@ -369,6 +376,7 @@ int dap_chain_net_anchor_unload(dap_chain_datum_anchor_t * a_anchor, dap_chain_t
             break;
             case DAP_CHAIN_DATUM_DECREE_COMMON_SUBTYPE_REWARD:{
                 // find previous anchor with rewarrd and apply it
+                dap_chain_net_decree_reset_applied(l_net, &l_hash);
                 dap_chain_net_remove_last_reward(dap_chain_net_by_id(a_chain->net_id));
             }
             break;
diff --git a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c
index 990b97dcdb..dc36b628c0 100644
--- a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c
+++ b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c
@@ -1893,7 +1893,7 @@ static int s_cli_srv_stake_delegate(int a_argc, char **a_argv, int a_arg_index,
     int l_check_result = dap_chain_net_srv_stake_verify_key_and_node(&l_signing_addr, &l_node_addr);
     if (l_check_result) {
         dap_cli_server_cmd_set_reply_text(a_str_reply, "Key and node verification error");
-        dap_chain_wallet_close(l_wallet);
+        dap_enc_key_delete(l_enc_key);
         return l_check_result;
     }
     uint256_t l_allowed_min = dap_chain_net_srv_stake_get_allowed_min_value(l_net->pub.id);
diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c
index 364539a96d..2e14ab9469 100644
--- a/modules/type/blocks/dap_chain_cs_blocks.c
+++ b/modules/type/blocks/dap_chain_cs_blocks.c
@@ -1433,14 +1433,17 @@ static int s_delete_atom_datums(dap_chain_cs_blocks_t *a_blocks, dap_chain_block
             i++, l_block_offset += l_datum_size){
         dap_hash_fast_t *l_datum_hash = a_block_cache->datum_hash + i;
         dap_chain_datum_t *l_datum = a_block_cache->datum[i];
-        int l_res = dap_chain_datum_remove(a_blocks->chain, l_datum, l_datum_size, l_datum_hash);
-        l_ret++;
-
         pthread_rwlock_wrlock(&PVT(a_blocks)->datums_rwlock);
         dap_chain_block_datum_index_t *l_datum_index = NULL;
+        size_t l_datum_data_size = l_datum->header.data_size;
+        l_datum_size = l_datum_data_size + sizeof(l_datum->header);
         HASH_FIND(hh, PVT(a_blocks)->datum_index, l_datum_hash, sizeof(dap_hash_fast_t), l_datum_index);
-        if (l_datum_index)
+        if (l_datum_index){
+            if (l_datum_index->ret_code >= 0)
+                dap_chain_datum_remove(a_blocks->chain, l_datum, l_datum_size, l_datum_hash);
+            l_ret++;
             HASH_DEL(PVT(a_blocks)->datum_index, l_datum_index);
+        }
         pthread_rwlock_unlock(&PVT(a_blocks)->datums_rwlock);
     }
     debug_if(s_debug_more, L_DEBUG, "Block %s checked, %s", a_block_cache->block_hash_str,
-- 
GitLab