Skip to content

Commit

Permalink
lightningd: move cmd pointer from struct pay_command directly into ht…
Browse files Browse the repository at this point in the history
…lc_out.

Set if a command is responsible.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jan 17, 2018
1 parent 559010f commit 0f8f273
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
6 changes: 5 additions & 1 deletion lightningd/htlc_end.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ struct htlc_out *htlc_out_check(const struct htlc_out *hout,
htlc_state_name(hout->hstate));
else if (hout->failuremsg && hout->preimage)
return corrupt(hout, abortstr, "Both failed and succeeded");
else if (hout->cmd && hout->in)
return corrupt(hout, abortstr, "Both local and forwarded");
else if (!hout->in && !hout->pay_command)
return corrupt(hout, abortstr,
"Neither hout->in nor paycommand");
Expand All @@ -142,7 +144,8 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
const u8 *onion_routing_packet,
struct htlc_in *in,
struct pay_command *pc,
struct wallet_payment *payment)
struct wallet_payment *payment,
struct command *cmd)
{
struct htlc_out *hout = tal(ctx, struct htlc_out);

Expand All @@ -155,6 +158,7 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
hout->cltv_expiry = cltv_expiry;
hout->payment_hash = *payment_hash;
hout->payment = tal_steal(hout, payment);
hout->cmd = cmd;
memcpy(hout->onion_routing_packet, onion_routing_packet,
sizeof(hout->onion_routing_packet));

Expand Down
6 changes: 5 additions & 1 deletion lightningd/htlc_end.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ struct htlc_out {
/* Where it's from, if not going to us. */
struct htlc_in *in;

/* Otherwise, this MAY be non-null if there's a pay command waiting */
struct command *cmd;

/* Otherwise, payment command which created it. */
struct pay_command *pay_command;

Expand Down Expand Up @@ -137,7 +140,8 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
const u8 *onion_routing_packet,
struct htlc_in *in,
struct pay_command *pc,
struct wallet_payment *payment);
struct wallet_payment *payment,
struct command *cmd);

void connect_htlc_in(struct htlc_in_map *map, struct htlc_in *hin);
void connect_htlc_out(struct htlc_out_map *map, struct htlc_out *hout);
Expand Down
45 changes: 21 additions & 24 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct pay_command {
struct htlc_out *out;
/* Preimage if this succeeded. */
const struct preimage *rval;
struct command *cmd;
};

static void json_pay_success(struct command *cmd, const struct preimage *rval)
Expand All @@ -45,20 +44,17 @@ static void json_pay_success(struct command *cmd, const struct preimage *rval)
command_success(cmd, response);
}

static void json_pay_failed(struct pay_command *pc,
static void json_pay_failed(struct command *cmd,
const struct pubkey *sender,
enum onion_type failure_code,
const char *details)
{
/* Can be NULL if JSON RPC goes away. */
if (!pc->cmd)
return;

/* FIXME: Report sender! */
command_fail(pc->cmd, "failed: %s (%s)",
onion_type_name(failure_code), details);

pc->out = NULL;
if (cmd) {
/* FIXME: Report sender! */
command_fail(cmd, "failed: %s (%s)",
onion_type_name(failure_code), details);
}
}

void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
Expand All @@ -69,7 +65,9 @@ void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
PAYMENT_COMPLETE, rval);
hout->pay_command->rval = tal_dup(hout->pay_command,
struct preimage, rval);
json_pay_success(hout->pay_command->cmd, rval);
/* Can be NULL if JSON RPC goes away. */
if (hout->cmd)
json_pay_success(hout->cmd, rval);
hout->pay_command->out = NULL;
}

Expand All @@ -84,10 +82,11 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,

wallet_payment_set_status(ld->wallet, &hout->payment_hash,
PAYMENT_FAILED, NULL);
pc->out = NULL;

/* This gives more details than a generic failure message */
if (localfail) {
json_pay_failed(pc, NULL, hout->failcode, localfail);
json_pay_failed(hout->cmd, NULL, hout->failcode, localfail);
tal_free(tmpctx);
return;
}
Expand Down Expand Up @@ -118,17 +117,15 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
/* FIXME: check for routing failure / perm fail. */
/* check_for_routing_failure(i, sender, failure_code); */

json_pay_failed(pc, NULL, failcode, "reply from remote");
json_pay_failed(hout->cmd, NULL, failcode, "reply from remote");
tal_free(tmpctx);
}

/* When JSON RPC goes away, cmd is freed: detach from any running paycommand */
static void remove_cmd_from_pc(struct command *cmd, struct pay_command *pc)
/* When JSON RPC goes away, cmd is freed: detach from the hout */
static void remove_cmd_from_hout(struct command *cmd, struct htlc_out *hout)
{
/* This can be false, in the case where another pay command
* re-uses the pc->cmd before we get around to cleaning up. */
if (pc->cmd == cmd)
pc->cmd = NULL;
assert(hout->cmd == cmd);
hout->cmd = NULL;
}

static struct pay_command *find_pay_command(struct lightningd *ld,
Expand Down Expand Up @@ -254,7 +251,6 @@ static bool send_payment(struct command *cmd,
payment->payment_preimage = NULL;
payment->path_secrets = tal_steal(payment, path_secrets);
}
pc->cmd = cmd;
pc->rhash = *rhash;
pc->rval = NULL;
pc->ids = tal_steal(pc, ids);
Expand All @@ -264,23 +260,24 @@ static bool send_payment(struct command *cmd,
log_info(cmd->ld->log, "Sending %u over %zu hops to deliver %"PRIu64,
route[0].amount, n_hops, pc->msatoshi);

/* Wait until we get response. */
tal_add_destructor2(cmd, remove_cmd_from_pc, pc);

/* They're both children of ld, but on shutdown make sure we
* destroy the command before the pc, otherwise the
* remove_cmd_from_pc destructor causes a use-after-free */
tal_steal(pc, cmd);

failcode = send_htlc_out(peer, route[0].amount,
base_expiry + route[0].delay,
rhash, onion, NULL, payment, pc,
rhash, onion, NULL, payment, pc, cmd,
&pc->out);
if (failcode) {
command_fail(cmd, "first peer not ready: %s",
onion_type_name(failcode));
return false;
}

/* If we fail, remove cmd ptr from htlc_out. */
tal_add_destructor2(cmd, remove_cmd_from_hout, pc->out);

tal_free(tmpctx);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv,
struct htlc_in *in,
struct wallet_payment *payment,
struct pay_command *pc,
struct command *cmd,
struct htlc_out **houtp)
{
struct htlc_out *hout;
Expand All @@ -394,7 +395,7 @@ enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv,
/* Make peer's daemon own it, catch if it dies. */
hout = new_htlc_out(out->owner, out, amount, cltv,
payment_hash, onion_routing_packet, in,
pc, payment);
pc, payment, cmd);
tal_add_destructor(hout, hout_subd_died);

msg = towire_channel_offer_htlc(out, amount, cltv, payment_hash,
Expand Down Expand Up @@ -486,7 +487,7 @@ static void forward_htlc(struct htlc_in *hin,

failcode = send_htlc_out(next, amt_to_forward,
outgoing_cltv_value, &hin->payment_hash,
next_onion, hin, NULL, NULL, NULL);
next_onion, hin, NULL, NULL, NULL, NULL);
if (!failcode)
return;

Expand Down
1 change: 1 addition & 0 deletions lightningd/peer_htlcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv,
struct htlc_in *in,
struct wallet_payment *payment,
struct pay_command *pc,
struct command *cmd,
struct htlc_out **houtp);

struct htlc_out *find_htlc_out_by_ripemd(const struct peer *peer,
Expand Down
1 change: 1 addition & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ static bool wallet_stmt2htlc_out(const struct wallet_channel *channel,

out->failuremsg = NULL;
out->failcode = 0;
out->cmd = NULL;

/* Need to defer wiring until we can look up all incoming
* htlcs, will wire using origin_htlc_id */
Expand Down

0 comments on commit 0f8f273

Please sign in to comment.