Skip to content

Commit

Permalink
Highly improved memory safety... still WIP.... reorganization and ref…
Browse files Browse the repository at this point in the history
…actoring as well
  • Loading branch information
dgski committed Jan 24, 2018
1 parent a53a4b8 commit 45186c3
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/node",
//"args": ["ipc:///tmp/pipeline_11.ipc"],
"args": ["ipc:///tmp/pipeline_11.ipc"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"__string": "c",
"dict.h": "c",
"pem.h": "c",
"rsa.h": "c"
"rsa.h": "c",
"__bit_reference": "c"
}
}
90 changes: 75 additions & 15 deletions blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,31 +353,78 @@ int extract_transactions_raw(transaction* trans_array, char* input_trans_string)
return 1;
}

/*
int validate_and_insert_trans(blockchain* in_chain, transaction* trans_array) {
int extract_transactions(blockchain* in_chain,transaction* trans_array, char* in_trans) {
/*
printf("\n\n\n\n\n\n");
printf("%s\n", in_trans);
printf("\n\n\n\n\n\n");
*/
for(int i = 0; trans_array[i] <20; i++) {
char output[2500] = {0};
string_trans_nosig(output,trans_array->sender,trans_array->recipient,atoi(trans_array->amount));
printf("VERIFYING TRANSACTION:\n");
if(!verify_signiture(output,sender,reciever,amount,signature))
return 0;
//Transaction is properly signed... now what? Update Quickledger.
//Addresses are different - cuurency generation
if(strcmp(sender, reciever)) {
void* sender_funds = dict_access(in_chain->quickledger, sender);
int sender_future_balance = 0;
if(sender_funds != NULL)
sender_future_balance = *((int*)sender_funds) - atoi(amount);
dict_insert(in_chain->quickledger, sender, &sender_future_balance, sizeof(sender_funds));
}
//Addresses are the same, Currency cap already met, and they are trying to give themselves more
if(!strcmp(sender, reciever) && in_chain->total_currency >= CURRENCY_CAP && atoi(amount) != 0) {
//return 0;
return 1; //temp
}
//Addresses are the same, Trying to givethemselves more than 2
if(!strcmp(sender, reciever) && (in_chain->total_currency < CURRENCY_CAP) && (atoi(amount) != CURRENCY_SPEED) ) {
//return 0;
return 1; //temp
}
in_chain->total_currency += CURRENCY_SPEED;
void* recipient_funds = dict_access(in_chain->quickledger, reciever);
int recipient_future_balance = 0;
if(recipient_funds != NULL)
recipient_future_balance = *((int*)recipient_funds);
recipient_future_balance += atoi(amount);
dict_insert(in_chain->quickledger, reciever, &recipient_future_balance, sizeof(recipient_future_balance));
}
memcpy(in_chain->trans_list,trans_array,sizeof(transaction) * 20);
return 1;
}*/


int extract_transactions(blockchain* in_chain,transaction* trans_array, const char* in_trans) {

char the_trans_chars[30000];
strcpy(the_trans_chars,in_trans);


char* trans_strings[20] = {0};

char* pointer = strtok(in_trans,"-");
char* pointer = strtok(the_trans_chars,"-");
trans_strings[0] = pointer;
int i = 1;
while(pointer != NULL) {
pointer = strtok(NULL,"-");
trans_strings[i++] = pointer;
}
/*
for(int i = 0; trans_strings[i] != 0; i++) {
printf("\n\n\n\n\n\n");
printf("TRANSACTION: %s\n", trans_strings[i]);
printf("\n\n\n\n\n\n");
}*/

char* sender;
char* reciever;
Expand Down Expand Up @@ -505,9 +552,12 @@ long proof_of_work(int* beaten, char* last_hash, char* trans_hash) {
//printf("%d\n", proof);
proof += 1;

if(*beaten) {
if(*beaten == 1) {
return -1;
}
if(*beaten == 2) {
return -2;
}
if(strcmp(old_trans_hash, trans_hash)) {
printf("Hash Changed. Resetting Nonce.\n");
proof = 0;
Expand Down Expand Up @@ -550,10 +600,15 @@ int create_keys(RSA** your_keys, char** pri_key, char** pub_key) {
BIO_read(pri,*pri_key,pri_len);
BIO_read(pub, *pub_key,pub_len);


//Terminate strings
(*pri_key)[pri_len] = '\0';
(*pub_key)[pub_len] = '\0';

//Free memory
BIO_vfree(pri);
BIO_vfree(pub);


return 1;

Expand Down Expand Up @@ -680,6 +735,11 @@ bool verify_signiture(const char* input, char* sender, char* recipient, char* am
RSA *rsa_pub = PEM_read_bio_RSAPublicKey(bio, NULL, NULL, NULL);

int rc = RSA_verify(NID_sha256, hash_value,32,sig,256,rsa_pub);

BIO_free_all(bio);
RSA_free(rsa_pub);


//printf("VERIFY RETURN: %d\n", rc);
if(rc != 1) printf("Invalid.\n"); else printf("Valid.\n");

Expand Down
3 changes: 2 additions & 1 deletion blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define BLOCK_DATA_SIZE 8
#define BLOCK_BUFFER_SIZE 5000
#define TRANS_LIST_SIZE 20
#define TRANS_STRING_LENGTH 2000
#define HASH_SIZE 32
#define HASH_HEX_SIZE 65
#define PUBLIC_ADDRESS_SIZE 500
Expand Down Expand Up @@ -90,7 +91,7 @@ char* posts, unsigned int trans_list_length, long proof);
void print_block(blink* in_block, char separator);
char* string_block(char* output, block* in_block);
char* hash_block(char* input, block* in_block);
int extract_transactions(blockchain* in_chain,transaction* trans_array, char* in_trans);
int extract_transactions(blockchain* in_chain,transaction* trans_array, const char* in_trans);
int extract_transactions_raw(transaction* trans_array, char* in_trans);

//Transaction functions
Expand Down
2 changes: 1 addition & 1 deletion data_containers/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bt_node* bt_node_create(char* in_key, void* in_data, size_t in_size) {
if(in_key == NULL || in_data == NULL) return NULL;

bt_node* temp = malloc(sizeof(bt_node));
temp->key = malloc(strlen(in_key));
temp->key = malloc(strlen(in_key) +1);
strcpy(temp->key, in_key);
temp->data = malloc(in_size);
memcpy(temp->data, in_data, in_size);
Expand Down
9 changes: 5 additions & 4 deletions data_containers/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,13 @@ void li_foreach(list* in_list, void* (*func)(list* in_list, li_node* input, void

li_node* temp = in_list->head;
while(temp != NULL) {

li_node* next = temp->next;

int to_delete = (int)(*func)(in_list, temp, data);
/*
if(to_delete)
li_delete_node(in_list,temp);*/

temp = temp->next;
if(next == NULL) return;
temp = next;
}
return;
}
Binary file modified node
Binary file not shown.
Loading

0 comments on commit 45186c3

Please sign in to comment.