Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.13)
# set the project name
project(OlinCoin C CXX)
enable_testing()
#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Debug)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change back before final PR

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_BINARY_DIR ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
14 changes: 14 additions & 0 deletions src/core/blocks/base_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ void hash_blockheader(unsigned char *dest, BlockHeader *header) {
free(header_buf);
}

void free_block(Block *block) {
if (block == NULL)
return;

if (block->txs != NULL) {
for (unsigned int i = 0; i < block->num_txs; i++)
if (block->txs[i] != NULL)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check isn't actually needed, it is done in free_tx

free_tx(block->txs[i]);
free(block->txs);
}

free(block);
}

void print_block_header(BlockHeader *header, char *prefix){
char *sub_prefix = malloc(strlen(prefix)+strlen(PRINT_TAB)+1);
strcpy(sub_prefix, prefix);
Expand Down
11 changes: 2 additions & 9 deletions src/core/blocks/ser_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
return data; \
}

size_t size_ser_blockheader() {
return sizeof(((BlockHeader*)0)->timestamp) +
sizeof(((BlockHeader*)0)->all_tx) +
sizeof(((BlockHeader*)0)->prev_header_hash) +
sizeof(((BlockHeader*)0)->nonce);
}

ssize_t ser_blockheader(unsigned char *dest, BlockHeader *blockheader) {
memcpy(dest, &(blockheader->timestamp), sizeof(blockheader->timestamp));

Expand All @@ -42,7 +35,7 @@ ssize_t ser_blockheader(unsigned char *dest, BlockHeader *blockheader) {
unsigned char *ser_blockheader_alloc(ssize_t *written, BlockHeader *blockheader) {
unsigned char *data;
ssize_t ret;
data = malloc(size_ser_blockheader());
data = malloc(BLOCKHEADER_SER_LEN);
ret = ser_blockheader(data, blockheader);
RETURN_SER(data, ret, written)
}
Expand Down Expand Up @@ -75,7 +68,7 @@ BlockHeader *deser_blockheader_alloc(ssize_t *read, unsigned char *src) {
}

size_t size_ser_block(Block *block){
size_t size = sizeof(block->num_txs) + size_ser_blockheader();
size_t size = sizeof(block->num_txs) + BLOCKHEADER_SER_LEN;
for(unsigned int i = 0; i < block->num_txs; i++)
size += size_ser_tx(block->txs[i]);
return size;
Expand Down
1 change: 1 addition & 0 deletions src/core/globals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(double_spend STATIC double_spend_set.c)
target_include_directories(double_spend PUBLIC ${INCLUDE_ALL})
target_link_libraries(double_spend
utxopool)

add_library(init_globals STATIC init_globals.c)
target_include_directories(init_globals PUBLIC ${INCLUDE_ALL})
target_link_libraries(init_globals
Expand Down
15 changes: 7 additions & 8 deletions src/core/globals/blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int read_chain(){
free(path);
if(!fp_chain){
return i;
}
}
int ret_scanf = fscanf(fp_chain, "%d", &i);
fclose(fp_chain);
if(ret_scanf != 1){
Expand Down Expand Up @@ -114,7 +114,7 @@ int blockchain_init_leveldb(char *db_env){
// Check if we are using an existing DB
if(found_genesis == 0){
free(test_genesis_block);

if(read_hash == 0 && file_chain_height != -1){
Block *test_top_block;
int found_top = blockchain_find_leveldb(&test_top_block, file_top_block_hash);
Expand All @@ -132,14 +132,13 @@ int blockchain_init_leveldb(char *db_env){
}
}
}


if(use_existing_db){
chain_height = file_chain_height;
memcpy(top_block_header_hash, file_top_block_hash, BLOCK_HASH_LEN);
}
else{

int ret = blockchain_add_leveldb(&genesis_block);
if(ret != 0){
return 3;
Expand Down Expand Up @@ -254,7 +253,7 @@ void print_blockchain_hashmap(char *prefix){

printf("%shashmap_id:\n", prefix);
dump_buf(sub_prefix, "block_hash:", key_ptr, BLOCK_HASH_LEN);


Block *read_block = deser_block_alloc(NULL, (unsigned char*)value_ptr);
print_block(read_block, prefix);
Expand All @@ -263,7 +262,7 @@ void print_blockchain_hashmap(char *prefix){
}
leveldb_iter_destroy(iter);
leveldb_readoptions_destroy(roptions);
leveldb_free(err);
leveldb_free(err);
free(sub_prefix);
}

Expand All @@ -285,7 +284,7 @@ void pretty_print_blockchain_hashmap(){
unsigned const char *value_ptr = (unsigned const char*) leveldb_iter_value(iter, &value_len);

dump_buf("", "Block_Hash:", key_ptr, BLOCK_HASH_LEN);


Block *read_block = deser_block_alloc(NULL, (unsigned char*)value_ptr);
pretty_print_block_header(&read_block->header, PRINT_TAB);
Expand All @@ -295,5 +294,5 @@ void pretty_print_blockchain_hashmap(){
}
leveldb_iter_destroy(iter);
leveldb_readoptions_destroy(roptions);
leveldb_free(err);
leveldb_free(err);
}
6 changes: 3 additions & 3 deletions src/core/globals/mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Transaction *mempool_add(Transaction *tx) {

new_entry = malloc(sizeof(MemPool));
hash_tx(new_entry->id, tx);
new_entry->tx = tx;
new_entry->tx = copy_tx(tx);

found_entry = mempool_find_node(new_entry->id);
if (found_entry == NULL) {
HASH_ADD(hh, mempool, id, TX_HASH_LEN, new_entry);
utxo_to_tx_add_tx(tx);
return tx;
return new_entry->tx;
}
free(new_entry);
return NULL;
Expand Down Expand Up @@ -51,7 +51,7 @@ Transaction *mempool_find(unsigned char *tx_hash) {

found_entry = mempool_find_node(tx_hash);
if (found_entry != NULL) {
return found_entry->tx;
return copy_tx(found_entry->tx);
}

return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/core/txs/base_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void hash_tx(unsigned char *dest, Transaction *tx) {
tx_buf = malloc(size_ser_tx(tx));
ser_tx(tx_buf, tx);
hash_sha256(dest, tx_buf, tx_buf_size);
free(tx_buf);
}

void free_tx(Transaction *tx){
Expand All @@ -40,7 +41,6 @@ Transaction *copy_tx(Transaction *tx){
return NULL;

// Copy inputs
// Harder since we need to also copy pub keys
copy = malloc(sizeof(Transaction));
copy->num_inputs = tx->num_inputs;
copy->inputs = malloc(tx->num_inputs * sizeof(Input));
Expand Down
3 changes: 2 additions & 1 deletion src/core/txs/ser_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ ssize_t ser_input(unsigned char *dest, Input *input) {
memcpy(sig_len, &(input->sig_len), sizeof(input->sig_len));

unsigned char *sig = sig_len + sizeof(input->sig_len);
memcpy(sig, input->signature, SIGNATURE_LEN);
memset(sig, 0, SIGNATURE_LEN);
memcpy(sig, input->signature, input->sig_len);

unsigned char *prev_tx = sig + SIGNATURE_LEN;
memcpy(prev_tx, input->prev_tx_id, TX_HASH_LEN);
Expand Down
10 changes: 9 additions & 1 deletion src/core/txs/wallet_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ int wallet_pool_count(unsigned int *num_entries){
return db_count(wallet_pool_db, num_entries);
}

void free_wallet_entry(WalletEntry *entry) {
if (entry == NULL)
return;
if (entry->key_pair != NULL)
mbedtls_ecp_keypair_free(entry->key_pair);
free(entry);
}

/* KEY POOL FUNCS */
int key_pool_add_leveldb(mbedtls_ecp_keypair *key_pair) {
unsigned char db_key[PUB_KEY_HASH_LEN];
Expand Down Expand Up @@ -161,7 +169,7 @@ int key_pool_find_leveldb(mbedtls_ecdsa_context **keypair, unsigned char *public
if(read == NULL){
return 1;
}

size_t read_bytes;
*keypair = deser_keypair_alloc(&read_bytes, (unsigned char*) read);
free(read);
Expand Down
7 changes: 7 additions & 0 deletions src/includes/blocks/base_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ typedef struct Block{
*/
void hash_blockheader(unsigned char *dest, BlockHeader *header);

/**
* @brief Frees a block
*
* @param block Block to free
*/
void free_block(Block *block);

/*
Prints a Block Header to stdout so data can be visualized

Expand Down
13 changes: 7 additions & 6 deletions src/includes/blocks/ser_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* Blockheaders
******************************************************************************/

/**
* @brief Gets size of serialized BlockHeader
*
* @return Size of serialized BlockHeader
*/
size_t size_ser_blockheader();
#define BLOCKHEADER_SER_LEN \
( \
sizeof(((BlockHeader*)0)->timestamp) + \
sizeof(((BlockHeader*)0)->all_tx) + \
sizeof(((BlockHeader*)0)->prev_header_hash) + \
sizeof(((BlockHeader*)0)->nonce) \
)

/**
* @brief Serialize a BlockHeader
Expand Down
7 changes: 7 additions & 0 deletions src/includes/txs/wallet_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ int wallet_pool_remove_leveldb(unsigned char *tx_hash, unsigned int vout);
*/
int wallet_pool_count(unsigned int *num_entries);

/**
* @brief Frees a wallet entry
*
* @param entry Wallet entry to free
*/
void free_wallet_entry(WalletEntry *entry);

/**
* @brief Add a keypair to the keypool
*
Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tests/")
message(STATUS "Test Output set to: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
add_subdirectory(includes)
add_subdirectory(core)


Expand Down
1 change: 1 addition & 0 deletions src/tests/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(blocks)
add_subdirectory(global)
add_subdirectory(txs)
add_subdirectory(utils)
add_subdirectory(fixtures)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you handled the cmake stuff very cleanly here

7 changes: 5 additions & 2 deletions src/tests/core/blocks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ target_link_libraries(test_handle_block
# Test ser block
add_executable(test_ser_block test_ser_block.c)
target_compile_options(test_ser_block PRIVATE ${COMPILE_OPTIONS_STD})
target_include_directories(test_ser_block PUBLIC ${INCLUDE_ALL})
target_include_directories(test_ser_block
PUBLIC ${INCLUDE_ALL}
PUBLIC ${INCLUDE_FIXTURES})
target_link_libraries(test_ser_block
core_mine)
core_mine
fixtures)

add_test(test_create_block ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_create_block)
add_test(test_validate_block ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_validate_block)
Expand Down
Loading