forked from rustyrussell/bitcoin-iterate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterate.c
239 lines (205 loc) · 6.28 KB
/
iterate.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
#include <ccan/err/err.h>
/* #include <ccan/tal/tal.h> */
/* #include <ccan/tal/path/path.h> */
/* #include <ccan/take/take.h> */
/* #include <ccan/short_types/short_types.h> */
/* #include <ccan/opt/opt.h> */
/* #include <ccan/htable/htable_type.h> */
/* #include <ccan/rbuf/rbuf.h> */
/* #include <sys/types.h> */
/* #include <stdbool.h> */
/* #include <sys/stat.h> */
#include <stdio.h>
#include "parse.h"
/* #include "space.h" */
#include "block.h"
#include "blockfiles.h"
/* #include "utxo.h" */
#include "cache.h"
/* #include "utils.h" */
#include "iterate.h"
static void set_heights_and_best(struct block **best, struct block *genesis, struct block_map *block_map)
{
struct block *b;
struct block_map_iter it;
for (b = block_map_first(block_map, &it);
b;
b = block_map_next(block_map, &it)) {
set_height(block_map, b);
if (b->height > (*best)->height)
*best = b;
}
}
static void set_blockchain_end(u8 *tip, struct block **best, struct block_map *block_map)
{
if (!is_zero(tip)) {
*best = block_map_get(block_map, tip);
if (!*best)
errx(1, "Unknown --end-hash block "SHA_FMT, SHA_VALS(tip));
}
}
static void set_blockchain_start(u8 *start_hash, struct block **start, struct block_map *block_map)
{
if (!is_zero(start_hash)) {
*start = block_map_get(block_map, start_hash);
if (!*start)
errx(1, "Unknown --start-hash block "SHA_FMT, SHA_VALS(start_hash));
}
}
static void link_blocks(struct block *best, struct block_map *block_map)
{
struct block *b, *next = NULL;
for (b = best; b; b = block_map_get(block_map, b->bh.prev_hash)) {
b->next = next;
next = b;
}
}
static void set_iteration_end(unsigned long block_end, struct block **best, struct block *genesis)
{
if (block_end != -1UL) {
struct block *b;
for (b = genesis; b->height != block_end; b = b->next) {
if (!b->next)
errx(1, "No block end %lu found", block_end);
}
*best = b;
b->next = NULL;
}
}
static void set_iteration_start(unsigned long block_start, struct block **start, struct block *genesis)
{
if (block_start != 0) {
struct block *b;
for (b = genesis; b->height != block_start; b = b->next) {
if (!b->next)
errx(1, "No block start %lu found", block_start);
}
*start = b;
}
}
void iterate(char *blockdir, char *cachedir,
bool use_testnet,
unsigned long block_start, unsigned long block_end,
u8 *start_hash, u8 *tip,
bool needs_utxo, unsigned int utxo_period,
bool use_mmap,
unsigned progress_marks, bool quiet,
block_function blockfn,
transaction_function txfn,
input_function inputfn,
output_function outputfn,
utxo_function utxofn)
{
void *tal_ctx = tal(NULL, char);
size_t i, block_count = 0;
bool needs_fee;
struct block *b, *best = NULL, *genesis = NULL, *start = NULL, *last_utxo_block = NULL;
struct block_map block_map;
char *blockcache = NULL;
struct utxo_map utxo_map;
struct space space;
static char **block_fnames;
block_fnames = block_filenames(tal_ctx, blockdir, use_testnet);
block_count = read_blockchain(tal_ctx,
quiet, use_mmap,
use_testnet, cachedir, blockcache,
block_fnames,
&block_map, &genesis);
best = genesis;
start = genesis;
set_heights_and_best(&best, genesis, &block_map);
set_blockchain_end(tip, &best, &block_map);
set_blockchain_start(start_hash, &start, &block_map);
link_blocks(best, &block_map);
set_iteration_end(block_end, &best, genesis);
set_iteration_start(block_start, &start, genesis);
if (!quiet) {
fprintf(stderr, "bitcoin-iterate: Iterating between block heights %u and %u (of %zu total blocks)\n",
start->height, best->height, block_count);
if (utxofn) {
fprintf(stderr, "bitcoin-iterate: Iterating over UTXOs every %u blocks\n", utxo_period);
}
}
utxo_map_init(&utxo_map);
needs_fee = needs_utxo;
/* Do we have cache utxo? */
if (cachedir && start && needs_utxo) {
if (read_utxo_cache(tal_ctx, quiet, &utxo_map, cachedir, start->sha)) {
needs_fee = false;
} else if (!quiet)
fprintf(stderr, "bitcoin-iterate: Did not find valid UTXO cache\n");
}
/* Now run forwards. */
for (b = genesis; b; b = b->next) {
off_t off;
struct transaction *tx;
if (b == start) {
/* Are we UTXO caching? */
if (cachedir && needs_utxo) {
if (needs_fee) {
/* Save cache for next time. */
write_utxo_cache(&utxo_map, quiet, cachedir, b->sha);
} else
/* We loaded cache, now we calc fee. */
needs_fee = true;
}
start = NULL;
}
if (!start && blockfn)
blockfn(&utxo_map, b);
if (!start && progress_marks
&& b->height % (best->height / progress_marks)
== (best->height / progress_marks) - 1)
fprintf(stderr, ".");
/* Don't read transactions if we don't have to */
if (!txfn && !inputfn && !outputfn && !utxofn)
continue;
/* If we haven't started and don't need to gather UTXO, skip */
if (start && !needs_fee)
continue;
off = b->pos;
space_init(&space);
tx = space_alloc_arr(&space, struct transaction,
b->bh.transaction_count);
for (i = 0; i < b->bh.transaction_count; i++) {
size_t j;
off_t txoff = off;
read_transaction(&space, &tx[i],
block_file(block_fnames, b->filenum, use_mmap), &off);
if (!start && txfn)
txfn(&utxo_map, b, &tx[i], i);
if (!start && inputfn) {
for (j = 0; j < tx[i].input_count; j++) {
inputfn(&utxo_map, b, &tx[i], i, &tx[i].input[j]);
}
}
if (!start && outputfn) {
for (j = 0; j < tx[i].output_count; j++) {
outputfn(&utxo_map, b, &tx[i], i, &tx[i].output[j]);
}
}
if (needs_fee) {
/* Now we can release consumed utxos;
* before there was a possibility of %tF */
/* Coinbase inputs are not real */
if (i != 0) {
for (j = 0; j < tx[i].input_count; j++)
release_utxo(&utxo_map,
&tx[i].input[j]);
}
/* And add this tx's outputs to utxo */
add_utxo(tal_ctx, &utxo_map, b, &tx[i], i, txoff);
}
}
if (utxofn && ((b->height % utxo_period) == 0)) {
struct utxo_map_iter it;
struct utxo *utxo;
for (utxo = utxo_map_first(&utxo_map, &it);
utxo;
utxo = utxo_map_next(&utxo_map, &it)) {
utxofn(&utxo_map, b, last_utxo_block, utxo);
}
last_utxo_block = b;
}
}
}