2016-01-21 21:08:08 +01:00
|
|
|
#include "permute_tx.h"
|
2020-05-21 03:57:00 +02:00
|
|
|
#include <wally_psbt.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
static bool input_better(const struct wally_tx_input *a,
|
|
|
|
const struct wally_tx_input *b)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
|
|
|
int cmp;
|
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
cmp = memcmp(a->txhash, b->txhash, sizeof(a->txhash));
|
2015-05-28 23:38:27 +02:00
|
|
|
if (cmp != 0)
|
|
|
|
return cmp < 0;
|
|
|
|
if (a->index != b->index)
|
|
|
|
return a->index < b->index;
|
|
|
|
|
|
|
|
/* These shouldn't happen, but let's get a canonical order anyway. */
|
2019-03-22 20:59:24 +01:00
|
|
|
if (a->script_len != b->script_len)
|
|
|
|
return a->script_len < b->script_len;
|
|
|
|
|
|
|
|
cmp = memcmp(a->script, b->script, a->script_len);
|
2015-05-28 23:38:27 +02:00
|
|
|
if (cmp != 0)
|
|
|
|
return cmp < 0;
|
2019-03-22 20:59:24 +01:00
|
|
|
return a->sequence < b->sequence;
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
static size_t find_best_in(struct wally_tx_input *inputs, size_t num)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
|
|
|
size_t i, best = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < num; i++) {
|
|
|
|
if (input_better(&inputs[i], &inputs[best]))
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:47:13 +01:00
|
|
|
static void swap_wally_inputs(struct wally_tx_input *inputs,
|
2020-05-21 03:57:00 +02:00
|
|
|
struct wally_tx_input *psbt_global_ins,
|
|
|
|
struct wally_psbt_input *psbt_ins,
|
|
|
|
const void **map,
|
|
|
|
size_t i1, size_t i2)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
2020-05-21 03:57:00 +02:00
|
|
|
struct wally_tx_input tmpinput;
|
|
|
|
struct wally_psbt_input tmppsbtin;
|
|
|
|
const void *tmp;
|
2019-03-14 17:47:13 +01:00
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
if (i1 == i2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tmpinput = inputs[i1];
|
|
|
|
inputs[i1] = inputs[i2];
|
|
|
|
inputs[i2] = tmpinput;
|
2019-03-14 17:47:13 +01:00
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
/* 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;
|
2019-03-14 17:47:13 +01:00
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-03-14 17:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void permute_inputs(struct bitcoin_tx *tx, const void **map)
|
|
|
|
{
|
|
|
|
size_t i, best_pos;
|
2019-03-22 20:59:24 +01:00
|
|
|
struct wally_tx_input *inputs = tx->wtx->inputs;
|
|
|
|
size_t num_inputs = tx->wtx->num_inputs;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2018-01-15 00:57:19 +01:00
|
|
|
/* We can't permute nothing! */
|
|
|
|
if (num_inputs == 0)
|
|
|
|
return;
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
/* Now do a dumb sort (num_inputs is small). */
|
2018-01-02 18:29:07 +01:00
|
|
|
for (i = 0; i < num_inputs-1; i++) {
|
2019-03-14 17:47:13 +01:00
|
|
|
best_pos = i + find_best_in(inputs + i, num_inputs - i);
|
2015-05-28 23:38:27 +02:00
|
|
|
/* Swap best into first place. */
|
2020-05-21 03:57:00 +02:00
|
|
|
swap_wally_inputs(tx->wtx->inputs,
|
|
|
|
tx->psbt->tx->inputs,
|
|
|
|
tx->psbt->inputs,
|
|
|
|
map, i, best_pos);
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:47:13 +01:00
|
|
|
static void swap_wally_outputs(struct wally_tx_output *outputs,
|
2020-05-21 03:57:00 +02:00
|
|
|
struct wally_tx_output *psbt_global_outs,
|
|
|
|
struct wally_psbt_output *psbt_outs,
|
|
|
|
const void **map, u32 *cltvs,
|
|
|
|
size_t i1, size_t i2)
|
2019-03-14 17:47:13 +01:00
|
|
|
{
|
|
|
|
struct wally_tx_output tmpoutput;
|
2020-05-21 03:57:00 +02:00
|
|
|
struct wally_psbt_output tmppsbtout;
|
2019-03-14 17:47:13 +01:00
|
|
|
|
|
|
|
if (i1 == i2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tmpoutput = outputs[i1];
|
|
|
|
outputs[i1] = outputs[i2];
|
|
|
|
outputs[i2] = tmpoutput;
|
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
/* For the PSBT, we swap the psbt outputs and
|
|
|
|
* the global tx's outputs */
|
|
|
|
tmpoutput = psbt_global_outs[i1];
|
|
|
|
psbt_global_outs[i1] = psbt_global_outs[i2];
|
|
|
|
psbt_global_outs[i2] = tmpoutput;
|
|
|
|
|
|
|
|
tmppsbtout = psbt_outs[i1];
|
|
|
|
psbt_outs[i1] = psbt_outs[i2];
|
|
|
|
psbt_outs[i2] = tmppsbtout;
|
|
|
|
|
2019-03-14 17:47:13 +01:00
|
|
|
if (map) {
|
|
|
|
const void *tmp = map[i1];
|
|
|
|
map[i1] = map[i2];
|
|
|
|
map[i2] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cltvs) {
|
|
|
|
u32 tmp = cltvs[i1];
|
|
|
|
cltvs[i1] = cltvs[i2];
|
|
|
|
cltvs[i2] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
static bool output_better(const struct wally_tx_output *a, u32 cltv_a,
|
|
|
|
const struct wally_tx_output *b, u32 cltv_b)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
2018-10-22 06:21:05 +02:00
|
|
|
size_t len, lena, lenb;
|
2015-07-20 02:26:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
if (a->satoshi != b->satoshi)
|
|
|
|
return a->satoshi < b->satoshi;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2017-12-07 23:59:39 +01:00
|
|
|
/* Lexicographical sort. */
|
2019-03-22 20:59:24 +01:00
|
|
|
lena = a->script_len;
|
|
|
|
lenb = b->script_len;
|
2018-10-22 06:21:05 +02:00
|
|
|
if (lena < lenb)
|
|
|
|
len = lena;
|
2015-07-20 02:26:15 +02:00
|
|
|
else
|
2018-10-22 06:21:05 +02:00
|
|
|
len = lenb;
|
2015-07-20 02:26:15 +02:00
|
|
|
|
|
|
|
ret = memcmp(a->script, b->script, len);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret < 0;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2018-10-22 06:21:05 +02:00
|
|
|
if (lena != lenb)
|
|
|
|
return lena < lenb;
|
|
|
|
|
|
|
|
return cltv_a < cltv_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 cltv_of(const u32 *cltvs, size_t idx)
|
|
|
|
{
|
|
|
|
if (!cltvs)
|
|
|
|
return 0;
|
|
|
|
return cltvs[idx];
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:59:24 +01:00
|
|
|
static size_t find_best_out(struct wally_tx_output *outputs, const u32 *cltvs,
|
2018-10-22 06:21:05 +02:00
|
|
|
size_t num)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
|
|
|
size_t i, best = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < num; i++) {
|
2018-10-22 06:21:05 +02:00
|
|
|
if (output_better(&outputs[i], cltv_of(cltvs, i),
|
|
|
|
&outputs[best], cltv_of(cltvs, best)))
|
2015-05-28 23:38:27 +02:00
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:47:13 +01:00
|
|
|
void permute_outputs(struct bitcoin_tx *tx, u32 *cltvs, const void **map)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
2019-03-14 17:47:13 +01:00
|
|
|
size_t i, best_pos;
|
2019-03-22 20:59:24 +01:00
|
|
|
struct wally_tx_output *outputs = tx->wtx->outputs;
|
|
|
|
size_t num_outputs = tx->wtx->num_outputs;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2018-01-15 00:57:19 +01:00
|
|
|
/* We can't permute nothing! */
|
|
|
|
if (num_outputs == 0)
|
|
|
|
return;
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
/* Now do a dumb sort (num_outputs is small). */
|
2019-03-22 20:59:24 +01:00
|
|
|
for (i = 0; i < num_outputs - 1; i++) {
|
2019-03-14 17:47:13 +01:00
|
|
|
best_pos =
|
|
|
|
i + find_best_out(outputs + i, cltvs ? cltvs + i : NULL,
|
|
|
|
num_outputs - i);
|
|
|
|
|
2015-05-28 23:38:27 +02:00
|
|
|
/* Swap best into first place. */
|
2020-05-21 03:57:00 +02:00
|
|
|
swap_wally_outputs(tx->wtx->outputs,
|
|
|
|
tx->psbt->tx->outputs,
|
|
|
|
tx->psbt->outputs,
|
|
|
|
map, cltvs, i, best_pos);
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
}
|