Toychain!
A toy blockchain in C
blockchain.c File Reference

File to create and handle blockchain structures. More...

#include "lib/blockchain.h"
#include "lib/dataio.h"
#include "lib/error.h"
#include "lib/vote.h"
#include <assert.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for blockchain.c:

Functions

bool write_block (const char *filename, const Block *b)
 print the content of a Block struct in a file More...
 
Blockread_block (const char *filename)
 read a file and save it's content in a Block object More...
 
char * hash_to_str (const uint8 *hash)
 convert sha256 hash to string representation More...
 
bool str_to_hash (const char *str, uint8 **RET)
 convert string sha256 hash representation to sha256 hash More...
 
char * block_head_to_str (const Block *b)
 convert block head to string and leave extra space for nonce to be appended More...
 
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 nonce and append the new one. More...
 
char * block_to_str (const Block *b)
 convert block to string More...
 
uint8hash_string (const char *s)
 hashes string s using sha256 More...
 
bool check_zeros (const uint8 *hash, size_t d)
 checks if the number of zeros in hash (as per the hexadecimal representation) is at least d More...
 
void compute_proof_of_work (Block *B, int d)
 compute proof of work of block B with d zeros in hexadecimal representation More...
 
void print_sha256_hash (const uint8 *hash)
 print sha256 hash More...
 
bool compare_hash (const uint8 *A, const uint8 *B)
 checks if A and B are the same hash More...
 
uint8copy_hash (const uint8 *hash)
 copy hash More...
 
bool verify_block (const Block *B, int d)
 verifies if the block is valid with d zeros in hexadecimal representation More...
 
Blockinit_block_raw (Key *auth, CellProtected *votes, uint8 *prev_hash)
 init a block object. Takes ownership of all auth, votes and prev_hash More...
 
Blockinit_block (const Key *auth, const CellProtected *votes, const uint8 *prev_hash)
 init block object, copies all parameters. More...
 
void free_block (Block *b)
 frees block (ce n'est pas delete_block) More...
 
void delete_block (Block *b)
 cette fonction ne free pas le champ author ni le contenu des votes More...
 
CellTreecreate_node (Block *b)
 Create a node object. Takes ownership of Block b! More...
 
bool update_height (CellTree *father, CellTree *child)
 Update height of a node. More...
 
void add_child (CellTree *father, CellTree *child)
 
void print_tree_r (CellTree *tr, int indent)
 print a tree recursively. More...
 
void print_tree (CellTree *tr)
 print a tree. More...
 
void delete_node (CellTree *node)
 Delete a node. See question 8.5 for more information. More...
 
void free_node (CellTree *node)
 free everything the node contains More...
 
void delete_tree (CellTree *node)
 fonction de l'enonce More...
 
void free_tree (CellTree *node)
 fonction qui free la totalite de memoire dans l'arbre More...
 
CellTreehighest_child (const CellTree *cell)
 returns the child with max height of cell More...
 
CellTreelast_node (CellTree *tree)
 Question 8.7 descpription is false. ~Returns the last node of a tree. More...
 
CellProtectedget_trusted_declarations (const CellTree *tree)
 Get all the trusted declarations from the longest chain. More...
 
void submit_vote (Protected *p)
 print a Protected object (declaration) to the Pending_vote file. More...
 
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 only if it is valid. The read declarations are removed from PENDING_VOTES_FILE. More...
 
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, PENDING_BLOCK_FILE is removed. More...
 
CellTreeread_tree ()
 
Keycompute_winner_BT (CellTree *tree, CellKey *candidates, CellKey *voters, size_t sizeC, size_t sizeV)
 

Detailed Description

File to create and handle blockchain structures.

Function Documentation

◆ add_block()

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, PENDING_BLOCK_FILE is removed.

Parameters
d
name

◆ block_head_to_str()

char * block_head_to_str ( const Block b)

convert block head to string and leave extra space for nonce to be appended

Parameters
b
Returns
char*

◆ block_nonce_to_str()

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 nonce and append the new one.

Parameters
headstr
b

◆ block_to_str()

char * block_to_str ( const Block b)

convert block to string

Parameters
b
Returns
char*

◆ check_zeros()

bool check_zeros ( const uint8 hash,
size_t  d 
)

checks if the number of zeros in hash (as per the hexadecimal representation) is at least d

Parameters
hash
d
Returns
true
false

◆ compare_hash()

bool compare_hash ( const uint8 A,
const uint8 B 
)

checks if A and B are the same hash

Parameters
A
B
Returns
true
false

◆ compute_proof_of_work()

void compute_proof_of_work ( Block B,
int  d 
)

compute proof of work of block B with d zeros in hexadecimal representation

Parameters
B
d

◆ copy_hash()

uint8 * copy_hash ( const uint8 hash)

copy hash

Parameters
hash
Returns
uint8*

◆ create_block()

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 only if it is valid. The read declarations are removed from PENDING_VOTES_FILE.

Parameters
tree
author
dnumber of zeros for proof of work

◆ create_node()

CellTree * create_node ( Block b)

Create a node object. Takes ownership of Block b!

Parameters
bBlock to initialize the node with
Returns
CellTree*

◆ delete_block()

void delete_block ( Block b)

cette fonction ne free pas le champ author ni le contenu des votes

Parameters
bblock to be deleted

◆ delete_node()

void delete_node ( CellTree node)

Delete a node. See question 8.5 for more information.

Parameters
node

◆ delete_tree()

void delete_tree ( CellTree node)

fonction de l'enonce

Parameters
node

◆ free_block()

void free_block ( Block b)

frees block (ce n'est pas delete_block)

Parameters
b

◆ free_node()

void free_node ( CellTree node)

free everything the node contains

Parameters
node

◆ free_tree()

void free_tree ( CellTree node)

fonction qui free la totalite de memoire dans l'arbre

Parameters
node

◆ get_trusted_declarations()

CellProtected * get_trusted_declarations ( const CellTree tree)

Get all the trusted declarations from the longest chain.

Parameters
tree
Returns
CellProtected*

◆ hash_string()

uint8 * hash_string ( const char *  s)

hashes string s using sha256

Parameters
s
Returns
uint8*

◆ hash_to_str()

char * hash_to_str ( const uint8 hash)

convert sha256 hash to string representation

Parameters
hash
Returns
char*

◆ highest_child()

CellTree * highest_child ( const CellTree cell)

returns the child with max height of cell

Parameters
cell
Returns
CellTree*

◆ init_block()

Block * init_block ( const Key auth,
const CellProtected votes,
const uint8 prev_hash 
)

init block object, copies all parameters.

Parameters
auth
votes
prev_hash
Returns
Block*

◆ init_block_raw()

Block * init_block_raw ( Key auth,
CellProtected votes,
uint8 prev_hash 
)

init a block object. Takes ownership of all auth, votes and prev_hash

Parameters
auth
votes
prev_hash
Returns
Block*

◆ last_node()

CellTree * last_node ( CellTree tree)

Question 8.7 descpription is false. ~Returns the last node of a tree.

Parameters
tree
Returns
CellTree*

◆ print_sha256_hash()

void print_sha256_hash ( const uint8 hash)

print sha256 hash

Parameters
hash

◆ print_tree()

void print_tree ( CellTree tr)

print a tree.

Parameters
tr

◆ print_tree_r()

void print_tree_r ( CellTree tr,
int  indent 
)

print a tree recursively.

Parameters
tr
indent

◆ read_block()

Block * read_block ( const char *  filename)

read a file and save it's content in a Block object

Parameters
filename
Returns
Block*

◆ str_to_hash()

bool str_to_hash ( const char *  str,
uint8 **  RET 
)

convert string sha256 hash representation to sha256 hash

Parameters
str
uint8*return value (can be NULL)
Returns
true : data read correctly
false : reading error

◆ submit_vote()

void submit_vote ( Protected p)

print a Protected object (declaration) to the Pending_vote file.

Parameters
pdelcaration to print

◆ update_height()

bool update_height ( CellTree father,
CellTree child 
)

Update height of a node.

Parameters
father
child
Returns
bool

◆ verify_block()

bool verify_block ( const Block B,
int  d 
)

verifies if the block is valid with d zeros in hexadecimal representation

Parameters
d
Returns
true
false

◆ write_block()

bool write_block ( const char *  filename,
const Block b 
)

print the content of a Block struct in a file

Parameters
filenamename of the file
bobject to print