From 524849bc11fa547adadd4cb1c10c6136ba0bd86f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 4 Jan 2018 18:16:56 +0100 Subject: [PATCH] bitcoin: Split pull_bitcoin_tx The deserialization of bitcoin transactions in wire/ is rather annoying in that we first allocate a new bitcoin_tx, then copy it's contents onto the destination and then still carry the newly allocated one around due to the tal-tree. This splits `pull_bitcoin_tx` into two: one part that does the allocation and another one that proceeds to parse. Signed-off-by: Christian Decker --- bitcoin/tx.c | 12 +++++++++--- bitcoin/tx.h | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index c31d31333..3040e1dda 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -383,10 +383,9 @@ static void pull_witness(struct bitcoin_tx_input *inputs, size_t i, } } -struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, - const u8 **cursor, size_t *max) +struct bitcoin_tx *pull_bitcoin_tx_onto(const tal_t *ctx, const u8 **cursor, + size_t *max, struct bitcoin_tx *tx) { - struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); size_t i; u64 count; u8 flag = 0; @@ -425,6 +424,13 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, return tx; } +struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, + const u8 **cursor, size_t *max) +{ + struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); + return pull_bitcoin_tx_onto(ctx, cursor, max, tx); +} + struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex, size_t hexlen) { diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 987c2e80e..1272a118d 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -71,4 +71,18 @@ bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid, struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, size_t *max); +/** + * pull_bitcoin_tx_onto - De-serialize a bitcoin tx into tx + * + * Like pull_bitcoin_tx, but skips the allocation of tx. Used by the + * wire implementation where the caller allocates, and the callee only + * fills in values. + * + * @ctx: Allocation context + * @cursor: buffer to read from + * @max: Buffer size left to read + * @tx (out): Destination transaction + */ +struct bitcoin_tx *pull_bitcoin_tx_onto(const tal_t *ctx, const u8 **cursor, + size_t *max, struct bitcoin_tx *tx); #endif /* LIGHTNING_BITCOIN_TX_H */