Browse Source

permute_tx: generic pointer map.

Turns out we want to permute transactions for the wallet too, so we
use void ** rather than assume we're shuffling htlc ** (and do inputs,
too!).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
08e95d59b5
  1. 3
      lightningd/commit_tx.c
  2. 32
      permute_tx.c
  3. 11
      permute_tx.h

3
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:
*

32
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));
}
}

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

Loading…
Cancel
Save