Toychain!
A toy blockchain in C
|
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>
Functions | |
bool | write_block (const char *filename, const Block *b) |
print the content of a Block struct in a file More... | |
Block * | read_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... | |
uint8 * | hash_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... | |
uint8 * | copy_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... | |
Block * | init_block_raw (Key *auth, CellProtected *votes, uint8 *prev_hash) |
init a block object. Takes ownership of all auth, votes and prev_hash More... | |
Block * | init_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... | |
CellTree * | create_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... | |
CellTree * | highest_child (const CellTree *cell) |
returns the child with max height of cell More... | |
CellTree * | last_node (CellTree *tree) |
Question 8.7 descpription is false. ~Returns the last node of a tree. More... | |
CellProtected * | get_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... | |
CellTree * | read_tree () |
Key * | compute_winner_BT (CellTree *tree, CellKey *candidates, CellKey *voters, size_t sizeC, size_t sizeV) |
File to create and handle blockchain structures.
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.
d | |
name |
char * block_head_to_str | ( | const Block * | b | ) |
convert block head to string and leave extra space for nonce to be appended
b |
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.
headstr | |
b |
char * block_to_str | ( | const Block * | b | ) |
convert block to string
b |
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
hash | |
d |
checks if A and B are the same hash
A | |
B |
void compute_proof_of_work | ( | Block * | B, |
int | d | ||
) |
compute proof of work of block B with d zeros in hexadecimal representation
B | |
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.
tree | |
author | |
d | number of zeros for proof of work |
Create a node object. Takes ownership of Block b!
b | Block to initialize the node with |
void delete_block | ( | Block * | b | ) |
cette fonction ne free pas le champ author ni le contenu des votes
b | block to be deleted |
void delete_node | ( | CellTree * | node | ) |
Delete a node. See question 8.5 for more information.
node |
void delete_tree | ( | CellTree * | node | ) |
fonction de l'enonce
node |
void free_block | ( | Block * | b | ) |
frees block (ce n'est pas delete_block)
b |
void free_node | ( | CellTree * | node | ) |
free everything the node contains
node |
void free_tree | ( | CellTree * | node | ) |
fonction qui free la totalite de memoire dans l'arbre
node |
CellProtected * get_trusted_declarations | ( | const CellTree * | tree | ) |
Get all the trusted declarations from the longest chain.
tree |
uint8 * hash_string | ( | const char * | s | ) |
hashes string s using sha256
s |
char * hash_to_str | ( | const uint8 * | hash | ) |
convert sha256 hash to string representation
hash |
returns the child with max height of cell
cell |
Block * init_block | ( | const Key * | auth, |
const CellProtected * | votes, | ||
const uint8 * | prev_hash | ||
) |
init block object, copies all parameters.
auth | |
votes | |
prev_hash |
Block * init_block_raw | ( | Key * | auth, |
CellProtected * | votes, | ||
uint8 * | prev_hash | ||
) |
init a block object. Takes ownership of all auth, votes and prev_hash
auth | |
votes | |
prev_hash |
Question 8.7 descpription is false. ~Returns the last node of a tree.
tree |
void print_sha256_hash | ( | const uint8 * | hash | ) |
print sha256 hash
hash |
void print_tree | ( | CellTree * | tr | ) |
print a tree.
tr |
void print_tree_r | ( | CellTree * | tr, |
int | indent | ||
) |
print a tree recursively.
tr | |
indent |
Block * read_block | ( | const char * | filename | ) |
read a file and save it's content in a Block object
filename |
bool str_to_hash | ( | const char * | str, |
uint8 ** | RET | ||
) |
convert string sha256 hash representation to sha256 hash
str | |
uint8* | return value (can be NULL) |
void submit_vote | ( | Protected * | p | ) |
print a Protected object (declaration) to the Pending_vote file.
p | delcaration to print |
Update height of a node.
father | |
child |
bool verify_block | ( | const Block * | B, |
int | d | ||
) |
verifies if the block is valid with d zeros in hexadecimal representation
d |
bool write_block | ( | const char * | filename, |
const Block * | b | ||
) |
print the content of a Block struct in a file
filename | name of the file |
b | object to print |