Skip to content

Commit

Permalink
fundpsbt: add reserve arg.
Browse files Browse the repository at this point in the history
It's easier for us to call it atomically than have the user loop and
retry!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jul 8, 2020
1 parent 59f839c commit cd65293
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions wallet/reservation.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ static void json_add_reservestatus(struct json_stream *response,
json_object_end(response);
}

/* Reserve these UTXOs and print to JSON */
static void reserve_and_report(struct json_stream *response,
struct wallet *wallet,
u32 current_height,
struct utxo **utxos)
{
json_array_start(response, "reservations");
for (size_t i = 0; i < tal_count(utxos); i++) {
enum output_status oldstatus;
u32 old_res;

oldstatus = utxos[i]->status;
old_res = utxos[i]->reserved_til ? *utxos[i]->reserved_til : 0;

if (!wallet_reserve_utxo(wallet,
utxos[i],
current_height)) {
fatal("Unable to reserve %s:%u!",
type_to_string(tmpctx,
struct bitcoin_txid,
&utxos[i]->txid),
utxos[i]->outnum);
}
json_add_reservestatus(response, utxos[i], oldstatus, old_res,
current_height);
}
json_array_end(response);
}

static struct command_result *json_reserveinputs(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
Expand Down Expand Up @@ -83,27 +112,7 @@ static struct command_result *json_reserveinputs(struct command *cmd,
}

response = json_stream_success(cmd);
json_array_start(response, "reservations");
for (size_t i = 0; i < tal_count(utxos); i++) {
enum output_status oldstatus;
u32 old_res;

oldstatus = utxos[i]->status;
old_res = utxos[i]->reserved_til ? *utxos[i]->reserved_til : 0;

if (!wallet_reserve_utxo(cmd->ld->wallet,
utxos[i],
current_height)) {
fatal("Unable to reserve %s:%u!",
type_to_string(tmpctx,
struct bitcoin_txid,
&utxos[i]->txid),
utxos[i]->outnum);
}
json_add_reservestatus(response, utxos[i], oldstatus, old_res,
current_height);
}
json_array_end(response);
reserve_and_report(response, cmd->ld->wallet, current_height, utxos);
return command_success(cmd, response);
}

Expand Down Expand Up @@ -173,27 +182,30 @@ static struct command_result *json_fundpsbt(struct command *cmd,
const jsmntok_t *params)
{
struct json_stream *response;
const struct utxo **utxos;
struct utxo **utxos;
u32 *feerate_per_kw;
u32 *minconf;
struct amount_sat *amount, input, needed, excess;
bool all;
u32 locktime, maxheight;
bool all, *reserve;
u32 locktime, maxheight, current_height;
struct bitcoin_tx *tx;

if (!param(cmd, buffer, params,
p_req("satoshi", param_sat_or_all, &amount),
p_req("feerate", param_feerate_val, &feerate_per_kw),
p_opt_def("minconf", param_number, &minconf, 1),
p_opt_def("reserve", param_bool, &reserve, true),
NULL))
return command_param_failed();

all = amount_sat_eq(*amount, AMOUNT_SAT(-1ULL));
maxheight = minconf_to_maxheight(*minconf, cmd->ld);

current_height = get_block_height(cmd->ld->topology);

/* We keep adding until we meet their output requirements. */
input = AMOUNT_SAT(0);
utxos = tal_arr(cmd, const struct utxo *, 0);
utxos = tal_arr(cmd, struct utxo *, 0);
while (amount_sat_sub(&needed, *amount, input)) {
struct utxo *utxo;

Expand All @@ -202,7 +214,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
&needed,
*feerate_per_kw,
maxheight,
utxos);
cast_const2(const struct utxo **, utxos));
if (utxo) {
struct amount_sat fee;
tal_arr_expand(&utxos, utxo);
Expand Down Expand Up @@ -246,15 +258,16 @@ static struct command_result *json_fundpsbt(struct command *cmd,
* 0xFFFFFFFE by default. Other wallets are likely to implement
* this too).
*/
locktime = cmd->ld->topology->tip->height;
locktime = current_height;

/* Eventually fuzz it too. */
if (locktime > 100 && pseudorand(10) == 0)
locktime -= pseudorand(100);

/* FIXME: tx_spending_utxos does more than we need, but there
* are other users right now. */
tx = tx_spending_utxos(cmd, chainparams, utxos,
tx = tx_spending_utxos(cmd, chainparams,
cast_const2(const struct utxo **, utxos),
cmd->ld->wallet->bip32_base,
false, 0, locktime,
BITCOIN_TX_RBF_SEQUENCE);
Expand All @@ -266,6 +279,9 @@ static struct command_result *json_fundpsbt(struct command *cmd,
response = json_stream_success(cmd);
json_add_psbt(response, "psbt", tx->psbt);
json_add_amount_sat_only(response, "excess_msat", excess);
if (*reserve)
reserve_and_report(response, cmd->ld->wallet, current_height,
utxos);
return command_success(cmd, response);
}

Expand Down

0 comments on commit cd65293

Please sign in to comment.