Skip to content

Update blockchain_serialize.c #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions blockchain/v0.1/blockchain_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
* @arg: The file in which the data will be written
* Return: 0 upon success, or -1 upon failure
*/
int write_block(llist_node_t ptr, unsigned int idx, void *arg)
int block_to_file(llist_node_t ptr, unsigned int no, void *file)
{
FILE *fp;
block_t *block = ptr;
UNUSED(idx);
fp = (FILE *)arg;
fwrite((void *)&block->info, sizeof(block->info), 1, fp);
fwrite((void *)&block->data.len, sizeof(block->data.len), 1, fp);
fwrite(block->data.buffer, block->data.len, 1, fp);
fwrite(block->hash, sizeof(block->hash), 1, fp);
return (0);
FILE *fptr;
no = -1;
if (file)
{
fptr=(FILE*)file;
fwrite((void *)&block->info, sizeof(block->info), 1, fptr);
fwrite((void *)&block->data.len, sizeof(block->data.len), 1, fptr);
fwrite(block->data.buffer, block->data.len, 1, fptr);
fwrite(block->hash, sizeof(block->hash), 1, fptr);
return no;
}
return 0;
}

/**
* blockchain_serialize - a function that serializes a Blockchain into a file
* @blockchain: pointer to blockchain to be serialized
Expand All @@ -28,34 +31,32 @@ return (0);
* Return: 0 upon success, or -1 upon failure
*/

int blockchain_serialize(blockchain_t const *blockchain,
char const *path)
int blockchain_serialize(blockchain_t const *blockchain, char const *path)
{
uint8_t hblk_magic[4];
uint8_t hblk_version[3];
uint8_t hblk_endian;
int32_t hblk_blocks;
FILE *fp;
if (!blockchain || !path)
FILE *fptr = fopen(path, "w");
if (!fptr)
return (-1);
if (blockchain && path)
{
hblk_blocks = llist_size(blockchain->chain);
if (hblk_blocks == -1)
return (-1);
memcpy(hblk_magic, "HBLK", 4);
memcpy(hblk_version, "0.1", 3);
hblk_endian = _get_endianness();
hblk_blocks = llist_size(blockchain->chain);
if (hblk_blocks == -1)
return (-1);
fp = fopen(path, "w");
if (!fp)
return (-1);
fwrite(&hblk_magic, sizeof(hblk_magic), 1, fp);
fwrite(&hblk_version, sizeof(hblk_version), 1, fp);
fwrite(&hblk_endian, sizeof(hblk_endian), 1, fp);
fwrite(&hblk_blocks, sizeof(hblk_blocks), 1, fp);
if (llist_for_each(blockchain->chain, write_block, fp) == -1)
fwrite(&hblk_magic, sizeof(hblk_magic), 1, fptr);
fwrite(&hblk_version, sizeof(hblk_version), 1, fptr);
fwrite(&hblk_endian, sizeof(hblk_endian), 1, fptr);
fwrite(&hblk_blocks, sizeof(hblk_blocks), 1, fptr);
if (llist_for_each(blockchain->chain, block_to_file, fptr))
{
fclose(fp);
return (-1);
fclose(fptr);
return 0;
}
fclose(fp);
return (0);
}
fclose(fptr);
return -1;