Browse Source

elements: Move blkid computation into its own function

The header is not a contiguous section of memory in elements, and it is of
variable length, so the simple trick of hashing in-memory data won't work
anymore. Some of the datafields would have been wrong on big-endian machines
anyway, so this is better anyway.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
travis-debug
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
436da7f231
  1. 29
      bitcoin/block.c
  2. 4
      bitcoin/block.h
  3. 2
      lightningd/chaintopology.c

29
bitcoin/block.c

@ -63,6 +63,35 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
return b;
}
void bitcoin_block_blkid(const struct bitcoin_block *b,
struct bitcoin_blkid *out)
{
struct sha256_ctx shactx;
u8 vt[VARINT_MAX_LEN];
size_t vtlen;
sha256_init(&shactx);
sha256_le32(&shactx, b->hdr.version);
sha256_update(&shactx, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
sha256_update(&shactx, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
sha256_le32(&shactx, b->hdr.timestamp);
if (is_elements) {
size_t clen = tal_bytelen(b->elements_hdr->proof.challenge);
sha256_le32(&shactx, b->elements_hdr->block_height);
vtlen = varint_put(vt, clen);
sha256_update(&shactx, vt, vtlen);
sha256_update(&shactx, b->elements_hdr->proof.challenge, clen);
/* The solution is skipped, since that'd create a circular
* dependency apparently */
} else {
sha256_le32(&shactx, b->hdr.target);
sha256_le32(&shactx, b->hdr.nonce);
}
sha256_double_done(&shactx, &out->shad);
}
/* We do the same hex-reversing crud as txids. */
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_blkid *blockid)

4
bitcoin/block.h

@ -46,6 +46,10 @@ struct bitcoin_block *
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
const char *hex, size_t hexlen);
/* Compute the double SHA block ID from the block header. */
void bitcoin_block_blkid(const struct bitcoin_block *block,
struct bitcoin_blkid *out);
/* Parse hex string to get blockid (reversed, a-la bitcoind). */
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_blkid *blockid);

2
lightningd/chaintopology.c

@ -691,7 +691,7 @@ static struct block *new_block(struct chain_topology *topo,
{
struct block *b = tal(topo, struct block);
sha256_double(&b->blkid.shad, &blk->hdr, sizeof(blk->hdr));
bitcoin_block_blkid(blk, &b->blkid);
log_debug(topo->log, "Adding block %u: %s",
height,
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));

Loading…
Cancel
Save