-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.c
58 lines (46 loc) · 1.5 KB
/
blockchain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "block.h"
#include "linkedlist.h"
#define NVOTES 10
extern hash string_hash(void *string);
typedef enum party_code_t {GOOD_PARTY, MEDIOCRE_PARTY, EVIL_PARTY, MAX_PARTIES} party_code;
char *party_name[MAX_PARTIES] = {"GOOD PARTY", "MEDIOCRE_PARTY", "EVIL_PARTY"};
static party_code get_vote()
{
int r = rand();
return r%MAX_PARTIES;
}
void main(int argc, char const *argv[])
{
srand(time(NULL));
NODE *head;
DATA genesis_element;
init(&head);
// First block is created manually with hash = 0
transaction genesis_transactions = {party_name[get_vote()]};
block_t genesis_block = {0, string_hash(genesis_transactions), genesis_transactions};
genesis_element.info = genesis_block;
head = add(head, genesis_element);
// Now, we are going to submmit n random votes
int i, previous_hash = genesis_element.info.previous_block_hash;
transaction trans_list = (transaction) malloc(NVOTES * sizeof(char)*10);
for(i=0;i<NVOTES;i++)
{
DATA *el = malloc(sizeof(DATA));
block_t *b = malloc(sizeof(block_t));
transaction t = {party_name[get_vote()]};
strcat(trans_list, t);
b->previous_block_hash = previous_hash;
b->block_hash = string_hash(trans_list);
b->transactions = t;
el->info = *b;
previous_hash = b->block_hash;
head = add(head, *el);
}
print_list(head);
return;
}