diff --git a/CMakeLists.txt b/CMakeLists.txt index 20e766ce894d3ac4b08e1cb49b4365d8a8aaa408..c84dba71921c9e4ee5c7d2ac1f458b455c52d47e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,9 @@ endif() add_subdirectory(dap-sdk) add_subdirectory(3rdparty/monero_crypto) add_subdirectory(3rdparty/cuttdb) +if (BUILD_WITH_GDB_DRIVER_MDBX) add_subdirectory(3rdparty/libmdbx) +endif() if(DAPSDK_MODULES MATCHES "ssl-support") add_subdirectory(3rdparty/wolfssl) diff --git a/dap-sdk/core/include/dap_math_ops.h b/dap-sdk/core/include/dap_math_ops.h index 7054d9d1944515b85417c36f4a56e9c2af835510..a6efc23c6f7f9468bc91767948eb094614954960 100755 --- a/dap-sdk/core/include/dap_math_ops.h +++ b/dap-sdk/core/include/dap_math_ops.h @@ -54,8 +54,8 @@ typedef int128_t _dap_int128_t; typedef struct uint256_t { union { struct { - uint128_t hi; - uint128_t lo; + uint128_t hi; + uint128_t lo; }; struct { struct { @@ -256,8 +256,8 @@ static inline void LEFT_SHIFT_256(uint256_t a_256_bit,uint256_t* b_256_bit,int n LEFT_SHIFT_256(a_256_bit,b_256_bit,n-128); } if (n == 0) { - b_256_bit->hi=a_256_bit.hi; - b_256_bit->lo=a_256_bit.lo; + b_256_bit->hi=a_256_bit.hi; + b_256_bit->lo=a_256_bit.lo; } else if (n<128) { uint128_t shift_temp=uint128_0; @@ -279,8 +279,8 @@ static inline void RIGHT_SHIFT_256(uint256_t a_256_bit,uint256_t* b_256_bit,int RIGHT_SHIFT_256(a_256_bit,b_256_bit,n-128); } if (n == 0) { - b_256_bit->hi=a_256_bit.hi; - b_256_bit->lo=a_256_bit.lo; + b_256_bit->hi=a_256_bit.hi; + b_256_bit->lo=a_256_bit.lo; } else if (n<128) { uint128_t shift_temp=uint128_0; @@ -311,10 +311,10 @@ static inline void INCR_128(uint128_t *a_128_bit){ static inline void DECR_128(uint128_t* a_128_bit){ #ifdef DAP_GLOBAL_IS_INT128 - (*a_128_bit)--; + (*a_128_bit)--; #else - a_128_bit->lo--; + a_128_bit->lo--; if(a_128_bit->hi == 0) { a_128_bit->hi--; @@ -714,7 +714,7 @@ static inline void MULT_256_512(uint256_t a_256_bit,uint256_t b_256_bit,uint512_ //product of .hi terms - stored in .hi field of c_512_bit MULT_128_256(a_256_bit.hi,b_256_bit.hi, &c_512_bit->hi); - //product of .lo terms - stored in .lo field of c_512_bit + //product of .lo terms - stored in .lo field of c_512_bit MULT_128_256(a_256_bit.lo,b_256_bit.lo, &c_512_bit->lo); //cross product of .hi and .lo terms @@ -846,7 +846,7 @@ static inline int compare128(uint128_t a, uint128_t b) static inline int compare256(uint256_t a, uint256_t b) { return (( compare128(a.hi, b.hi) == 1 || (compare128(a.hi, b.hi) == 0 && compare128(a.lo, b.lo) == 1)) ? 1 : 0) - - (( compare128(a.hi, b.hi) == -1 || (compare128(a.hi, b.hi) == 0 && compare128(a.lo, b.lo) == -1)) ? 1 : 0); + - (( compare128(a.hi, b.hi) == -1 || (compare128(a.hi, b.hi) == 0 && compare128(a.lo, b.lo) == -1)) ? 1 : 0); } static inline int nlz64(uint64_t N) @@ -999,16 +999,37 @@ static inline void DIV_256(uint256_t a_256_bit, uint256_t b_256_bit, uint256_t* *c_256_bit = l_ret; } +/* Multiplicates 256-bit value to fixed-point value, represented as 256-bit value + * @param a_val + * @param a_mult + * @param result is a fixed-point value, represented as 256-bit value + * @return + */ +static inline int MULT_256_FRAC_FRAC(uint256_t a_val, uint256_t a_mult, uint256_t* result) { + return MULT_256_256(a_val, a_mult, result); +} -// TODO replace it with fixed point MUL -//#define CONV_256_FLOAT 10000000000000ULL // 10^13, so max float number to mult is 1.000.000 -//static inline uint256_t MULT_256_FLOAT(uint256_t a_val, long double a_mult) -//{ -// uint256_t l_ret = GET_256_FROM_64((uint64_t)(a_mult * CONV_256_FLOAT)); -// MULT_256_256(l_ret, a_val, &l_ret); -// DIV_256(l_ret, GET_256_FROM_64(CONV_256_FLOAT), &l_ret); -// return l_ret; -//} +/** + * Multiplicates to fixed-point values, represented as 256-bit values + * @param a_val + * @param b_val + * @param result is a fixed-point value, represented as 256-bit value + * @return + */ +static inline int MULT_256_COIN(uint256_t a_val, uint256_t b_val, uint256_t* result) { + uint256_t tmp; + uint256_t rem; + uint256_t ten17 = GET_256_FROM_64(100000000000000000ULL); + uint256_t ten = GET_256_FROM_64(10ULL); + uint256_t five = GET_256_FROM_64(500000000000000000); + int overflow = MULT_256_256(a_val, b_val, &tmp); + divmod_impl_256(tmp, ten17, &tmp, &rem); + if (compare256(rem, five) >= 0) { + SUM_256_256(tmp, ten, &tmp); + } + DIV_256(tmp, ten, result); + return overflow; +} #ifdef __cplusplus } diff --git a/dap-sdk/core/include/dap_tsd.h b/dap-sdk/core/include/dap_tsd.h index c96385913f9e5f62ccadd15ae07845b17a682ccd..d6530350986ece3241dd433c7a8b13a91472cbb5 100644 --- a/dap-sdk/core/include/dap_tsd.h +++ b/dap-sdk/core/include/dap_tsd.h @@ -41,4 +41,4 @@ dap_tsd_t* dap_tsd_find(byte_t * a_data, size_t a_data_size,uint16_t a_type); #define dap_tsd_get_string(a) ( ((char*) a->data )[a->size-1] == '\0'? (char*) a->data : "<CORRUPTED STRING>" ) #define dap_tsd_get_string_const(a) ( ((const char*) a->data )[a->size-1] == '\0'? (const char*) a->data : "<CORRUPTED STRING>" ) -#define dap_tsd_size(a) (sizeof(*a)+a->size) +#define dap_tsd_size(a) (sizeof(*a)+(size_t)a->size) diff --git a/dap-sdk/net/client/dap_client.c b/dap-sdk/net/client/dap_client.c index 1b0bbd0c4189d218e5fc47c6679e3fcdab5044b1..d09fd83fb6f5eb35886df8b270d60ed60118343b 100644 --- a/dap-sdk/net/client/dap_client.c +++ b/dap-sdk/net/client/dap_client.c @@ -195,34 +195,38 @@ void dap_client_set_auth_cert_unsafe(dap_client_t * a_client, dap_cert_t *a_cert */ void dap_client_set_auth_cert(dap_client_t *a_client, const char *a_chain_net_name) { - if(a_client == NULL || a_chain_net_name == NULL){ + static dap_cert_t *l_cert = NULL; + static bool l_config_read = false; + + if (a_client == NULL || a_chain_net_name == NULL) { log_it(L_ERROR,"Chain-net is NULL for dap_client_set_auth_cert"); return; } - char *l_path = dap_strdup_printf("network/%s", a_chain_net_name); - if (!l_path) { - log_it(L_ERROR, "Can't allocate memory: file: %s line: %d", __FILE__, __LINE__); - return; - } - dap_config_t *l_cfg = dap_config_open(l_path); - free(l_path); - if (!l_cfg) { - log_it(L_ERROR, "Can't allocate memory: file: %s line: %d", __FILE__, __LINE__); - return; - } - const char *l_cert_name = dap_config_get_item_str(l_cfg, "general", "auth_cert"); - if (!l_cert_name) - return; - dap_cert_t *l_cert = dap_cert_find_by_name(l_cert_name); - if (!l_cert) { + if (!l_config_read) { + char *l_path = dap_strdup_printf("network/%s", a_chain_net_name); + if (!l_path) { + log_it(L_ERROR, "Can't allocate memory: file: %s line: %d", __FILE__, __LINE__); + return; + } + dap_config_t *l_cfg = dap_config_open(l_path); + l_config_read = true; + free(l_path); + if (!l_cfg) { + log_it(L_ERROR, "Can't allocate memory: file: %s line: %d", __FILE__, __LINE__); + return; + } + const char *l_cert_name = dap_config_get_item_str(l_cfg, "general", "auth_cert"); dap_config_close(l_cfg); - log_it(L_ERROR,"l_cert is NULL by dap_cert_find_by_name"); - return; + if (!l_cert_name) + return; + dap_cert_find_by_name(l_cert_name); + if (!l_cert) { + log_it(L_ERROR,"l_cert is NULL by dap_cert_find_by_name"); + return; + } } - dap_client_set_auth_cert_unsafe(a_client, l_cert); - - //dap_cert_delete(l_cert); - dap_config_close(l_cfg); + if (l_cert) + dap_client_set_auth_cert_unsafe(a_client, l_cert); } /** diff --git a/dap-sdk/net/core/dap_events_socket.c b/dap-sdk/net/core/dap_events_socket.c index 553f7ec2ef57d37e964f636bbdadba75ad1b9d4d..7dc6f1fe619fa29244046321e9417134b129de1d 100644 --- a/dap-sdk/net/core/dap_events_socket.c +++ b/dap-sdk/net/core/dap_events_socket.c @@ -1167,10 +1167,8 @@ static void *dap_events_socket_buf_thread(void *arg) #elif defined(DAP_EVENTS_CAPS_QUEUE_MQUEUE) l_sock = l_item->es->mqd; #endif -#ifndef DAP_EVENTS_CAPS_KQUEUE // wait max 5 min l_res = wait_send_socket(l_sock, 300000); -#endif if (l_res == 0) { dap_events_socket_queue_ptr_send(l_item->es, l_item->arg); break; @@ -1379,11 +1377,11 @@ int dap_events_socket_queue_ptr_send( dap_events_socket_t *a_es, void *a_arg) } if(l_n != -1 ){ - l_ret = sizeof (a_arg); + return sizeof(a_arg); }else{ l_errno = errno; - log_it(L_ERROR,"Sending kevent error code %d", l_n); - l_ret = -1; + log_it(L_ERROR,"Sending kevent error code %d", l_errno); + return l_errno; } #else @@ -1852,6 +1850,9 @@ void dap_events_socket_delete_unsafe( dap_events_socket_t * a_esocket , bool a_p DAP_DEL_Z(a_esocket->buf_in) DAP_DEL_Z(a_esocket->buf_out) DAP_DEL_Z(a_esocket->remote_addr_str) + DAP_DEL_Z(a_esocket->remote_addr_str6) + DAP_DEL_Z(a_esocket->hostaddr) + DAP_DEL_Z(a_esocket->service) DAP_DEL_Z( a_esocket ) } diff --git a/dap-sdk/net/core/include/dap_events_socket.h b/dap-sdk/net/core/include/dap_events_socket.h index 58a268640dbf09321fe7e7961b69a824713dcacd..d0b406a1c7c31ee99fe3625f716dcd939b022f5a 100644 --- a/dap-sdk/net/core/include/dap_events_socket.h +++ b/dap-sdk/net/core/include/dap_events_socket.h @@ -374,7 +374,6 @@ void dap_events_socket_descriptor_close(dap_events_socket_t *a_socket); void dap_events_socket_remove_from_worker_unsafe( dap_events_socket_t *a_es, dap_worker_t * a_worker); // Buffer functions -size_t dap_events_socket_pop_from_buf_in(dap_events_socket_t *sc, void * data, size_t data_size); void dap_events_socket_shrink_buf_in(dap_events_socket_t * a_es, size_t shrink_size); DAP_STATIC_INLINE size_t dap_events_socket_get_free_buf_size(dap_events_socket_t *a_es) { return a_es->buf_out_size_max - a_es->buf_out_size; } size_t dap_events_socket_pop_from_buf_in(dap_events_socket_t *sc, void * data, size_t data_size); diff --git a/dap-sdk/net/server/http_server/dap_http_simple.c b/dap-sdk/net/server/http_server/dap_http_simple.c index 6dc17c6f8ddd496e3abe677958222864cb2c7eb1..c37bd1ab719d972f2e0fde41e6ce87454c1675ae 100644 --- a/dap-sdk/net/server/http_server/dap_http_simple.c +++ b/dap-sdk/net/server/http_server/dap_http_simple.c @@ -217,20 +217,11 @@ inline static bool s_is_supported_user_agents_list_setted(void) return cnt; } -inline static void s_write_data_to_socket(dap_proc_thread_t *a_thread, dap_http_simple_t * a_simple) +inline static void s_write_data_to_socket(dap_proc_thread_t *a_thread, dap_http_simple_t *a_simple) { - if (!a_simple->reply) { - a_simple->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE; - log_it( L_WARNING, "No reply to write, close connection" ); - } else { -#if 0 - a_simple->reply_sent += dap_events_socket_write_unsafe(a_simple->esocket, - a_simple->reply_byte + a_simple->reply_sent, - a_simple->http_client->out_content_length - a_simple->reply_sent); -#endif - dap_events_socket_set_writable_unsafe(a_simple->esocket, true); - } - + dap_http_client_out_header_generate(a_simple->http_client); + a_simple->http_client->state_write = DAP_HTTP_CLIENT_STATE_START; + dap_http_client_write(a_simple->esocket, NULL); dap_proc_thread_assign_on_worker_inter(a_thread, a_simple->worker, a_simple->esocket); } @@ -247,7 +238,6 @@ static void s_http_client_data_write(dap_http_client_t * a_http_client, void *a_ l_http_simple->reply_sent += dap_events_socket_write_unsafe(l_http_simple->esocket, l_http_simple->reply_byte + l_http_simple->reply_sent, l_http_simple->http_client->out_content_length - l_http_simple->reply_sent); - dap_events_socket_set_writable_unsafe(l_http_simple->esocket, true); } } @@ -331,9 +321,6 @@ static bool s_proc_queue_callback(dap_proc_thread_t * a_thread, void * a_arg ) log_it(L_ERROR, "Request was processed with ERROR"); l_http_simple->http_client->reply_status_code = Http_Status_InternalServerError; } - dap_http_client_out_header_generate(l_http_simple->http_client); - - l_http_simple->http_client->state_write = DAP_HTTP_CLIENT_STATE_START; s_write_data_to_socket(a_thread, l_http_simple); return true; } @@ -388,7 +375,6 @@ static void s_http_client_headers_read( dap_http_client_t *a_http_client, void * } } - void s_http_client_data_read( dap_http_client_t *a_http_client, void * a_arg ) { int *ret = (int *)a_arg; diff --git a/dap-sdk/net/server/http_server/http_client/dap_http_client.c b/dap-sdk/net/server/http_server/http_client/dap_http_client.c index ac3c3b74cfb4ec9b796a5f345db67bd2bf0bfb83..edf33ea62d61a861eb369847e4b309df76ca38f7 100644 --- a/dap-sdk/net/server/http_server/http_client/dap_http_client.c +++ b/dap-sdk/net/server/http_server/http_client/dap_http_client.c @@ -356,7 +356,6 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg ) break; } - l_peol++; /* Count terminal <LF> */ l_len = l_peol - (char*)a_esocket->buf_in; /* <l_len> - actual data length of the HTTP's start-line */ @@ -401,29 +400,29 @@ void dap_http_client_read( dap_events_socket_t *a_esocket, void *a_arg ) // Check if present cache pthread_rwlock_rdlock(&l_http_client->proc->cache_rwlock); - if ( (l_http_cache = l_http_client->proc->cache) ) - { - if ( ! l_http_cache->ts_expire || l_http_cache->ts_expire >= time(NULL) ){ - l_http_client->out_headers = dap_http_headers_dup(l_http_cache->headers); - l_http_client->out_content_length = l_http_cache->body_size; - l_http_client->reply_status_code = l_http_cache->response_code; - if(l_http_cache->response_phrase) - strncpy(l_http_client->reply_reason_phrase,l_http_cache->response_phrase,sizeof (l_http_client->reply_reason_phrase)-1); + if ( (l_http_cache = l_http_client->proc->cache) ) { + if ( ! l_http_cache->ts_expire || l_http_cache->ts_expire >= time(NULL) ){ + l_http_client->out_headers = dap_http_headers_dup(l_http_cache->headers); + l_http_client->out_content_length = l_http_cache->body_size; + l_http_client->reply_status_code = l_http_cache->response_code; + if(l_http_cache->response_phrase) + strncpy(l_http_client->reply_reason_phrase,l_http_cache->response_phrase,sizeof (l_http_client->reply_reason_phrase)-1); - debug_if (s_debug_http, L_DEBUG,"%"DAP_FORMAT_SOCKET" Out: prepare cached headers", l_http_client->esocket->socket); + debug_if (s_debug_http, L_DEBUG,"%"DAP_FORMAT_SOCKET" Out: prepare cached headers", l_http_client->esocket->socket); - }else if (l_http_cache){ - pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); - pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock); - dap_http_cache_delete(l_http_cache); - l_http_client->proc->cache = NULL; - l_http_cache = NULL; - } + } else if (l_http_cache) { pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); + pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock); + dap_http_cache_delete(l_http_cache); + l_http_client->proc->cache = NULL; + l_http_cache = NULL; + } - if ( !l_http_cache && (l_http_client->proc->new_callback) ) /* Call client constructor */ - l_http_client->proc->new_callback(l_http_client, NULL); - } + + if ( !l_http_cache && (l_http_client->proc->new_callback) ) /* Call client constructor */ + l_http_client->proc->new_callback(l_http_client, NULL); + } else + pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); } /* case DAP_HTTP_CLIENT_STATE_START: */ /* no break here just step to next phase */ @@ -537,8 +536,7 @@ size_t l_to_send, l_sent; return; case DAP_HTTP_CLIENT_STATE_START: - if ( l_http_client->proc ) - { + if ( l_http_client->proc ) { // We check out_headers because if they are - we send only cached headers and don't call headers_write_callback at all if ( !l_http_client->out_headers && l_http_client->proc->headers_write_callback ){ l_http_client->proc->headers_write_callback( l_http_client, NULL ); @@ -550,74 +548,63 @@ size_t l_to_send, l_sent; } log_it( L_INFO," HTTP response with %u status code", l_http_client->reply_status_code ); - l_http_client->esocket->buf_out_size = dap_snprintf((char *) l_http_client->esocket->buf_out, l_http_client->esocket->buf_out_size_max, + l_http_client->esocket->buf_out_size += dap_snprintf((char *) l_http_client->esocket->buf_out + l_http_client->esocket->buf_out_size, + l_http_client->esocket->buf_out_size_max - l_http_client->esocket->buf_out_size, "HTTP/1.1 %u %s" CRLF, l_http_client->reply_status_code, l_http_client->reply_reason_phrase[0] ? l_http_client->reply_reason_phrase : http_status_reason_phrase(l_http_client->reply_status_code) ); - l_http_client->state_write = DAP_HTTP_CLIENT_STATE_HEADERS; - /* No break; Just jump to next step == DAP_HTTP_CLIENT_STATE_HEADERS */ + /* No break; Just jump to next step == DAP_HTTP_CLIENT_STATE_DATA */ case DAP_HTTP_CLIENT_STATE_HEADERS: dap_time_to_str_rfc822( l_buf, sizeof(l_buf) - 1, time( NULL ) ); dap_http_header_add( &l_http_client->out_headers, "Date", l_buf ); - for ( hdr = l_http_client->out_headers; hdr; hdr = l_http_client->out_headers ) - { + for ( hdr = l_http_client->out_headers; hdr; hdr = l_http_client->out_headers ) { l_http_client->esocket->buf_out_size += dap_snprintf((char *) l_http_client->esocket->buf_out + l_http_client->esocket->buf_out_size, l_http_client->esocket->buf_out_size_max - l_http_client->esocket->buf_out_size, "%s: %s" CRLF, hdr->name, hdr->value); dap_http_header_remove( &l_http_client->out_headers, hdr ); } - dap_events_socket_write_unsafe(l_http_client->esocket, CRLF, 2);/* Add final CRLF - HTTP's End-Of-Header */ - dap_events_socket_set_writable_unsafe(l_http_client->esocket, true); - l_http_client->state_write = DAP_HTTP_CLIENT_STATE_DATA; /* No break; Just jump to next step == DAP_HTTP_CLIENT_STATE_DATA */ - case DAP_HTTP_CLIENT_STATE_DATA: { - if ( l_http_client->proc ){ - pthread_rwlock_rdlock(&l_http_client->proc->cache_rwlock); - if ( ( l_http_client->proc->cache == NULL && - l_http_client->proc->data_write_callback ) - ){ - if (l_http_client->proc->cache){ - pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); - pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock); - dap_http_cache_delete(l_http_client->proc->cache); - l_http_client->proc->cache = NULL; - if(s_debug_http) - log_it(L_NOTICE,"Cache expired and dropped out"); - }else if (s_debug_http) - log_it(L_DEBUG, "No cache so we call write callback"); - + case DAP_HTTP_CLIENT_STATE_DATA: + if (l_http_client->proc && l_http_client->proc->data_write_callback) { + pthread_rwlock_wrlock(&l_http_client->proc->cache_rwlock); + if (!l_http_client->proc->cache) { + debug_if(s_debug_http, L_DEBUG, "No cache so we call write callback"); pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); - l_http_client->proc->data_write_callback( l_http_client, NULL ); - }else if(l_http_client->proc->cache) { - l_to_send = l_http_client->proc->cache->body_size - l_http_client->out_cache_position; + l_http_client->proc->data_write_callback( l_http_client, NULL ); + if (l_http_client->esocket->flags & DAP_SOCK_SIGNAL_CLOSE) + l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; + } else { + l_to_send = l_http_client->proc->cache->body_size-l_http_client->out_cache_position ; l_sent = dap_events_socket_write_unsafe(l_http_client->esocket, l_http_client->proc->cache->body + l_http_client->out_cache_position, l_to_send); - if(l_sent){ - if ( l_http_client->out_cache_position + l_sent >= l_http_client->proc->cache->body_size ){ // All is sent - if(s_debug_http) - log_it(L_DEBUG,"Out %"DAP_FORMAT_SOCKET" All cached data over, signal to close connection", l_http_client->esocket->socket); - l_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE; - l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; - dap_events_socket_set_writable_unsafe( a_esocket, false ); - }else - l_http_client->out_cache_position += l_sent; - } + if (!l_sent || l_http_client->out_cache_position + l_sent >= l_http_client->proc->cache->body_size) { // All is sent + if (!l_sent) + debug_if(s_debug_http, L_ERROR, "Can't send data to socket"); + else + debug_if(s_debug_http, L_DEBUG, "Out %"DAP_FORMAT_SOCKET" All cached data over, signal to close connection", + l_http_client->esocket->socket); + l_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE; + l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; + } else + l_http_client->out_cache_position += l_sent; pthread_rwlock_unlock(&l_http_client->proc->cache_rwlock); } - }else{ + } else { log_it(L_WARNING, "No http proc, nothing to write"); + l_http_client->esocket->flags |= DAP_SOCK_SIGNAL_CLOSE; + l_http_client->state_write = DAP_HTTP_CLIENT_STATE_NONE; } - } - break; - } + return; + } + dap_http_client_write(a_esocket, a_arg); } /** diff --git a/modules/chain/dap_chain.c b/modules/chain/dap_chain.c index 20dcd078c9c8428a3b67827093ad41b603bf0075..f1560c317d9569f1d6ab5bc2f444d2b2b4957683 100644 --- a/modules/chain/dap_chain.c +++ b/modules/chain/dap_chain.c @@ -196,6 +196,7 @@ void dap_chain_delete(dap_chain_t * a_chain) DAP_DELETE(a_chain->datum_types); a_chain->autoproc_datum_types_count = 0; DAP_DELETE(a_chain->autoproc_datum_types); + pthread_rwlock_destroy(&a_chain->rwlock); pthread_rwlock_destroy(&a_chain->atoms_rwlock); pthread_rwlock_destroy(&a_chain->cell_rwlock); pthread_rwlock_destroy(&a_chain->rwlock); @@ -439,7 +440,7 @@ dap_chain_t * dap_chain_load_from_cfg(dap_ledger_t* a_ledger, const char * a_cha l_chain = NULL; } - if (l_chain) + if (l_chain) { // load priority for chain l_chain->load_priority = dap_config_get_item_uint16_default(l_cfg, "chain", "load_priority", 100); @@ -667,6 +668,7 @@ void dap_chain_add_callback_notify(dap_chain_t * a_chain, dap_chain_callback_not pthread_rwlock_unlock(&a_chain->rwlock); } + /** * @brief dap_chain_get_last_atom_hash * @param a_chain diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c index b63aa8d905d22f536005a0d420b84573c96ef35a..74a65ce7a6e01731ee471a6947135d4d55f2dd90 100644 --- a/modules/chain/dap_chain_cell.c +++ b/modules/chain/dap_chain_cell.c @@ -318,7 +318,6 @@ int dap_chain_cell_file_append( dap_chain_cell_t * a_cell, const void* a_atom, s } l_total_wrote_bytes += l_atom_size; - if(a_cell->chain && a_cell->chain->atom_notifiers ){ for( dap_list_t * l_iter = a_cell->chain->atom_notifiers;l_iter; l_iter = dap_list_next(l_iter) ){ dap_chain_atom_notifier_t * i = (dap_chain_atom_notifier_t *) l_iter->data; diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h index 9405bb5bbf96696fbbee2423ca2adf9b2d0a0f93..56cbeec55b1e4b204c88b48ed533f7604fc7f5f3 100644 --- a/modules/chain/include/dap_chain.h +++ b/modules/chain/include/dap_chain.h @@ -105,9 +105,7 @@ typedef enum dap_chain_type CHAIN_TYPE_LAST } dap_chain_type_t; - - -typedef struct dap_chain{ +typedef struct dap_chain { pthread_rwlock_t rwlock; // Common rwlock for the whole structure dap_chain_id_t id; diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c index 16d648f851774e1b0476b09ea2f5202ef8bc09c9..add1f75c8d46dddb91ef9aafedcd1434df9341eb 100644 --- a/modules/channel/chain/dap_stream_ch_chain.c +++ b/modules/channel/chain/dap_stream_ch_chain.c @@ -1547,7 +1547,8 @@ struct chain_io_complete { static void s_stream_ch_io_complete(dap_events_socket_t *a_es, void *a_arg, int a_errno) { - UNUSED(a_errno); // TODO process it + if (a_errno) + return; if (!a_arg) { if (a_es->callbacks.write_callback) a_es->callbacks.write_callback(a_es, NULL); @@ -1756,12 +1757,12 @@ void s_stream_ch_packet_out(dap_stream_ch_t* a_ch, void* a_arg) HASH_FIND_BYHASHVALUE(hh, l_ch_chain->remote_atoms, l_ch_chain->request_atom_iter->cur_hash, sizeof(dap_chain_hash_fast_t), l_hash_item_hashv, l_hash_item); if( l_hash_item ){ // If found - skip it - if(s_debug_more){ + /*if(s_debug_more){ char l_request_atom_hash_str[81]={[0]='\0'}; dap_chain_hash_fast_to_str(l_ch_chain->request_atom_iter->cur_hash,l_request_atom_hash_str,sizeof (l_request_atom_hash_str)); log_it(L_DEBUG, "Out CHAIN: skip atom hash %s because its already present in remote atom hash table", l_request_atom_hash_str); - } + }*/ }else{ l_hash_item = DAP_NEW(dap_stream_ch_chain_hash_item_t); memcpy(&l_hash_item->hash, l_ch_chain->request_atom_iter->cur_hash, sizeof(dap_chain_hash_fast_t)); diff --git a/modules/common/dap_chain_common.c b/modules/common/dap_chain_common.c index 0d0a695ced70a0862be7a7a456dc22c38be7d1fb..3592d548586e937689c4d9f564ad560108a8ce09 100644 --- a/modules/common/dap_chain_common.c +++ b/modules/common/dap_chain_common.c @@ -343,7 +343,7 @@ char *dap_chain_balance_to_coins256(uint256_t a_balance) /* 123000...456 -> "123000...456" */ if ( !(l_buf = dap_cvt_uint256_to_str(a_balance)) ) - return NULL; + return NULL; l_strlen = strlen(l_buf); @@ -567,9 +567,9 @@ uint256_t dap_chain_coins_to_balance256(const char *a_coins) uint256_t l_nul = {0}; /* "12300000000.0000456" */ - if ( (l_len = strnlen(a_coins, 2*DATOSHI_POW + 2 )) > 2*DATOSHI_POW + 2)/* Check for legal length */ + if ( (l_len = strnlen(a_coins, DATOSHI_POW256 + 3)) > DATOSHI_POW256 + 2)/* Check for legal length */ /* 1 symbol for \0, one for '.', if more, there is an error */ return log_it(L_WARNING, "Incorrect balance format of '%s' - too long (%d > %d)", a_coins, - l_len, 2*DATOSHI_POW + 2), l_nul; + l_len, DATOSHI_POW256 + 2), l_nul; /* Find , check and remove 'precision' dot symbol */ memcpy (l_buf, a_coins, l_len); /* Make local coy */ @@ -603,7 +603,7 @@ uint256_t dap_chain_coins_to_balance256(const char *a_coins) char *dap_cvt_uint256_to_str(uint256_t a_uint256) { - char *l_buf = DAP_NEW_Z_SIZE(char, DATOSHI_POW * 2 + 3); + char *l_buf = DAP_NEW_Z_SIZE(char, DATOSHI_POW256 + 3); int l_pos = 0; uint256_t l_value = a_uint256; uint256_t uint256_ten = {.hi = 0, .lo = 10}; @@ -688,7 +688,7 @@ const union __c_pow10_double__ { { .u64 = {0, 29387358770557187ULL, 12898303124178706663ULL, 13150510911921848320ULL} }, // 55 { .u64 = {0, 293873587705571876ULL, 18302566799529756941ULL, 2377900603251621888ULL} }, // 56 { .u64 = {0, 2938735877055718769ULL, 17004971331911604867ULL, 5332261958806667264ULL} }, // 57 - { .u64 = {0, 10940614696847636083ULL, 4029016655730084128ULL, 16429131440647569408ULL} }, // 58 + { .u64 = {1, 10940614696847636083ULL, 4029016655730084128ULL, 16429131440647569408ULL} }, // 58 { .u64 = {15ULL, 17172426599928602752ULL, 3396678409881738056ULL, 16717361816799281152ULL} }, // 59 { .u64 = {159ULL, 5703569335900062977ULL, 15520040025107828953ULL, 1152921504606846976ULL} }, // 60 { .u64 = {1593ULL, 1695461137871974930ULL, 7626447661401876602ULL, 11529215046068469760ULL} }, // 61 diff --git a/modules/consensus/block-ton/dap_chain_cs_block_ton.c b/modules/consensus/block-ton/dap_chain_cs_block_ton.c index 2fe9d834ecef4bb487347f652016867bf3a027e6..47e910b96709a95a4cd42e65442daeff63bc9574 100644 --- a/modules/consensus/block-ton/dap_chain_cs_block_ton.c +++ b/modules/consensus/block-ton/dap_chain_cs_block_ton.c @@ -338,7 +338,7 @@ static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cf DL_APPEND(s_session_items, l_session); dap_chain_node_role_t l_role = dap_chain_net_get_role(l_net); if ( PVT(l_session->ton)->validators_list_by_stake || - (l_role.enums == NODE_ROLE_ROOT_MASTER || l_role.enums == NODE_ROLE_ROOT) ) { + (l_role.enums == NODE_ROLE_MASTER || l_role.enums == NODE_ROLE_ROOT) ) { if ( s_session_get_validator(l_session, l_session->my_addr, l_session->cur_round.validators_list) ) { if (!s_session_cs_timer) { s_session_cs_timer = dap_timerfd_start(1*1000, diff --git a/modules/global-db/dap_chain_global_db_driver_mdbx.c b/modules/global-db/dap_chain_global_db_driver_mdbx.c index 147c03a38aa21dc4b89ab05b5e7d6b83a96865ae..ee11529285685942c0f53ded57ab11fa3009ddda 100644 --- a/modules/global-db/dap_chain_global_db_driver_mdbx.c +++ b/modules/global-db/dap_chain_global_db_driver_mdbx.c @@ -31,7 +31,6 @@ 4-MAY-2022 RRL Developing for actual version of the LibMDBX 12-MAY-2022 RRL Finished developing of preliminary version - 19-MAY-2022 RRL Added routines' decsriptions */ @@ -53,8 +52,6 @@ #include "dap_file_utils.h" #include "dap_common.h" -#ifdef DAP_CHAIN_GDB_ENGINE_MDBX - #include "mdbx.h" /* LibMDBX API */ #define LOG_TAG "dap_chain_global_db_mdbx" @@ -158,8 +155,6 @@ char l_buf[1024] = {0}; } #endif /* __SYS$DEBUG__ */ - - /* * DESCRIPTION: Open or create (if a_flag=MDBX_CREATE) a DB context for a given group. * Initialize an MDBX's internal context for the subDB (== a_group); @@ -308,7 +303,6 @@ dap_db_ctx_t *l_db_ctx = NULL, *l_tmp; return 0; } - /* * DESCRIPTION: Performs an initial module internal context creation and setup, * Fill dispatch procedure table (a_drv_callback) by entries of this module; @@ -344,7 +338,6 @@ size_t l_upper_limit_of_db_size = 32*1024*1024*1024ULL; l_upper_limit_of_db_size *= 1024*1024*1024; log_it(L_INFO, "Set MDBX Upper Limit of DB Size to %zu octets", l_upper_limit_of_db_size); - snprintf(s_db_path, sizeof(s_db_path), "%s/%s", a_mdbx_path, s_subdir );/* Make a path to MDBX root */ dap_mkdir_with_parents(s_db_path); /* Create directory for the MDBX storage */ @@ -444,8 +437,6 @@ size_t l_upper_limit_of_db_size = 32*1024*1024*1024ULL; return MDBX_SUCCESS; } - - /* * DESCRIPTION: Get a DB context for the specified group/table name * from the DB context hash table. This context is just pointer to the DB Context @@ -476,7 +467,6 @@ dap_db_ctx_t *l_db_ctx = NULL; return l_db_ctx; } - /* * DESCRIPTION: Action routine - perform flushing action. Actualy MDBX internaly maintain processes of the flushing * and other things related to data integrity. @@ -495,7 +485,6 @@ static int s_db_mdbx_flush(void) return log_it(L_DEBUG, "Flushing resident part of the MDBX to disk"), 0; } - /* * DESCRIPTION: Action routine - lookup in the group/table a last store record. * We mainatain internaly <id> of record (it's just sequence), @@ -599,7 +588,6 @@ dap_store_obj_t *l_obj; return l_obj; } - /* * DESCRIPTION: An action routine to check a presence specified key in the group/table * @@ -647,7 +635,6 @@ MDBX_val l_key, l_data; return ( l_rc == MDBX_SUCCESS ); /*0 - RNF, 1 - SUCCESS */ } - /* * DESCRIPTION: Action routine to read record with a give <id > from the table * @@ -748,7 +735,6 @@ dap_store_obj_t *l_obj; return l_obj; } - /* * DESCRIPTION: Action routine to retrieve a number of records for specified record's id. * @@ -813,9 +799,6 @@ struct __record_suffix__ *l_suff; return l_count_out; } - - - /* * DESCRIPTION: Action routine - returns a list of group/table names in DB contexts hash table is matched * to specified pattern. @@ -1069,7 +1052,6 @@ struct __record_suffix__ *l_suff; return log_it (L_ERROR, "mdbx_txn_begin: (%d) %s", l_rc, mdbx_strerror(l_rc)), NULL; } - if ( a_count_out ) *a_count_out = 0; @@ -1207,6 +1189,3 @@ struct __record_suffix__ *l_suff; return l_obj_arr; } - - -#endif /* DAP_CHAIN_GDB_ENGINE_MDBX */ diff --git a/modules/global-db/dap_chain_global_db_remote.c b/modules/global-db/dap_chain_global_db_remote.c index f08c793925971bac020d517dcaec4e553db57293..26d8dbd8445d2f4b64860ed631f417aea18fe3de 100644 --- a/modules/global-db/dap_chain_global_db_remote.c +++ b/modules/global-db/dap_chain_global_db_remote.c @@ -144,7 +144,6 @@ dap_db_log_list_t* dap_db_log_list_start(dap_chain_net_t *l_net, dap_chain_node_ if (a_flags & F_DB_LOG_ADD_EXTRA_GROUPS) { dap_list_t *l_extra_groups_masks = dap_chain_db_get_sync_extra_groups(l_net_name); l_groups_masks = dap_list_concat(l_groups_masks, l_extra_groups_masks); - dap_list_free(l_extra_groups_masks); } dap_list_t *l_groups_names = NULL; for (dap_list_t *l_cur_mask = l_groups_masks; l_cur_mask; l_cur_mask = dap_list_next(l_cur_mask)) { @@ -659,7 +658,8 @@ dap_store_obj_t *dap_store_unpacket_multiple(const dap_store_obj_pkt_t *a_pkt, s memcpy(&l_str_length, a_pkt->data + l_offset, sizeof(uint16_t)); l_offset += sizeof(uint16_t); - if (l_offset + l_str_length > a_pkt->data_size || !l_str_length) {log_it(L_ERROR, "Broken GDB element: can't read 'key' field"); + if (l_offset + l_str_length > a_pkt->data_size || !l_str_length) {log_it(L_ERROR, "Broken GDB element: can't read 'key' field: len %s", + l_str_length ? "OVER" : "NULL"); DAP_DELETE(l_obj->group); break;} // Check for buffer boundries l_obj->key = DAP_NEW_Z_SIZE(char, l_str_length + 1); memcpy((char *)l_obj->key, a_pkt->data + l_offset, l_str_length); diff --git a/modules/global-db/include/dap_chain_global_db_driver.h b/modules/global-db/include/dap_chain_global_db_driver.h index 1a0bac6f68ee7909a64ca9a91c6358deee28b9fb..1e84c2d9dbbab8cdd2db860bbc53d8a7017d4253 100644 --- a/modules/global-db/include/dap_chain_global_db_driver.h +++ b/modules/global-db/include/dap_chain_global_db_driver.h @@ -36,6 +36,11 @@ #include <stddef.h> #include <stdint.h> +#define DAP_DB$SZ_MAXGROUPNAME 128 /* A maximum size of group name */ +#define DAP_DB$K_MAXGROUPS 1024 /* A maximum number of groups */ +#define DAP_DB$SZ_MAXKEY 512 /* A limit for the key's length in DB */ +#define DAP_DB$K_MAXOBJS 8192 /* A maximum number of objects to be returned by + read_srore_obj() */ enum RECORD_FLAGS { RECORD_COMMON = 0, // 0000 diff --git a/modules/service/stake/dap_chain_net_srv_stake.c b/modules/service/stake/dap_chain_net_srv_stake.c index c5f3e64720ef048247a1b43d7a8c13ed582a8d84..37595e2889ccedc5126eeb68a45f868acad063c9 100644 --- a/modules/service/stake/dap_chain_net_srv_stake.c +++ b/modules/service/stake/dap_chain_net_srv_stake.c @@ -343,7 +343,7 @@ bool dap_chain_net_srv_stake_validator(dap_chain_addr_t *a_addr, dap_chain_datum } } dap_list_free(l_list_out_items); - uint256_t l_fee = {}; // TODO replace with fractional mult MULT_256_FLOAT(l_outs_sum, l_stake->fee_value / 100.0); + uint256_t l_fee = {}; // TODO replace with fractional mult MULT_256_FRAC_FRAC(l_outs_sum, l_stake->fee_value / 100.0); if (compare256(l_fee_sum, l_fee) == -1) { return false; } diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index b75af2c623964437a0532e05835e115b74335ebc..c863f63da1e1477924e23ebe72995e45d5c4600c 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -142,7 +142,7 @@ static dap_chain_datum_tx_receipt_t *s_xchage_receipt_create(dap_chain_net_srv_x { uint32_t l_ext_size = sizeof(uint256_t) + DAP_CHAIN_TICKER_SIZE_MAX; uint8_t *l_ext = DAP_NEW_S_SIZE(uint8_t, l_ext_size); - uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FLOAT(a_price->datoshi_sell, 1 / a_price->rate); + uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FRAC_FRAC(a_price->datoshi_sell, 1 / a_price->rate); memcpy(l_ext, &l_datoshi_buy, sizeof(uint256_t)); strcpy((char *)&l_ext[sizeof(uint256_t)], a_price->token_buy); dap_chain_net_srv_price_unit_uid_t l_unit = { .uint32 = SERV_UNIT_UNDEFINED}; @@ -237,7 +237,7 @@ static dap_chain_datum_tx_t *s_xchange_tx_create_exchange(dap_chain_net_srv_xcha dap_enc_key_t *l_seller_key = dap_chain_wallet_get_key(a_wallet, 0); uint256_t l_value_buy = {}; // how many coins to transfer // list of transaction with 'out' items to sell - uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FLOAT(a_price->datoshi_sell, 1 / a_price->rate); + uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FRAC_FRAC(a_price->datoshi_sell, 1 / a_price->rate); dap_list_t *l_list_used_out = dap_chain_ledger_get_list_tx_outs_with_val(l_ledger, a_price->token_buy, l_seller_addr, l_datoshi_buy, &l_value_buy); if(!l_list_used_out) { @@ -414,7 +414,7 @@ char *s_xchange_order_create(dap_chain_net_srv_xchange_price_t *a_price, dap_cha dap_chain_node_addr_t *l_node_addr = dap_chain_net_get_cur_addr(a_price->net_sell); dap_chain_net_srv_price_unit_uid_t l_unit = { .uint32 = SERV_UNIT_UNDEFINED}; dap_chain_net_srv_uid_t l_uid = { .uint64 = DAP_CHAIN_NET_SRV_XCHANGE_ID }; - uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FLOAT(a_price->datoshi_sell, 1 / a_price->rate); + uint256_t l_datoshi_buy = {}; // TODO rework it with fixed point MULT_256_FRAC_FRAC(a_price->datoshi_sell, 1 / a_price->rate); char *l_order_hash_str = dap_chain_net_srv_order_create(a_price->net_buy, SERV_DIR_SELL, l_uid, *l_node_addr, l_tx_hash, l_datoshi_buy, l_unit, a_price->token_buy, 0, (uint8_t *)&l_ext, l_ext_size, NULL, 0, a_price->wallet_key); diff --git a/modules/type/dag/dap_chain_cs_dag_event.c b/modules/type/dag/dap_chain_cs_dag_event.c index 118961d585e4ad2ba5fb0d6a8f2241ed2e28c29a..0941d153cb4fed945f844dbf93c7a2124ea761f5 100644 --- a/modules/type/dag/dap_chain_cs_dag_event.c +++ b/modules/type/dag/dap_chain_cs_dag_event.c @@ -233,23 +233,23 @@ size_t dap_chain_cs_dag_event_round_sign_add(dap_chain_cs_dag_event_round_item_t size_t l_offset = (size_t)l_round_item->event_size; while ( l_offset < (size_t)l_round_item->data_size ) { - dap_sign_t * l_item_sign = (dap_sign_t *)(l_round_item->event_n_signs+l_offset); - size_t l_sign_item_size = dap_sign_get_size(l_item_sign); - dap_chain_addr_t l_item_addr = {0}; - dap_chain_hash_fast_t l_item_pkey_hash; - dap_sign_get_pkey_hash(l_item_sign, &l_item_pkey_hash); - dap_chain_addr_fill(&l_item_addr, l_item_sign->header.type, &l_item_pkey_hash, a_net->pub.id); - if (memcmp(&l_addr, &l_item_addr, sizeof(l_item_addr)) == 0) { - DAP_DELETE(l_sign); - return 0; - } - l_offset += l_sign_item_size; - } - *a_round_item_ptr = l_round_item = DAP_REALLOC(l_round_item, a_round_item_size+l_sign_size); - memcpy(l_round_item->event_n_signs+l_offset, l_sign, l_sign_size); - DAP_DELETE(l_sign); - l_round_item->data_size += (uint32_t)l_sign_size; - return a_round_item_size+l_sign_size; + dap_sign_t * l_item_sign = (dap_sign_t *)(l_round_item->event_n_signs+l_offset); + size_t l_sign_item_size = dap_sign_get_size(l_item_sign); + dap_chain_addr_t l_item_addr = {0}; + dap_chain_hash_fast_t l_item_pkey_hash; + dap_sign_get_pkey_hash(l_item_sign, &l_item_pkey_hash); + dap_chain_addr_fill(&l_item_addr, l_item_sign->header.type, &l_item_pkey_hash, a_net->pub.id); + if (memcmp(&l_addr, &l_item_addr, sizeof(l_item_addr)) == 0) { + DAP_DELETE(l_sign); + return 0; + } + l_offset += l_sign_item_size; + } + *a_round_item_ptr = l_round_item = DAP_REALLOC(l_round_item, a_round_item_size+l_sign_size); + memcpy(l_round_item->event_n_signs+l_offset, l_sign, l_sign_size); + DAP_DELETE(l_sign); + l_round_item->data_size += (uint32_t)l_sign_size; + return a_round_item_size+l_sign_size; } static bool s_event_broadcast_send(dap_chain_cs_dag_event_round_broadcast_t *l_arg) {