Skip to content

Commit

Permalink
bitcoin: Add chainparams to transactions from blocks
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Decker <[email protected]>
  • Loading branch information
cdecker authored and rustyrussell committed Jul 31, 2019
1 parent 9288a79 commit d14bd28
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <common/type_to_string.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 *
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
const char *hex, size_t hexlen)
{
struct bitcoin_block *b;
u8 *linear_tx;
Expand All @@ -28,8 +29,10 @@ struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
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++)
for (i = 0; i < num; i++) {
b->tx[i] = pull_bitcoin_tx(b->tx, &p, &len);
b->tx[i]->chainparams = chainparams;
}

/* We should end up not overrunning, nor have extra */
if (!p || len)
Expand Down
7 changes: 5 additions & 2 deletions bitcoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <ccan/tal/tal.h>
#include <stdbool.h>

struct chainparams;

struct bitcoin_blkid {
struct sha256_double shad;
};
Expand All @@ -29,8 +31,9 @@ struct bitcoin_block {
struct bitcoin_tx **tx;
};

struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
const char *hex, size_t hexlen);
struct bitcoin_block *
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
const char *hex, size_t hexlen);

/* Parse hex string to get blockid (reversed, a-la bitcoind). */
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BITCOIN_TEST_PROGRAMS := $(BITCOIN_TEST_OBJS:.o=)

BITCOIN_TEST_COMMON_OBJS := common/utils.o

$(BITCOIN_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_TEST_COMMON_OBJS)
$(BITCOIN_TEST_PROGRAMS): $(CCAN_OBJS) $(BITCOIN_TEST_COMMON_OBJS) bitcoin/chainparams.o
$(BITCOIN_TEST_OBJS): $(CCAN_HEADERS) $(BITCOIN_HEADERS) $(BITCOIN_SRC)

ALL_TEST_PROGRAMS += $(BITCOIN_TEST_PROGRAMS)
Expand Down
3 changes: 2 additions & 1 deletion bitcoin/test/run-bitcoin_block_from_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ int main(void)
struct bitcoin_block *b;

setup_locale();
b = bitcoin_block_from_hex(NULL, block, strlen(block));
b = bitcoin_block_from_hex(NULL, chainparams_for_network("bitcoin"),
block, strlen(block));

assert(b);
assert(b->hdr.version == CPU_TO_LE32(0x6592a000));
Expand Down
3 changes: 2 additions & 1 deletion lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ static bool process_rawblock(struct bitcoin_cli *bcli)
struct bitcoin_block *blk,
void *arg) = bcli->cb;

blk = bitcoin_block_from_hex(bcli, bcli->output, bcli->output_bytes);
blk = bitcoin_block_from_hex(bcli, bcli->bitcoind->chainparams,
bcli->output, bcli->output_bytes);
if (!blk)
fatal("%s: bad block '%.*s'?",
bcli_args(tmpctx, bcli),
Expand Down

0 comments on commit d14bd28

Please sign in to comment.