From 3937b17e2bd4c841d64041e001366b85182982ad Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 15 Mar 2019 14:09:11 +0100 Subject: [PATCH] wally: Add a consistency check for old and new style txs During the migration to `libwally` we want to make absolutely sure that both transactions are generated identical, and can eventually be switched over. Signed-off-by: Christian Decker --- bitcoin/tx.c | 21 +++++++++++++++++++++ bitcoin/tx.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index fe45c8b03..40f5d9719 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -63,6 +63,27 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, return i; } +bool bitcoin_tx_check(const struct bitcoin_tx *tx) +{ + u8 *oldtx = linearize_tx(tmpctx, tx); + u8 *newtx; + size_t written; + + if (wally_tx_get_length(tx->wtx, WALLY_TX_FLAG_USE_WITNESS, &written) != + WALLY_OK) + return false; + + newtx = tal_arr(tmpctx, u8, written); + if (wally_tx_to_bytes(tx->wtx, WALLY_TX_FLAG_USE_WITNESS, newtx, written, + &written) != WALLY_OK) + return false; + + if (written != tal_bytelen(newtx)) + return false; + + return memeq(oldtx, tal_bytelen(oldtx), newtx, tal_bytelen(newtx)); +} + static void push_tx_input(const struct bitcoin_tx_input *input, const u8 *input_script, void (*push)(const void *, size_t, void *), void *pushp) diff --git a/bitcoin/tx.h b/bitcoin/tx.h index e33e1adfd..3c15077a2 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -92,4 +92,12 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, u32 outnum, u32 sequence, const struct amount_sat *amount, u8 *script); +/** + * Check a transaction for consistency. + * + * Mainly for the transition from `bitcoin_tx` to the `wally_tx`. Checks that + * both transactions serialize to two identical representations. + */ +bool bitcoin_tx_check(const struct bitcoin_tx *tx); + #endif /* LIGHTNING_BITCOIN_TX_H */