bitcoin: Add chainparams to transactions from blocks

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-07-30 19:51:53 +02:00 committed by Rusty Russell
parent 9288a7906b
commit d14bd286ba
5 changed files with 16 additions and 8 deletions

View File

@ -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;
@ -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)

View File

@ -8,6 +8,8 @@
#include <ccan/tal/tal.h>
#include <stdbool.h>
struct chainparams;
struct bitcoin_blkid {
struct sha256_double shad;
};
@ -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,

View File

@ -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)

View File

@ -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));

View File

@ -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),