From 4553b5c2f24de751d8d309bd515b42a4ddbf3460 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 12 Apr 2019 21:57:52 +0200 Subject: [PATCH] elements: Implement block parsing for elements block headers Since the difference between non-elements and elements block headers is just the middle 2 fields, I split the old parsing code so I could add the middle part. Signed-off-by: Christian Decker --- bitcoin/block.c | 23 ++++++++++++++++++++++- bitcoin/block.h | 11 +++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bitcoin/block.c b/bitcoin/block.c index 81b575399..8d484d3c9 100644 --- a/bitcoin/block.c +++ b/bitcoin/block.c @@ -26,7 +26,28 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams, if (!hex_decode(hex, hexlen, linear_tx, len)) return tal_free(b); - pull(&p, &len, &b->hdr, sizeof(b->hdr)); + b->hdr.version = pull_le32(&p, &len); + pull(&p, &len, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash)); + pull(&p, &len, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash)); + b->hdr.timestamp = pull_le32(&p, &len); + + if (is_elements) { + b->elements_hdr = tal(b, struct elements_block_hdr); + b->elements_hdr->block_height = pull_le32(&p, &len); + + size_t challenge_len = pull_varint(&p, &len); + b->elements_hdr->proof.challenge = tal_arr(b->elements_hdr, u8, challenge_len); + pull(&p, &len, b->elements_hdr->proof.challenge, challenge_len); + + size_t solution_len = pull_varint(&p, &len); + b->elements_hdr->proof.solution = tal_arr(b->elements_hdr, u8, solution_len); + pull(&p, &len, b->elements_hdr->proof.solution, solution_len); + + } else { + b->hdr.target = pull_le32(&p, &len); + b->hdr.nonce = pull_le32(&p, &len); + } + num = pull_varint(&p, &len); b->tx = tal_arr(b, struct bitcoin_tx *, num); for (i = 0; i < num; i++) { diff --git a/bitcoin/block.h b/bitcoin/block.h index 106ba0edb..28fcb1323 100644 --- a/bitcoin/block.h +++ b/bitcoin/block.h @@ -25,8 +25,19 @@ struct bitcoin_block_hdr { le32 nonce; }; +struct elements_block_proof { + u8 *challenge; + u8 *solution; +}; + +struct elements_block_hdr { + u32 block_height; + struct elements_block_proof proof; +}; + struct bitcoin_block { struct bitcoin_block_hdr hdr; + struct elements_block_hdr *elements_hdr; /* tal_count shows now many */ struct bitcoin_tx **tx; };