From 1245ca5b445bd4630c28562b6d8854a1e2d90ce1 Mon Sep 17 00:00:00 2001
From: Dmitriy Gerasimov <naeper@demlabs.net>
Date: Wed, 23 Jun 2021 18:20:51 +0700
Subject: [PATCH] ...

---
 dap-sdk/core/include/dap_math_ops.h       | 37 ++++++++++++-----------
 dap-sdk/net/core/dap_worker.c             | 28 +++++++++++++++++
 modules/common/include/dap_chain_common.h |  2 +-
 3 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/dap-sdk/core/include/dap_math_ops.h b/dap-sdk/core/include/dap_math_ops.h
index 20f4250f0c..606a7e684a 100755
--- a/dap-sdk/core/include/dap_math_ops.h
+++ b/dap-sdk/core/include/dap_math_ops.h
@@ -4,20 +4,22 @@
 #include <stdio.h>
 
 
-
-typedef struct unsign128_t {
+#ifdef DAP_GLOBAL_IS_INT128
+typedef __int128 int128_t;
+typedef __uint128 uint128_t;
+#else
+typedef struct uint128 {
     uint64_t hi;
     uint64_t lo;
-    
-    } unsign128_t;
-
-typedef struct unsign256_t {
-    unsign128_t hi;
-    unsign128_t lo;
+} uint128_t;
+#endif
 
-    } unsign256_t;
+typedef struct unsign256 {
+    uint128_t hi;
+    uint128_t lo;
+} unsign256_t;
 
-const  unsign128_t two_power_64={ .hi = 1, .lo = 0};
+const  uint128_t two_power_64={ .hi = 1, .lo = 0};
 
 const uint64_t lo_32=0xffffffff;
 const uint64_t hi_32=0xffffffff00000000;
@@ -32,13 +34,13 @@ overflow_flag=(*c_64_bit<a_64_bit);
 return overflow_flag;}
 
 //!!!!This function returns void because THERE CANNOT BE OVERFLOW IN A (64,64)->128 SUM!!!!
-static inline void SUM_64_128(uint64_t a_64_bit,uint64_t b_64_bit,unsign128_t* c_128_bit ) {
+static inline void SUM_64_128(uint64_t a_64_bit,uint64_t b_64_bit,uint128_t* c_128_bit ) {
 int overflow_flag;
 c_128_bit->lo=a_64_bit+b_64_bit;
 c_128_bit->hi=(c_128_bit->lo<a_64_bit);}
 
 //Mixed precision: add a uint64_t into a unsign128_t
-static inline int ADD_64_INTO_128(uint64_t a_64_bit,unsign128_t* c_128_bit ) {
+static inline int ADD_64_INTO_128(uint64_t a_64_bit,uint128_t* c_128_bit ) {
     int overflow_flag;
     uint64_t overflow_64;
     uint64_t temp;
@@ -49,7 +51,7 @@ static inline int ADD_64_INTO_128(uint64_t a_64_bit,unsign128_t* c_128_bit ) {
     overflow_flag=SUM_64_64(overflow_64,temp,&c_128_bit->hi);
     return overflow_flag;}
 
-static inline int  SUM_128_128(unsign128_t a_128_bit,unsign128_t b_128_bit,unsign128_t* c_128_bit){
+static inline int  SUM_128_128(uint128_t a_128_bit,uint128_t b_128_bit,uint128_t* c_128_bit){
     int overflow_flag;
     overflow_flag=SUM_64_64(a_128_bit.lo,b_128_bit.lo,&c_128_bit->lo);
     uint64_t carry_in_64=overflow_flag;
@@ -59,16 +61,17 @@ static inline int  SUM_128_128(unsign128_t a_128_bit,unsign128_t b_128_bit,unsig
     carry_in_64=overflow_flag;
     overflow_flag=0;
     overflow_flag=SUM_64_64(carry_in_64,intermediate_value,&c_128_bit->hi);
-    return overflow_flag;}
+    return overflow_flag;
+}
 
 
 static inline int  SUM_256_256(unsign256_t a_256_bit,unsign256_t b_256_bit,unsign256_t* c_256_bit){
     int overflow_flag;
     overflow_flag=SUM_128_128(a_256_bit.lo,b_256_bit.lo,&c_256_bit->lo);
-    unsign128_t carry_in_128;
+    uint128_t carry_in_128;
     carry_in_128.hi=0;
     carry_in_128.lo=overflow_flag;
-    unsign128_t intermediate_value;
+    uint128_t intermediate_value;
     intermediate_value.hi=0;
     intermediate_value.lo=0;
     overflow_flag=0;
@@ -82,7 +85,7 @@ static inline int  SUM_256_256(unsign256_t a_256_bit,unsign256_t b_256_bit,unsig
     return overflow_flag;}
 
 
-static inline bool dap_unsign128_t_check_equal(unsign128_t a_128_bit, unsign128_t b_128_bit)
+static inline bool dap_unsign128_t_check_equal(uint128_t a_128_bit, uint128_t b_128_bit)
 {
 #ifdef DAP_GLOBAL_IS_INT128
     return a_128_bit == b_128_bit;
diff --git a/dap-sdk/net/core/dap_worker.c b/dap-sdk/net/core/dap_worker.c
index d57521e7d3..06fbebb723 100644
--- a/dap-sdk/net/core/dap_worker.c
+++ b/dap-sdk/net/core/dap_worker.c
@@ -806,6 +806,34 @@ void *dap_worker_thread(void *arg)
                     if(s_debug_reactor)
                         log_it(L_INFO, "Process signal to close %s sock %u type %d [thread %u]",
                            l_cur->remote_addr_str ? l_cur->remote_addr_str : "", l_cur->socket, l_cur->type, l_tn);
+
+                    for(size_t nn=n+1; nn<l_sockets_max; nn++){ // Check for current selection if it has event duplication
+                        dap_events_socket_t *l_es_selected = NULL;
+#ifdef DAP_EVENTS_CAPS_EPOLL
+                        l_es_selected = (dap_events_socket_t *) l_epoll_events[nn].data.ptr;
+#elif defined ( DAP_EVENTS_CAPS_POLL)
+                        l_es_selected = l_worker->poll_esocket[nn];
+#elif defined (DAP_EVENTS_CAPS_KQUEUE)
+                        struct kevent * l_kevent_selected = &l_worker->kqueue_events_selected[n];
+                        if ( l_kevent_selected->filter == EVFILT_USER){ // If we have USER event it sends little different pointer
+                            dap_events_socket_w_data_t * l_es_w_data = (dap_events_socket_w_data_t *) l_kevent_selected->udata;
+                            l_es_selected = l_es_w_data->esocket;
+                        }else{
+                            l_es_selected = (dap_events_socket_t*) l_kevent_selected->udata;
+                        }
+#else
+#error "No selection esockets left to proc implemenetation"
+#endif
+                        if(l_es_selected == NULL || l_es_selected == l_cur ){
+                            if(l_es_selected == NULL)
+                                log_it(L_CRITICAL,"NULL esocket found when cleaning selected list");
+                            else if(s_debug_reactor)
+                                log_it(L_INFO,"Duplicate esockets removed from selected event list");
+                            n=nn; // TODO here we need to make smth like poll() array compressing.
+                                  // Here we expect thats event duplicates goes together in it. If not - we lose some events between.
+                        }
+                    }
+
                     dap_events_socket_remove_and_delete_unsafe( l_cur, false);
 #ifdef DAP_EVENTS_CAPS_KQUEUE
                     l_worker->kqueue_events_count--;
diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h
index 6a94a2241f..19333779c7 100644
--- a/modules/common/include/dap_chain_common.h
+++ b/modules/common/include/dap_chain_common.h
@@ -246,7 +246,7 @@ DAP_STATIC_INLINE uint128_t dap_chain_uint128_from(uint64_t a_from)
 #ifdef DAP_GLOBAL_IS_INT128
     return (uint128_t)a_from;
 #else
-    uint128_t l_ret = { .u64 = {0, a_from} };
+    uint128_t l_ret = { .0, a_from};
     return l_ret;
 #endif
 }
-- 
GitLab