diff --git a/lightningd/commit_tx.c b/lightningd/commit_tx.c index dbd79adf1..7f8358466 100644 --- a/lightningd/commit_tx.c +++ b/lightningd/commit_tx.c @@ -318,7 +318,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * 7. Sort the outputs into [BIP 69 * order](#transaction-input-and-output-ordering) */ - permute_outputs(tx->output, tal_count(tx->output), *htlcmap); + permute_outputs(tx->output, tal_count(tx->output), + (const void **)*htlcmap); /* BOLT #3: * diff --git a/permute_tx.c b/permute_tx.c index 5c06c2274..6f2e19508 100644 --- a/permute_tx.c +++ b/permute_tx.c @@ -34,42 +34,51 @@ static size_t find_best_in(struct bitcoin_tx_input *inputs, size_t num) } static void swap_inputs(struct bitcoin_tx_input *inputs, + const void **map, size_t i1, size_t i2) { struct bitcoin_tx_input tmpinput; + const void *tmp; tmpinput = inputs[i1]; inputs[i1] = inputs[i2]; inputs[i2] = tmpinput; + + if (map) { + tmp = map[i1]; + map[i1] = map[i2]; + map[i2] = tmp; + } } -void permute_inputs(struct bitcoin_tx_input *inputs, size_t num_inputs) +void permute_inputs(struct bitcoin_tx_input *inputs, size_t num_inputs, + const void **map) { size_t i; /* Now do a dumb sort (num_inputs is small). */ for (i = 0; i < num_inputs; i++) { /* Swap best into first place. */ - swap_inputs(inputs, + swap_inputs(inputs, map, i, i + find_best_in(inputs + i, num_inputs - i)); } } static void swap_outputs(struct bitcoin_tx_output *outputs, - const struct htlc **htlcmap, + const void **map, size_t i1, size_t i2) { struct bitcoin_tx_output tmpoutput; - const struct htlc *tmphtlc; + const void *tmp; tmpoutput = outputs[i1]; outputs[i1] = outputs[i2]; outputs[i2] = tmpoutput; - if (htlcmap) { - tmphtlc = htlcmap[i1]; - htlcmap[i1] = htlcmap[i2]; - htlcmap[i2] = tmphtlc; + if (map) { + tmp = map[i1]; + map[i1] = map[i2]; + map[i2] = tmp; } } @@ -106,16 +115,15 @@ static size_t find_best_out(struct bitcoin_tx_output *outputs, size_t num) return best; } -void permute_outputs(struct bitcoin_tx_output *outputs, - size_t num_outputs, - const struct htlc **htlcmap) +void permute_outputs(struct bitcoin_tx_output *outputs, size_t num_outputs, + const void **map) { size_t i; /* Now do a dumb sort (num_outputs is small). */ for (i = 0; i < num_outputs; i++) { /* Swap best into first place. */ - swap_outputs(outputs, htlcmap, + swap_outputs(outputs, map, i, i + find_best_out(outputs + i, num_outputs - i)); } } diff --git a/permute_tx.h b/permute_tx.h index 30416d5f2..32d0aa047 100644 --- a/permute_tx.h +++ b/permute_tx.h @@ -6,13 +6,14 @@ struct htlc; /* Permute the transaction into BIP69 order. */ -void permute_inputs(struct bitcoin_tx_input *inputs, size_t num_inputs); +void permute_inputs(struct bitcoin_tx_input *inputs, size_t num_inputs, + const void **map); -/* If @htlcmap is non-NULL, it will be permuted the same as the outputs. +/* If @map is non-NULL, it will be permuted the same as the outputs. * - * So the caller initiates the htlcsmap with which htlcs are used, it - * can easily see which htlc (if any) is in output #0 with htlcmap[0]. + * So the caller initiates the map with which htlcs are used, it + * can easily see which htlc (if any) is in output #0 with map[0]. */ void permute_outputs(struct bitcoin_tx_output *outputs, size_t num_outputs, - const struct htlc **htlcmap); + const void **map); #endif /* LIGHTNING_PERMUTE_TX_H */