Browse Source

bitcoin: Compute block hash while parsing

This avoids having to re-serialize the block header just to compute the
hash. It also frees us from having to carry around all the details in the
header and we can hand around a minimal version.
travis-debug
Christian Decker 5 years ago
parent
commit
c2434ad4fb
  1. 14
      bitcoin/block.c
  2. 1
      bitcoin/block.h

14
bitcoin/block.c

@ -13,6 +13,7 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
u8 *linear_tx;
const u8 *p;
size_t len, i, num;
struct sha256_ctx shactx;
if (hexlen && hex[hexlen-1] == '\n')
hexlen--;
@ -26,10 +27,19 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
if (!hex_decode(hex, hexlen, linear_tx, len))
return tal_free(b);
sha256_init(&shactx);
b->hdr.version = pull_le32(&p, &len);
sha256_le32(&shactx, b->hdr.version);
pull(&p, &len, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
sha256_update(&shactx, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
pull(&p, &len, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
sha256_update(&shactx, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
b->hdr.timestamp = pull_le32(&p, &len);
sha256_le32(&shactx, b->hdr.timestamp);
if (is_elements(chainparams)) {
b->elements_hdr = tal(b, struct elements_block_hdr);
@ -45,8 +55,12 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
} else {
b->hdr.target = pull_le32(&p, &len);
sha256_le32(&shactx, b->hdr.target);
b->hdr.nonce = pull_le32(&p, &len);
sha256_le32(&shactx, b->hdr.nonce);
}
sha256_double_done(&shactx, &b->hdr.hash);
num = pull_varint(&p, &len);
b->tx = tal_arr(b, struct bitcoin_tx *, num);

1
bitcoin/block.h

@ -23,6 +23,7 @@ struct bitcoin_block_hdr {
le32 timestamp;
le32 target;
le32 nonce;
struct sha256_double hash;
};
struct elements_block_proof {

Loading…
Cancel
Save