-
Notifications
You must be signed in to change notification settings - Fork 16
/
ckb_streaming.h
46 lines (39 loc) · 1.46 KB
/
ckb_streaming.h
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
#ifndef CKB_C_STDLIB_CKB_STREAMING_H_
#define CKB_C_STDLIB_CKB_STREAMING_H_
#ifndef CKB_ONE_BATCH_SIZE
#define CKB_ONE_BATCH_SIZE 16384
#endif /* CKB_ONE_BATCH_SIZE */
#include "blake2b.h"
#include "ckb_syscalls.h"
typedef int (*load_function)(void *, uint64_t *, size_t, size_t, size_t);
int ckb_load_and_hash(blake2b_state *ctx, size_t start, size_t index,
size_t source, load_function f) {
uint8_t temp[CKB_ONE_BATCH_SIZE];
uint64_t len = CKB_ONE_BATCH_SIZE;
int ret = f(temp, &len, start, index, source);
if (ret != CKB_SUCCESS) {
return ret;
}
blake2b_update(ctx, (char *)&len, sizeof(uint64_t));
uint64_t offset = (len > CKB_ONE_BATCH_SIZE) ? CKB_ONE_BATCH_SIZE : len;
blake2b_update(ctx, temp, offset);
while (offset < len) {
uint64_t current_len = CKB_ONE_BATCH_SIZE;
ret = f(temp, ¤t_len, start + offset, index, source);
if (ret != CKB_SUCCESS) {
return ret;
}
uint64_t current_read =
(current_len > CKB_ONE_BATCH_SIZE) ? CKB_ONE_BATCH_SIZE : current_len;
blake2b_update(ctx, temp, current_read);
offset += current_read;
}
return CKB_SUCCESS;
}
int ckb_hash_cell(blake2b_state *ctx, size_t index_code, size_t source) {
return ckb_load_and_hash(ctx, 0, index_code, source, ckb_load_cell);
}
int ckb_hash_input(blake2b_state *ctx, size_t index_code, size_t source) {
return ckb_load_and_hash(ctx, 0, index_code, source, ckb_load_input);
}
#endif /* CKB_C_STDLIB_CKB_STREAMING_H_ */