Skip to content

Commit

Permalink
Merge pull request #58 from teadetime/level_DB
Browse files Browse the repository at this point in the history
Level db persistent storage!
  • Loading branch information
teadetime authored Apr 23, 2022
2 parents 68f0b39 + ae1f63b commit 8365b8a
Show file tree
Hide file tree
Showing 40 changed files with 1,541 additions and 723 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.13)

# set the project name
project(OlinCoin C)
project(OlinCoin C CXX)
enable_testing()
# set(CMAKE_BUILD_TYPE Debug)
#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BINARY_DIR ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
10 changes: 8 additions & 2 deletions src/core/blocks/create_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Transaction *create_coinbase_tx(unsigned long tx_fees){
miner_output->amt = tx_fees + calc_block_reward(chain_height);

mbedtls_ecdsa_context *key_pair = gen_keys();
key_pool_add(key_pair);
key_pool_add_leveldb(key_pair);
hash_pub_key(miner_output->public_key_hash, key_pair);

coinbase_tx->inputs = NULL;
Expand All @@ -49,8 +49,14 @@ unsigned int calc_tx_fees(Transaction *tx){
unsigned long total_out = 0;

for(unsigned int i = 0; i < tx->num_inputs; i++){
UTXO* input_utxo = utxo_pool_find(tx->inputs[i].prev_tx_id, tx->inputs[i].prev_utxo_output);
UTXO *input_utxo = NULL;// = utxo_pool_find(tx->inputs[i].prev_tx_id, tx->inputs[i].prev_utxo_output);
int utxo_found = utxo_pool_find_leveldb(&input_utxo, tx->inputs[i].prev_tx_id, tx->inputs[i].prev_utxo_output);
if(utxo_found != 0){
fprintf(stderr, "UTXO Found failed calculaiting tx fees: %i\n", utxo_found);
exit(1);
}
total_in += input_utxo->amt; // This could be null if no uxto found so make sure to confirm tx has all valid inputs
free(input_utxo);
}
for(unsigned int i = 0; i < tx->num_outputs; i++){
total_out += tx->outputs->amt;
Expand Down
19 changes: 12 additions & 7 deletions src/core/blocks/handle_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@

void update_local_blockchain(Block *block){
// NOTE: NOT DEALING WITH BRANCHES HERE This is left for branch resolution code here
blockchain_add(block); // T\NOTE this increases chain height and sets new top header hash
int ret_add = blockchain_add_leveldb(block);
if(ret_add != 0){
fprintf(stderr, "Blockchain Add Failed: \n");
exit(1);
}
}

void update_UTXO_pool_and_wallet_pool(Block *block){
for(unsigned int i = 0; i < block->num_txs; i++){
for(unsigned int j = 0; j < block->txs[i]->num_outputs; j++){
utxo_pool_add(block->txs[i], j); // Don't need to do anything with return
mbedtls_ecdsa_context *keypair = check_if_output_unlockable(block->txs[i], j);

utxo_pool_add_leveldb(block->txs[i], j);
mbedtls_ecdsa_context *keypair = check_if_output_unlockable_leveldb(block->txs[i], j);
if(keypair != NULL){
wallet_pool_add(block->txs[i], j, keypair);
wallet_pool_build_add_leveldb(block->txs[i], j, keypair);
mbedtls_ecdsa_free(keypair);
}
}
for(unsigned int k = 0; k < block->txs[i]->num_inputs; k++){
UTXO *spent_utxo = utxo_pool_remove(block->txs[i]->inputs[k].prev_tx_id,
utxo_pool_remove_leveldb(block->txs[i]->inputs[k].prev_tx_id,
block->txs[i]->inputs[k].prev_utxo_output);
free(spent_utxo);
wallet_pool_remove(block->txs[i]->inputs[k].prev_tx_id,
wallet_pool_remove_leveldb(block->txs[i]->inputs[k].prev_tx_id,
block->txs[i]->inputs[k].prev_utxo_output);
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/core/blocks/validate_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ int validate_prev_block_exists(Block *block){
// Also check if it's in blockchain already....
// Check if prev block is our latest top block

Block *prev_block = blockchain_find(block->header.prev_header_hash);
if(prev_block == NULL){
Block *prev_block = NULL;
int ret_find = blockchain_find_leveldb(&prev_block, block->header.prev_header_hash);
if(prev_block == NULL || ret_find != 0){

add_to_pending_blocks(block->header.prev_header_hash);
request_prev_block(block->header.prev_header_hash);
return 1;
}

free(prev_block);
if(memcmp(block->header.prev_header_hash, top_block_header_hash, BLOCK_HASH_LEN) != 0 ){
// NOTE this means we already have atleast one block ahead
return 2;
Expand Down Expand Up @@ -86,14 +88,13 @@ int validate_incoming_block_txs(Transaction **txs, unsigned int num_txs){
int validate_block_double_spend(Block *block){
UTXOPool *double_spend_set;

/* double_spend_set_init(double_spend_set); */
double_spend_set = NULL;
double_spend_set_init(&double_spend_set);
for (unsigned int i = 0; i < block->num_txs; i++) {
for (unsigned int j = 0; j < block->txs[i]->num_inputs; j++) {
if (double_spend_add(
double_spend_set,
block->txs[i]->inputs[i].prev_tx_id,
block->txs[i]->inputs[i].prev_utxo_output
block->txs[i]->inputs[j].prev_tx_id,
block->txs[i]->inputs[j].prev_utxo_output
) != 0) {
delete_double_spend_set(double_spend_set);
return 1;
Expand Down
6 changes: 4 additions & 2 deletions src/core/globals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ target_link_libraries(mempool
add_library(utxopool STATIC utxo_pool.c)
target_include_directories(utxopool PUBLIC ${INCLUDE_ALL})
target_link_libraries(utxopool
core_tx)
core_tx
init_db)

add_library(lists STATIC list.c)
target_include_directories(lists PUBLIC ${INCLUDE_ALL})

add_library(blockchain STATIC blockchain.c)
target_include_directories(blockchain PUBLIC ${INCLUDE_ALL})
target_link_libraries(blockchain
core_block)
core_block
init_db)

add_library(double_spend STATIC double_spend_set.c)
target_include_directories(double_spend PUBLIC ${INCLUDE_ALL})
Expand Down
Loading

0 comments on commit 8365b8a

Please sign in to comment.