mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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>
This commit is contained in:
parent
f19726b4aa
commit
436da7f231
@ -63,6 +63,35 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
|
|||||||
return b;
|
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. */
|
/* We do the same hex-reversing crud as txids. */
|
||||||
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
||||||
struct bitcoin_blkid *blockid)
|
struct bitcoin_blkid *blockid)
|
||||||
|
@ -46,6 +46,10 @@ struct bitcoin_block *
|
|||||||
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
|
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
|
||||||
const char *hex, size_t hexlen);
|
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). */
|
/* Parse hex string to get blockid (reversed, a-la bitcoind). */
|
||||||
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
||||||
struct bitcoin_blkid *blockid);
|
struct bitcoin_blkid *blockid);
|
||||||
|
@ -691,7 +691,7 @@ static struct block *new_block(struct chain_topology *topo,
|
|||||||
{
|
{
|
||||||
struct block *b = tal(topo, struct block);
|
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",
|
log_debug(topo->log, "Adding block %u: %s",
|
||||||
height,
|
height,
|
||||||
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));
|
type_to_string(tmpctx, struct bitcoin_blkid, &b->blkid));
|
||||||
|
Loading…
Reference in New Issue
Block a user