Skip to content

Commit 89841d3

Browse files
committedJul 30, 2014
Don't check the entire chain every time.
With over 1700 blocks, this was taking a *lot* of CPU. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent de2e9c8 commit 89841d3

11 files changed

+67
-46
lines changed
 

‎block.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "block.h"
22
#include "blockfile.h"
33
#include "chain.h"
4+
#include "check_block.h"
45
#include "difficulty.h"
56
#include "features.h"
67
#include "generating.h"
@@ -116,13 +117,14 @@ struct block *block_add(struct state *state,
116117

117118
block->complaint = prev->complaint;
118119
if (block->complaint) {
119-
check_chains(state);
120+
check_chains(state, false);
120121
/* It's not a candidate for real use. */
121122
return block;
122123
}
123124

124125
update_block_ptrs_new_block(state, block);
125-
check_chains(state);
126+
check_chains(state, false);
127+
check_block(state, block, false);
126128
return block;
127129
}
128130

‎blockfile.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "block.h"
22
#include "blockfile.h"
3+
#include "chain.h"
34
#include "check_block.h"
45
#include "marshal.h"
56
#include "packet_io.h"
@@ -123,6 +124,10 @@ void load_blocks(struct state *state)
123124
out:
124125
/* Now we can save more. */
125126
state->blockfd = fd;
127+
128+
log_info(state->log, "Checking chains...");
129+
check_chains(state, true);
130+
log_add(state->log, " ...completed");
126131
}
127132

128133
void save_block(struct state *state, struct block *new)

‎chain.c

+36-34
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static bool find_connected_pair(const struct state *state,
7373
return false;
7474
}
7575

76-
void check_chains(struct state *state)
76+
void check_chains(struct state *state, bool all)
7777
{
7878
const struct block *i;
7979
size_t n, num_next_level = 1;
@@ -88,6 +88,37 @@ void check_chains(struct state *state)
8888
assert(cmp_work(state->longest_knowns[n],
8989
state->longest_knowns[0]) == 0);
9090

91+
92+
/* preferred_chain should be a descendent of longest_knowns[0] */
93+
for (i = state->preferred_chain;
94+
i != state->longest_knowns[0];
95+
i = i->prev) {
96+
assert(i != genesis_block(state));
97+
assert(!i->all_known);
98+
}
99+
100+
/*
101+
* preferred_chain is *not* state->longest_chains[0], then no
102+
* chain should connect any longest_knowns to longest_chains.
103+
*/
104+
if (state->preferred_chain != state->longest_chains[0]) {
105+
size_t a, b;
106+
assert(!find_connected_pair(state, &a, &b));
107+
}
108+
109+
/* We ignore blocks which have a problem. */
110+
assert(!state->preferred_chain->complaint);
111+
112+
for (n = 0; n < tal_count(state->longest_knowns); n++)
113+
assert(!state->longest_knowns[n]->complaint);
114+
115+
for (n = 0; n < tal_count(state->longest_chains); n++)
116+
assert(!state->longest_chains[n]->complaint);
117+
118+
/* Checking the actual blocks is expensive! */
119+
if (!all)
120+
return;
121+
91122
for (n = 0; n < tal_count(state->block_depth); n++) {
92123
size_t num_this_level = num_next_level;
93124
list_check(state->block_depth[n], "bad block depth");
@@ -109,42 +140,16 @@ void check_chains(struct state *state)
109140
cmp_work(i, state->longest_chains[0]) <= 0);
110141
if (!i->complaint && i->all_known)
111142
assert(cmp_work(i, state->longest_knowns[0]) <= 0);
112-
143+
113144
list_for_each(&i->children, b, sibling) {
114145
num_next_level++;
115146
assert(b->prev == i);
116147
}
117-
check_block(state, i);
148+
check_block(state, i, all);
118149
}
119-
assert(num_this_level == 0);
150+
assert(num_this_level == 0);
120151
}
121152
assert(num_next_level == 0);
122-
123-
/* preferred_chain should be a descendent of longest_knowns[0] */
124-
for (i = state->preferred_chain;
125-
i != state->longest_knowns[0];
126-
i = i->prev) {
127-
assert(i != genesis_block(state));
128-
assert(!i->all_known);
129-
}
130-
131-
/*
132-
* preferred_chain is *not* state->longest_chains[0], then no
133-
* chain should connect any longest_knowns to longest_chains.
134-
*/
135-
if (state->preferred_chain != state->longest_chains[0]) {
136-
size_t a, b;
137-
assert(!find_connected_pair(state, &a, &b));
138-
}
139-
140-
/* We ignore blocks which have a problem. */
141-
assert(!state->preferred_chain->complaint);
142-
143-
for (n = 0; n < tal_count(state->longest_knowns); n++)
144-
assert(!state->longest_knowns[n]->complaint);
145-
146-
for (n = 0; n < tal_count(state->longest_chains); n++)
147-
assert(!state->longest_chains[n]->complaint);
148153
}
149154

150155
static void swap_blockptr(const struct block **a, const struct block **b)
@@ -301,7 +306,6 @@ static bool update_known(struct state *state, struct block *block)
301306

302307
order_block_pointers(state);
303308
update_preferred_chain(state);
304-
check_chains(state);
305309

306310
if (state->longest_knowns[0] != prev_known) {
307311
/* Any transactions from old branch go into pending. */
@@ -409,8 +413,6 @@ void update_block_ptrs_new_block(struct state *state, struct block *block)
409413

410414
/* FIXME: Only needed if a descendent of known[0] */
411415
update_preferred_chain(state);
412-
413-
check_chains(state);
414416
}
415417

416418
/* Filled a new shard; update state->longest_chains, state->longest_knowns,
@@ -446,7 +448,7 @@ void update_block_ptrs_invalidated(struct state *state,
446448
find_longest_descendents(g, &state->longest_chains);
447449
update_known(state, cast_const(struct block *, g));
448450

449-
check_chains(state);
451+
check_chains(state, false);
450452

451453
/* We don't need to know anything about this or any decendents. */
452454
forget_about_all(state, block);

‎chain.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ void update_block_ptrs_new_shard(struct state *state, struct block *block,
5151
void update_block_ptrs_invalidated(struct state *state, const struct block *block);
5252

5353
/* Debugging check */
54-
void check_chains(struct state *state);
54+
void check_chains(struct state *state, bool all);
5555
#endif /* PETTYCOIN_CHAIN_H */

‎check_block.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ void put_tx_in_shard(struct state *state,
249249

250250
/* Tell peers about the new tx in block. */
251251
send_tx_in_block_to_peers(state, source, block, shard->shardnum, txoff);
252+
253+
/* Debugging check */
254+
check_block_shard(state, block, shard);
252255
}
253256

254257
bool put_txhash_in_shard(struct state *state,
@@ -278,6 +281,10 @@ bool put_txhash_in_shard(struct state *state,
278281

279282
/* This could eliminate a pending tx. */
280283
state->pending->needs_recheck = true;
284+
285+
/* Debugging check */
286+
check_block_shard(state, block, shard);
287+
281288
return true;
282289
}
283290

@@ -356,7 +363,7 @@ bool check_prev_txhashes(struct state *state, const struct block *block,
356363
return true;
357364
}
358365

359-
void check_block(struct state *state, const struct block *block)
366+
void check_block(struct state *state, const struct block *block, bool all)
360367
{
361368
u32 diff = le32_to_cpu(block->tailer->difficulty);
362369
struct protocol_double_sha sha;
@@ -381,8 +388,10 @@ void check_block(struct state *state, const struct block *block)
381388

382389
/* FIXME: check block->prev_txhashes! */
383390

384-
for (shard = 0; shard < num_shards(block->hdr); shard++) {
385-
check_block_shard(state, block, block->shard[shard]);
391+
if (all) {
392+
for (shard = 0; shard < num_shards(block->hdr); shard++) {
393+
check_block_shard(state, block, block->shard[shard]);
394+
}
386395
}
387396
}
388397

‎check_block.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ bool check_tx_inputs_and_refs(struct state *state,
7474
struct protocol_input_ref *refs);
7575

7676
/* Various assertions about a block */
77-
void check_block(struct state *state, const struct block *block);
77+
void check_block(struct state *state, const struct block *block, bool all);
7878
#endif /* PETTYCOIN_CHECK_BLOCK_H */

‎test/run-02-prev_txhashes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ size_t marshal_input_ref_len(const union protocol_tx *tx)
8383
return 0;
8484
}
8585

86-
void check_block(struct state *state, const struct block *block)
86+
void check_block(struct state *state, const struct block *block, bool all)
8787
{
8888
}
8989

‎test/run-03-check_block.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int main(int argc, char *argv[])
224224
/* We need enough of state to use the real init function here. */
225225
pseudorand_init();
226226
s = new_state(true);
227-
check_chains(s);
227+
check_chains(s, true);
228228

229229
fake_time = le32_to_cpu(genesis_tlr.timestamp) + 1;
230230

‎test/run-08-simple-chain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
void block_to_pending(struct state *state, const struct block *block)
1515
{ fprintf(stderr, "block_to_pending called!\n"); abort(); }
1616
/* Generated stub for check_block */
17-
void check_block(struct state *state, const struct block *block)
17+
void check_block(struct state *state, const struct block *block, bool all)
1818
{ fprintf(stderr, "check_block called!\n"); abort(); }
1919
/* Generated stub for check_prev_txhashes */
2020
bool check_prev_txhashes(struct state *state, const struct block *block,

‎test/run-08-tx_shard.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
#include "helper_gateway_key.h"
1414

1515
/* AUTOGENERATED MOCKS START */
16+
/* Generated stub for check_block */
17+
void check_block(struct state *state, const struct block *block, bool all)
18+
{ fprintf(stderr, "check_block called!\n"); abort(); }
1619
/* Generated stub for check_chains */
17-
void check_chains(struct state *state)
20+
void check_chains(struct state *state, bool all)
1821
{ fprintf(stderr, "check_chains called!\n"); abort(); }
1922
/* Generated stub for check_tx */
2023
enum protocol_ecode check_tx(struct state *state, const union protocol_tx *tx,

‎test/run-09-chain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void block_to_pending(struct state *state, const struct block *block)
6363
{
6464
}
6565

66-
void check_block(struct state *state, const struct block *block)
66+
void check_block(struct state *state, const struct block *block, bool all)
6767
{
6868
}
6969

0 commit comments

Comments
 (0)
Please sign in to comment.