From 149a449c99dd00026b6bf0d2e13576f4c4c58401 Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Mon, 18 Oct 2021 21:08:47 +0300
Subject: [PATCH] [*] Balances fix

---
 dap-sdk/core/include/dap_common.h             |  6 ++
 dap-sdk/core/include/dap_fnmatch_loop.h       |  2 +-
 dap-sdk/core/include/dap_list.h               |  6 +-
 dap-sdk/core/src/dap_circular_buffer.c        |  3 +-
 dap-sdk/core/src/dap_common.c                 |  3 +-
 dap-sdk/core/src/dap_list.c                   | 34 ++++---
 dap-sdk/crypto/src/dap_cert.c                 |  8 +-
 dap-sdk/crypto/src/dap_enc_bliss.c            |  4 +-
 dap-sdk/crypto/src/dap_enc_key.c              |  4 +-
 dap-sdk/net/client/dap_client_http.c          |  8 +-
 dap-sdk/net/client/dap_client_pvt_hh.c        |  2 +-
 dap-sdk/net/client/include/dap_client.h       |  2 +-
 dap-sdk/net/core/dap_events_socket.c          | 52 ++++-------
 dap-sdk/net/core/dap_worker.c                 |  4 +-
 dap-sdk/net/core/include/dap_events_socket.h  | 63 ++++++++++---
 dap-sdk/net/core/include/dap_net.h            |  3 +-
 dap-sdk/net/core/include/dap_server.h         | 14 +--
 dap-sdk/net/core/include/dap_timerfd.h        |  2 +-
 dap-sdk/net/core/include/dap_worker.h         |  5 +-
 dap-sdk/net/server/enc_server/dap_enc_http.c  |  7 +-
 .../server/notify_server/src/dap_notify_srv.c |  3 +-
 dap-sdk/net/stream/stream/dap_stream_worker.c |  2 +-
 .../net/stream/stream/include/dap_stream.h    |  3 +-
 modules/chain/dap_chain_ledger.c              | 91 ++++++++++---------
 .../dap_stream_ch_chain_net_srv.c             |  2 +-
 .../dap_stream_ch_chain_net_srv_pkt.c         |  2 +-
 modules/consensus/none/dap_chain_cs_none.c    |  2 +-
 modules/net/dap_chain_net.c                   |  2 +-
 modules/net/include/dap_chain_net.h           |  2 +-
 modules/net/srv/dap_chain_net_srv_client.c    |  4 +-
 modules/net/srv/dap_chain_net_srv_geoip.c     |  2 +-
 modules/net/srv/dap_chain_net_srv_order.c     |  1 -
 .../srv/dap_chain_net_srv_stream_session.c    |  2 +-
 .../srv/include/dap_chain_net_srv_common.h    |  2 +-
 .../net/srv/include/dap_chain_net_srv_order.h |  2 +-
 modules/net/srv/libmaxminddb/maxminddb.c      |  2 +-
 modules/net/srv/libmaxminddb/mmdblookup.c     |  4 +-
 modules/type/blocks/dap_chain_block_chunk.c   |  2 +-
 38 files changed, 197 insertions(+), 165 deletions(-)

diff --git a/dap-sdk/core/include/dap_common.h b/dap-sdk/core/include/dap_common.h
index 7d24b8126d..9a02eaee64 100755
--- a/dap-sdk/core/include/dap_common.h
+++ b/dap-sdk/core/include/dap_common.h
@@ -196,6 +196,7 @@ DAP_STATIC_INLINE void _dap_aligned_free( void *ptr )
 #define DAP_UINT32_FORMAT_X  "X"
 #define DAP_UINT32_FORMAT_x  "x"
 #define DAP_UINT32_FORMAT_U  "u"
+#define DAP_SIZET_FORMAT_Z   "lu"
 #elif __SIZEOF_LONG__==4
 #define DAP_UINT64_FORMAT_X  "llX"
 #define DAP_UINT64_FORMAT_x  "llx"
@@ -203,6 +204,11 @@ DAP_STATIC_INLINE void _dap_aligned_free( void *ptr )
 #define DAP_UINT32_FORMAT_X  "lX"
 #define DAP_UINT32_FORMAT_x  "lx"
 #define DAP_UINT32_FORMAT_U  "lu"
+#ifdef DAP_OS_WINDOWS
+#define DAP_SIZET_FORMAT_Z   "I64u"
+#else
+#define DAP_SIZET_FORMAT_Z   "llu"
+#endif
 #else
 #error "DAP_UINT64_FORMAT_* are undefined for your platform"
 #endif
diff --git a/dap-sdk/core/include/dap_fnmatch_loop.h b/dap-sdk/core/include/dap_fnmatch_loop.h
index 550b5d8016..7d7c7f6643 100644
--- a/dap-sdk/core/include/dap_fnmatch_loop.h
+++ b/dap-sdk/core/include/dap_fnmatch_loop.h
@@ -1055,7 +1055,7 @@ EXT (int opt, const char *pattern, const char *string, const char *string_end,
 #define NEW_PATTERN \
         struct patternlist *newp;                         \
         size_t slen = (opt == '?' || opt == '@'             \
-               ? pattern_len : (p - startp + 1));             \
+               ? pattern_len : (size_t)(p - startp + 1));             \
         slen = sizeof (struct patternlist) + (slen * sizeof (char));      \
         int malloced = ! __libc_use_alloca (alloca_used + slen);          \
         if (__builtin_expect (malloced, 0))                   \
diff --git a/dap-sdk/core/include/dap_list.h b/dap-sdk/core/include/dap_list.h
index ef5718ead9..1bf990e7fe 100755
--- a/dap-sdk/core/include/dap_list.h
+++ b/dap-sdk/core/include/dap_list.h
@@ -31,11 +31,11 @@ struct _dap_list
 dap_list_t* dap_list_alloc(void);
 void dap_list_free(dap_list_t *list);
 void dap_list_free1(dap_list_t *list);
-void dap_list_free_full(dap_list_t *list, dap_callback_destroyed_t free_func);
+void dap_list_free_full(dap_list_t *a_list, dap_callback_destroyed_t free_func);
 dap_list_t* dap_list_append(dap_list_t *list, void* data);
 dap_list_t* dap_list_prepend(dap_list_t *list, void* data);
 dap_list_t* dap_list_insert(dap_list_t *list, void* data, int position);
-dap_list_t* dap_list_insert_sorted(dap_list_t *list, void* data, dap_callback_compare_t func);
+dap_list_t* dap_list_insert_sorted(dap_list_t *list, void* data, dap_callback_compare_data_t func);
 dap_list_t* dap_list_insert_sorted_with_data(dap_list_t *list, void* data, dap_callback_compare_data_t func, void* user_data);
 dap_list_t* dap_list_insert_before(dap_list_t *list, dap_list_t *sibling, void* data);
 dap_list_t* dap_list_concat(dap_list_t *list1, dap_list_t *list2);
@@ -58,7 +58,7 @@ dap_list_t* dap_list_last(dap_list_t *list);
 dap_list_t* dap_list_first(dap_list_t *list);
 unsigned int dap_list_length(dap_list_t *list);
 void dap_list_foreach(dap_list_t *list, dap_callback_t func, void* user_data);
-dap_list_t* dap_list_sort(dap_list_t *list, dap_callback_compare_t compare_func);
+dap_list_t* dap_list_sort(dap_list_t *list, dap_callback_compare_data_t compare_func);
 dap_list_t* dap_list_sort_with_data(dap_list_t *list, dap_callback_compare_data_t compare_func, void* user_data);
 void* dap_list_nth_data(dap_list_t *list, unsigned int n);
 
diff --git a/dap-sdk/core/src/dap_circular_buffer.c b/dap-sdk/core/src/dap_circular_buffer.c
index 254571627a..5aaf60c648 100755
--- a/dap-sdk/core/src/dap_circular_buffer.c
+++ b/dap-sdk/core/src/dap_circular_buffer.c
@@ -340,7 +340,8 @@ void circular_buffer_print(circular_buffer_t cBuf, bool hex)
             dap_sprintf(str+i*2, "%c|",c);
     }
 
-    printf("CircularBuffer: %s <size %zu dataSize:%zu>\n",str,circular_buffer_get_capacity(cBuf),circular_buffer_get_data_size(cBuf));
+    printf("CircularBuffer: %s <size %"DAP_SIZET_FORMAT_Z" dataSize:%"DAP_SIZET_FORMAT_Z">\n",
+           str,circular_buffer_get_capacity(cBuf),circular_buffer_get_data_size(cBuf));
 
     free(str);
 }
diff --git a/dap-sdk/core/src/dap_common.c b/dap-sdk/core/src/dap_common.c
index 4ef9badc14..79a166e69a 100755
--- a/dap-sdk/core/src/dap_common.c
+++ b/dap-sdk/core/src/dap_common.c
@@ -262,7 +262,7 @@ int wdap_common_init( const char *a_console_title, const wchar_t *a_log_filename
         if( s_log_file == NULL)
             s_log_file = _wfopen( a_log_filename , L"w" );
         if ( s_log_file == NULL ) {
-            dap_fprintf( stderr, "Can't open log file %s to append\n", a_log_filename );
+            dap_fprintf( stderr, "Can't open log file %ls to append\n", a_log_filename );
             return -1;
         }
         //dap_stpcpy(s_log_file_path, a_log_filename);
@@ -355,6 +355,7 @@ void _log_it(const char *a_log_tag, enum dap_log_level a_ll, const char *a_fmt,
     va_end( va );
     char *dummy = (offset2 == 0) ? memcpy(&l_log_string->str[sizeof(l_log_string->str) - 6], "...\n\0", 5)
         : memcpy(&l_log_string->str[offset], "\n", 1);
+    UNUSED(dummy);
     pthread_mutex_lock(&s_log_mutex);
     DL_APPEND(s_log_buffer, l_log_string);
     ++s_log_count;
diff --git a/dap-sdk/core/src/dap_list.c b/dap-sdk/core/src/dap_list.c
index 6308e74123..9fa0d16b3a 100755
--- a/dap-sdk/core/src/dap_list.c
+++ b/dap-sdk/core/src/dap_list.c
@@ -62,10 +62,14 @@ void dap_list_free1(dap_list_t *list)
  * Convenience method, which frees all the memory used by a DapList,
  * and calls @free_func on every element's data.
  */
-void dap_list_free_full(dap_list_t *list, dap_callback_destroyed_t free_func)
+void dap_list_free_full(dap_list_t *a_list, dap_callback_destroyed_t a_free_func)
 {
-    dap_list_foreach(list, (dap_callback_t) free_func, NULL);
-    dap_list_free(list);
+    dap_list_t *l_list = a_list;
+    while (l_list) {
+        a_free_func(l_list->data);
+        l_list = l_list->next;
+    }
+    dap_list_free(a_list);
 }
 
 /**
@@ -790,7 +794,7 @@ void dap_list_foreach(dap_list_t *list, dap_callback_t func, void* user_data)
     }
 }
 
-static dap_list_t* dap_list_insert_sorted_real(dap_list_t *list, void* data, dap_callback_t func, void* user_data)
+static dap_list_t* dap_list_insert_sorted_real(dap_list_t *list, void* data, dap_callback_compare_data_t func, void* user_data)
 {
     dap_list_t *tmp_list = list;
     dap_list_t *new_list;
@@ -805,13 +809,13 @@ static dap_list_t* dap_list_insert_sorted_real(dap_list_t *list, void* data, dap
         return new_list;
     }
 
-    cmp = ((dap_callback_compare_data_t) func)(data, tmp_list->data, user_data);
+    cmp = func(data, tmp_list->data, user_data);
 
     while((tmp_list->next) && (cmp > 0))
     {
         tmp_list = tmp_list->next;
 
-        cmp = ((dap_callback_compare_data_t) func)(data, tmp_list->data, user_data);
+        cmp = func(data, tmp_list->data, user_data);
     }
 
     new_list = dap_list_alloc();
@@ -857,9 +861,9 @@ static dap_list_t* dap_list_insert_sorted_real(dap_list_t *list, void* data, dap
  *
  * Returns: the (possibly changed) start of the DapList
  */
-dap_list_t *dap_list_insert_sorted(dap_list_t *list, void* data, dap_callback_compare_t func)
+dap_list_t *dap_list_insert_sorted(dap_list_t *list, void* data, dap_callback_compare_data_t func)
 {
-    return dap_list_insert_sorted_real(list, data, (dap_callback_t) func, NULL);
+    return dap_list_insert_sorted_real(list, data, func, NULL);
 }
 
 /**
@@ -884,10 +888,10 @@ dap_list_t *dap_list_insert_sorted(dap_list_t *list, void* data, dap_callback_co
  */
 dap_list_t * dap_list_insert_sorted_with_data(dap_list_t *list, void* data, dap_callback_compare_data_t func, void* user_data)
 {
-    return dap_list_insert_sorted_real(list, data, (dap_callback_t) func, user_data);
+    return dap_list_insert_sorted_real(list, data, func, user_data);
 }
 
-static dap_list_t *dap_list_sort_merge(dap_list_t *l1, dap_list_t *l2, dap_callback_t compare_func, void* user_data)
+static dap_list_t *dap_list_sort_merge(dap_list_t *l1, dap_list_t *l2, dap_callback_compare_data_t compare_func, void* user_data)
 {
     dap_list_t list, *l, *lprev;
     int cmp;
@@ -897,7 +901,7 @@ static dap_list_t *dap_list_sort_merge(dap_list_t *l1, dap_list_t *l2, dap_callb
 
     while(l1 && l2)
     {
-        cmp = ((dap_callback_compare_data_t) compare_func)(l1->data, l2->data, user_data);
+        cmp = compare_func(l1->data, l2->data, user_data);
 
         if(cmp <= 0)
                 {
@@ -919,7 +923,7 @@ static dap_list_t *dap_list_sort_merge(dap_list_t *l1, dap_list_t *l2, dap_callb
     return list.next;
 }
 
-static dap_list_t *dap_list_sort_real(dap_list_t *list, dap_callback_t compare_func, void* user_data)
+static dap_list_t *dap_list_sort_real(dap_list_t *list, dap_callback_compare_data_t compare_func, void* user_data)
 {
     dap_list_t *l1, *l2;
 
@@ -973,9 +977,9 @@ static dap_list_t *dap_list_sort_real(dap_list_t *list, dap_callback_t compare_f
  * Returns: negative value if @a < @b; zero if @a = @b; positive
  *          value if @a > @b
  */
-dap_list_t *dap_list_sort(dap_list_t *list, dap_callback_compare_t compare_func)
+dap_list_t *dap_list_sort(dap_list_t *list, dap_callback_compare_data_t compare_func)
 {
-    return dap_list_sort_real(list, (dap_callback_t) compare_func, NULL);
+    return dap_list_sort_real(list, compare_func, NULL);
 }
 
 /**
@@ -1005,5 +1009,5 @@ dap_list_t *dap_list_sort(dap_list_t *list, dap_callback_compare_t compare_func)
  */
 dap_list_t *dap_list_sort_with_data(dap_list_t *list, dap_callback_compare_data_t compare_func, void* user_data)
 {
-    return dap_list_sort_real(list, (dap_callback_t) compare_func, user_data);
+    return dap_list_sort_real(list, compare_func, user_data);
 }
diff --git a/dap-sdk/crypto/src/dap_cert.c b/dap-sdk/crypto/src/dap_cert.c
index 131b7415c5..6d3d082906 100755
--- a/dap-sdk/crypto/src/dap_cert.c
+++ b/dap-sdk/crypto/src/dap_cert.c
@@ -455,11 +455,11 @@ void dap_cert_dump(dap_cert_t * a_cert)
 {
     printf ("Certificate name: %s\n",a_cert->name);
     printf ("Signature type: %s\n", dap_sign_type_to_str( dap_sign_type_from_key_type(a_cert->enc_key->type) ) );
-    printf ("Private key size: %lu\n",a_cert->enc_key->priv_key_data_size);
-    printf ("Public key size: %lu\n", a_cert->enc_key->pub_key_data_size);
+    printf ("Private key size: %"DAP_SIZET_FORMAT_Z"\n",a_cert->enc_key->priv_key_data_size);
+    printf ("Public key size: %"DAP_SIZET_FORMAT_Z"\n", a_cert->enc_key->pub_key_data_size);
     size_t l_meta_items_cnt = dap_binary_tree_count(a_cert->metadata);
-    printf ("Metadata section count: %lu\n", l_meta_items_cnt);
-    printf ("Certificates signatures chain size: %lu\n",dap_cert_count_cert_sign (a_cert));
+    printf ("Metadata section count: %"DAP_SIZET_FORMAT_Z"\n", l_meta_items_cnt);
+    printf ("Certificates signatures chain size: %"DAP_SIZET_FORMAT_Z"\n",dap_cert_count_cert_sign (a_cert));
     if (l_meta_items_cnt) {
         printf ("Metadata sections\n");
         dap_list_t *l_meta_list = dap_binary_tree_inorder_list(a_cert->metadata);
diff --git a/dap-sdk/crypto/src/dap_enc_bliss.c b/dap-sdk/crypto/src/dap_enc_bliss.c
index e75d0f8d8d..29e103aba9 100755
--- a/dap-sdk/crypto/src/dap_enc_bliss.c
+++ b/dap-sdk/crypto/src/dap_enc_bliss.c
@@ -22,8 +22,8 @@ void dap_enc_sig_bliss_key_new(struct dap_enc_key *key) {
 
     key->type = DAP_ENC_KEY_TYPE_SIG_BLISS;
     key->enc = NULL;
-    key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_bliss_get_sign;
-    key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_bliss_verify_sign;
+    //key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_bliss_get_sign;
+    //key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_bliss_verify_sign;
 }
 
 /**
diff --git a/dap-sdk/crypto/src/dap_enc_key.c b/dap-sdk/crypto/src/dap_enc_key.c
index 8d4c458ae0..db75d707d3 100755
--- a/dap-sdk/crypto/src/dap_enc_key.c
+++ b/dap-sdk/crypto/src/dap_enc_key.c
@@ -760,7 +760,7 @@ dap_enc_key_t* dap_enc_key_deserealize(const void *buf, size_t buf_size)
 dap_enc_key_t *dap_enc_key_new(dap_enc_key_type_t a_key_type)
 {
     dap_enc_key_t * ret = NULL;
-    if(a_key_type < c_callbacks_size ){
+    if ((size_t)a_key_type < c_callbacks_size) {
         ret = DAP_NEW_Z(dap_enc_key_t);
         if(s_callbacks[a_key_type].new_callback){
             s_callbacks[a_key_type].new_callback(ret);
@@ -786,7 +786,7 @@ dap_enc_key_t *dap_enc_key_new_generate(dap_enc_key_type_t a_key_type, const voi
                                         size_t seed_size, size_t key_size)
 {
     dap_enc_key_t * ret = NULL;
-    if(a_key_type< c_callbacks_size ) {
+    if ((size_t)a_key_type < c_callbacks_size) {
         ret = dap_enc_key_new(a_key_type);
         if(s_callbacks[a_key_type].new_generate_callback) {
             s_callbacks[a_key_type].new_generate_callback( ret, kex_buf, kex_size, seed, seed_size, key_size);
diff --git a/dap-sdk/net/client/dap_client_http.c b/dap-sdk/net/client/dap_client_http.c
index 48c4210d42..c1ebf56430 100644
--- a/dap-sdk/net/client/dap_client_http.c
+++ b/dap-sdk/net/client/dap_client_http.c
@@ -24,10 +24,10 @@
 #include <errno.h>
 #include <fcntl.h>
 
+#include "dap_net.h"
 #include "dap_common.h"
 #include "dap_strfuncs.h"
 #include "dap_string.h"
-#include "dap_net.h"
 #include "dap_events_socket.h"
 #include "dap_timerfd.h"
 #include "dap_stream_ch_proc.h"
@@ -642,6 +642,7 @@ void* dap_client_http_request_custom(dap_worker_t * a_worker, const char *a_upli
         return NULL;
     }
 #endif
+    return NULL;
 }
 
 #ifndef DAP_NET_CLIENT_NO_SSL
@@ -678,8 +679,7 @@ static void s_http_connected(dap_events_socket_t * a_esocket)
     assert(a_esocket);
     dap_client_http_pvt_t * l_http_pvt = PVT(a_esocket);
     assert(l_http_pvt);
-    dap_worker_t *l_worker = l_http_pvt->worker;
-    assert(l_worker);
+    assert(l_http_pvt->worker);
 
     log_it(L_INFO, "Remote address connected (%s:%u) with sock_id %d", l_http_pvt->uplink_addr, l_http_pvt->uplink_port, a_esocket->socket);
     // add to dap_worker
@@ -713,7 +713,7 @@ static void s_http_connected(dap_events_socket_t * a_esocket)
                 : 0;
 
         // Set request size as Content-Length header
-        l_offset += dap_snprintf(l_request_headers + l_offset, l_offset2 -= l_offset, "Content-Length: %lu\r\n", l_http_pvt->request_size);
+        l_offset += dap_snprintf(l_request_headers + l_offset, l_offset2 -= l_offset, "Content-Length: %"DAP_SIZET_FORMAT_Z"\r\n", l_http_pvt->request_size);
     }
 
     // adding string for GET request
diff --git a/dap-sdk/net/client/dap_client_pvt_hh.c b/dap-sdk/net/client/dap_client_pvt_hh.c
index a469913d0f..22c0e6869f 100644
--- a/dap-sdk/net/client/dap_client_pvt_hh.c
+++ b/dap-sdk/net/client/dap_client_pvt_hh.c
@@ -24,8 +24,8 @@
 #include <pthread.h>
 #include <uthash.h>
 
-#include "dap_common.h"
 #include "dap_client_pvt.h"
+#include "dap_common.h"
 
 typedef struct dap_client_pvt_hh {
     uint64_t client_pvt_uuid;
diff --git a/dap-sdk/net/client/include/dap_client.h b/dap-sdk/net/client/include/dap_client.h
index 1c6920b1b6..8457e838b7 100644
--- a/dap-sdk/net/client/include/dap_client.h
+++ b/dap-sdk/net/client/include/dap_client.h
@@ -23,9 +23,9 @@
 */
 #pragma once
 
+#include "dap_events.h"
 #include <stdint.h>
 #include "dap_enc_key.h"
-#include "dap_events.h"
 #include "dap_stream.h"
 #include "dap_stream_ch.h"
 #include "dap_cert.h"
diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c
index 59dd701d62..4317deeb59 100644
--- a/dap-sdk/net/core/dap_events_socket.c
+++ b/dap-sdk/net/core/dap_events_socket.c
@@ -149,6 +149,12 @@ void dap_events_socket_deinit( )
 #ifdef DAP_OS_WINDOWS
 void __stdcall mq_receive_cb(HRESULT hr, QUEUEHANDLE qh, DWORD timeout
                              , DWORD action, MQMSGPROPS *pmsgprops, LPOVERLAPPED pov, HANDLE cursor) {
+    UNUSED(hr);
+    UNUSED(qh);
+    UNUSED(timeout);
+    UNUSED(action);
+    UNUSED(pmsgprops);
+    UNUSED(cursor);
     switch (hr) {
     case MQ_OK:
         SetEvent(pov->hEvent);
@@ -263,7 +269,11 @@ void dap_events_socket_reassign_between_workers_mt(dap_worker_t * a_worker_old,
     l_msg->esocket_uuid = a_es->uuid;
     l_msg->worker_new = a_worker_new;
     if( dap_events_socket_queue_ptr_send(a_worker_old->queue_es_reassign, l_msg) != 0 ){
+#ifdef DAP_OS_WINDOWS
+        log_it(L_ERROR,"Haven't sent reassign message with esocket %"DAP_UINT64_FORMAT_U, a_es ? a_es->socket : (SOCKET)-1);
+#else
         log_it(L_ERROR,"Haven't sent reassign message with esocket %d", a_es?a_es->socket:-1);
+#endif
         DAP_DELETE(l_msg);
     }
 }
@@ -278,6 +288,9 @@ void dap_events_socket_reassign_between_workers_mt(dap_worker_t * a_worker_old,
 dap_events_socket_t * s_create_type_pipe(dap_worker_t * a_w, dap_events_socket_callback_t a_callback, uint32_t a_flags)
 {
 #ifdef DAP_OS_WINDOWS
+    UNUSED(a_w);
+    UNUSED(a_callback);
+    UNUSED(a_flags);
     return NULL;
 #else
     UNUSED(a_flags);
@@ -1091,7 +1104,11 @@ typedef struct dap_events_socket_buf_item
  *  Waits on the socket
  *  return 0: timeout, 1: may send data, -1 error
  */
+#ifdef DAP_OS_WINDOWS
+static int wait_send_socket(SOCKET a_sockfd, long timeout_ms)
+#else
 static int wait_send_socket(int a_sockfd, long timeout_ms)
+#endif
 {
     struct timeval l_tv;
     fd_set l_outfd, l_errfd;
@@ -1162,6 +1179,7 @@ static void *dap_events_socket_buf_thread(void *arg)
 
     DAP_DELETE(l_item);
     pthread_exit(0);
+    return NULL;
 }
 
 static void add_ptr_to_buf(dap_events_socket_t * a_es, void* a_arg)
@@ -2197,37 +2215,3 @@ void dap_events_socket_shrink_buf_in(dap_events_socket_t * cl, size_t shrink_siz
     }
 
 }
-
-#ifdef DAP_OS_WINDOWS
-inline int dap_recvfrom(SOCKET s, void* buf_in, size_t buf_size) {
-    struct sockaddr_in l_dummy;
-    socklen_t l_size = sizeof(l_dummy);
-    int ret;
-    if (buf_in) {
-        memset(buf_in, 0, buf_size);
-        ret = recvfrom(s, (char*)buf_in, (long)buf_size, 0, (struct sockaddr *)&l_dummy, &l_size);
-    } else {
-        char l_tempbuf[sizeof(void*)];
-        ret = recvfrom(s, l_tempbuf, sizeof(l_tempbuf), 0, (struct sockaddr *)&l_dummy, &l_size);
-    }
-    return ret;
-}
-
-inline int dap_sendto(SOCKET s, u_short port, void* buf_out, size_t buf_out_size) {
-    int l_addr_len;
-    struct sockaddr_in l_addr;
-    l_addr.sin_family = AF_INET;
-    IN_ADDR _in_addr = { { .S_addr = htonl(INADDR_LOOPBACK) } };
-    l_addr.sin_addr = _in_addr;
-    l_addr.sin_port = port;
-    l_addr_len = sizeof(struct sockaddr_in);
-    int ret;
-    if (buf_out) {
-        ret = sendto(s, (char*)buf_out, (long)buf_out_size, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&l_addr, l_addr_len);
-    } else {
-        char l_bytes[sizeof(void*)] = { 0 };
-        ret = sendto(s, l_bytes, sizeof(l_bytes), MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&l_addr, l_addr_len);
-    }
-    return ret;
-}
-#endif
diff --git a/dap-sdk/net/core/dap_worker.c b/dap-sdk/net/core/dap_worker.c
index 4320297355..d05d1847ad 100644
--- a/dap-sdk/net/core/dap_worker.c
+++ b/dap-sdk/net/core/dap_worker.c
@@ -916,7 +916,7 @@ static void s_queue_add_es_callback( dap_events_socket_t * a_es, void * a_arg)
         l_es_new->type != DESCRIPTOR_TYPE_TIMER
             )
 #else
-    if(l_es_new->socket!=0 && l_es_new->socket != -1)
+    if(l_es_new->socket!=0 && l_es_new->socket != INVALID_SOCKET)
 
 #endif
     if(dap_worker_esocket_find_uuid( l_worker, l_es_new->uuid)){
@@ -955,7 +955,7 @@ static void s_queue_add_es_callback( dap_events_socket_t * a_es, void * a_arg)
     }else{
         // Add in worker
         l_es_new->me = l_es_new;
-        if (l_es_new->socket!=0 && l_es_new->socket != -1){
+        if (l_es_new->socket!=0 && l_es_new->socket != INVALID_SOCKET){
             pthread_rwlock_wrlock(&l_worker->esocket_rwlock);
             HASH_ADD(hh_worker, l_worker->esockets, uuid, sizeof(l_es_new->uuid), l_es_new );
             l_worker->event_sockets_count++;
diff --git a/dap-sdk/net/core/include/dap_events_socket.h b/dap-sdk/net/core/include/dap_events_socket.h
index b1b641a0c2..0bebbb2fff 100644
--- a/dap-sdk/net/core/include/dap_events_socket.h
+++ b/dap-sdk/net/core/include/dap_events_socket.h
@@ -21,6 +21,18 @@
     along with any DAP SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 #pragma once
+#ifndef DAP_OS_WINDOWS
+#include "unistd.h"
+typedef int SOCKET;
+#define closesocket close
+#define INVALID_SOCKET  -1  // for win32 =  (SOCKET)(~0)
+#define SOCKET_ERROR    -1  // for win32 =  (-1)
+#else
+#include <ws2tcpip.h>
+#include <mq.h>
+#define INVALID_SOCKET (SOCKET)(~0)
+#endif
+
 #include <pthread.h>
 #include "uthash.h"
 
@@ -29,14 +41,6 @@
 
 #define DAP_EVENTS_SOCKET_MAX 8194
 
-#ifndef _WIN32
-#include "unistd.h"
-typedef int SOCKET;
-#define closesocket close
-#define INVALID_SOCKET  -1  // for win32 =  (SOCKET)(~0)
-#define SOCKET_ERROR    -1  // for win32 =  (-1)
-#endif
-
 // Caps for different platforms
 #if defined (DAP_OS_ANDROID)
 #define DAP_EVENTS_CAPS_POLL
@@ -76,10 +80,12 @@ typedef int SOCKET;
     #define DAP_EVENTS_CAPS_EVENT_WEVENT
     //#define DAP_EVENTS_CAPS_PIPE_POSIX
     #define DAP_EVENTS_CAPS_MSMQ
-    #define INET_ADDRSTRLEN     16
-    #define INET6_ADDRSTRLEN    46
-#include <mq.h>
-#include <ws2tcpip.h>
+    #ifndef INET_ADDRSTRLEN
+        #define INET_ADDRSTRLEN     16
+    #endif
+    #ifndef INET6_ADDRSTRLEN
+        #define INET6_ADDRSTRLEN    46
+    #endif
 #define MSG_DONTWAIT 0
 #define MSG_NOSIGNAL 0
 #endif
@@ -378,8 +384,37 @@ void dap_events_socket_remove_from_worker_unsafe( dap_events_socket_t *a_es, dap
 void dap_events_socket_shrink_buf_in(dap_events_socket_t * cl, size_t shrink_size);
 
 #ifdef DAP_OS_WINDOWS
-extern inline int dap_recvfrom(SOCKET s, void* buf_in, size_t buf_size);
-extern inline int dap_sendto(SOCKET s, u_short port, void* buf_in, size_t buf_size);
+DAP_STATIC_INLINE int dap_recvfrom(SOCKET s, void* buf_in, size_t buf_size) {
+    struct sockaddr_in l_dummy;
+    socklen_t l_size = sizeof(l_dummy);
+    int ret;
+    if (buf_in) {
+        memset(buf_in, 0, buf_size);
+        ret = recvfrom(s, (char*)buf_in, (long)buf_size, 0, (struct sockaddr *)&l_dummy, &l_size);
+    } else {
+        char l_tempbuf[sizeof(void*)];
+        ret = recvfrom(s, l_tempbuf, sizeof(l_tempbuf), 0, (struct sockaddr *)&l_dummy, &l_size);
+    }
+    return ret;
+}
+
+DAP_STATIC_INLINE int dap_sendto(SOCKET s, u_short port, void* buf_out, size_t buf_out_size) {
+    int l_addr_len;
+    struct sockaddr_in l_addr;
+    l_addr.sin_family = AF_INET;
+    IN_ADDR _in_addr = { { .S_addr = htonl(INADDR_LOOPBACK) } };
+    l_addr.sin_addr = _in_addr;
+    l_addr.sin_port = port;
+    l_addr_len = sizeof(struct sockaddr_in);
+    int ret;
+    if (buf_out) {
+        ret = sendto(s, (char*)buf_out, (long)buf_out_size, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&l_addr, l_addr_len);
+    } else {
+        char l_bytes[sizeof(void*)] = { 0 };
+        ret = sendto(s, l_bytes, sizeof(l_bytes), MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *)&l_addr, l_addr_len);
+    }
+    return ret;
+}
 #endif
 
 
diff --git a/dap-sdk/net/core/include/dap_net.h b/dap-sdk/net/core/include/dap_net.h
index 92ce284136..2c79b8971f 100644
--- a/dap-sdk/net/core/include/dap_net.h
+++ b/dap-sdk/net/core/include/dap_net.h
@@ -22,7 +22,6 @@
 */
 #pragma once
 
-#include "dap_common.h"
 #ifdef WIN32
 // for Windows
 #include <winsock2.h>
@@ -43,4 +42,6 @@
 
 #endif
 
+#include "dap_common.h"
+
 int dap_net_resolve_host(const char *a_host, int ai_family, struct sockaddr *a_addr_out);
diff --git a/dap-sdk/net/core/include/dap_server.h b/dap-sdk/net/core/include/dap_server.h
index 94c7d503bc..362e7f2390 100644
--- a/dap-sdk/net/core/include/dap_server.h
+++ b/dap-sdk/net/core/include/dap_server.h
@@ -24,6 +24,13 @@
 
 #pragma once
 
+#include <pthread.h>
+#include "uthash.h"
+#include "utlist.h"
+
+#include "dap_list.h"
+#include "dap_cpu_monitor.h"
+#include "dap_events_socket.h"
 #include "dap_common.h"
 
 #ifdef DAP_OS_UNIX
@@ -49,13 +56,6 @@
 #error "No poll headers for your platform"
 #endif
 
-#include <pthread.h>
-#include "uthash.h"
-#include "utlist.h"
-
-#include "dap_list.h"
-#include "dap_cpu_monitor.h"
-#include "dap_events_socket.h"
 
 typedef enum dap_server_type {SERVER_TCP, SERVER_UDP,SERVER_LOCAL} dap_server_type_t;
 
diff --git a/dap-sdk/net/core/include/dap_timerfd.h b/dap-sdk/net/core/include/dap_timerfd.h
index 6ce1acbf48..6247112a51 100644
--- a/dap-sdk/net/core/include/dap_timerfd.h
+++ b/dap-sdk/net/core/include/dap_timerfd.h
@@ -38,8 +38,8 @@
 #define _MSEC -10000
 #endif
 
-#include "dap_common.h"
 #include "dap_events_socket.h"
+#include "dap_common.h"
 #include "dap_proc_thread.h"
 
 typedef bool (*dap_timerfd_callback_t)(void* ); // Callback for timer. If return true,
diff --git a/dap-sdk/net/core/include/dap_worker.h b/dap-sdk/net/core/include/dap_worker.h
index 553746982c..849d567299 100644
--- a/dap-sdk/net/core/include/dap_worker.h
+++ b/dap-sdk/net/core/include/dap_worker.h
@@ -22,11 +22,10 @@
 */
 #pragma once
 
-
+#include "dap_events_socket.h"
 #include <pthread.h>
-
 #include "dap_common.h"
-#include "dap_events_socket.h"
+
 
 
 typedef struct dap_proc_queue dap_proc_queue_t;
diff --git a/dap-sdk/net/server/enc_server/dap_enc_http.c b/dap-sdk/net/server/enc_server/dap_enc_http.c
index 6d296da4e9..aea5a1dbd5 100644
--- a/dap-sdk/net/server/enc_server/dap_enc_http.c
+++ b/dap-sdk/net/server/enc_server/dap_enc_http.c
@@ -22,9 +22,7 @@
 #include <string.h>
 #include <stdint.h>
 
-#include "dap_common.h"
-
-#ifdef _WIN32
+#ifdef DAP_OS_WINDOWS
 #include <winsock2.h>
 #include <windows.h>
 #include <mswsock.h>
@@ -33,6 +31,7 @@
 #include <time.h>
 #endif
 
+#include "dap_common.h"
 #include <pthread.h>
 
 
@@ -104,7 +103,7 @@ void enc_http_proc(struct dap_http_simple *cl_st, void * arg)
         dap_enc_key_type_t l_enc_block_type = DAP_ENC_KEY_TYPE_IAES;
         size_t l_pkey_exchange_size=MSRLN_PKA_BYTES;
         size_t l_block_key_size=32;
-        sscanf(cl_st->http_client->in_query_string, "enc_type=%d,pkey_exchange_type=%d,pkey_exchange_size=%zd,block_key_size=%zd",
+        sscanf(cl_st->http_client->in_query_string, "enc_type=%d,pkey_exchange_type=%d,pkey_exchange_size=%"DAP_SIZET_FORMAT_Z",block_key_size=%"DAP_SIZET_FORMAT_Z,
                                       &l_enc_block_type,&l_pkey_exchange_type,&l_pkey_exchange_size,&l_block_key_size);
 
         log_it(L_DEBUG, "Stream encryption: %s\t public key exchange: %s",dap_enc_get_type_name(l_enc_block_type),
diff --git a/dap-sdk/net/server/notify_server/src/dap_notify_srv.c b/dap-sdk/net/server/notify_server/src/dap_notify_srv.c
index 922a37a7a3..b0838bc0e8 100644
--- a/dap-sdk/net/server/notify_server/src/dap_notify_srv.c
+++ b/dap-sdk/net/server/notify_server/src/dap_notify_srv.c
@@ -24,12 +24,11 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <unistd.h>
-
+#include "dap_events_socket.h"
 #include "dap_common.h"
 #include "dap_config.h"
 #include "dap_list.h"
 #include "dap_strfuncs.h"
-#include "dap_events_socket.h"
 #include "dap_server.h"
 #include "dap_events.h"
 #include "dap_notify_srv.h"
diff --git a/dap-sdk/net/stream/stream/dap_stream_worker.c b/dap-sdk/net/stream/stream/dap_stream_worker.c
index 45ab6fc065..ca8ab22b36 100644
--- a/dap-sdk/net/stream/stream/dap_stream_worker.c
+++ b/dap-sdk/net/stream/stream/dap_stream_worker.c
@@ -20,9 +20,9 @@
     You should have received a copy of the GNU General Public License
     along with any DAP SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
-#include "dap_common.h"
 #include "dap_events.h"
 #include "dap_events_socket.h"
+#include "dap_common.h"
 #include "dap_stream_worker.h"
 #include "dap_stream_ch_pkt.h"
 
diff --git a/dap-sdk/net/stream/stream/include/dap_stream.h b/dap-sdk/net/stream/stream/include/dap_stream.h
index 21b3aee36a..4cc86e6633 100644
--- a/dap-sdk/net/stream/stream/include/dap_stream.h
+++ b/dap-sdk/net/stream/stream/include/dap_stream.h
@@ -27,12 +27,11 @@
 #include <stdbool.h>
 #include <pthread.h>
 
+#include "dap_events_socket.h"
 #include "dap_config.h"
 #include "dap_stream_session.h"
 #include "dap_stream_ch.h"
 
-#include "dap_events_socket.h"
-
 #define CHUNK_SIZE_MAX (3 * 1024)
 
 typedef struct dap_client_remote dap_client_remote_t;
diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c
index 94208cc05f..9f3b12182d 100644
--- a/modules/chain/dap_chain_ledger.c
+++ b/modules/chain/dap_chain_ledger.c
@@ -1426,9 +1426,9 @@ dap_chain_datum_tx_t* dap_chain_ledger_tx_find_by_hash(dap_ledger_t *a_ledger, d
 static bool dap_chain_ledger_item_is_used_out(dap_chain_ledger_tx_item_t *a_item, int a_idx_out)
 {
     bool l_used_out = false;
-    if(!a_item) {
+    if (!a_item || !a_item->cache_data.n_outs) {
         //log_it(L_DEBUG, "list_cached_item is NULL");
-        return false;
+        return true;
     }
     if(a_idx_out >= MAX_OUT_ITEMS) {
         if(s_debug_more)
@@ -1995,7 +1995,7 @@ int dap_chain_ledger_tx_add_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *
     return 0;
 }
 
-int dap_chain_ledger_balance_cache_update(dap_ledger_t *a_ledger, dap_ledger_wallet_balance_t *a_balance)
+static int s_balance_cache_update(dap_ledger_t *a_ledger, dap_ledger_wallet_balance_t *a_balance)
 {
     char *l_gdb_group = dap_chain_ledger_get_gdb_group(a_ledger, DAP_CHAIN_LEDGER_BALANCES_STR);
     uint128_t *l_balance_value = DAP_NEW_Z(uint128_t);
@@ -2006,6 +2006,25 @@ int dap_chain_ledger_balance_cache_update(dap_ledger_t *a_ledger, dap_ledger_wal
         DAP_DELETE(l_balance_value);
         return -1;
     }
+    DAP_DELETE(l_gdb_group);
+    return 0;
+}
+
+static int s_tx_cache_update(dap_ledger_t *a_ledger, dap_chain_ledger_tx_item_t *a_item)
+{
+    size_t l_tx_size = dap_chain_datum_tx_get_size(a_item->tx);
+    uint8_t *l_tx_cache = DAP_NEW_Z_SIZE(uint8_t, l_tx_size + sizeof(a_item->cache_data));
+    memcpy(l_tx_cache, &a_item->cache_data, sizeof(a_item->cache_data));
+    memcpy(l_tx_cache + sizeof(a_item->cache_data), a_item->tx, l_tx_size);
+    char *l_gdb_group = dap_chain_ledger_get_gdb_group(a_ledger, DAP_CHAIN_LEDGER_TXS_STR);
+    char *l_tx_hash_str = dap_chain_hash_fast_to_str_new(&a_item->tx_hash_fast);
+    if (!dap_chain_global_db_gr_set(l_tx_hash_str, l_tx_cache, l_tx_size + sizeof(a_item->cache_data), l_gdb_group)) {
+        if(s_debug_more)
+            log_it(L_WARNING, "Ledger cache mismatch");
+        DAP_DELETE(l_tx_cache);
+        return -1;
+    }
+    DAP_DELETE(l_gdb_group);
     return 0;
 }
 
@@ -2110,7 +2129,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
             l_multichannel = true;
         }
         l_ticker_old_trl = dap_stpcpy(l_token_ticker_old, l_token_ticker);
-        dap_chain_hash_fast_t *l_tx_prev_hash;
+        int l_tx_prev_out_used_idx;
         if (l_type == TX_ITEM_TYPE_IN) {
             dap_chain_tx_in_t *l_tx_in = bound_item->in.tx_cur_in;
             dap_ledger_wallet_balance_t *wallet_balance = NULL;
@@ -2133,7 +2152,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
                 uint128_t l_sub = dap_chain_uint128_from(l_value);
                 wallet_balance->balance = dap_uint128_substract(wallet_balance->balance, l_sub);
                 // Update the cache
-                dap_chain_ledger_balance_cache_update(a_ledger, wallet_balance);
+                s_balance_cache_update(a_ledger, wallet_balance);
             } else {
                 if(s_debug_more)
                     log_it(L_ERROR,"!!! Attempt to SPEND from some non-existent balance !!!: %s %s", l_addr_str, l_token_ticker);
@@ -2141,17 +2160,18 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
             DAP_DELETE(l_addr_str);
             DAP_DELETE(l_wallet_balance_key);
             /// Mark 'out' item in cache because it used
-            l_tx_prev_hash = &(l_prev_item_out->cache_data.tx_hash_spent_fast[l_tx_in->header.tx_out_prev_idx]);
+            l_tx_prev_out_used_idx = l_tx_in->header.tx_out_prev_idx;
         } else { // TX_ITEM_TYPE_IN_COND
             // all balance deducts performed with previous conditional transaction
             dap_chain_tx_in_cond_t *l_tx_in_cond = bound_item->in.tx_cur_in_cond;
             /// Mark 'out' item in cache because it used
-            l_tx_prev_hash = &(l_prev_item_out->cache_data.tx_hash_spent_fast[l_tx_in_cond->header.tx_out_prev_idx]);
+            l_tx_prev_out_used_idx = l_tx_in_cond->header.tx_out_prev_idx;
         }
-        memcpy(l_tx_prev_hash, l_tx_hash, sizeof(dap_chain_hash_fast_t));
         // add a used output
+        memcpy(&(l_prev_item_out->cache_data.tx_hash_spent_fast[l_tx_prev_out_used_idx]), l_tx_hash, sizeof(dap_chain_hash_fast_t));
         l_prev_item_out->cache_data.n_outs_used++;
-        char * l_tx_prev_hash_str = dap_chain_hash_fast_to_str_new(l_tx_prev_hash);
+        // mirror it in the cache
+        s_tx_cache_update(a_ledger, l_prev_item_out);
 
         // delete previous transactions from cache because all out is used
         if(l_prev_item_out->cache_data.n_outs_used == l_prev_item_out->cache_data.n_outs) {
@@ -2159,31 +2179,26 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
             // remove from memory ledger
             int res = dap_chain_ledger_tx_remove(a_ledger, &l_tx_prev_hash_to_del);
             if(res == -2) {
-                if(s_debug_more)
+                if(s_debug_more) {
+                    char * l_tx_prev_hash_str = dap_chain_hash_fast_to_str_new(&l_tx_prev_hash_to_del);
                     log_it(L_ERROR, "Can't delete previous transactions because hash=0x%x not found", l_tx_prev_hash_str);
+                    DAP_DELETE(l_tx_prev_hash_str);
+                }
                 ret = -100;
-                DAP_DELETE(l_tx_prev_hash_str);
                 dap_list_free_full(l_list_bound_items, free);
                 goto FIN;
             }
             else if(res != 1) {
-                if(s_debug_more)
+                if(s_debug_more) {
+                    char * l_tx_prev_hash_str = dap_chain_hash_fast_to_str_new(&l_tx_prev_hash_to_del);
                     log_it(L_ERROR, "Can't delete previous transactions with hash=0x%x", l_tx_prev_hash_str);
+                    DAP_DELETE(l_tx_prev_hash_str);
+                }
                 ret = -101;
-                DAP_DELETE(l_tx_prev_hash_str);
                 dap_list_free_full(l_list_bound_items, free);
                 goto FIN;
             }
-            // TODO restore when the blockchain appears
-            // remove from mempool ledger
-            /*char *l_tx_prev_hash_to_del_str = dap_chain_hash_fast_to_str_new(&l_tx_prev_hash_to_del);
-             if(!dap_chain_global_db_gr_del( dap_strdup(l_tx_prev_hash_to_del_str), c_dap_datum_mempool_gdb_group)) {
-             log_it(L_ERROR, "Can't delete previous transactions from mempool with hash=0x%x",
-             l_tx_prev_hash_str);
-             }
-             DAP_DELETE( l_tx_prev_hash_to_del_str);*/
-        }
-        DAP_DELETE(l_tx_prev_hash_str);
+        }
         // go to next previous transaction
         l_list_tmp = dap_list_next(l_list_tmp);
     }
@@ -2242,7 +2257,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
                 wallet_balance->balance = dap_uint128_add(wallet_balance->balance, l_add);
                 DAP_DELETE (l_wallet_balance_key);
                 // Update the cache
-                dap_chain_ledger_balance_cache_update(a_ledger, wallet_balance);
+                s_balance_cache_update(a_ledger, wallet_balance);
             } else {
                 wallet_balance = DAP_NEW_Z(dap_ledger_wallet_balance_t);
                 wallet_balance->key = l_wallet_balance_key;
@@ -2256,7 +2271,7 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
                                 strlen(l_wallet_balance_key), wallet_balance);
                 pthread_rwlock_unlock(&l_ledger_priv->balance_accounts_rwlock);
                 // Add it to cache
-                dap_chain_ledger_balance_cache_update(a_ledger, wallet_balance);
+                s_balance_cache_update(a_ledger, wallet_balance);
             }
             DAP_DELETE (l_addr_str);
         } else {
@@ -2274,26 +2289,18 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx,
         memcpy(&l_item_tmp->tx_hash_fast, l_tx_hash, sizeof(dap_chain_hash_fast_t));
         l_item_tmp->tx = DAP_NEW_SIZE(dap_chain_datum_tx_t, dap_chain_datum_tx_get_size(a_tx));
         l_item_tmp->cache_data.ts_created = (time_t) a_tx->header.ts_created;
-        //calculate l_item_tmp->n_outs;
-
+        dap_list_t *l_tist_tmp = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT_ALL, &l_item_tmp->cache_data.n_outs);
         // If debug mode dump the UTXO
-        if ( dap_log_level_get() == L_DEBUG){
-            l_item_tmp->cache_data.n_outs = 0;
-            if( l_item_tmp->cache_data.n_outs){
-                dap_list_t *l_tist_tmp = dap_chain_datum_tx_items_get(a_tx, TX_ITEM_TYPE_OUT, &l_item_tmp->cache_data.n_outs);
-                for (size_t i =0; i < (size_t) l_item_tmp->cache_data.n_outs; i++){
-                    // TODO list conditional outputs
-                    dap_chain_tx_out_t * l_tx_out = l_tist_tmp->data;
-                    char * l_tx_out_addr_str = dap_chain_addr_to_str( &l_tx_out->addr );
-                    if(s_debug_more)
-                        log_it(L_DEBUG,"Added tx out to %s",l_tx_out_addr_str );
-                    DAP_DELETE (l_tx_out_addr_str);
-                }
-                if(l_tist_tmp)
-                    dap_list_free(l_tist_tmp);
+        if (dap_log_level_get() == L_DEBUG && s_debug_more) {
+            for (size_t i =0; i < (size_t) l_item_tmp->cache_data.n_outs; i++){
+                dap_chain_tx_out_t * l_tx_out = l_tist_tmp->data;
+                char * l_tx_out_addr_str = dap_chain_addr_to_str( &l_tx_out->addr );
+                log_it(L_DEBUG,"Added tx out to %s",l_tx_out_addr_str );
+                DAP_DELETE (l_tx_out_addr_str);
             }
         }
-
+        if(l_tist_tmp)
+            dap_list_free(l_tist_tmp);
         if (!l_ticker_trl) { //No token ticker in previous txs
             if(s_debug_more)
                 log_it(L_DEBUG, "No token ticker in previous txs");
diff --git a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
index 51d4da3c79..807f755d14 100644
--- a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
+++ b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c
@@ -23,10 +23,10 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 */
 
 #include <sys/time.h>
+#include "dap_timerfd.h"
 #include "dap_common.h"
 #include "dap_hash.h"
 #include "rand/dap_rand.h"
-#include "dap_timerfd.h"
 
 #include "dap_chain.h"
 #include "dap_chain_datum_tx.h"
diff --git a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv_pkt.c b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv_pkt.c
index a6f028efe7..e630e2df12 100644
--- a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv_pkt.c
+++ b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv_pkt.c
@@ -30,8 +30,8 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 #include <stdint.h>
 #include <string.h>
 
-#include <dap_common.h>
 #include <dap_stream.h>
+#include <dap_common.h>
 #include <dap_stream_pkt.h>
 #include <dap_stream_ch_pkt.h>
 #include "dap_stream_ch_chain_net_srv_pkt.h"
diff --git a/modules/consensus/none/dap_chain_cs_none.c b/modules/consensus/none/dap_chain_cs_none.c
index a4ece1bc76..a8206306e7 100644
--- a/modules/consensus/none/dap_chain_cs_none.c
+++ b/modules/consensus/none/dap_chain_cs_none.c
@@ -28,6 +28,7 @@
 
 #include "utlist.h"
 
+#include "dap_chain_net.h"
 #include "dap_common.h"
 #include "dap_strfuncs.h"
 #include "dap_config.h"
@@ -35,7 +36,6 @@
 #include "dap_chain_ledger.h"
 #include "dap_chain_global_db.h"
 #include "dap_chain_global_db_driver.h"
-#include "dap_chain_net.h"
 #include "dap_chain_cs.h"
 #include "dap_chain_cs_none.h"
 
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index cebea33c10..9a8b56dfe9 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -1002,7 +1002,7 @@ static dap_chain_net_t *s_net_new(const char * a_id, const char * a_name ,
     PVT(ret)->state_proc_cond = CreateEventA( NULL, FALSE, FALSE, NULL );
 #endif
 
-    if ( sscanf(a_id,"0x%016lx", &ret->pub.id.uint64 ) == 1 ){
+    if ( sscanf(a_id,"0x%016"DAP_UINT64_FORMAT_X, &ret->pub.id.uint64 ) == 1 ){
         if (strcmp (a_node_role, "root_master")==0){
             PVT(ret)->node_role.enums = NODE_ROLE_ROOT_MASTER;
             log_it (L_NOTICE, "Node role \"root master\" selected");
diff --git a/modules/net/include/dap_chain_net.h b/modules/net/include/dap_chain_net.h
index ad6475656f..bb7fe912de 100644
--- a/modules/net/include/dap_chain_net.h
+++ b/modules/net/include/dap_chain_net.h
@@ -27,9 +27,9 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 
 #include <stdint.h>
 #include <string.h>
+#include "dap_net.h"
 #include "dap_strfuncs.h"
 #include "dap_string.h"
-#include "dap_net.h"
 #include "dap_chain_common.h"
 #include "dap_chain_node.h"
 #include "dap_chain.h"
diff --git a/modules/net/srv/dap_chain_net_srv_client.c b/modules/net/srv/dap_chain_net_srv_client.c
index 595a280210..09ad3a42c8 100644
--- a/modules/net/srv/dap_chain_net_srv_client.c
+++ b/modules/net/srv/dap_chain_net_srv_client.c
@@ -22,11 +22,9 @@ You should have received a copy of the GNU General Public License
 along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "dap_common.h"
-
 #include "dap_chain_net_srv.h"
 #include "dap_chain_net_srv_client.h"
-
+#include "dap_common.h"
 
 #define LOG_TAG "dap_chain_net_srv_client"
 
diff --git a/modules/net/srv/dap_chain_net_srv_geoip.c b/modules/net/srv/dap_chain_net_srv_geoip.c
index 70247873cc..f174bb026a 100644
--- a/modules/net/srv/dap_chain_net_srv_geoip.c
+++ b/modules/net/srv/dap_chain_net_srv_geoip.c
@@ -25,12 +25,12 @@
 #include <stdio.h>
 #include <stddef.h>
 
+#include "dap_client_http.h"
 #include "dap_common.h"
 #include "dap_strfuncs.h"
 #include "dap_file_utils.h"
 #include "dap_enc_key.h"
 #include "dap_enc_base64.h"
-#include "dap_client_http.h"
 #include "dap_chain_net_srv_geoip.h"
 #include "libmaxminddb/maxminddb.h"
 
diff --git a/modules/net/srv/dap_chain_net_srv_order.c b/modules/net/srv/dap_chain_net_srv_order.c
index 221cedd6e5..77af94a609 100644
--- a/modules/net/srv/dap_chain_net_srv_order.c
+++ b/modules/net/srv/dap_chain_net_srv_order.c
@@ -26,7 +26,6 @@
 #include <strings.h>
 
 #include "dap_chain_net_srv_order.h"
-
 #include "dap_hash.h"
 #include "dap_enc_base58.h"
 #include "dap_chain_global_db.h"
diff --git a/modules/net/srv/dap_chain_net_srv_stream_session.c b/modules/net/srv/dap_chain_net_srv_stream_session.c
index d5a20c2735..37392b5330 100644
--- a/modules/net/srv/dap_chain_net_srv_stream_session.c
+++ b/modules/net/srv/dap_chain_net_srv_stream_session.c
@@ -22,9 +22,9 @@ You should have received a copy of the GNU General Public License
 along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "dap_chain_net_srv.h"
 #include "dap_common.h"
 #include "rand/dap_rand.h"
-#include "dap_chain_net_srv.h"
 #include "dap_chain_net_srv_stream_session.h"
 
 #define LOG_TAG "dap_stream_ch_chain_net_srv_session"
diff --git a/modules/net/srv/include/dap_chain_net_srv_common.h b/modules/net/srv/include/dap_chain_net_srv_common.h
index cfad3553bc..a6ab4bf083 100755
--- a/modules/net/srv/include/dap_chain_net_srv_common.h
+++ b/modules/net/srv/include/dap_chain_net_srv_common.h
@@ -25,9 +25,9 @@
 #pragma once
 #include <stdint.h>
 #include <stdbool.h>
+#include "dap_server.h"
 #include "dap_common.h"
 #include "dap_math_ops.h"
-#include "dap_server.h"
 #include "dap_stream_ch.h"
 #include "dap_chain_common.h"
 #include "dap_chain_ledger.h"
diff --git a/modules/net/srv/include/dap_chain_net_srv_order.h b/modules/net/srv/include/dap_chain_net_srv_order.h
index a39ef94d3b..809305a7cf 100644
--- a/modules/net/srv/include/dap_chain_net_srv_order.h
+++ b/modules/net/srv/include/dap_chain_net_srv_order.h
@@ -23,10 +23,10 @@ along with any CellFrame SDK based project.  If not, see <http://www.gnu.org/lic
 */
 
 #pragma once
+#include "dap_chain_net.h"
 #include "dap_common.h"
 #include "dap_string.h"
 #include "dap_chain_common.h"
-#include "dap_chain_net.h"
 #include "dap_chain_net_srv_common.h"
 
 typedef struct dap_chain_net_srv_order
diff --git a/modules/net/srv/libmaxminddb/maxminddb.c b/modules/net/srv/libmaxminddb/maxminddb.c
index e7c9d3b921..21c18f2b62 100644
--- a/modules/net/srv/libmaxminddb/maxminddb.c
+++ b/modules/net/srv/libmaxminddb/maxminddb.c
@@ -343,7 +343,7 @@ LOCAL int map_file(MMDB_s *const mmdb)
         status = MMDB_FILE_OPEN_ERROR;
         goto cleanup;
     }
-    fd = CreateFile(utf16_filename, GENERIC_READ, FILE_SHARE_READ, NULL,
+    fd = CreateFileW(utf16_filename, GENERIC_READ, FILE_SHARE_READ, NULL,
                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     if (fd == INVALID_HANDLE_VALUE) {
         status = MMDB_FILE_OPEN_ERROR;
diff --git a/modules/net/srv/libmaxminddb/mmdblookup.c b/modules/net/srv/libmaxminddb/mmdblookup.c
index 66e9ef178e..da4bb601ef 100644
--- a/modules/net/srv/libmaxminddb/mmdblookup.c
+++ b/modules/net/srv/libmaxminddb/mmdblookup.c
@@ -75,7 +75,7 @@ int wmain(int argc, wchar_t **wargv)
         utf8_width = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0,
                                          NULL, NULL);
         if (utf8_width < 1) {
-            fprintf(stderr, "WideCharToMultiByte() failed: %d\n",
+            fprintf(stderr, "WideCharToMultiByte() failed: %ld\n",
                     GetLastError());
             exit(1);
         }
@@ -86,7 +86,7 @@ int wmain(int argc, wchar_t **wargv)
         }
         if (WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8_string,
                                 utf8_width, NULL, NULL) < 1) {
-            fprintf(stderr, "WideCharToMultiByte() failed: %d\n",
+            fprintf(stderr, "WideCharToMultiByte() failed: %ld\n",
                     GetLastError());
             exit(1);
         }
diff --git a/modules/type/blocks/dap_chain_block_chunk.c b/modules/type/blocks/dap_chain_block_chunk.c
index 6f39238f6c..4661144d4c 100644
--- a/modules/type/blocks/dap_chain_block_chunk.c
+++ b/modules/type/blocks/dap_chain_block_chunk.c
@@ -20,9 +20,9 @@
     You should have received a copy of the GNU General Public License
     along with any DAP SDK based project.  If not, see <http://www.gnu.org/licenses/>.
 */
+#include "dap_chain_net.h"
 #include "dap_common.h"
 #include "dap_strfuncs.h"
-#include "dap_chain_net.h"
 #include "dap_chain_global_db.h"
 #include "dap_chain_block_chunk.h"
 
-- 
GitLab