src/blockchain.c
bool bchain_is_valid(const Blockchain *bchain)
{
    /* recalculate and check fist block */
    Block *curr_b = bchain->blocks[0]; /* first block */
    uint8_t recalculated_hash[SHA256_BLOCK_SIZE] = {0};
    block_hash_calc(recalculated_hash, curr_b);
    int cmp = memcmp(recalculated_hash, curr_b->hash, SHA256_BLOCK_SIZE);
    if (cmp != 0)
        return false;

    /* recalculate compare with previous and check blocks */
    for (uint16_t i = 1; i < bchain->size; i++)
    {
        Block *prev_b = bchain->blocks[i - 1]; /* previous block */
        Block *curr_b = bchain->blocks[i];     /* current block */

        /* check combination */
        cmp = memcmp(prev_b->hash, curr_b->prev_hash, SHA256_BLOCK_SIZE);
        if (cmp != 0)
            return false;

        /* check if calculation is correct */
        uint8_t recalculated_hash[SHA256_BLOCK_SIZE] = {0};
        block_hash_calc(recalculated_hash, curr_b);
        cmp = memcmp(recalculated_hash, curr_b->hash, SHA256_BLOCK_SIZE);
        if (cmp != 0)
            return false;
    }

    return true;
}
/* recalculate and test if at least one hash combination differ */
recalculate and test if at least one hash combination differ
recalculate and test if at least one hash combination differ
bool bchain_is_valid(const Blockchain *bchain)
{
    Block *curr_b = bchain->blocks[0]; 
    uint8_t recalculated_hash[SHA256_BLOCK_SIZE] = {0};
    block_hash_calc(recalculated_hash, curr_b);
    int cmp = memcmp(recalculated_hash, curr_b->hash, SHA256_BLOCK_SIZE);
    if (cmp != 0)
        return false;
    for (uint16_t i = 1; i < bchain->size; i++)
    {
        Block *prev_b = bchain->blocks[i - 1]; 
        Block *curr_b = bchain->blocks[i];     
        cmp = memcmp(prev_b->hash, curr_b->prev_hash, SHA256_BLOCK_SIZE);
        if (cmp != 0)
            return false;
        uint8_t recalculated_hash[SHA256_BLOCK_SIZE] = {0};
        block_hash_calc(recalculated_hash, curr_b);
        cmp = memcmp(recalculated_hash, curr_b->hash, SHA256_BLOCK_SIZE);
        if (cmp != 0)
            return false;
    }
    return true;
}
bool bchain_is_valid ( const Blockchain * bchain ) { Block * curr_b = bchain -> blocks [ 0 ] ; uint8_t recalculated_hash [ SHA256_BLOCK_SIZE ] = { 0 } ; block_hash_calc ( recalculated_hash , curr_b ) ; int cmp = memcmp ( recalculated_hash , curr_b -> hash , SHA256_BLOCK_SIZE ) ; if ( cmp != 0 ) return false ; for ( uint16_t i = 1 ; i < bchain -> size ; i ++ ) { Block * prev_b = bchain -> blocks [ i - 1 ] ; Block * curr_b = bchain -> blocks [ i ] ; cmp = memcmp ( prev_b -> hash , curr_b -> prev_hash , SHA256_BLOCK_SIZE ) ; if ( cmp != 0 ) return false ; uint8_t recalculated_hash [ SHA256_BLOCK_SIZE ] = { 0 } ; block_hash_calc ( recalculated_hash , curr_b ) ; cmp = memcmp ( recalculated_hash , curr_b -> hash , SHA256_BLOCK_SIZE ) ; if ( cmp != 0 ) return false ; } return true ; }
recalculate and test if at least one hash combination differ
recalculate and test if at least one hash combination differ
/* recalculate and check fist block */ /* first block */ /* recalculate compare with previous and check blocks */ /* previous block */ /* current block */ /* check combination */ /* check if calculation is correct */