Skip to content

Commit

Permalink
Added ability to save blockchain into file
Browse files Browse the repository at this point in the history
  • Loading branch information
dgski committed Feb 2, 2018
1 parent d511fea commit 00a4ee2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
29 changes: 28 additions & 1 deletion blockchain.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ bool valid_proof(char* last_hash, char* trans_hash, long proof) {
hash256(hash_value,guess);

if(1)
return (hash_value[0] == '0' && hash_value[1] == '0' && hash_value[2] == '0' /*&& (hash_value[3] > 60 && hash_value[3] < 127)*/);
return (hash_value[0] == '0' && hash_value[1] == '0' /* && hash_value[2] == '0' && (hash_value[3] > 60 && hash_value[3] < 127)*/);
else
return (hash_value[0] == '0' && hash_value[1] == '0' && hash_value[2] == '0' && (hash_value[3] > 60 && hash_value[3] < 127));

Expand Down Expand Up @@ -1011,6 +1011,33 @@ int strip_pub_key(char* output, char* pub_key) {
}



int save_chain_to_file(blockchain* in_chain, char* file_name) {

printf("Saving our chain to file: '%s'\n", file_name);
FILE* chain_file = fopen(file_name, "w"); //blockchain_file name
if(chain_file == NULL) return 0;

blink* temp = in_chain->head;

while(temp != NULL) {

char block_to_write[BLOCK_STR_SIZE];
string_block(block_to_write,&temp->data);
strcat(block_to_write,"\n");
fwrite(block_to_write,1,strlen(block_to_write), chain_file);

temp = temp->next;
}

fclose(chain_file);

return 1;

}



//Block-link FUNCTIONS:
blink* blink_create()
{
Expand Down
2 changes: 1 addition & 1 deletion blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ typedef struct alt_chain {
//Chain functions
blockchain* new_chain();
int discard_chain(blockchain* in_chain);
int save_chain_to_file(blockchain* in_chain, char* file_name);
void new_transaction(blockchain* in_chain, char* in_sender, char* in_recipient, int in_amount,char* in_signature);
void new_post(blockchain* in_chain, char* in_sender, char in_data, char* in_signature);
blink* append_current_block(blockchain* in_chain, long in_proof);
Expand All @@ -107,7 +108,6 @@ int extract_transactions_raw(transaction* trans_array, char* in_trans);

int extract_posts_raw(post* post_array, char* input_posts_string);
int validate_posts(blockchain* in_chain, post* new_post_array, int nr_of_posts);
int verify_post_format(const char* post);



Expand Down
5 changes: 5 additions & 0 deletions chain_0.noins
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0000000000.0000000000.0.0..00.00000000000000000100.
0000000001.1517614974.0.0.MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:0000000002:7b247f88e90b96d22e71b7fb95886a449759301a1cc46ea32a86821862f22337f07a4e098b4c7a370ca70b2c2fcd402d6b02f0680e208ef44b506a489a0039227cf50119265f5da9c853bf735faaba42a13b11fb16f9ea40509e7458d7790d4318b594aed602cca7e24a305a8afa9ceb5d697a45eecdda5ace1dd1ba778952caa8028e701198bd00f8ff68bc24079ee8412158c263b203cc6ae84c7ce456bdcd3ace02913dc4fe3cf176cce8191a13165124e2b633f7035ddf2c4f86fb821cf7c3d5bb66c20757c90535d931c6cda5290c8d926cddf5c932710fd09009579af560e76de7df2f105f1581219b6e72aedcece375c6220dea99c2c9c3bc3c50c4fa.01.00000000000000008826.02153e70947dde7ad84fb844d13cbb6ae349cd5ae8cb1d847704a5e52366462d
0000000002.1517614976.0.0.MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:0000000002:7b247f88e90b96d22e71b7fb95886a449759301a1cc46ea32a86821862f22337f07a4e098b4c7a370ca70b2c2fcd402d6b02f0680e208ef44b506a489a0039227cf50119265f5da9c853bf735faaba42a13b11fb16f9ea40509e7458d7790d4318b594aed602cca7e24a305a8afa9ceb5d697a45eecdda5ace1dd1ba778952caa8028e701198bd00f8ff68bc24079ee8412158c263b203cc6ae84c7ce456bdcd3ace02913dc4fe3cf176cce8191a13165124e2b633f7035ddf2c4f86fb821cf7c3d5bb66c20757c90535d931c6cda5290c8d926cddf5c932710fd09009579af560e76de7df2f105f1581219b6e72aedcece375c6220dea99c2c9c3bc3c50c4fa.01.00000000000000008826.02153e70947dde7ad84fb844d13cbb6ae349cd5ae8cb1d847704a5e52366462d
0000000003.1517614978.0.0.MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:0000000002:7b247f88e90b96d22e71b7fb95886a449759301a1cc46ea32a86821862f22337f07a4e098b4c7a370ca70b2c2fcd402d6b02f0680e208ef44b506a489a0039227cf50119265f5da9c853bf735faaba42a13b11fb16f9ea40509e7458d7790d4318b594aed602cca7e24a305a8afa9ceb5d697a45eecdda5ace1dd1ba778952caa8028e701198bd00f8ff68bc24079ee8412158c263b203cc6ae84c7ce456bdcd3ace02913dc4fe3cf176cce8191a13165124e2b633f7035ddf2c4f86fb821cf7c3d5bb66c20757c90535d931c6cda5290c8d926cddf5c932710fd09009579af560e76de7df2f105f1581219b6e72aedcece375c6220dea99c2c9c3bc3c50c4fa.01.00000000000000008826.02153e70947dde7ad84fb844d13cbb6ae349cd5ae8cb1d847704a5e52366462d
0000000004.1517614980.0.0.MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:MIIBCgKCAQEAw2rs/S9hdaxRqoJITwpzDqHaB3u7T8fjCw24y7X6wM+/xAuQC6wnDXMnsAw4ofkjXb5rw4osmyjFIT/iJjvXe5+5aKUk4S5rX/wMe5blq6eBU00pkEullYMjdXjV3sOGPzsBq2a5pqAZi07ozbzKNHQbqz5QuoYqFpTkRqNJvLCv8Hqb/UWdJPpdZsHEO+zL3VkBzOIaLQ8UA6AFYgR1S3hFKd+8lZoimKor/aTLgKpnwfZKQazDU9nkk/O/F43tW+ckXWKhevZuJ64y48H1kx994VzTRHKgZcEwweP2TQ7pfoqdJlBQJ6aZEjj48l8+bhUVLMCdRMj1Uq+ZqvMABQIDAP//:0000000002:7b247f88e90b96d22e71b7fb95886a449759301a1cc46ea32a86821862f22337f07a4e098b4c7a370ca70b2c2fcd402d6b02f0680e208ef44b506a489a0039227cf50119265f5da9c853bf735faaba42a13b11fb16f9ea40509e7458d7790d4318b594aed602cca7e24a305a8afa9ceb5d697a45eecdda5ace1dd1ba778952caa8028e701198bd00f8ff68bc24079ee8412158c263b203cc6ae84c7ce456bdcd3ace02913dc4fe3cf176cce8191a13165124e2b633f7035ddf2c4f86fb821cf7c3d5bb66c20757c90535d931c6cda5290c8d926cddf5c932710fd09009579af560e76de7df2f105f1581219b6e72aedcece375c6220dea99c2c9c3bc3c50c4fa.01.00000000000000008826.02153e70947dde7ad84fb844d13cbb6ae349cd5ae8cb1d847704a5e52366462d
Binary file modified node
Binary file not shown.
7 changes: 6 additions & 1 deletion node.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "data_containers/linked_list.h"
#include "node.h"

#define DEBUG 0
#define DEBUG 1
#define MESSAGE_LENGTH 100000
#define SHORT_MESSAGE_LENGTH 300

Expand All @@ -31,6 +31,7 @@ char* pri_key;

char pri_file[300];
char pub_file[300];
char our_chain_file[300];

//Blockchains
blockchain* our_chain;
Expand Down Expand Up @@ -844,6 +845,9 @@ void graceful_shutdown(int dummy) {
li_discard(outbound_msg_queue);
li_discard(inbound_msg_queue);

//Save blockchain to file
save_chain_to_file(our_chain, our_chain_file);

//Discard blockchains
discard_chain(our_chain);
dict_foreach(foreign_chains, destroy_chains_in_dict, NULL);
Expand Down Expand Up @@ -977,6 +981,7 @@ int main(int argc, char* argv[]) {
strcpy(node_list_file, "node.cfg");
strcpy(pri_file, "pri_0.pem");
strcpy(pub_file, "pub_0.pem");
strcpy(our_chain_file, "chain_0.noins");

//Create foreign chain dict
foreign_chains = dict_create();
Expand Down
Binary file modified node.dSYM/Contents/Resources/DWARF/node
Binary file not shown.

0 comments on commit 00a4ee2

Please sign in to comment.