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 <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-03-15 14:09:11 +01:00 committed by Rusty Russell
parent 98983006d6
commit 3937b17e2b
2 changed files with 29 additions and 0 deletions

View file

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

View file

@ -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 */