Browse Source
It turns out using the verbose=false parameter, you can get the raw block from getblock. Do that. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>ppa-0.6.1
Rusty Russell
9 years ago
7 changed files with 135 additions and 6 deletions
@ -0,0 +1,38 @@ |
|||
#include "bitcoin/block.h" |
|||
#include "bitcoin/tx.h" |
|||
#include <ccan/str/hex/hex.h> |
|||
|
|||
/* Encoding is <blockhdr> <varint-num-txs> <tx>... */ |
|||
struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx, |
|||
const char *hex, size_t hexlen) |
|||
{ |
|||
struct bitcoin_block *b; |
|||
u8 *linear_tx; |
|||
const u8 *p; |
|||
size_t len, i, num; |
|||
|
|||
if (hexlen && hex[hexlen-1] == '\n') |
|||
hexlen--; |
|||
|
|||
/* Set up the block for success. */ |
|||
b = tal(ctx, struct bitcoin_block); |
|||
|
|||
/* De-hex the array. */ |
|||
len = hex_data_size(hexlen); |
|||
p = linear_tx = tal_arr(ctx, u8, len); |
|||
if (!hex_decode(hex, hexlen, linear_tx, len)) |
|||
return tal_free(b); |
|||
|
|||
pull(&p, &len, &b->hdr, sizeof(b->hdr)); |
|||
num = pull_varint(&p, &len); |
|||
b->tx = tal_arr(b, struct bitcoin_tx *, num); |
|||
for (i = 0; i < num; i++) |
|||
b->tx[i] = pull_bitcoin_tx(b->tx, &p, &len); |
|||
|
|||
/* We should end up not overrunning, nor have extra */ |
|||
if (!p || len) |
|||
return tal_free(b); |
|||
|
|||
tal_free(linear_tx); |
|||
return b; |
|||
} |
@ -0,0 +1,28 @@ |
|||
#ifndef LIGHTNING_BITCOIN_BLOCK_H |
|||
#define LIGHTNING_BITCOIN_BLOCK_H |
|||
#include "config.h" |
|||
#include "bitcoin/shadouble.h" |
|||
#include <ccan/endian/endian.h> |
|||
#include <ccan/short_types/short_types.h> |
|||
#include <ccan/tal/tal.h> |
|||
#include <stdbool.h> |
|||
|
|||
struct bitcoin_block_hdr { |
|||
le32 version; |
|||
struct sha256_double prev_hash; |
|||
struct sha256_double merkle_hash; |
|||
le32 timestamp; |
|||
le32 target; |
|||
le32 nonce; |
|||
}; |
|||
|
|||
struct bitcoin_block { |
|||
struct bitcoin_block_hdr hdr; |
|||
/* tal_count shows now many */ |
|||
struct bitcoin_tx **tx; |
|||
}; |
|||
|
|||
struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx, |
|||
const char *hex, size_t hexlen); |
|||
|
|||
#endif /* LIGHTNING_BITCOIN_BLOCK_H */ |
Loading…
Reference in new issue