Browse Source

psbt: add transaction inputs to the psbt struct

Make sure that we permute them also!

Fixes weird spacing also
nifty/pset-pre
niftynei 4 years ago
committed by Rusty Russell
parent
commit
cf9de86dba
  1. 5
      bitcoin/test/run-bitcoin_block_from_hex.c
  2. 5
      bitcoin/test/run-tx-encode.c
  3. 1
      bitcoin/tx.c
  4. 46
      common/permute_tx.c

5
bitcoin/test/run-bitcoin_block_from_hex.c

@ -42,6 +42,11 @@ u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
/* Generated stub for fromwire_u16 */
u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); }
/* Generated stub for psbt_add_input */
struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt UNNEEDED,
struct wally_tx_input *input UNNEEDED,
size_t insert_at UNNEEDED)
{ fprintf(stderr, "psbt_add_input called!\n"); abort(); }
/* Generated stub for psbt_add_output */
struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt UNNEEDED,
struct wally_tx_output *output UNNEEDED,

5
bitcoin/test/run-tx-encode.c

@ -43,6 +43,11 @@ u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
/* Generated stub for fromwire_u16 */
u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u16 called!\n"); abort(); }
/* Generated stub for psbt_add_input */
struct wally_psbt_input *psbt_add_input(struct wally_psbt *psbt UNNEEDED,
struct wally_tx_input *input UNNEEDED,
size_t insert_at UNNEEDED)
{ fprintf(stderr, "psbt_add_input called!\n"); abort(); }
/* Generated stub for psbt_add_output */
struct wally_psbt_output *psbt_add_output(struct wally_psbt *psbt UNNEEDED,
struct wally_tx_output *output UNNEEDED,

1
bitcoin/tx.c

@ -168,6 +168,7 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid,
NULL /* Empty witness stack */, &input);
input->features = chainparams->is_elements ? WALLY_TX_IS_ELEMENTS : 0;
wally_tx_add_input(tx->wtx, input);
psbt_add_input(tx->psbt, input, i);
wally_tx_input_free(input);
/* Now store the input amount if we know it, so we can sign later */

46
common/permute_tx.c

@ -36,24 +36,37 @@ static size_t find_best_in(struct wally_tx_input *inputs, size_t num)
}
static void swap_wally_inputs(struct wally_tx_input *inputs,
const void **map,
size_t i1, size_t i2)
struct wally_tx_input *psbt_global_ins,
struct wally_psbt_input *psbt_ins,
const void **map,
size_t i1, size_t i2)
{
struct wally_tx_input tmpinput;
const void *tmp;
struct wally_tx_input tmpinput;
struct wally_psbt_input tmppsbtin;
const void *tmp;
if (i1 == i2)
return;
if (i1 == i2)
return;
tmpinput = inputs[i1];
inputs[i1] = inputs[i2];
inputs[i2] = tmpinput;
/* For the PSBT, we swap the psbt inputs and
* the global tx's inputs */
tmpinput = psbt_global_ins[i1];
psbt_global_ins[i1] = psbt_global_ins[i2];
psbt_global_ins[i2] = tmpinput;
tmpinput = inputs[i1];
inputs[i1] = inputs[i2];
inputs[i2] = tmpinput;
tmppsbtin = psbt_ins[i1];
psbt_ins[i1] = psbt_ins[i2];
psbt_ins[i2] = tmppsbtin;
if (map) {
tmp = map[i1];
map[i1] = map[i2];
map[i2] = tmp;
}
if (map) {
tmp = map[i1];
map[i1] = map[i2];
map[i2] = tmp;
}
}
static void swap_input_amounts(struct amount_sat **amounts, size_t i1,
@ -78,7 +91,10 @@ void permute_inputs(struct bitcoin_tx *tx, const void **map)
for (i = 0; i < num_inputs-1; i++) {
best_pos = i + find_best_in(inputs + i, num_inputs - i);
/* Swap best into first place. */
swap_wally_inputs(tx->wtx->inputs, map, i, best_pos);
swap_wally_inputs(tx->wtx->inputs,
tx->psbt->tx->inputs,
tx->psbt->inputs,
map, i, best_pos);
swap_input_amounts(tx->input_amounts, i, best_pos);
}
}

Loading…
Cancel
Save