diff --git a/CMakeLists.txt b/CMakeLists.txt index af8b8769ce26c1ba032a1a1dd16d79318a7d84c8..4cad786450e8bc9b9d7016fd84913178d93287ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ project(cellframe-sdk C) cmake_minimum_required(VERSION 2.8) set(CMAKE_C_STANDARD 11) -set(CELLFRAME_SDK_NATIVE_VERSION "2.6-5") +set(CELLFRAME_SDK_NATIVE_VERSION "2.6-6") add_definitions ("-DCELLFRAME_SDK_VERSION=\"${CELLFRAME_SDK_NATIVE_VERSION}\"") set(DAPSDK_MODULES "") diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c index ee1c7a52132379d00a58c9f8bdfb6d2dc176c61a..238e97a4a69df20aafab2d60b05c1d165f0dfa1c 100644 --- a/modules/chain/dap_chain_cell.c +++ b/modules/chain/dap_chain_cell.c @@ -133,14 +133,20 @@ int dap_chain_cell_load(dap_chain_t * a_chain, const char * a_cell_file_path) sizeof(l_element_size) ){ if ( l_element_size > 0){ dap_chain_atom_ptr_t * l_element = DAP_NEW_Z_SIZE (dap_chain_atom_ptr_t, l_element_size ); - if (l_element){ - if ( fread( l_element,1,l_element_size,l_cell->file_storage ) == l_element_size ) { + if ( l_element){ + size_t l_read_bytes = fread( l_element,1,l_element_size,l_cell->file_storage ); + if ( l_read_bytes == l_element_size ) { a_chain->callback_atom_add (a_chain, l_element, l_element_size); + }else{ + log_it (L_ERROR, "Can't read %zd bytes (processed only %zd), stop cell load process", l_element_size, + l_read_bytes); + break; } }else{ log_it (L_ERROR, "Can't allocate %zd bytes, stop cell load process", l_element_size); break; } + } else { log_it (L_ERROR, "Zero element size, file is corrupted"); break; diff --git a/modules/chain/include/dap_chain.h b/modules/chain/include/dap_chain.h index 0229999b387fef74d3121bee193121001cbfe0df..baad228c8660bfde5ac7a7cb9a425e7b0d6107c0 100644 --- a/modules/chain/include/dap_chain.h +++ b/modules/chain/include/dap_chain.h @@ -64,6 +64,7 @@ typedef int (*dap_chain_callback_new_cfg_t)(dap_chain_t*, dap_config_t *); typedef void (*dap_chain_callback_ptr_t)(dap_chain_t *, void * ); typedef dap_chain_atom_verify_res_t (*dap_chain_callback_atom_t)(dap_chain_t *, dap_chain_atom_ptr_t, size_t ); +typedef dap_chain_atom_ptr_t (*dap_chain_callback_atom_form_treshold_t)(dap_chain_t *, size_t *); typedef dap_chain_atom_verify_res_t (*dap_chain_callback_atom_verify_t)(dap_chain_t *, dap_chain_atom_ptr_t , size_t); typedef size_t (*dap_chain_callback_atom_get_hdr_size_t)(void); @@ -120,6 +121,7 @@ typedef struct dap_chain{ dap_chain_callback_t callback_purge; dap_chain_callback_atom_t callback_atom_add; + dap_chain_callback_atom_form_treshold_t callback_atom_add_from_treshold; dap_chain_callback_atom_verify_t callback_atom_verify; dap_chain_callback_add_datums_t callback_add_datums; diff --git a/modules/channel/chain/dap_stream_ch_chain.c b/modules/channel/chain/dap_stream_ch_chain.c index 7cc13bfb3cdba6872e7943c42d5014bc7742422e..e7a5ce4f4d815fa870297537332323fa67776c8b 100644 --- a/modules/channel/chain/dap_stream_ch_chain.c +++ b/modules/channel/chain/dap_stream_ch_chain.c @@ -235,16 +235,41 @@ bool s_chain_pkt_callback(dap_proc_thread_t *a_thread, void *a_arg) if(l_atom_add_res == ATOM_ACCEPT && dap_chain_has_file_store(l_chain)) { // append to file dap_chain_cell_t *l_cell = dap_chain_cell_create_fill(l_chain, l_ch_chain->request_cell_id); - // add one atom only - int l_res = dap_chain_cell_file_append(l_cell, l_atom_copy, l_atom_copy_size); - // rewrite all file - //l_res = dap_chain_cell_file_update(l_cell); - if(!l_cell || l_res < 0) { - log_it(L_ERROR, "Can't save event 0x%x to the file '%s'", l_atom_hash, - l_cell ? l_cell->file_storage_path : "[null]"); + if(l_cell){ + // add one atom only + int l_res = dap_chain_cell_file_append(l_cell, l_atom_copy, l_atom_copy_size); + // rewrite all file + //l_res = dap_chain_cell_file_update(l_cell); + if(l_res < 0) { + log_it(L_ERROR, "Can't save event 0x%x to the file '%s'", l_atom_hash, + l_cell ? l_cell->file_storage_path : "[null]"); + } + // add all atoms from treshold + if(l_res && l_chain->callback_atom_add_from_treshold){ + dap_chain_atom_ptr_t l_atom_treshold; + do{ + size_t l_atom_treshold_size; + // add into ledger + l_atom_treshold = l_chain->callback_atom_add_from_treshold(l_chain, &l_atom_treshold_size); + // add into file + if(l_atom_treshold) { + int l_res = dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size); + if(l_res < 0) { + log_it(L_ERROR, "Can't save event 0x%x from treshold to the file '%s'", + l_atom_treshold, l_cell ? l_cell->file_storage_path : "[null]"); + } + } + } + while(l_atom_treshold); + } + + // delete cell and close file + dap_chain_cell_delete(l_cell); + } + else{ + log_it(L_ERROR, "Can't get cell for cell_id 0x%x for save event to file", l_ch_chain->request_cell_id); + } - // delete cell and close file - dap_chain_cell_delete(l_cell); } if(l_atom_add_res == ATOM_PASS) DAP_DELETE(l_atom_copy); diff --git a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c index 60b04baf49f1a973f1ae0739752db6c60366b58b..0f2e907ceb136ef828cd49a367a09c11986a3c2d 100644 --- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c +++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c @@ -393,6 +393,15 @@ static int s_callback_event_verify(dap_chain_cs_dag_t * a_dag, dap_chain_cs_dag_ } return l_verified >= l_poa_pvt->auth_certs_count_verify ? 0 : -1; + }else if (a_dag_event->header.hash_count == 0){ + dap_chain_hash_fast_t l_event_hash; + dap_chain_cs_dag_event_calc_hash(a_dag_event,a_dag_event_size, &l_event_hash); + if ( memcmp( &l_event_hash, &a_dag->static_genesis_event_hash, sizeof(l_event_hash) ) == 0 ){ + return 0; + }else{ + log_it(L_WARNING,"Wrong genesis event %p: hash is not equels to what in config", a_dag_event); + return -20; // Wrong signatures number + } }else{ log_it(L_WARNING,"Wrong signatures number with event %p", a_dag_event); return -2; // Wrong signatures number diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index 53de10dec0993374a7c926921abdc4e01727d69e..69e50b3968bee1cc102c902ec75dc5b5039fa99b 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -79,9 +79,11 @@ typedef struct dap_chain_cs_dag_pvt { #define PVT(a) ((dap_chain_cs_dag_pvt_t *) a->_pvt ) static void s_dap_chain_cs_dag_purge(dap_chain_t *a_chain); +dap_chain_cs_dag_event_item_t* dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a_ledger); // Atomic element organization callbacks static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_chain, dap_chain_atom_ptr_t , size_t); // Accept new event in dag +static dap_chain_atom_ptr_t s_chain_callback_atom_add_from_treshold(dap_chain_t * a_chain, size_t *a_event_size_out); // Accept new event in dag from treshold static dap_chain_atom_verify_res_t s_chain_callback_atom_verify(dap_chain_t * a_chain, dap_chain_atom_ptr_t , size_t); // Verify new event in dag static size_t s_chain_callback_atom_get_static_hdr_size(void); // Get dag event header size @@ -174,6 +176,7 @@ int dap_chain_cs_dag_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg) // Atom element callbacks a_chain->callback_atom_add = s_chain_callback_atom_add ; // Accept new element in chain + a_chain->callback_atom_add_from_treshold = s_chain_callback_atom_add_from_treshold; // Accept new elements in chain from treshold a_chain->callback_atom_verify = s_chain_callback_atom_verify ; // Verify new element in chain a_chain->callback_atom_get_hdr_static_size = s_chain_callback_atom_get_static_hdr_size; // Get dag event hdr size @@ -325,9 +328,13 @@ static int s_dap_chain_add_atom_to_events_table(dap_chain_cs_dag_t * a_dag, dap_ HASH_ADD(hh, PVT(a_dag)->events,hash,sizeof (a_event_item->hash), a_event_item); s_dag_events_lasts_process_new_last_event(a_dag, a_event_item); } else { - char l_buf_hash[128]; - dap_chain_hash_fast_to_str(&a_event_item->hash,l_buf_hash,sizeof(l_buf_hash)-1); - log_it(L_WARNING,"Dag event %s check failed: code %d", l_buf_hash, res ); + if ( memcmp( &a_event_item->hash, &a_dag->static_genesis_event_hash, sizeof(a_event_item->hash) ) == 0 ){ + res = 0; + }else{ + char l_buf_hash[128]; + dap_chain_hash_fast_to_str(&a_event_item->hash,l_buf_hash,sizeof(l_buf_hash)-1); + log_it(L_WARNING,"Dag event %s check failed: code %d", l_buf_hash, res ); + } } return res; } @@ -401,8 +408,8 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_cha ret = ATOM_REJECT; } } - - while(dap_chain_cs_dag_proc_treshold(l_dag, a_chain->ledger)); + // will added in callback s_chain_callback_atom_add_from_treshold() + //while(dap_chain_cs_dag_proc_treshold(l_dag, a_chain->ledger)); pthread_rwlock_unlock( l_events_rwlock ); if(ret == ATOM_PASS){ @@ -414,6 +421,29 @@ static dap_chain_atom_verify_res_t s_chain_callback_atom_add(dap_chain_t * a_cha return ret; } + +/** + * @brief s_chain_callback_atom_add_from_treshold Accept new event in dag + * @param a_chain DAG object + * @return true if added one item, otherwise false + */ +static dap_chain_atom_ptr_t s_chain_callback_atom_add_from_treshold(dap_chain_t * a_chain, size_t *a_event_size_out) +{ + dap_chain_cs_dag_t * l_dag = DAP_CHAIN_CS_DAG(a_chain); + pthread_rwlock_t * l_events_rwlock = &PVT(l_dag)->events_rwlock; + + pthread_rwlock_wrlock(l_events_rwlock); + dap_chain_cs_dag_event_item_t *l_item = dap_chain_cs_dag_proc_treshold(l_dag, a_chain->ledger); + pthread_rwlock_unlock(l_events_rwlock); +// dap_chain_cs_dag_event_calc_size() + if(l_item) { + if(a_event_size_out) + *a_event_size_out = l_item->event_size; + return l_item->event; + } + return NULL; +} + /** * @brief s_chain_callback_datums_add * @param a_chain @@ -532,6 +562,24 @@ static size_t s_chain_callback_datums_pool_proc(dap_chain_t * a_chain, dap_chain log_it(L_ERROR, "Can't add new event to the file '%s'", l_cell->file_storage_path); continue; } + // add all atoms from treshold + { + dap_chain_atom_ptr_t l_atom_treshold; + do { + size_t l_atom_treshold_size; + // add in ledger + l_atom_treshold = s_chain_callback_atom_add_from_treshold(a_chain, &l_atom_treshold_size); + // add into file + if(l_atom_treshold) { + int l_res = dap_chain_cell_file_append(l_cell, l_atom_treshold, l_atom_treshold_size); + if(l_res < 0) { + log_it(L_ERROR, "Can't save event 0x%x from treshold to the file '%s'", + l_atom_treshold, l_cell ? l_cell->file_storage_path : "[null]"); + } + } + } + while(l_atom_treshold); + } l_datum_processed++; } else { @@ -769,7 +817,7 @@ int dap_chain_cs_dag_event_verify_hashes_with_treshold(dap_chain_cs_dag_t * a_da * @param a_dag * @returns true if some atoms were moved from threshold to events */ -bool dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a_ledger) +dap_chain_cs_dag_event_item_t* dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a_ledger) { bool res = false; // TODO Process finish treshold. For now - easiest from possible @@ -787,18 +835,25 @@ bool dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a int l_add_res = s_dap_chain_add_atom_to_events_table(a_dag, a_ledger, l_event_item); if(! l_add_res){ log_it(L_DEBUG, "... added", l_event_hash_str); + DAP_DELETE(l_event_hash_str); + res = true; + break; }else{ log_it(L_DEBUG, "... error adding", l_event_hash_str); //todo: delete event } DAP_DELETE(l_event_hash_str); - res = true; + //res = true; }else if(ret == DAP_THRESHOLD_CONFLICTING) HASH_ADD(hh, PVT(a_dag)->events_treshold_conflicted, hash,sizeof (l_event_item->hash), l_event_item); } } - return res; + //return res; + if(res){ + return l_event_item; + } + return NULL; } /** @@ -868,11 +923,15 @@ static dap_chain_datum_t* s_chain_callback_atom_get_datum(dap_chain_atom_ptr_t a */ static dap_chain_atom_ptr_t s_chain_callback_atom_iter_get_first(dap_chain_atom_iter_t * a_atom_iter, size_t * a_ret_size ) { + if(! a_atom_iter){ + log_it(L_ERROR, "NULL iterator on input for atom_iter_get_first function"); + return NULL; + } dap_chain_cs_dag_t * l_dag = DAP_CHAIN_CS_DAG(a_atom_iter->chain); dap_chain_cs_dag_pvt_t *l_dag_pvt = l_dag ? PVT(l_dag) : NULL; a_atom_iter->cur_item = l_dag_pvt->events; a_atom_iter->cur = (dap_chain_cs_dag_event_t*) (l_dag_pvt->events ? l_dag_pvt->events->event : NULL); - a_atom_iter->cur_size =l_dag_pvt->events->event_size; + a_atom_iter->cur_size = l_dag_pvt->events ? l_dag_pvt->events->event_size : 0; // a_atom_iter->cur = a_atom_iter->cur ? // (dap_chain_cs_dag_event_t*) PVT (DAP_CHAIN_CS_DAG( a_atom_iter->chain) )->events->event : NULL; diff --git a/modules/type/dag/include/dap_chain_cs_dag.h b/modules/type/dag/include/dap_chain_cs_dag.h index 6befe38ae351a1f4d04298ef2a8b8f588ca3a3de..fab0c320166bb1a5a4fd60d86f059843521515ed 100644 --- a/modules/type/dag/include/dap_chain_cs_dag.h +++ b/modules/type/dag/include/dap_chain_cs_dag.h @@ -65,7 +65,7 @@ void dap_chain_cs_dag_deinit(void); int dap_chain_cs_dag_new(dap_chain_t * a_chain, dap_config_t * a_chain_cfg); void dap_chain_cs_dag_delete(dap_chain_t * a_chain); -bool dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a_ledger); +//dap_chain_cs_dag_event_item_t* dap_chain_cs_dag_proc_treshold(dap_chain_cs_dag_t * a_dag, dap_ledger_t * a_ledger); void dap_chain_cs_dag_proc_event_round_new(dap_chain_cs_dag_t *a_dag); dap_chain_cs_dag_event_t* dap_chain_cs_dag_find_event_by_hash(dap_chain_cs_dag_t * a_dag, diff --git a/modules/type/dag/include/dap_chain_cs_dag_event.h b/modules/type/dag/include/dap_chain_cs_dag_event.h index 5f3661be72d0cf47ea6bb17fc73fb116c2b47ee3..02203ef78792c8d7ec2a4e2b48d974c4692bc661 100644 --- a/modules/type/dag/include/dap_chain_cs_dag_event.h +++ b/modules/type/dag/include/dap_chain_cs_dag_event.h @@ -74,7 +74,6 @@ dap_sign_t * dap_chain_cs_dag_event_get_sign( dap_chain_cs_dag_event_t * a_event * @param a_event * @return */ - /** static inline size_t dap_chain_cs_dag_event_calc_size(dap_chain_cs_dag_event_t * a_event) { @@ -96,6 +95,7 @@ static inline size_t dap_chain_cs_dag_event_calc_size(dap_chain_cs_dag_event_t * return sizeof( a_event->header ) + l_hashes_size +l_signs_offset +l_datum_size; } **/ + /** * @brief dap_chain_cs_dag_event_calc_size_excl_signs * @param a_event