Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 531671cdf2b2
Choose a base ref
...
head repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 90928e26553e
Choose a head ref
  • 4 commits
  • 12 files changed
  • 1 contributor

Commits on Oct 27, 2014

  1. block: consider a block "known" if we know sufficient previous blocks.

    We currently insist on knowing the contents of *all* previous blocks,
    but that's overkill.  Instead of a boolean, keep a counter, and compare
    it against the most we need to know to mine.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 27, 2014

    Verified

    This commit was signed with the committer’s verified signature.
    eddumelendez Eddú Meléndez Gonzales
    Copy the full SHA
    f03e63c View commit details
  2. recv_block: don't get ancient transactions.

    Now we don't need all txs, don't get them.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 27, 2014
    Copy the full SHA
    ccc5dd5 View commit details

Commits on Oct 28, 2014

  1. recv_block: Don't log UNKNOWN_PREV errors during sync.

    They're expected.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 28, 2014
    Copy the full SHA
    d78668a View commit details
  2. generating: take care never to log { or } characters.

    The fast-path in pettycoin-query introduced in 994a7fa
    won't work if we do this.  So hex-ize the nonce.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 28, 2014
    Copy the full SHA
    90928e2 View commit details
Showing with 361 additions and 63 deletions.
  1. +5 −1 block.c
  2. +3 −2 block.h
  3. +3 −1 blockfile.c
  4. +41 −26 chain.c
  5. +3 −0 chain.h
  6. +5 −2 check_block.c
  7. +4 −3 generating.c
  8. +1 −1 mkgenesis.c
  9. +9 −8 recv_block.c
  10. +0 −1 test/run-01-difficulty.c
  11. +258 −0 test/run-09-chain-horizon-limit.c
  12. +29 −18 test/run-09-chain.c
6 changes: 5 additions & 1 deletion block.c
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ static struct block *new_block(const tal_t *ctx,
prev_work, &block->total_work);

block->bi = *bi;
block->all_known = false;
block->known_in_a_row = 0;
list_head_init(&block->children);
block->sha = *sha;
block->shard = tal_arr(block, struct block_shard *,
@@ -72,6 +72,10 @@ struct block *block_add(struct state *state,
block = new_block(state, &prev->total_work, sha, bi);
block->prev = prev;

/* Empty block case. */
if (block_all_known(block))
block->known_in_a_row = prev->known_in_a_row + 1;

/* Add to list for that generation. */
if (height >= tal_count(state->block_height)) {
/* We can only increment block heights. */
5 changes: 3 additions & 2 deletions block.h
Original file line number Diff line number Diff line change
@@ -25,8 +25,9 @@ struct block {
/* What features have been locked in for next fortnight? */
u8 pending_features;

/* Do we know all transactions for this and ancestors? */
bool all_known;
/* How many continuous predecessor blocks do we know completely?
* (Including this one). */
unsigned int known_in_a_row;

/* Total work to get to this block. */
BIGNUM total_work;
4 changes: 3 additions & 1 deletion blockfile.c
Original file line number Diff line number Diff line change
@@ -101,9 +101,11 @@ static void get_unknown_contents(struct state *state)
for (i = 0; i < tal_count(state->longest_chains); i++) {
for (b = state->longest_chains[i]; b; b = b->prev) {
/* Stop if we know from here down. */
if (b->all_known)
if (b->known_in_a_row == block_height(&b->bi))
break;
/* FIXME: Don't get over horizon */

/* A noop if we already know it. */
get_block_contents(state, b);
}
}
67 changes: 41 additions & 26 deletions chain.c
Original file line number Diff line number Diff line change
@@ -78,6 +78,21 @@ static bool find_connected_pair(const struct state *state,
return false;
}

/* This means we know the complete contents of enough predecessors to
* mine new blocks. */
bool predecessors_all_known(const struct block *b)
{
/* To build the next block, we need blocks b-0, b-1, b-2, b-4, b-8 ...
* b - 2^(PROTOCOL_PREV_BLOCK_TXHASHES-1) */
size_t prev_needed = 1U << (PROTOCOL_PREV_BLOCK_TXHASHES - 1);

/* +1 is for the genesis block, which is height 0 */
if (block_height(&b->bi) < prev_needed)
prev_needed = block_height(&b->bi) + 1;

return b->known_in_a_row >= prev_needed;
}

void check_chains(struct state *state, bool all)
{
const struct block *i;
@@ -99,7 +114,7 @@ void check_chains(struct state *state, bool all)
i != state->longest_knowns[0];
i = i->prev) {
assert(i != genesis_block(state));
assert(!i->all_known);
assert(!predecessors_all_known(i));
}

/*
@@ -146,7 +161,7 @@ void check_chains(struct state *state, bool all)
}
assert(i->complaint ||
cmp_work(i, state->longest_chains[0]) <= 0);
if (!i->complaint && i->all_known)
if (!i->complaint && predecessors_all_known(i))
assert(cmp_work(i, state->longest_knowns[0]) <= 0);

list_for_each(&i->children, b, sibling) {
@@ -240,39 +255,39 @@ static bool update_known_recursive(struct state *state, struct block *block)
struct block *b;
bool knowns_changed;

if (block->prev && !block->prev->all_known)
return false;

if (!block_all_known(block))
return false;

/* FIXME: Hack avoids writing to read-only genesis block. */
if (!block->all_known)
block->all_known = true;
/* We know one more than the previous block. */
if (block->prev)
block->known_in_a_row = block->prev->known_in_a_row + 1;

/* Blocks which are flawed are not useful */
if (block->complaint)
return false;

switch (cmp_work(block, state->longest_knowns[0])) {
case 1:
log_debug(state->log, "New known block work ");
log_add_struct(state->log, BIGNUM, &block->total_work);
log_add(state->log, " exceeds old known work ");
log_add_struct(state->log, BIGNUM,
&state->longest_knowns[0]->total_work);
/* They're no longer longest, we are. */
set_single(&state->longest_knowns, block);
knowns_changed = true;
break;
case 0:
tal_arr_append(&state->longest_knowns, block);
knowns_changed = true;
break;
case -1:
if (predecessors_all_known(block)) {
switch (cmp_work(block, state->longest_knowns[0])) {
case 1:
log_debug(state->log, "New known block work ");
log_add_struct(state->log, BIGNUM, &block->total_work);
log_add(state->log, " exceeds old known work ");
log_add_struct(state->log, BIGNUM,
&state->longest_knowns[0]->total_work);
/* They're no longer longest, we are. */
set_single(&state->longest_knowns, block);
knowns_changed = true;
break;
case 0:
tal_arr_append(&state->longest_knowns, block);
knowns_changed = true;
break;
case -1:
knowns_changed = false;
break;
}
} else
knowns_changed = false;
break;
}

/* Check descendents. */
list_for_each(&block->children, b, sibling) {
3 changes: 3 additions & 0 deletions chain.h
Original file line number Diff line number Diff line change
@@ -51,6 +51,9 @@ void update_block_ptrs_new_shard(struct state *state, struct block *block,
/* We've invalidated a block. */
void update_block_ptrs_invalidated(struct state *state, const struct block *block);

/* Are enough predecessors known that we can mine? */
bool predecessors_all_known(const struct block *b);

/* Debugging check */
void check_chains(struct state *state, bool all);
#endif /* PETTYCOIN_CHAIN_H */
7 changes: 5 additions & 2 deletions check_block.c
Original file line number Diff line number Diff line change
@@ -391,8 +391,11 @@ void check_block(struct state *state, const struct block *block, bool all)
assert(structeq(&sha, &block->sha));

if (block->prev) {
if (block->all_known)
assert(block->prev->all_known);
if (block_all_known(block))
assert(block->known_in_a_row
== block->prev->known_in_a_row + 1);
else
assert(block->known_in_a_row == 0);

if (block->prev->complaint)
assert(block->complaint);
7 changes: 4 additions & 3 deletions generating.c
Original file line number Diff line number Diff line change
@@ -241,7 +241,7 @@ static void exec_generator(struct generator *gen)
prev_merkle_str[STR_MAX_CHARS(u32)],
height[STR_MAX_CHARS(u32)],
shard_order[STR_MAX_CHARS(u8)];
char *prevstr;
char *prevstr, *hexnonce;
struct protocol_block_id prevs[PROTOCOL_NUM_PREV_IDS];
char fees_to[sizeof(struct protocol_address) * 2 + 1];
char nonce[14 + 1];
@@ -268,6 +268,7 @@ static void exec_generator(struct generator *gen)
for (i = 0; i < sizeof(nonce)-1; i++)
nonce[i] = 32 + isaac64_next_uint(isaac64, 224);
nonce[i] = '\0';
hexnonce = to_hex(gen, nonce, sizeof(nonce)-1);

if (pipe(outfd) != 0 || pipe(infd) != 0)
fatal(gen->state, "pipe: %s", strerror(errno));
@@ -296,11 +297,11 @@ static void exec_generator(struct generator *gen)

gen->log = new_log(gen, gen->state->lr, "%sGenerator %u:",
log_prefix(gen->state->log), gen->pid);
log_debug(gen->log, "Running '%s' '%s' '%s' '%s' %s' '%s' '%s' '%s'",
log_debug(gen->log, "Running '%s' '%s' '%s' '%s' %s' '%s' '%s' 0x%s",
gen->state->generator,
fees_to,
difficulty, prevstr, prev_merkle_str, height, shard_order,
nonce);
hexnonce);

close(outfd[1]);
close(infd[0]);
2 changes: 1 addition & 1 deletion mkgenesis.c
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ int main(int argc, char *argv[])
"},\n"
" .shard = genesis_shards,\n"
" .children = LIST_HEAD_INIT(genesis.children),\n"
" .all_known = true,\n"
" .known_in_a_row = 1,\n"
" .sha = { { ");

dump_array(sha.sha.sha, ARRAY_SIZE(sha.sha.sha));
17 changes: 9 additions & 8 deletions recv_block.c
Original file line number Diff line number Diff line change
@@ -139,9 +139,12 @@ recv_block(struct state *state, struct log *log, struct peer *peer,

e = check_block_header(state, bi, &prev, &sha.sha);
if (e != PROTOCOL_ECODE_NONE) {
log_unusual(log, "checking new block %u gave ",
block_height(bi));
log_add_enum(log, enum protocol_ecode, e);
/* Don't spam log during sync phase. */
if (!need_contents || e != PROTOCOL_ECODE_PRIV_UNKNOWN_PREV) {
log_unusual(log, "checking new block %u gave ",
block_height(bi));
log_add_enum(log, enum protocol_ecode, e);
}

/* If it was due to unknown prev, ask about that. */
if (peer) {
@@ -192,12 +195,10 @@ recv_block(struct state *state, struct log *log, struct peer *peer,

/* Don't bother about contents or extra
* children of expired blocks. */
if (!block_expired_by(expiry, current_time()))
if (!block_expired_by(expiry, current_time())) {
todo_add_get_children(state, &b->sha);

/* FIXME: We currently need *all* txs, for
* longest_known calculation */
get_block_contents(state, b);
get_block_contents(state, b);
}

} else {
/* Otherwise, tell peers about new block. */
1 change: 0 additions & 1 deletion test/run-01-difficulty.c
Original file line number Diff line number Diff line change
@@ -174,7 +174,6 @@ int main(int argc, char *argv[])
BN_init(&genesis.total_work);
if (!BN_zero(&genesis.total_work))
errx(1, "Failed to initialize genesis block");
genesis.all_known = true;
list_head_init(&genesis.children);
list_add_tail(state->block_height[0], &genesis.list);

Loading