Browse Source

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 <decker.christian@gmail.com>
travis-debug
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
4553b5c2f2
  1. 23
      bitcoin/block.c
  2. 11
      bitcoin/block.h

23
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)) if (!hex_decode(hex, hexlen, linear_tx, len))
return tal_free(b); 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); num = pull_varint(&p, &len);
b->tx = tal_arr(b, struct bitcoin_tx *, num); b->tx = tal_arr(b, struct bitcoin_tx *, num);
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {

11
bitcoin/block.h

@ -25,8 +25,19 @@ struct bitcoin_block_hdr {
le32 nonce; 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 {
struct bitcoin_block_hdr hdr; struct bitcoin_block_hdr hdr;
struct elements_block_hdr *elements_hdr;
/* tal_count shows now many */ /* tal_count shows now many */
struct bitcoin_tx **tx; struct bitcoin_tx **tx;
}; };

Loading…
Cancel
Save