-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_deserialize.c
260 lines (238 loc) · 6.67 KB
/
blockchain_deserialize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* block_t blockchain_t strE_LLIST bc_file_hdr_t HBLK_MAG* HBLK_VER* */
#include "blockchain.h"
/* lstat `struct stat` */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* errno */
#include <errno.h>
/* fprintf perror read */
#include <stdio.h>
/* open O_* close */
#include <fcntl.h>
/* _get_endianness _swap_endian */
#include "provided/endianness.h"
/* llist_* */
#include <llist.h>
/* strerror strncmp */
#include <string.h>
/**
* pathToReadFD - validates a path and opens a file descriptor in
* read-only mode at that path
*
* @path: contains the potential path to a file to open
*
* Return: file descriptor upon success, or -1 upon failure
*/
int pathToReadFD(char const *path)
{
struct stat st;
int fd;
if (lstat(path, &st) == -1)
{
fprintf(stderr, "pathToReadFD: lstat '%s': %s\n", path,
strerror(errno));
return (-1);
}
/* st.st_size is type off_t, or long int */
if ((size_t)(st.st_size) < sizeof(bc_file_hdr_t) + GEN_BLK_SERIAL_SZ)
{
fprintf(stderr, "pathToReadFD: %s\n",
"file too small to contain header and Genesis Block");
return (-1);
}
fd = open(path, O_RDONLY);
if (fd == -1)
{
fprintf(stderr, "pathToReadFD: open '%s': %s\n", path,
strerror(errno));
}
return (fd);
}
/**
* readBlkchnFileHdr - reads and validates a serialized file header from a
* file storing a blockchain
*
* @fd: file descriptor already open for reading
* @local_endianness: 1 for little endian, 2 for big endian
* @file_endianness: 1 for little endian, 2 for big endian, modifed by
* reference
* @block_ct: stores count of blocks in blockchain, modifed by reference
*
* Return: 0 on success, or 1 upon failure
*/
int readBlkchnFileHdr(int fd, uint8_t local_endianness,
uint8_t *file_endianness, uint32_t *block_ct)
{
bc_file_hdr_t header;
if (!file_endianness || !block_ct)
{
fprintf(stderr, "readBlkchnFileHdr: NULL parameter(s)\n");
return (1);
}
if (local_endianness != 1 && local_endianness != 2)
{
fprintf(stderr,
"readBlkchnFileHdr: invalid local_endianness\n");
return (1);
}
if (read(fd, &header, sizeof(bc_file_hdr_t)) == -1)
{
perror("readBlkchnFileHdr: read");
return (1);
}
if (strncmp((char *)header.hblk_magic, HBLK_MAG, HBLK_MAG_LEN) != 0)
{
fprintf(stderr,
"readBlkchnFileHdr: invalid magic number\n");
return (1);
}
if (strncmp((char *)header.hblk_version, HBLK_VER, HBLK_VER_LEN) != 0)
{
fprintf(stderr, "readBlkchnFileHdr: %s\n",
"serialized with incompatible version number");
return (1);
}
*file_endianness = header.hblk_endian;
if (*file_endianness != local_endianness)
#ifdef __GNUC__ /* compiled with gcc, can use gcc builtins for fast assembly */
header.hblk_blocks = __builtin_bswap32(header.hblk_blocks);
#else /* use function to manually byte swap */
_swap_endian(&(header.hblk_blocks), 32);
#endif
*block_ct = header.hblk_blocks;
return (0);
}
/**
* bswapBlock - reverses endianness of relevant values in a block_t struct
*
* @block: pointer to the block to have values' byte order reversed
*
* Note: __builtin_bswapXX preferred over _swap_endian in
* provided/_endianness.c due to its efficiency, being compiled as only
* one assembly instruction.
*/
void bswapBlock(block_t *block)
{
if (!block)
{
fprintf(stderr, "bswapBlock: NULL parameter\n");
return;
}
#ifdef __GNUC__ /* compiled with gcc, can use gcc builtins for fast assembly */
block->info.index = __builtin_bswap32(block->info.index);
block->info.difficulty = __builtin_bswap32(block->info.difficulty);
block->info.timestamp = __builtin_bswap64(block->info.timestamp);
block->info.nonce = __builtin_bswap64(block->info.nonce);
block->data.len = __builtin_bswap32(block->data.len);
#else /* use function to manually byte swap */
_swap_endian(&(block->info.index), 32);
_swap_endian(&(block->info.difficulty), 32);
_swap_endian(&(block->info.timestamp), 64);
_swap_endian(&(block->info.nonce), 64);
_swap_endian(&(block->data.len), 32);
#endif
}
/**
* readBlocks - reads serialized blocks from a storage file into a blockchain
* data structure
*
* @fd: file descriptor already open for reading
* @blockchain: pointer to an empty blockchain struct (Genesis Block removed)
* to contain the deserialized blocks
* @local_endianness: 1 for little endian, 2 for big endian
* @file_endianness: 1 for little endian, 2 for big endian
* @block_ct: count of blocks in blockchain
*
* Return: 0 on success, or 1 upon failure
*/
int readBlocks(int fd, const blockchain_t *blockchain,
uint8_t local_endianness, uint8_t file_endianness,
uint32_t block_ct)
{
uint32_t i;
block_t *block;
if (!blockchain)
{
fprintf(stderr, "readBlocks: NULL parameter\n");
return (1);
}
if (!llist_is_empty(blockchain->chain))
{
fprintf(stderr, "readBlocks: target blockchain not empty\n");
return (1);
}
for (i = 0; i < block_ct; i++)
{ /* assumes header has already been read from fd */
block = calloc(1, sizeof(block_t));
if (!block)
{
fprintf(stderr, "readBlocks: calloc failure\n");
return (1);
}
if (read(fd, &(block->info), sizeof(block_info_t)) == -1 ||
read(fd, &(block->data.len), sizeof(uint32_t)) == -1 ||
read(fd, &(block->data.buffer), block->data.len) == -1 ||
read(fd, &(block->hash), SHA256_DIGEST_LENGTH) == -1)
{
perror("readBlocks: read");
return (1);
}
if (local_endianness != file_endianness)
bswapBlock(block);
if (llist_add_node(blockchain->chain, (llist_node_t)block,
ADD_NODE_REAR) != 0)
{
fprintf(stderr, "readBlocks: llist_add_node: %s\n",
strE_LLIST(llist_errno));
return (1);
}
}
return (0);
}
/**
* blockchain_deserialize - deserializes a blockchain from a file; see
* blockchain_serialize for serialization format
*
* @path: full path to file containing serialized blockchain
*
* Return: pointer to deserialized blockchain, or NULL on failure
*/
blockchain_t *blockchain_deserialize(char const *path)
{
int fd;
uint8_t local_endianness, file_endianness;
uint32_t block_ct;
blockchain_t *blockchain;
block_t *genesis;
if (!path)
{
fprintf(stderr, "blockchain_deserialize: NULL parameter\n");
return (NULL);
}
fd = pathToReadFD(path);
if (fd == -1)
return (NULL);
blockchain = blockchain_create();
if (!blockchain)
{
close(fd);
return (NULL);
}
/* remove preloaded Genesis Block at head of list */
genesis = (block_t *)llist_pop(blockchain->chain);
if (genesis)
free(genesis);
local_endianness = _get_endianness();
if (readBlkchnFileHdr(fd, local_endianness, &file_endianness,
&block_ct) != 0 ||
readBlocks(fd, blockchain, local_endianness, file_endianness,
block_ct) != 0)
{
close(fd);
blockchain_destroy(blockchain);
return (NULL);
}
close(fd);
return (blockchain);
}