Skip to content
Snippets Groups Projects
Commit ac554f3e authored by Dmitriy A. Gerasimov's avatar Dmitriy A. Gerasimov
Browse files

[+] Chunks managment (for BTFC)

parent 94965421
No related branches found
No related tags found
No related merge requests found
Pipeline #6258 passed with stage
in 16 seconds
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Ltd https://demlabs.net
* Copyright (c) 2020
* All rights reserved.
This file is part of DAP SDK the open source project
DAP SDK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP SDK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dap_common.h"
#include "dap_strfuncs.h"
#include "dap_chain_net.h"
#include "dap_chain_global_db.h"
#include "dap_chain_block_chunk.h"
#define LOG_TAG "dap_chain_block_chunk"
/**
* @brief dap_chain_block_chunks_create
* @param a_blocks
* @return
*/
dap_chain_block_chunks_t * dap_chain_block_chunks_create(dap_chain_cs_blocks_t * a_blocks)
{
assert(a_blocks);
assert(a_blocks->chain);
dap_chain_block_chunks_t * l_ret = DAP_NEW_Z(dap_chain_block_chunks_t);
l_ret->blocks = a_blocks;
l_ret->gdb_group = dap_strdup_printf("local.%s.%s.block.chunks",a_blocks->chain->net_name, a_blocks->chain->name );
size_t l_objs_count =0;
dap_global_db_obj_t * l_objs= dap_chain_global_db_gr_load(l_ret->gdb_group, &l_objs_count);
for(size_t n=0; n< l_objs_count; n++){
dap_chain_block_chunks_add(l_ret,(dap_chain_block_t*) l_objs[n].value,l_objs[n].value_len );
}
dap_chain_global_db_objs_delete(l_objs,l_objs_count);
return l_ret;
}
/**
* @brief dap_chain_block_chunks_delete
* @param a_chunks
*/
void dap_chain_block_chunks_delete(dap_chain_block_chunks_t * a_chunks)
{
dap_chain_block_cache_t * l_block_cache = NULL, *l_tmp = NULL;
HASH_ITER(hh, a_chunks->cache, l_block_cache, l_tmp){
HASH_DEL(a_chunks->cache, l_block_cache);
dap_chain_block_cache_delete(l_block_cache);
}
DAP_DELETE(a_chunks->gdb_group);
DAP_DELETE(a_chunks);
}
/**
* @brief dap_chain_block_chunks_add
* @param a_chunks
* @param a_block
* @param a_block_size
* @return
*/
dap_chain_block_cache_t * dap_chain_block_chunks_add(dap_chain_block_chunks_t * a_chunks, dap_chain_block_t *a_block ,size_t a_block_size)
{
dap_chain_block_cache_t * l_block_cache = dap_chain_block_cache_new(a_block,a_block_size);
dap_chain_block_cache_t * l_block_cache_check = NULL;
HASH_FIND(hh, a_chunks->cache, &l_block_cache->block_hash, sizeof (l_block_cache->block_hash), l_block_cache_check);
if (l_block_cache_check){
log_it(L_WARNING, "Already present block %s in cache",l_block_cache_check->block_hash_str);
dap_chain_block_cache_delete(l_block_cache);
return l_block_cache_check;
}
dap_chain_global_db_gr_set(dap_strdup(l_block_cache->block_hash_str),a_block,a_block_size, a_chunks->gdb_group);
HASH_ADD(hh,a_chunks->cache,block_hash,sizeof (l_block_cache->block_hash), l_block_cache);
return l_block_cache;
}
......@@ -30,6 +30,8 @@
#include "dap_chain_cs_blocks.h"
#include "dap_chain_block.h"
#include "dap_chain_block_cache.h"
#include "dap_chain_block_chunk.h"
#include "dap_chain_node_cli.h"
#include "dap_chain_node_cli_cmd.h"
#define LOG_TAG "dap_chain_cs_blocks"
......@@ -52,6 +54,9 @@ typedef struct dap_chain_cs_blocks_pvt
dap_chain_block_cache_t * blocks;
dap_chain_block_cache_t * blocks_tx_treshold;
// Chunks treshold
dap_chain_block_chunks_t * chunks;
dap_chain_tx_block_index_t * tx_block_index; // To find block hash by tx hash
// General lins
......@@ -62,6 +67,8 @@ typedef struct dap_chain_cs_blocks_pvt
uint64_t blocks_count;
uint64_t difficulty;
time_t time_between_blocks_minimum; // Minimal time between blocks
size_t block_size_maximum; // Maximum block size
bool is_celled;
} dap_chain_cs_blocks_pvt_t;
......@@ -210,6 +217,8 @@ int dap_chain_cs_blocks_new(dap_chain_t * a_chain, dap_config_t * a_chain_config
}
}
l_cs_blocks_pvt->is_celled = dap_config_get_item_bool_default(a_chain_config,"blocks","is_celled",false);
l_cs_blocks_pvt->chunks = dap_chain_block_chunks_create(l_cs_blocks);
// dap_chain_node_role_t l_net_role= dap_chain_net_get_role( dap_chain_net_by_id(a_chain->net_id) );
// Datum operations callbacks
......@@ -228,7 +237,8 @@ int dap_chain_cs_blocks_new(dap_chain_t * a_chain, dap_config_t * a_chain_config
*/
void dap_chain_cs_blocks_delete(dap_chain_t * a_chain)
{
pthread_rwlock_destroy(&PVT(a_chain)->rwlock );
pthread_rwlock_destroy(&PVT( DAP_CHAIN_CS_BLOCKS(a_chain) )->rwlock );
dap_chain_block_chunks_delete(PVT(DAP_CHAIN_CS_BLOCKS(a_chain))->chunks );
}
/**
......@@ -653,12 +663,12 @@ static int s_add_atom_to_blocks(dap_chain_cs_blocks_t * a_blocks, dap_ledger_t *
pthread_rwlock_rdlock( &PVT(a_blocks)->rwlock );
int res = a_blocks->callback_block_verify(a_blocks,a_block_cache->block, a_block_cache->block_size);
if (res == 0 || memcmp( &a_block_cache->block_hash, &PVT(a_blocks)->genesis_block_hash, sizeof(a_block_cache->block_hash) ) == 0) {
log_it(L_DEBUG,"Dag event %s checked, add it to ledger", a_block_cache->block_hash_str );
log_it(L_DEBUG,"Block %s checked, add it to ledger", a_block_cache->block_hash_str );
pthread_rwlock_unlock( &PVT(a_blocks)->rwlock );
res = s_add_atom_to_ledger(a_blocks, a_ledger, a_block_cache);
if (res) {
pthread_rwlock_rdlock( &PVT(a_blocks)->rwlock );
log_it(L_INFO,"Dag event %s checked, but ledger declined", a_block_cache->block_hash_str );
log_it(L_INFO,"Block %s checked, but ledger declined", a_block_cache->block_hash_str );
pthread_rwlock_unlock( &PVT(a_blocks)->rwlock );
return res;
}
......@@ -672,7 +682,7 @@ static int s_add_atom_to_blocks(dap_chain_cs_blocks_t * a_blocks, dap_ledger_t *
PVT(a_blocks)->block_cache_last = a_block_cache;
} else {
log_it(L_WARNING,"Dag event %s check failed: code %d", a_block_cache->block_hash_str, res );
log_it(L_WARNING,"Block %s check failed: code %d", a_block_cache->block_hash_str, res );
}
pthread_rwlock_unlock( &PVT(a_blocks)->rwlock );
return res;
......
......@@ -2,7 +2,7 @@
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Ltd https://demlabs.net
* Copyright (c) 2017-2020
* Copyright (c) 2017
* All rights reserved.
This file is part of DAP SDK the open source project
......
/*
* Authors:
* Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Ltd https://demlabs.net
* Copyright (c) 2020
* All rights reserved.
This file is part of DAP SDK the open source project
DAP SDK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DAP SDK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with any DAP SDK based project. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "dap_chain_block_cache.h"
#include "dap_chain_cs_blocks.h"
typedef struct dap_chain_block_chunks{
dap_chain_cs_blocks_t * blocks;
dap_chain_block_cache_t * cache;
dap_chain_block_cache_t * chunk_longest;
size_t chunk_longest_length;
char * gdb_group;
} dap_chain_block_chunks_t;
dap_chain_block_chunks_t * dap_chain_block_chunks_create(dap_chain_cs_blocks_t * a_blocks);
void dap_chain_block_chunks_delete(dap_chain_block_chunks_t * a_chunks);
dap_chain_block_cache_t * dap_chain_block_chunks_add(dap_chain_block_chunks_t * a_chunks, dap_chain_block_t *a_block ,size_t a_block_size);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment