Toychain!
A toy blockchain in C
blockchain.h
Go to the documentation of this file.
1#ifndef _BLOCKCHAIN_H
2#define _BLOCKCHAIN_H
3
4#include <openssl/sha.h>
5#include "lib/types.h"
6#include "lib/rsa.h"
7#include "lib/dataio.h"
8
14#define BLOCK_HASH_SIZE SHA256_DIGEST_LENGTH
15
16
21typedef struct block {
22 Key* author;
23 CellProtected* votes;
24 uint8* hash;
25 uint8* previous_hash;
26 int nonce;
28
29bool write_block(const char* filename, const Block* b);
30Block* read_block(const char* filename);
31
32char* block_head_to_str(const Block* b);
33void block_nonce_to_str(char* headstr, const Block* b);
34char* block_to_str(const Block* b);
35
36uint8* hash_string(const char* s);
37void print_sha256_hash(const uint8* hash);
38bool compare_hash(const uint8* A, const uint8* B);
39uint8* copy_hash(const uint8* hash);
40
41bool str_to_hash(const char* str, uint8** ret);
42char* hash_to_str(const uint8* hash);
43
44void compute_proof_of_work(Block *B, int d);
45
46bool verify_block(const Block* B, int d);
47void free_block(Block* b);
48void delete_block(Block* b);
49
50Block* init_block_raw(Key* auth, CellProtected* votes, uint8* prev_hash);
51Block* init_block(const Key* auth, const CellProtected* votes, const uint8* prev_hash);
52
57typedef struct block_tree_cell {
58 Block* block;
59 struct block_tree_cell* father;
60 struct block_tree_cell* firstChild;
61 struct block_tree_cell* nextBro;
62 size_t height;
64
65void print_tree(CellTree* tr);
66
68bool update_height(CellTree* father, CellTree* child);
69void add_child(CellTree* father, CellTree* child);
70void delete_node(CellTree* node);
71void free_node(CellTree* node);
72void delete_tree(CellTree* node);
73void free_tree(CellTree* node);
74
75CellTree* highest_child(const CellTree* cell);
77
79
80void submit_vote(Protected* p);
81void create_block(CellTree** tree, Key* author, int d);
82void add_block(int d, const char* name);
83
84CellTree* read_tree();
85Key* compute_winner_BT(CellTree* tree, CellKey* candidates, CellKey* voters, size_t sizeC, size_t sizeV);
86
87
88#endif
Block * init_block(const Key *auth, const CellProtected *votes, const uint8 *prev_hash)
init block object, copies all parameters.
Definition: blockchain.c:474
CellTree * create_node(Block *b)
Create a node object. Takes ownership of Block b!
Definition: blockchain.c:515
char * block_head_to_str(const Block *b)
convert block head to string and leave extra space for nonce to be appended
Definition: blockchain.c:269
void delete_tree(CellTree *node)
fonction de l'enonce
Definition: blockchain.c:631
void compute_proof_of_work(Block *B, int d)
compute proof of work of block B with d zeros in hexadecimal representation
Definition: blockchain.c:367
bool update_height(CellTree *father, CellTree *child)
Update height of a node.
Definition: blockchain.c:540
char * hash_to_str(const uint8 *hash)
convert sha256 hash to string representation
Definition: blockchain.c:196
bool compare_hash(const uint8 *A, const uint8 *B)
checks if A and B are the same hash
Definition: blockchain.c:402
char * block_to_str(const Block *b)
convert block to string
Definition: blockchain.c:324
Block * read_block(const char *filename)
read a file and save it's content in a Block object
Definition: blockchain.c:72
CellTree * highest_child(const CellTree *cell)
returns the child with max height of cell
Definition: blockchain.c:660
void add_block(int d, const char *name)
Read the last created Block. If it is valid, add it to BLOCKCHAIN_DIR. In any case,...
Definition: blockchain.c:764
void free_node(CellTree *node)
free everything the node contains
Definition: blockchain.c:620
Block * init_block_raw(Key *auth, CellProtected *votes, uint8 *prev_hash)
init a block object. Takes ownership of all auth, votes and prev_hash
Definition: blockchain.c:452
void submit_vote(Protected *p)
print a Protected object (declaration) to the Pending_vote file.
Definition: blockchain.c:714
uint8 * hash_string(const char *s)
hashes string s using sha256
Definition: blockchain.c:336
uint8 * copy_hash(const uint8 *hash)
copy hash
Definition: blockchain.c:417
void create_block(CellTree **tree, Key *author, int d)
Create a Block object using PENDING_VOTES_FILE declarations. Write the block in PENDING_BLOCK_FILE on...
Definition: blockchain.c:739
void print_tree(CellTree *tr)
print a tree.
Definition: blockchain.c:600
bool write_block(const char *filename, const Block *b)
print the content of a Block struct in a file
Definition: blockchain.c:24
struct block_tree_cell CellTree
Tree structure for Block objects.
bool verify_block(const Block *B, int d)
verifies if the block is valid with d zeros in hexadecimal representation
Definition: blockchain.c:435
void free_tree(CellTree *node)
fonction qui free la totalite de memoire dans l'arbre
Definition: blockchain.c:645
void delete_block(Block *b)
cette fonction ne free pas le champ author ni le contenu des votes
Definition: blockchain.c:497
void free_block(Block *b)
frees block (ce n'est pas delete_block)
Definition: blockchain.c:483
struct block Block
Block structure to store declarations.
void print_sha256_hash(const uint8 *hash)
print sha256 hash
Definition: blockchain.c:387
void block_nonce_to_str(char *headstr, const Block *b)
append nonce to the block head string. If nonce is already appended, the function will delete the old...
Definition: blockchain.c:305
CellTree * last_node(CellTree *tree)
Question 8.7 descpription is false. ~Returns the last node of a tree.
Definition: blockchain.c:679
bool str_to_hash(const char *str, uint8 **ret)
convert string sha256 hash representation to sha256 hash
Definition: blockchain.c:232
CellProtected * get_trusted_declarations(const CellTree *tree)
Get all the trusted declarations from the longest chain.
Definition: blockchain.c:696
void delete_node(CellTree *node)
Delete a node. See question 8.5 for more information.
Definition: blockchain.c:609
header for dataio.c
header of rsa.c
Definition: rsa.h:16
Definition: sign.h:25
Tree structure for Block objects.
Definition: blockchain.h:57
Block structure to store declarations.
Definition: blockchain.h:21
Definition: dataio.h:20
Definition: dataio.h:28
a file with comfy aliases for long type signatures
#define uint8
alias for unsigned char
Definition: types.h:32