channeld: return htlc from channel_add_htlc and channel_fail_htlc.

Callers often want to know, and it saves them doing another lookup.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-11-28 15:33:09 +10:30
committed by Christian Decker
parent be35895847
commit fc4a7cf103
4 changed files with 21 additions and 16 deletions

View File

@@ -427,7 +427,7 @@ static struct io_plan *handle_peer_add_htlc(struct io_conn *conn,
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount_msat,
cltv_expiry, &payment_hash,
onion_routing_packet);
onion_routing_packet, NULL);
if (add_err != CHANNEL_ERR_ADD_OK)
peer_failed(io_conn_fd(peer->peer_conn),
&peer->pcs.cs,
@@ -1258,7 +1258,7 @@ static struct io_plan *handle_peer_fail_htlc(struct io_conn *conn,
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
}
e = channel_fail_htlc(peer->channel, LOCAL, id);
e = channel_fail_htlc(peer->channel, LOCAL, id, NULL);
switch (e) {
case CHANNEL_ERR_REMOVE_OK:
/* Save reason for when we tell master. */
@@ -1315,10 +1315,9 @@ static struct io_plan *handle_peer_fail_malformed_htlc(struct io_conn *conn,
failure_code);
}
e = channel_fail_htlc(peer->channel, LOCAL, id);
e = channel_fail_htlc(peer->channel, LOCAL, id, &htlc);
switch (e) {
case CHANNEL_ERR_REMOVE_OK:
htlc = channel_get_htlc(peer->channel, LOCAL, id);
/* FIXME: Do this! */
/* BOLT #2:
*
@@ -1862,7 +1861,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
amount_msat, cltv_expiry, &payment_hash,
onion_routing_packet);
onion_routing_packet, NULL);
status_trace("Adding HTLC %"PRIu64" msat=%"PRIu64" cltv=%u gave %i",
peer->htlc_id, amount_msat, cltv_expiry, e);
@@ -1987,6 +1986,7 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)
u8 *errpkt;
u16 malformed;
enum channel_remove_err e;
struct htlc *h;
if (!fromwire_channel_fail_htlc(inmsg, inmsg, NULL, &id, &malformed,
&errpkt))
@@ -1997,15 +1997,13 @@ static void handle_fail(struct peer *peer, const u8 *inmsg)
"Invalid channel_fail_htlc: bad malformed 0x%x",
malformed);
e = channel_fail_htlc(peer->channel, REMOTE, id);
e = channel_fail_htlc(peer->channel, REMOTE, id, &h);
switch (e) {
case CHANNEL_ERR_REMOVE_OK:
if (malformed) {
struct htlc *h;
struct sha256 sha256_of_onion;
status_trace("Failing %"PRIu64" with code %u",
id, malformed);
h = channel_get_htlc(peer->channel, REMOTE, id);
sha256(&sha256_of_onion, h->routing,
tal_len(h->routing));
msg = towire_update_fail_malformed_htlc(peer,

View File

@@ -471,7 +471,8 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
u64 msatoshi,
u32 cltv_expiry,
const struct sha256 *payment_hash,
const u8 routing[TOTAL_PACKET_SIZE])
const u8 routing[TOTAL_PACKET_SIZE],
struct htlc **htlcp)
{
enum htlc_state state;
@@ -482,7 +483,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
/* FIXME: check expiry etc. against config. */
return add_htlc(channel, state, id, msatoshi, cltv_expiry,
payment_hash, routing, NULL, true);
payment_hash, routing, htlcp, true);
}
struct htlc *channel_get_htlc(struct channel *channel, enum side sender, u64 id)
@@ -555,7 +556,8 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
}
enum channel_remove_err channel_fail_htlc(struct channel *channel,
enum side owner, u64 id)
enum side owner, u64 id,
struct htlc **htlcp)
{
struct htlc *htlc;
@@ -590,7 +592,8 @@ enum channel_remove_err channel_fail_htlc(struct channel *channel,
channel->changes_pending[owner] = true;
dump_htlc(htlc, "FAIL:");
if (htlcp)
*htlcp = htlc;
return CHANNEL_ERR_REMOVE_OK;
}

View File

@@ -105,6 +105,7 @@ enum channel_add_err {
* @cltv_expiry: block number when HTLC can no longer be redeemed.
* @payment_hash: hash whose preimage can redeem HTLC.
* @routing: routing information (copied)
* @htlcp: optional pointer for resulting htlc: filled in iff CHANNEL_ERR_NONE.
*
* If this returns CHANNEL_ERR_NONE, the fee htlc was added and
* the output amounts adjusted accordingly. Otherwise nothing
@@ -116,7 +117,8 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
u64 msatoshi,
u32 cltv_expiry,
const struct sha256 *payment_hash,
const u8 routing[TOTAL_PACKET_SIZE]);
const u8 routing[TOTAL_PACKET_SIZE],
struct htlc **htlcp);
/**
* channel_get_htlc: find an HTLC
@@ -146,12 +148,14 @@ enum channel_remove_err {
* @channel: The channel state
* @owner: the side who offered the HTLC (opposite to that failing it)
* @id: unique HTLC id.
* @htlcp: optional pointer for failed htlc: filled in iff CHANNEL_ERR_REMOVE_OK.
*
* This will remove the htlc and credit the value of the HTLC (back)
* to its offerer.
*/
enum channel_remove_err channel_fail_htlc(struct channel *channel,
enum side owner, u64 id);
enum side owner, u64 id,
struct htlc **htlcp);
/**
* channel_fulfill_htlc: remove an HTLC, funds to side which accepted it.

View File

@@ -150,7 +150,7 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side
memset(&preimage, i, sizeof(preimage));
sha256(&hash, &preimage, sizeof(preimage));
e = channel_add_htlc(channel, sender, i, msatoshi, 500+i, &hash,
dummy_routing);
dummy_routing, NULL);
assert(e == CHANNEL_ERR_ADD_OK);
htlcs[i] = channel_get_htlc(channel, sender, i);
}
@@ -248,7 +248,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
sha256(&rhash, &r, sizeof(r));
assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, &rhash,
dummy_routing) == CHANNEL_ERR_ADD_OK);
dummy_routing, NULL) == CHANNEL_ERR_ADD_OK);
changed_htlcs = tal_arr(channel, const struct htlc *, 0);