From c99786e72018132670500df7c072d4e7399d5c05 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 24 Jan 2020 21:02:07 +0100 Subject: [PATCH] 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. --- bitcoin/block.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/bitcoin/block.c b/bitcoin/block.c index 292881adc..059d0a584 100644 --- a/bitcoin/block.c +++ b/bitcoin/block.c @@ -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. */