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: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set(INCLUDE_TX "" CACHE INTERNAL "used internally")
set(INCLUDE_BLOCK "" CACHE INTERNAL "used internally")
set(INCLUDE_GLOBAL "" CACHE INTERNAL "used internally")
set(INCLUDE_TEST "" CACHE INTERNAL "used internally")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

set(UTHASH_IN_PROJ "src/includes/uthash/")
set(INCLUDE_ALL
Expand Down
13 changes: 13 additions & 0 deletions src/core/blocks/base_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>

#include "base_block.h"
#include "base_tx.h"
#include "ser_block.h"
#include "crypto.h"

Expand Down Expand Up @@ -56,3 +57,15 @@ void pretty_print_block(Block *block, char *prefix){
printf(LINE_BREAK);
}
}

void free_block(Block *block){
if(block != NULL){
if(block->txs != NULL){
for(unsigned int i = 0; i < block->num_txs; i++ ){
free_tx(block->txs[i]);
}
free(block->txs);
}
free(block);
}
}
3 changes: 3 additions & 0 deletions src/core/globals/init_globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include "utxo_pool.h"
#include "mempool.h"
#include "init_db.h"
#include "wallet_pool.h"

void node_init(char *db_env) {
// Read from file for chain height and top block hash
blockchain_init_leveldb(db_env);
utxo_pool_init_leveldb(db_env);
wallet_init_leveldb(db_env);
mempool_init();
}

void miner_init() {
Expand Down
3 changes: 2 additions & 1 deletion src/core/txs/handle_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include "utxo_to_tx.h"

int handle_tx(Transaction *tx){
if(mempool_add(tx) == NULL){
Transaction *coppied_tx = copy_tx(tx);
if(mempool_add(coppied_tx) == NULL){
return 1;
}
return 0;
Expand Down
8 changes: 7 additions & 1 deletion src/core/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ target_link_libraries(crypto
add_library(init_db STATIC init_db.c)
target_include_directories(init_db PUBLIC ${INCLUDE_ALL})
target_link_libraries(init_db
leveldb)
leveldb)

add_library(queue STATIC queue.c)
target_include_directories(queue PUBLIC ${INCLUDE_ALL})
target_link_libraries(queue
core_tx
core_block)
73 changes: 73 additions & 0 deletions src/core/utils/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include "pthread.h"
#include "queue.h"
#include "base_tx.h"
#include "base_block.h"
#include <unistd.h>

Queue *queue_init(){
Queue *new_q = malloc(sizeof(Queue));
new_q->head = NULL;
new_q->tail = NULL;
//INitialize sepmaphores and lock
sem_init(&new_q->sem_len, 0, 0);
if (pthread_mutex_init(&new_q->lock, NULL) != 0) {
printf("\n mutex init has failed\n");
return NULL;
}
//pthread_mutex_unlock(&new_q->lock);
return new_q;
}

void queue_destroy(Queue *existing_q){
pthread_mutex_lock(&existing_q->lock); // Maybe we don't need to lock if we are destroying?
pthread_mutex_destroy(&existing_q->lock);
QueueItem *cur_item = existing_q->head;
QueueItem *next_item = NULL;
while(cur_item != NULL){
next_item = cur_item->next;
free(cur_item);
cur_item = next_item;
}
free(existing_q);
}

int queue_add_void(Queue *existing_q, void *new_item){
pthread_mutex_lock(&existing_q->lock);
QueueItem *new_entry = malloc(sizeof(QueueItem));
new_entry->item = new_item;
new_entry->next = NULL;
if(!existing_q->tail && !existing_q->head){
existing_q->tail = new_entry;
existing_q->head = new_entry;
}
else{
existing_q->tail->next = new_entry;
existing_q->tail = new_entry;
}
pthread_mutex_unlock(&existing_q->lock);
int ret = sem_post(&existing_q->sem_len);
return 0;
}

void *queue_pop_void(Queue *existing_q){
sem_wait(&existing_q->sem_len);
pthread_mutex_lock(&existing_q->lock);
if(!existing_q->head){
fprintf(stderr, "Popping off empty Queue");
exit(1);
}

QueueItem *popped_item = existing_q->head;
existing_q->head = existing_q->head->next;
// Check to see if tail should be set to Null
if(!existing_q->head){
existing_q->tail = NULL;
}
void *ret = popped_item->item;
free(popped_item);
pthread_mutex_unlock(&existing_q->lock);
return ret;
}
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 and it's containing transactions safely
*
* @param block to free
*/
void free_block(Block *block);

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

Expand Down
2 changes: 1 addition & 1 deletion src/includes/globals/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define ERR_BUF 1024
#define BLOCK_REWARD 100
#define DESIRED_NUM_TX 10
#define HASH_DIFFICULTY 2
#define HASH_DIFFICULTY 3
#define MINING_NODE 1
#define WALLET_NODE 1

Expand Down
26 changes: 26 additions & 0 deletions src/includes/utils/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "base_tx.h"
#include "base_block.h"

typedef struct QueueItem{
void *item;
struct QueueItem *next;
} QueueItem;

typedef struct Queue {
QueueItem *head;
QueueItem *tail;
pthread_mutex_t lock;
sem_t sem_len;
} Queue;

Queue *queue_init();

void queue_destroy(Queue *exisiting_q);

int queue_add_void(Queue *existing_q, void *new_item);
void *queue_pop_void(Queue *existing_q);
14 changes: 13 additions & 1 deletion src/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ message(STATUS "Building Runtime")
# Add inlcudes for the runtime headers
add_subdirectory(includes)

add_executable(shell shell.c)
add_library(shell shell.c)
target_compile_options(shell PRIVATE ${COMPILE_OPTIONS_STD})
target_include_directories(shell PUBLIC
${INCLUDE_ALL}
Expand All @@ -14,3 +14,15 @@ target_link_libraries(shell
wallet
handle_block
init_globals)

add_executable(runtime runtime.c)
target_compile_options(runtime PRIVATE ${COMPILE_OPTIONS_STD})
target_include_directories(runtime PUBLIC ${INCLUDE_ALL})
target_link_libraries(runtime
queue
handle_block
Threads::Threads
init_globals
validate_tx
handle_tx
shell)
17 changes: 17 additions & 0 deletions src/runtime/includes/runtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "queue.h"
#include "pthread.h"
#include "semaphore.h"

typedef struct Globals {
Queue *queue_block;
Queue *queue_tx;
pthread_mutex_t utxo_pool_lock;
pthread_mutex_t utxo_to_tx_lock;
pthread_mutex_t blockchain_lock;
pthread_mutex_t wallet_pool_lock;
pthread_mutex_t key_pool_lock;
pthread_mutex_t mempool_lock;
int *miner_update;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to malloc this int?

pthread_mutex_t miner_update_lock;
} Globals;
19 changes: 10 additions & 9 deletions src/runtime/includes/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#pragma once

#include <uthash.h>
#include "runtime.h"

#define COMMAND_NAME_LEN 128

typedef struct {
char *name;
int (*func)(char **);
int (*func)(Globals *, char **);
size_t num_args;
char *help;
} Command;
Expand All @@ -37,17 +38,17 @@ void str_to_buf(
size_t dest_len, size_t src_len
);

int shell_mine(char **args);
int shell_build_tx(char **args);
int shell_print_chain(char **args);
int shell_print_block(char **args);
int shell_exit(char **args);
int shell_help(char **args);
int shell_mine(Globals *globals, char **args);
int shell_build_tx(Globals *globals, char **args);
int shell_print_chain(Globals *globals, char **args);
int shell_print_block(Globals *globals, char **args);
int shell_exit(Globals *globals, char **args);
int shell_help(Globals *globals, char **args);

char *shell_read_line();
char **shell_tokenize(char *line);
int shell_execute(size_t num_args, char **args);
void shell_loop();
int shell_execute(Globals *globals, size_t num_args, char **args);
void shell_loop(Globals *globals);

void shell_init_globals();
void shell_init_commands();
Expand Down
Loading