From e3debe5adb878fa3acbf0dcda1ec68e1360f40e9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 20 Jun 2017 15:44:03 +0930 Subject: [PATCH] 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 --- lightningd/pay.c | 22 ++++++++---------- lightningd/peer_htlcs.c | 51 ++++++++++++++++++++++------------------- lightningd/peer_htlcs.h | 11 +++++---- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/lightningd/pay.c b/lightningd/pay.c index 7b399ca5b..04acd4d16 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -163,6 +163,7 @@ static void json_sendpay(struct command *cmd, u64 amount, lastamount; struct onionpacket *packet; struct secret *path_secrets; + enum onion_type failcode; if (!json_get_params(buffer, params, "route", &routetok, @@ -307,17 +308,6 @@ static void json_sendpay(struct command *cmd, 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)); /* 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, 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. */ 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 = { diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index b0cbe79b2..431e516b9 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -359,15 +359,28 @@ static bool rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds, return true; } -struct htlc_out *send_htlc_out(struct peer *out, u64 amount, u32 cltv, - const struct sha256 *payment_hash, - const u8 *onion_routing_packet, - struct htlc_in *in, - struct pay_command *pc) +enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv, + const struct sha256 *payment_hash, + const u8 *onion_routing_packet, + struct htlc_in *in, + struct pay_command *pc, + struct htlc_out **houtp) { struct htlc_out *hout; 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. */ hout = new_htlc_out(out->owner, out, amount, cltv, 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, onion_routing_packet); 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, @@ -397,20 +413,6 @@ static void forward_htlc(struct htlc_in *hin, 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: * * The node creating `channel_update` SHOULD accept HTLCs which pay a @@ -456,10 +458,11 @@ static void forward_htlc(struct htlc_in *hin, goto fail; } - send_htlc_out(next, amt_to_forward, - outgoing_cltv_value, &hin->payment_hash, - next_onion, hin, NULL); - return; + failcode = send_htlc_out(next, amt_to_forward, + outgoing_cltv_value, &hin->payment_hash, + next_onion, hin, NULL, NULL); + if (!failcode) + return; fail: fail_htlc(hin, failcode); diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index bb44ff6fd..0578324a8 100644 --- a/lightningd/peer_htlcs.h +++ b/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_revoke(struct peer *peer, const u8 *msg); -struct htlc_out *send_htlc_out(struct peer *out, u64 amount, u32 cltv, - const struct sha256 *payment_hash, - const u8 *onion_routing_packet, - struct htlc_in *in, - struct pay_command *pc); +enum onion_type send_htlc_out(struct peer *out, u64 amount, u32 cltv, + const struct sha256 *payment_hash, + const u8 *onion_routing_packet, + struct htlc_in *in, + struct pay_command *pc, + struct htlc_out **houtp); #endif /* LIGHTNING_LIGHTNINGD_PEER_HTLCS_H */