Skip to content

Commit

Permalink
channeld: make channel_fulfill_htlc return the HTLC it fulfulled.
Browse files Browse the repository at this point in the history
This is the same pattern as channel_fail_htlc, and in fact one caller
wanted it already.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 8, 2018
1 parent e92f244 commit 8155bfc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
7 changes: 3 additions & 4 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ static void handle_peer_fulfill_htlc(struct peer *peer, const u8 *msg)
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
}

e = channel_fulfill_htlc(peer->channel, LOCAL, id, &preimage);
e = channel_fulfill_htlc(peer->channel, LOCAL, id, &preimage, NULL);
switch (e) {
case CHANNEL_ERR_REMOVE_OK:
/* FIXME: We could send preimages to master immediately. */
Expand Down Expand Up @@ -1509,11 +1509,10 @@ static void handle_peer_fail_htlc(struct peer *peer, const u8 *msg)
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
}

e = channel_fail_htlc(peer->channel, LOCAL, id, NULL);
e = channel_fail_htlc(peer->channel, LOCAL, id, &htlc);
switch (e) {
case CHANNEL_ERR_REMOVE_OK:
/* Save reason for when we tell master. */
htlc = channel_get_htlc(peer->channel, LOCAL, id);
htlc->fail = tal_steal(htlc, reason);
start_commit_timer(peer);
return;
Expand Down Expand Up @@ -2202,7 +2201,7 @@ static void handle_preimage(struct peer *peer, const u8 *inmsg)
if (!fromwire_channel_fulfill_htlc(inmsg, &id, &preimage))
master_badmsg(WIRE_CHANNEL_FULFILL_HTLC, inmsg);

switch (channel_fulfill_htlc(peer->channel, REMOTE, id, &preimage)) {
switch (channel_fulfill_htlc(peer->channel, REMOTE, id, &preimage, NULL)) {
case CHANNEL_ERR_REMOVE_OK:
msg = towire_update_fulfill_htlc(NULL, &peer->channel_id,
id, &preimage);
Expand Down
10 changes: 7 additions & 3 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,10 @@ struct htlc *channel_get_htlc(struct channel *channel, enum side sender, u64 id)
}

enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
enum side owner,
u64 id,
const struct preimage *preimage)
enum side owner,
u64 id,
const struct preimage *preimage,
struct htlc **htlcp)
{
struct sha256 hash;
struct htlc *htlc;
Expand Down Expand Up @@ -556,6 +557,9 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,

dump_htlc(htlc, "FULFILL:");

if (htlcp)
*htlcp = htlc;

return CHANNEL_ERR_REMOVE_OK;
}

Expand Down
4 changes: 3 additions & 1 deletion channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ enum channel_remove_err channel_fail_htlc(struct channel *channel,
* @channel: The channel state
* @owner: the side who offered the HTLC (opposite to that fulfilling it)
* @id: unique HTLC id.
* @htlcp: optional pointer for resulting htlc: filled in if and only if CHANNEL_ERR_FULFILL_OK.
*
* If the htlc exists, is not already fulfilled, the preimage is correct and
* HTLC committed at the recipient, this will add a pending change to
Expand All @@ -138,7 +139,8 @@ enum channel_remove_err channel_fail_htlc(struct channel *channel,
enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
enum side owner,
u64 id,
const struct preimage *preimage);
const struct preimage *preimage,
struct htlc **htlcp);

/**
* approx_max_feerate: what's the max funder could raise fee rate to?
Expand Down
4 changes: 2 additions & 2 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
assert(ret);
ret = channel_sending_revoke_and_ack(channel);
assert(!ret);
assert(channel_fulfill_htlc(channel, LOCAL, 1337, &r)
assert(channel_fulfill_htlc(channel, LOCAL, 1337, &r, NULL)
== CHANNEL_ERR_REMOVE_OK);
ret = channel_rcvd_commit(channel, &changed_htlcs);
assert(ret);
Expand All @@ -271,7 +271,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
assert(ret);
ret = channel_rcvd_revoke_and_ack(channel, &changed_htlcs);
assert(!ret);
assert(channel_fulfill_htlc(channel, REMOTE, 1337, &r)
assert(channel_fulfill_htlc(channel, REMOTE, 1337, &r, NULL)
== CHANNEL_ERR_REMOVE_OK);
ret = channel_sending_commit(channel, &changed_htlcs);
assert(ret);
Expand Down

0 comments on commit 8155bfc

Please sign in to comment.