From d06e490fd26ec6c583135fca39401a2fe48f50b5 Mon Sep 17 00:00:00 2001
From: "roman.khlopkov" <roman.khlopkov@demlabs.net>
Date: Fri, 24 Nov 2023 22:14:56 +0300
Subject: [PATCH] [*] Stake lock segfault fix

---
 modules/net/dap_chain_ledger.c            | 50 +++++++++++++----------
 modules/type/blocks/dap_chain_cs_blocks.c |  9 +++-
 2 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/modules/net/dap_chain_ledger.c b/modules/net/dap_chain_ledger.c
index 5ebbb90e5b..07ce853212 100644
--- a/modules/net/dap_chain_ledger.c
+++ b/modules/net/dap_chain_ledger.c
@@ -4388,36 +4388,42 @@ int dap_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx, dap_ha
             l_spent_idx++;
         }
 
+        if ((l_type == TX_ITEM_TYPE_IN_EMS_LOCK || l_type == TX_ITEM_TYPE_IN_REWARD) &&
+                !s_ledger_token_supply_check_update(a_ledger, l_bound_item->token_item, l_bound_item->value))
+            log_it(L_ERROR, "Insufficient supply for token %s", l_bound_item->token_item->ticker);
+
         switch (l_type) {
         case TX_ITEM_TYPE_IN_EMS:
+            // Mark it as used with current tx hash
+            l_bound_item->emission_item->tx_used_out = *a_tx_hash;
+            s_ledger_emission_cache_update(a_ledger, l_bound_item->emission_item);
+            l_outs_used--; // Do not calc this output with tx used items
+            continue;
+
         case TX_ITEM_TYPE_IN_EMS_LOCK:
-        case TX_ITEM_TYPE_IN_REWARD: {
-             // It's the emission behind
-            if (!s_ledger_token_supply_check_update(a_ledger, l_bound_item->token_item, l_bound_item->value))
-                log_it(L_ERROR, "Insufficient supply for token %s", l_bound_item->token_item->ticker);
-            if (l_type == TX_ITEM_TYPE_IN_EMS_LOCK) {
+            if (l_bound_item->stake_lock_item) { // Legacy stake lock emission
                 // Mark it as used with current tx hash
                 l_bound_item->stake_lock_item->tx_used_out = *a_tx_hash;
                 s_ledger_stake_lock_cache_update(a_ledger, l_bound_item->stake_lock_item);
-            } else if (l_type == TX_ITEM_TYPE_IN_EMS) {
-                // Mark it as used with current tx hash
-                l_bound_item->emission_item->tx_used_out = *a_tx_hash;
-                s_ledger_emission_cache_update(a_ledger, l_bound_item->emission_item);
-            } else { // l_type == TX_ITEM_TYPE_IN_REWARD
-                dap_ledger_reward_item_t *l_item = DAP_NEW_Z(dap_ledger_reward_item_t);
-                if (!l_item) {
-                    log_it(L_CRITICAL, "Memory allocation error");
-                    l_ret = -1;
-                    goto FIN;
-                }
-                l_item->key = l_bound_item->reward_key;
-                l_item->spender_tx = *a_tx_hash;
-                pthread_rwlock_wrlock(&l_ledger_pvt->rewards_rwlock);
-                HASH_ADD(hh, l_ledger_pvt->rewards, key, sizeof(l_item->key), l_item);
-                pthread_rwlock_unlock(&l_ledger_pvt->rewards_rwlock);
             }
             l_outs_used--; // Do not calc this output with tx used items
-        } continue;
+            continue;
+
+        case TX_ITEM_TYPE_IN_REWARD: {
+            dap_ledger_reward_item_t *l_item = DAP_NEW_Z(dap_ledger_reward_item_t);
+            if (!l_item) {
+                log_it(L_CRITICAL, "Memory allocation error");
+                l_ret = -1;
+                goto FIN;
+            }
+            l_item->key = l_bound_item->reward_key;
+            l_item->spender_tx = *a_tx_hash;
+            pthread_rwlock_wrlock(&l_ledger_pvt->rewards_rwlock);
+            HASH_ADD(hh, l_ledger_pvt->rewards, key, sizeof(l_item->key), l_item);
+            pthread_rwlock_unlock(&l_ledger_pvt->rewards_rwlock);
+        }
+        l_outs_used--; // Do not calc this output with tx used items
+        continue;
 
         case TX_ITEM_TYPE_IN: {
             dap_ledger_wallet_balance_t *wallet_balance = NULL;
diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c
index 88cb1a7ad1..f6a971c781 100644
--- a/modules/type/blocks/dap_chain_cs_blocks.c
+++ b/modules/type/blocks/dap_chain_cs_blocks.c
@@ -1833,8 +1833,13 @@ static const dap_time_t s_block_timediff_unit = 60;
 static uint256_t s_callback_calc_reward(dap_chain_t *a_chain, dap_hash_fast_t *a_block_hash, dap_pkey_t *a_block_sign_pkey)
 {
     uint256_t l_ret = uint256_0;
-    size_t l_block_size = 0;
-    const dap_chain_block_t *l_block = dap_chain_get_atom_by_hash(a_chain, a_block_hash, &l_block_size);
+    dap_chain_cs_blocks_t *l_blocks = DAP_CHAIN_CS_BLOCKS(a_chain);
+    dap_chain_block_cache_t *l_block_cache;
+    HASH_FIND(hh, PVT(l_blocks)->blocks, a_block_hash, sizeof(*a_block_hash), l_block_cache);
+    if (!l_block_cache)
+        return l_ret;
+    const dap_chain_block_t *l_block = l_block_cache->block;
+    size_t l_block_size = l_block_cache->block_size;
     if (dap_chain_block_sign_match_pkey(l_block, l_block_size, a_block_sign_pkey)) {
         dap_chain_net_t *l_net = dap_chain_net_by_id(a_chain->net_id);
         if (!l_net) {
-- 
GitLab