Skip to content

Commit 4dbd450

Browse files
committed
tx: Strengthen transaction construction checks
We roll the `elements_add_fee_output` function and the cropping of overallocated arrays into the `bitcoin_tx_finalize` function. This is supposed to be the final cleanup and compaction step before a tx can be sent to bitcoin or passed off to other daemons. This is the cleanup promised in ElementsProject#3491
1 parent e9d3bb2 commit 4dbd450

File tree

7 files changed

+36
-8
lines changed

7 files changed

+36
-8
lines changed

bitcoin/tx.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ bool bitcoin_tx_check(const struct bitcoin_tx *tx)
160160
size_t written;
161161
int flags = WALLY_TX_FLAG_USE_WITNESS;
162162

163+
if (tal_count(tx->input_amounts) != tx->wtx->num_inputs)
164+
return false;
165+
166+
if (tal_count(tx->output_witscripts) != tx->wtx->num_outputs)
167+
return false;
168+
163169
if (wally_tx_get_length(tx->wtx, flags, &written) != WALLY_OK)
164170
return false;
165171

@@ -408,6 +414,18 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
408414
return tx;
409415
}
410416

417+
void bitcoin_tx_finalize(struct bitcoin_tx *tx)
418+
{
419+
size_t num_outputs, num_inputs;
420+
elements_tx_add_fee_output(tx);
421+
422+
num_outputs = tx->wtx->num_outputs;
423+
tal_resize(&(tx->output_witscripts), num_outputs);
424+
425+
num_inputs = tx->wtx->num_inputs;
426+
tal_resize(&tx->input_amounts, num_inputs);
427+
}
428+
411429
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor,
412430
size_t *max)
413431
{

bitcoin/tx.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ void bitcoin_tx_input_get_txid(const struct bitcoin_tx *tx, int innum,
156156
*/
157157
bool bitcoin_tx_check(const struct bitcoin_tx *tx);
158158

159+
160+
/**
161+
* Finalize a transaction by truncating overallocated and temporary
162+
* fields. This includes adding a fee output for elements transactions or
163+
* adjusting an existing fee output, and resizing metadata arrays for inputs
164+
* and outputs.
165+
*/
166+
void bitcoin_tx_finalize(struct bitcoin_tx *tx);
167+
159168
/**
160169
* Add an explicit fee output if necessary.
161170
*

channeld/commit_tx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
305305
u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
306306
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL);
307307

308-
elements_tx_add_fee_output(tx);
309-
tal_resize(&(tx->output_witscripts), tx->wtx->num_outputs);
308+
bitcoin_tx_finalize(tx);
309+
assert(bitcoin_tx_check(tx));
310310

311311
return tx;
312312
}

common/close_tx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
6161
return tal_free(tx);
6262

6363
permute_outputs(tx, NULL, NULL);
64-
elements_tx_add_fee_output(tx);
6564

65+
bitcoin_tx_finalize(tx);
6666
assert(bitcoin_tx_check(tx));
6767
return tx;
6868
}

common/htlc_tx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
6161
wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay, revocation_pubkey,
6262
local_delayedkey);
6363
bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tx, wscript), amount);
64-
elements_tx_add_fee_output(tx);
64+
65+
bitcoin_tx_finalize(tx);
66+
assert(bitcoin_tx_check(tx));
6567

6668
tal_free(wscript);
6769

common/initial_commit_tx.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
241241
sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
242242
bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL);
243243

244-
elements_tx_add_fee_output(tx);
245-
tal_resize(&(tx->output_witscripts), tx->wtx->num_outputs);
246-
244+
bitcoin_tx_finalize(tx);
247245
assert(bitcoin_tx_check(tx));
248246

249247
return tx;

common/withdraw_tx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
5252
*change_outnum = -1;
5353

5454
permute_inputs(tx, (const void **)utxos);
55-
elements_tx_add_fee_output(tx);
55+
56+
bitcoin_tx_finalize(tx);
5657
assert(bitcoin_tx_check(tx));
5758
return tx;
5859
}

0 commit comments

Comments
 (0)