diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a56530d4c9bfb37f2c3bac38001dd7303ecb136..59356e700d97004b94177902e874a8d19216e0b2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,14 +38,25 @@ set(DAP_CHAIN_HEADERS dap_chain_ledger.h ) +if(WIN32) + include_directories(../libdap/src/win32/) + include_directories(../3rdparty/libmemcached/) + include_directories(../3rdparty/libmemcached/win32/) + include_directories(../3rdparty/wepoll/include/) + include_directories(../3rdparty/uthash/src/) + include_directories(../3rdparty/libjson-c/) + include_directories(../3rdparty/libmagic/src/) + include_directories(../3rdparty/curl/include/) + include_directories(../3rdparty/libsqlite3/) +endif() + add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_SRCS} ${DAP_CHAIN_HEADERS}) -find_package(PkgConfig REQUIRED) -pkg_search_module(GLIB REQUIRED glib-2.0) +#find_package(PkgConfig REQUIRED) +#pkg_search_module(GLIB REQUIRED glib-2.0) target_link_libraries(dap_chain dap_core dap_chain_crypto dap_chain_mempool dap_chain_global_db ${GLIB_LDFLAGS}) target_include_directories(dap_chain INTERFACE . ${GLIB_INCLUDE_DIRS}) - set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE) set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE) diff --git a/dap_chain.c b/dap_chain.c index c48ef7ac9f2432092a05f88bd43e6e69b00edd28..2a4282e99ebdd274179ca8d4c0fbb44830c3ef81 100755 --- a/dap_chain.c +++ b/dap_chain.c @@ -71,11 +71,7 @@ int dap_chain_init(void) l_ca_folders = dap_config_get_array_str(g_config,"resources","ca_folders",&l_ca_folders_size); for (uint16_t i=0; i < l_ca_folders_size; i++){ dap_chain_cert_add_folder(l_ca_folders[i]); - DAP_DELETE( l_ca_folders[i]); } - - DAP_DELETE( l_ca_folders); - // Cell sharding init dap_chain_cell_init(); @@ -191,7 +187,7 @@ dap_chain_t * dap_chain_find_by_id(dap_chain_net_id_t a_chain_net_id,dap_chain_i pthread_rwlock_rdlock(&s_chain_items_rwlock); HASH_FIND(hh,s_chain_items,&l_chain_item_id,sizeof(dap_chain_item_id_t),l_ret_item); - pthread_rwlock_rdlock(&s_chain_items_rwlock); + pthread_rwlock_unlock(&s_chain_items_rwlock); if ( l_ret_item ){ return l_ret_item->chain; }else diff --git a/dap_chain_cell.c b/dap_chain_cell.c index 752b3b6468680ae1aa132b86c5a3071ef402dd15..a041296e700843003ab28d4db20d72897580f9ee 100755 --- a/dap_chain_cell.c +++ b/dap_chain_cell.c @@ -87,8 +87,7 @@ int dap_chain_cell_load(dap_chain_t * a_chain, const char * a_cell_file_path) l_cell->file_storage_path = dap_strdup( a_cell_file_path ); - - l_cell->file_storage = fopen(l_cell->file_storage_path,"a+"); + l_cell->file_storage = fopen(l_cell->file_storage_path,"a+b"); if ( l_cell->file_storage ){ dap_chain_cell_file_header_t l_hdr = {0}; @@ -165,7 +164,7 @@ int dap_chain_cell_file_append( dap_chain_cell_t * a_cell, const void* a_atom, s int dap_chain_cell_file_update( dap_chain_cell_t * a_cell) { if(a_cell->file_storage == NULL ){ // File need to be created - a_cell->file_storage = fopen(a_cell->file_storage_path,"w"); + a_cell->file_storage = fopen(a_cell->file_storage_path,"wb"); if ( a_cell->file_storage ){ dap_chain_cell_file_header_t l_hdr = { .signature = DAP_CHAIN_CELL_FILE_SIGNATURE, diff --git a/dap_chain_common.c b/dap_chain_common.c index ae94273522f69174270ff30aa59a71b2deee3e9a..3bb1f23ef1f45976d6f8b376762d21fc9150d641 100755 --- a/dap_chain_common.c +++ b/dap_chain_common.c @@ -38,17 +38,24 @@ * @param a_str_max * @return */ -size_t dap_chain_hash_slow_to_str(dap_chain_hash_slow_t * a_hash, char * a_str, size_t a_str_max) +size_t dap_chain_hash_slow_to_str( dap_chain_hash_slow_t *a_hash, char *a_str, size_t a_str_max ) { const size_t c_hash_str_size = sizeof(*a_hash) * 2 + 1 /*trailing zero*/+ 2 /* heading 0x */; + if(a_str_max < c_hash_str_size) { log_it(L_ERROR, "String for hash too small, need %u but have only %u", c_hash_str_size, a_str_max); } size_t i; - snprintf(a_str, 3, "0x"); + dap_snprintf(a_str, 3, "0x"); + for(i = 0; i < sizeof(a_hash->raw); ++i) - snprintf(a_str + i * 2 + 2, 3, "%02x", a_hash->raw[i]); + dap_snprintf( a_str + i * 2 + 2, 3, "%02x", a_hash->raw[i] ); + a_str[c_hash_str_size] = '\0'; + +//#define dap_htoa64( out, in, len ) \ + + return strlen(a_str); } @@ -59,23 +66,34 @@ size_t dap_chain_hash_slow_to_str(dap_chain_hash_slow_t * a_hash, char * a_str, * @param a_str_max * @return */ -size_t dap_chain_hash_fast_to_str(dap_chain_hash_fast_t * a_hash, char * a_str, size_t a_str_max) +#if 0 +size_t dap_chain_hash_fast_to_str( dap_chain_hash_fast_t *a_hash, char *a_str, size_t a_str_max ) { const size_t c_hash_str_size = sizeof(*a_hash) * 2 + 1 /*trailing zero*/+ 2 /* heading 0x */; - if(a_str_max < c_hash_str_size) { - log_it(L_ERROR, "String for hash too small, need %u but have only %u", c_hash_str_size, a_str_max); + + if ( a_str_max < c_hash_str_size ) { + log_it( L_ERROR, "String for hash too small, need %u but have only %u", c_hash_str_size, a_str_max ); } - size_t i; + +// size_t i; // faster conversion to string - snprintf(a_str, 3, "0x"); + + dap_snprintf( a_str, 3, "0x" ); + size_t l_ret = dap_bin2hex(a_str + 2, a_hash->raw, sizeof(a_hash->raw)); + //for(i = 0; i < sizeof(a_hash->raw); ++i) - // snprintf(a_str + i * 2 + 2, 3, "%02x", (a_hash->raw[i])); + // dap_snprintf(a_str + i * 2 + 2, 3, "%02x", (a_hash->raw[i])); + a_str[c_hash_str_size - 1] = '\0'; + if(!l_ret) return 0; + return c_hash_str_size - 1; //strlen(a_str); } +#endif + /** * @brief dap_chain_str_to_hash_fast_to_str @@ -87,11 +105,12 @@ int dap_chain_str_to_hash_fast( const char * a_hash_str, dap_chain_hash_fast_t * { const size_t c_hash_str_size = sizeof(*a_hash) * 2 + 1 /*trailing zero*/+ 2 /* heading 0x */; size_t l_hash_str_len = strlen( a_hash_str); - if ( l_hash_str_len == c_hash_str_size ){ - for (size_t l_offset = 2; l_offset < c_hash_str_size ; l_offset+=2 ){ - if ( ( sscanf(a_hash_str+l_offset,"%02hhx",a_hash->raw+l_offset-2) != 1) || - ( sscanf(a_hash_str+l_offset,"%02hhX",a_hash->raw+l_offset-2) != 1) + if ( l_hash_str_len + 1 == c_hash_str_size ){ + for (size_t l_offset = 2; l_offset < l_hash_str_len; l_offset+=2 ){ + if ( ( sscanf(a_hash_str+l_offset,"%02hhx",a_hash->raw+l_offset/2-1) != 1) || + ( sscanf(a_hash_str+l_offset,"%02hhX",a_hash->raw+l_offset/2-1) != 1) ) + printf("dap_chain_str_to_hash_fast Error\n "); return -10* ((int) l_offset); // Wrong char } return 0; diff --git a/dap_chain_common.h b/dap_chain_common.h index b8bef3f23fe967225cf77712bd56577ec9dfceb2..20c28fa62e8323cb85b9e0d08953ea23ba92d80e 100755 --- a/dap_chain_common.h +++ b/dap_chain_common.h @@ -32,22 +32,23 @@ #define DAP_CHAIN_ADDR_VERSION_CURRENT 1 -#define DAP_CHAIN_ID_SIZE 8 -#define DAP_CHAIN_SHARD_ID_SIZE 8 -#define DAP_CHAIN_NET_ID_SIZE 8 -#define DAP_CHAIN_NODE_ROLE_SIZE 2 -#define DAP_CHAIN_HASH_SLOW_SIZE 32 -#define DAP_CHAIN_HASH_FAST_SIZE 32 -#define DAP_CHAIN_TIMESTAMP_SIZE 8 -#define DAP_CHAIN_TICKER_SIZE_MAX 10 +#define DAP_CHAIN_ID_SIZE 8 +#define DAP_CHAIN_SHARD_ID_SIZE 8 +#define DAP_CHAIN_NET_ID_SIZE 8 +#define DAP_CHAIN_NODE_ROLE_SIZE 2 +#define DAP_CHAIN_HASH_SLOW_SIZE 32 +#define DAP_CHAIN_HASH_FAST_SIZE 32 +#define DAP_CHAIN_TIMESTAMP_SIZE 8 +#define DAP_CHAIN_TICKER_SIZE_MAX 10 + // Chain ID of the whole system -typedef union dap_chain_id{ +typedef union dap_chain_id { uint8_t raw[DAP_CHAIN_ID_SIZE]; uint64_t uint64; } DAP_ALIGN_PACKED dap_chain_id_t; // Shard ID -typedef union dap_chain_cell_id{ +typedef union dap_chain_cell_id { uint8_t raw[DAP_CHAIN_SHARD_ID_SIZE]; uint64_t uint64; } DAP_ALIGN_PACKED dap_chain_cell_id_t; @@ -57,7 +58,7 @@ typedef union dap_chain_cell_id{ * @struct Node address * */ -typedef union dap_chain_node_addr{ +typedef union dap_chain_node_addr { uint64_t uint64; uint16_t words[sizeof(uint64_t)/2]; uint8_t raw[sizeof(uint64_t)]; // Access to selected octects @@ -105,7 +106,6 @@ typedef union dap_chain_net_id{ uint8_t raw[DAP_CHAIN_NET_ID_SIZE]; } DAP_ALIGN_PACKED dap_chain_net_id_t; - typedef union dap_chain_hash_slow{ uint8_t raw[DAP_CHAIN_HASH_SLOW_SIZE]; } dap_chain_hash_slow_t; @@ -173,7 +173,7 @@ typedef struct dap_chain_addr{ } DAP_ALIGN_PACKED dap_chain_addr_t; size_t dap_chain_hash_slow_to_str(dap_chain_hash_slow_t * a_hash, char * a_str, size_t a_str_max); -size_t dap_chain_hash_fast_to_str(dap_chain_hash_fast_t * a_hash, char * a_str, size_t a_str_max); +//size_t dap_chain_hash_fast_to_str(dap_chain_hash_fast_t * a_hash, char * a_str, size_t a_str_max); int dap_chain_str_to_hash_fast( const char * a_hash_str, dap_chain_hash_fast_t * a_hash); char* dap_chain_addr_to_str(const dap_chain_addr_t *a_addr); @@ -192,6 +192,7 @@ static inline long double dap_chain_balance_to_coins( uint64_t a_balance){ * @param a_hash * @return */ + static inline char * dap_chain_hash_slow_to_str_new(dap_chain_hash_slow_t * a_hash) { const size_t c_hash_str_size = sizeof(*a_hash)*2 +1 /*trailing zero*/ +2 /* heading 0x */ ; @@ -200,11 +201,20 @@ static inline char * dap_chain_hash_slow_to_str_new(dap_chain_hash_slow_t * a_ha return ret; } -static inline char * dap_chain_hash_fast_to_str_new(dap_chain_hash_fast_t * a_hash) +DAP_STATIC_INLINE int dap_chain_hash_fast_to_str( dap_chain_hash_fast_t *a_hash, char *a_str, size_t a_str_max ) +{ + a_str[0] = '0'; + a_str[1] = 'x'; + a_str[ DAP_CHAIN_HASH_FAST_SIZE * 2 + 2 ] = 0; + dap_htoa64( (a_str + 2), a_hash->raw, DAP_CHAIN_HASH_FAST_SIZE ); + return DAP_CHAIN_HASH_FAST_SIZE * 2 + 2; +} + +static inline char *dap_chain_hash_fast_to_str_new(dap_chain_hash_fast_t * a_hash) { const size_t c_hash_str_size = sizeof(*a_hash)*2 +1 /*trailing zero*/ +2 /* heading 0x */+4/*just to be sure*/ ; char * ret = DAP_NEW_Z_SIZE(char, c_hash_str_size); - dap_chain_hash_fast_to_str(a_hash,ret,c_hash_str_size); + dap_chain_hash_fast_to_str( a_hash, ret, c_hash_str_size ); return ret; } diff --git a/dap_chain_ledger.c b/dap_chain_ledger.c index 990955229fc924346c13099f8ffaaafee18edef5..f09feef70abc208a5b36740693170e1c2db6bfa0 100755 --- a/dap_chain_ledger.c +++ b/dap_chain_ledger.c @@ -23,11 +23,27 @@ along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. */ -#include "dap_chain_ledger.h" - +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <stdbool.h> +#include <stddef.h> #include <string.h> #include <pthread.h> -#include <malloc.h> +//#include <malloc.h> + +#ifdef _WIN32 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#include <winsock2.h> +#include <windows.h> +#include <mswsock.h> +#include <ws2tcpip.h> +#include <io.h> +#include <time.h> +//#include "wrappers.h" +#include <wepoll.h> +#endif #include "uthash.h" #include "utlist.h" @@ -41,6 +57,7 @@ #include "dap_chain_mempool.h" #include "dap_chain_global_db.h" #include "dap_chain_net.h" +#include "dap_chain_ledger.h" #define LOG_TAG "dap_chain_ledger" @@ -202,7 +219,7 @@ int dap_chain_ledger_token_add(dap_ledger_t * a_ledger, dap_chain_datum_token_t if ( l_token_item == NULL ){ l_token_item = DAP_NEW_Z(dap_chain_ledger_token_item_t); - snprintf(l_token_item->ticker,sizeof (l_token_item->ticker),"%s",a_token->header.ticker); + dap_snprintf(l_token_item->ticker,sizeof (l_token_item->ticker),"%s",a_token->header.ticker); l_token_item->datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, a_token_size); memcpy(l_token_item->datum_token, a_token,a_token_size); l_token_item->total_supply = a_token->header.total_supply; @@ -453,6 +470,9 @@ static dap_chain_datum_tx_t* s_find_datum_tx_by_hash(dap_ledger_t *a_ledger, { if(!a_tx_hash) return NULL; + +// log_it( L_ERROR, "s_find_datum_tx_by_hash( )..."); + dap_ledger_private_t *l_ledger_priv = PVT(a_ledger); dap_chain_datum_tx_t *l_tx_ret = NULL; dap_chain_ledger_tx_item_t *l_tx_item; @@ -464,6 +484,7 @@ static dap_chain_datum_tx_t* s_find_datum_tx_by_hash(dap_ledger_t *a_ledger, *a_item_out = l_tx_item; } pthread_rwlock_unlock(&l_ledger_priv->ledger_rwlock); + return l_tx_ret; } @@ -544,7 +565,7 @@ int dap_chain_ledger_tx_cache_check(dap_ledger_t *a_ledger, dap_chain_datum_tx_t char l_tx_prev_hash_str[70]; if (l_is_blank){ log_it(L_DEBUG, "Tx check: blank prev hash "); - snprintf(l_tx_prev_hash_str,sizeof( l_tx_prev_hash_str),"BLANK"); + dap_snprintf(l_tx_prev_hash_str,sizeof( l_tx_prev_hash_str),"BLANK"); }else{ dap_chain_hash_fast_to_str(&l_tx_prev_hash,l_tx_prev_hash_str,sizeof(l_tx_prev_hash_str)); log_it(L_DEBUG, "Tx check: tx prev hash %s",l_tx_prev_hash_str); @@ -885,8 +906,9 @@ int dap_chain_ledger_tx_add(dap_ledger_t *a_ledger, dap_chain_datum_tx_t *a_tx) } log_it(L_INFO, "Updated balance +%.3Lf %s, now %.3Lf on addr %s", dap_chain_balance_to_coins (l_out_item->header.value), + l_token_ticker, dap_chain_balance_to_coins (wallet_balance->balance), - l_token_ticker, l_addr_str); + l_addr_str); DAP_DELETE (l_addr_str); } else { log_it(L_WARNING, "Can't detect tx ticker or matching output, can't append balances cache");