diff --git a/client_mempool.c b/client_mempool.c index 5885e293170035484eb8c24a7838e0834aa3f83b..1a7bf546240f0043cb0556d77e641dc0e1ff165c 100644 --- a/client_mempool.c +++ b/client_mempool.c @@ -142,6 +142,8 @@ void client_mempool_close(client_mempool_t *mempool) { if(mempool) { + // TODO send last request for dehandshake with "SessionCloseAfterRequest=true" + // ... dap_client_pvt_t *l_client_internal = DAP_CLIENT_PVT(mempool->a_client); DAP_DELETE(l_client_internal->uplink_addr); dap_client_delete(mempool->a_client); @@ -152,19 +154,64 @@ void client_mempool_close(client_mempool_t *mempool) } } +/** + * datum add in mempool + * + * return -1 not connected, 1 Send packet OK + */ int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) { + if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED) + return -1; const char * a_path = "mempool"; const char *a_suburl = "mempool"; //"enc_init"; const char* a_query = ""; + uint8_t action = DAP_DATUM_MEMPOOL_ADD; size_t a_request_size = 0; uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size); - uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2); - bin2hex(a_request_out, a_request, a_request_size); - //uint8_t *a_request = "1234567\089012345643634346i34itkreghrth"; - //size_t a_request_size = 20; - dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size*2, + uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2 + 1); // a_request + 1 byte for type action + *((uint8_t*) a_request_out) = action; + bin2hex(a_request_out + 1, a_request, a_request_size); + mempool->state = CLIENT_MEMPOOL_SEND; + dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2 + 1, a_response_proc, a_response_error); - //DAP_DELETE(a_request); + DAP_DELETE(a_request); + DAP_DELETE(a_request_out); return 1; } + +/** + * datum check in mempool + * + * return -1 not connected or error, 1 present in mempool, 0 absent in mempool + */ +int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) +{ + if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED) + return -1; + + const char * a_path = "mempool"; + const char *a_suburl = "mempool"; //"enc_init"; + const char* a_query = ""; + uint8_t action = DAP_DATUM_MEMPOOL_CHECK; + size_t a_request_size = 0; + uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size); + uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2 + 1); // a_request + 1 byte for type action + *((uint8_t*) a_request_out) = action; + bin2hex(a_request_out + 1, a_request, a_request_size); + dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2 + 1, + a_response_proc, a_response_error); + DAP_DELETE(a_request); + DAP_DELETE(a_request_out); + return 1; +} + +/** + * datum delete from mempool + */ +int client_mempool_del_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) +{ + if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED) + return -1; + return 0; +} diff --git a/client_mempool.h b/client_mempool.h index 972515eeb867534a74bbddc570c6fc318d5239f5..c90d645e901759ebde716b8d1bec9af8165c5eba 100644 --- a/client_mempool.h +++ b/client_mempool.h @@ -6,7 +6,13 @@ // connection states enum { - CLIENT_MEMPOOL_ERROR = -1, CLIENT_MEMPOOL_INIT, CLIENT_MEMPOOL_CONNECT, CLIENT_MEMPOOL_CONNECTED, CLIENT_MEMPOOL_SENDED, CLIENT_MEMPOOL_END + CLIENT_MEMPOOL_ERROR = -1, + CLIENT_MEMPOOL_INIT, + CLIENT_MEMPOOL_CONNECT, + CLIENT_MEMPOOL_CONNECTED, + CLIENT_MEMPOOL_SEND, + CLIENT_MEMPOOL_SENDED, + CLIENT_MEMPOOL_END }; // state for a client connection with mempool @@ -21,7 +27,6 @@ typedef struct client_mempool_t { int client_mempool_init(void); void client_mempool_deinit(); - client_mempool_t* client_mempool_connect(const char *addr); void client_mempool_close(client_mempool_t *mempool); @@ -31,5 +36,15 @@ void client_mempool_close(client_mempool_t *mempool); */ int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout_ms); - +/** + * datum add in mempool + */ int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool); +/** + * datum check in mempool + */ +int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool); +/** + * datum delete from mempool + */ +int client_mempool_del_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool); diff --git a/dap_chain_mempool.c b/dap_chain_mempool.c index 9b2adff7c28e97c0f21293ee31bbce412d718541..188535bdfbe088c02d688c641a695d370dde06ba 100644 --- a/dap_chain_mempool.c +++ b/dap_chain_mempool.c @@ -19,6 +19,8 @@ #define FILE_MEMPOOL_DB "1.db" // TODO get from settings +#define LOG_TAG "MEMPOOL" + uint8_t* dap_datum_mempool_serialize(dap_datum_mempool_t *datum_mempool, size_t *size) { size_t a_request_size = 2 * sizeof(uint16_t), shift_size = 0; @@ -86,14 +88,14 @@ void dap_datum_mempool_free(dap_datum_mempool_t *datum) /** * */ -char* calc_datum_hash(const char *datum_str, size_t datum_size) +static char* calc_datum_hash(const char *datum_str, size_t datum_size) { dap_chain_hash_t a_hash; dap_hash((char*) datum_str, datum_size, a_hash.raw, sizeof(a_hash.raw), DAP_HASH_TYPE_SLOW_0); - size_t a_str_max = sizeof(a_hash.raw) * 2; + size_t a_str_max = (sizeof(a_hash.raw) + 1) * 2 + 2; /* heading 0x */ char *a_str = DAP_NEW_Z_SIZE(char, a_str_max); size_t hash_len = dap_chain_hash_to_str(&a_hash, a_str, a_str_max); - if(hash_len) { + if(!hash_len) { DAP_DELETE(a_str); return NULL; } @@ -136,7 +138,7 @@ int hex2bin(char *out, const unsigned char *in, int len) // 'a'-'f' = 0x61-0x66 // 'A'-'F' = 0x41-0x46 int ct = len; - if(!in || !out || len < 0 || len & 1) + if(!in || !out || len < 0 || (len & 1)) return -1; while(ct > 0) { @@ -162,28 +164,64 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) char *request_str = dg->request_str; int request_size = dg->request_size; printf("!!***!!! chain_mempool_proc arg=%d url=%s str=%s len=%d\n", arg, url, request_str, request_size); - if(request_str && request_size > 0) { + if(request_str && request_size > 1) { + uint8_t action = *(uint8_t*) request_str; + request_str++; + request_size--; dap_datum_mempool_t *datum_mempool = dap_datum_mempool_deserialize(request_str, (size_t) request_size); if(datum_mempool) { dap_datum_mempool_free(datum_mempool); char *a_key = calc_datum_hash(request_str, (size_t) request_size); - char *a_value = DAP_NEW_Z_SIZE(char, request_size * 2); - bin2hex((char*) a_value, (const unsigned char*) request_str, request_size); - if(dap_chain_global_db_set(a_key, a_value)) { + char *a_value; + switch (action) + { + case DAP_DATUM_MEMPOOL_ADD: + a_value = DAP_NEW_Z_SIZE(char, request_size * 2); + bin2hex((char*) a_value, (const unsigned char*) request_str, request_size); + if(dap_chain_global_db_set(a_key, a_value)) { + *return_code = Http_Status_OK; + } + log_it(L_NOTICE, "Insert hash: key=%s result:%s", a_key, + (*return_code == Http_Status_OK) ? "OK" : "False!"); + DAP_DELETE(a_value); + break; + case DAP_DATUM_MEMPOOL_CHECK: + + strcpy(cl_st->reply_mime, "text/text"); + char *str = dap_chain_global_db_get(a_key); + if(str) { + *return_code = Http_Status_OK; + dg->request = strdup("1"); + ; //cl_st->reply = strdup("1"); + DAP_DELETE(str); + } + else + dg->request = strdup("0"); //cl_st->reply = strdup("0"); + dg->in_query_size = strlen(dg->request); //cl_st->reply_size = strlen(cl_st->reply); + enc_http_reply_encode(cl_st, dg); + log_it(L_NOTICE, "Check hash: key=%s result:%s", a_key, + (*return_code == Http_Status_OK) ? "Present" : "Absent"); + break; + case DAP_DATUM_MEMPOOL_DEL: *return_code = Http_Status_OK; + log_it(L_NOTICE, "Delete hash: key=%s result:%s", a_key, + (*return_code == Http_Status_OK) ? "OK" : "False!"); + break; + default: + log_it(L_NOTICE, "Unknown request=%d! key=%s", action, a_key); DAP_DELETE(a_key); - DAP_DELETE(a_value); + enc_http_delegate_delete(dg); return; } DAP_DELETE(a_key); - DAP_DELETE(a_value); } else *return_code = Http_Status_InternalServerError; } else *return_code = Http_Status_BadRequest; + enc_http_delegate_delete(dg); } else { *return_code = Http_Status_Unauthorized; diff --git a/dap_chain_mempool.h b/dap_chain_mempool.h index 2a9896f0850d6845ec874c692f3008fd3d10bf10..10df5273c0fe9c9d54e0e7d8bf0a61a9c30c78a9 100644 --- a/dap_chain_mempool.h +++ b/dap_chain_mempool.h @@ -16,10 +16,15 @@ #define DAP_DATUM_MEMPOOL_VERSION "01" +// action +enum { + DAP_DATUM_MEMPOOL_ADD = 1, DAP_DATUM_MEMPOOL_CHECK, DAP_DATUM_MEMPOOL_DEL +}; + // datum mempool structure typedef struct dap_datum_mempool { - uint16_t version; // structure version - uint16_t datum_count;// datums count + uint16_t version; // structure version + uint16_t datum_count; // datums count dap_chain_datum_t **data;// mass of datums }DAP_ALIGN_PACKED dap_datum_mempool_t;