Browse Source

lightningd: move common "can I send HTLC" checks into send_htlc_out.

The pay.c ones were out of date, so unify.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
e3debe5adb
  1. 22
      lightningd/pay.c
  2. 41
      lightningd/peer_htlcs.c
  3. 5
      lightningd/peer_htlcs.h

22
lightningd/pay.c

@ -163,6 +163,7 @@ static void json_sendpay(struct command *cmd,
u64 amount, lastamount; u64 amount, lastamount;
struct onionpacket *packet; struct onionpacket *packet;
struct secret *path_secrets; struct secret *path_secrets;
enum onion_type failcode;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"route", &routetok, "route", &routetok,
@ -307,17 +308,6 @@ static void json_sendpay(struct command *cmd,
return; return;
} }
if (!peer->scid) {
command_fail(cmd, "first peer channel not locked");
return;
}
if (!peer->owner || !streq(peer->owner->name, "lightningd_channel")) {
command_fail(cmd, "first peer in %s",
peer->owner ? peer->owner->name : "limbo");
return;
}
randombytes_buf(&sessionkey, sizeof(sessionkey)); randombytes_buf(&sessionkey, sizeof(sessionkey));
/* Onion will carry us from first peer onwards. */ /* Onion will carry us from first peer onwards. */
@ -341,11 +331,17 @@ static void json_sendpay(struct command *cmd,
log_info(ld->log, "Sending %"PRIu64" over %zu hops to deliver %"PRIu64, log_info(ld->log, "Sending %"PRIu64" over %zu hops to deliver %"PRIu64,
amount, n_hops, lastamount); amount, n_hops, lastamount);
pc->out = send_htlc_out(peer, amount, first_hop_data.outgoing_cltv,
&rhash, onion, NULL, pc);
/* Wait until we get response. */ /* Wait until we get response. */
tal_add_destructor2(cmd, remove_cmd_from_pc, pc); tal_add_destructor2(cmd, remove_cmd_from_pc, pc);
failcode = send_htlc_out(peer, amount, first_hop_data.outgoing_cltv,
&rhash, onion, NULL, pc, &pc->out);
if (failcode) {
command_fail(cmd, "first peer not ready: %s",
onion_type_name(failcode));
return;
}
} }
static const struct json_command sendpay_command = { static const struct json_command sendpay_command = {

41
lightningd/peer_htlcs.c

@ -359,15 +359,28 @@ static bool rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds,
return true; return true;
} }
struct htlc_out *send_htlc_out(struct peer *out, u64 amount, u32 cltv, enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
const u8 *onion_routing_packet, const u8 *onion_routing_packet,
struct htlc_in *in, struct htlc_in *in,
struct pay_command *pc) struct pay_command *pc,
struct htlc_out **houtp)
{ {
struct htlc_out *hout; struct htlc_out *hout;
u8 *msg; u8 *msg;
if (!peer_can_add_htlc(out)) {
log_info(out->log, "Attempt to send HTLC but not ready (%s)",
peer_state_name(out->state));
return WIRE_UNKNOWN_NEXT_PEER;
}
if (!out->owner) {
log_info(out->log, "Attempt to send HTLC but unowned (%s)",
peer_state_name(out->state));
return WIRE_TEMPORARY_CHANNEL_FAILURE;
}
/* Make peer's daemon own it, catch if it dies. */ /* Make peer's daemon own it, catch if it dies. */
hout = new_htlc_out(out->owner, out, amount, cltv, hout = new_htlc_out(out->owner, out, amount, cltv,
payment_hash, onion_routing_packet, in, pc); payment_hash, onion_routing_packet, in, pc);
@ -376,7 +389,10 @@ struct htlc_out *send_htlc_out(struct peer *out, u64 amount, u32 cltv,
msg = towire_channel_offer_htlc(out, amount, cltv, payment_hash, msg = towire_channel_offer_htlc(out, amount, cltv, payment_hash,
onion_routing_packet); onion_routing_packet);
subd_req(out->ld, out->owner, take(msg), -1, 0, rcvd_htlc_reply, hout); subd_req(out->ld, out->owner, take(msg), -1, 0, rcvd_htlc_reply, hout);
return hout;
if (houtp)
*houtp = hout;
return 0;
} }
static void forward_htlc(struct htlc_in *hin, static void forward_htlc(struct htlc_in *hin,
@ -397,20 +413,6 @@ static void forward_htlc(struct htlc_in *hin,
goto fail; goto fail;
} }
if (!peer_can_add_htlc(next)) {
log_info(next->log, "Attempt to forward HTLC but not ready (%s)",
peer_state_name(next->state));
failcode = WIRE_UNKNOWN_NEXT_PEER;
goto fail;
}
if (!next->owner) {
log_info(next->log, "Attempt to forward HTLC but unowned (%s)",
peer_state_name(next->state));
failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
goto fail;
}
/* BOLT #7: /* BOLT #7:
* *
* The node creating `channel_update` SHOULD accept HTLCs which pay a * The node creating `channel_update` SHOULD accept HTLCs which pay a
@ -456,9 +458,10 @@ static void forward_htlc(struct htlc_in *hin,
goto fail; goto fail;
} }
send_htlc_out(next, amt_to_forward, failcode = send_htlc_out(next, amt_to_forward,
outgoing_cltv_value, &hin->payment_hash, outgoing_cltv_value, &hin->payment_hash,
next_onion, hin, NULL); next_onion, hin, NULL, NULL);
if (!failcode)
return; return;
fail: fail:

5
lightningd/peer_htlcs.h

@ -31,9 +31,10 @@ int peer_sending_commitsig(struct peer *peer, const u8 *msg);
int peer_got_commitsig(struct peer *peer, const u8 *msg); int peer_got_commitsig(struct peer *peer, const u8 *msg);
int peer_got_revoke(struct peer *peer, const u8 *msg); int peer_got_revoke(struct peer *peer, const u8 *msg);
struct htlc_out *send_htlc_out(struct peer *out, u64 amount, u32 cltv, enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
const u8 *onion_routing_packet, const u8 *onion_routing_packet,
struct htlc_in *in, struct htlc_in *in,
struct pay_command *pc); struct pay_command *pc,
struct htlc_out **houtp);
#endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */ #endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */

Loading…
Cancel
Save