bitcoin: Use the block hash we computed while deserializing it

Since we now compute the hash while deserializing the block header we can now
just use it, no reason to serialize the header just to hash it again. This
also allows us to throw away all the added dynafed fields in the next commit
instead of having to carry them around.
This commit is contained in:
Christian Decker 2020-01-24 21:02:07 +01:00
parent fce05d74e1
commit c99786e720
1 changed files with 1 additions and 21 deletions

View File

@ -91,27 +91,7 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
void bitcoin_block_blkid(const struct bitcoin_block *b,
struct bitcoin_blkid *out)
{
struct sha256_ctx shactx;
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(chainparams)) {
size_t clen = tal_bytelen(b->elements_hdr->proof.challenge);
sha256_le32(&shactx, b->elements_hdr->block_height);
sha256_varint(&shactx, clen);
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);
*out = b->hdr.hash;
}
/* We do the same hex-reversing crud as txids. */