From 1c0b43f04d11590b626aa9506691400e75e914c9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 1 Apr 2017 21:31:13 +1030 Subject: [PATCH] channel: tell master about the HTLC fulfillment. Signed-off-by: Rusty Russell --- lightningd/channel/channel.c | 3 ++- lightningd/peer_control.c | 32 ++++++++++++++++++++++++++++++++ lightningd/test/test-basic | 10 +++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 74d943ef6..3f522dcb9 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -700,7 +700,8 @@ static void handle_peer_fulfill_htlc(struct peer *peer, const u8 *msg) e = channel_fulfill_htlc(peer->channel, LOCAL, id, &preimage); switch (e) { case CHANNEL_ERR_REMOVE_OK: - /* FIXME: tell master about HTLC preimage */ + msg = towire_channel_fulfilled_htlc(msg, id, &preimage); + daemon_conn_send(&peer->master, take(msg)); start_commit_timer(peer); return; /* These shouldn't happen, because any offered HTLC (which would give diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index c936f3120..7d756d4b4 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -742,6 +742,37 @@ static int peer_accepted_htlc(struct peer *peer, const u8 *msg) return 0; } +static int peer_fulfilled_htlc(struct peer *peer, const u8 *msg) +{ + u64 id; + struct preimage preimage; + struct htlc_end *hend; + + if (!fromwire_channel_fulfilled_htlc(msg, NULL, &id, &preimage)) { + log_broken(peer->log, "bad fromwire_channel_fulfilled_htlc %s", + tal_hex(peer, msg)); + return -1; + } + + hend = find_htlc_end(&peer->ld->htlc_ends, peer, id, HTLC_DST); + if (!hend) { + log_broken(peer->log, + "channel_fulfilled_htlc unknown htlc %"PRIu64, + id); + return -1; + } + + /* They fulfilled our HTLC. Credit them, forward as required. */ + peer->balance[REMOTE] += hend->msatoshis; + peer->balance[LOCAL] -= hend->msatoshis; + + /* FIXME: Forward! */ + assert(!hend->other_end); + tal_free(hend); + + return 0; +} + static int channel_msg(struct subd *sd, const u8 *msg, const int *unused) { enum channel_wire_type t = fromwire_peektype(msg); @@ -756,6 +787,7 @@ static int channel_msg(struct subd *sd, const u8 *msg, const int *unused) case WIRE_CHANNEL_ACCEPTED_HTLC: return peer_accepted_htlc(sd->peer, msg); case WIRE_CHANNEL_FULFILLED_HTLC: + return peer_fulfilled_htlc(sd->peer, msg); case WIRE_CHANNEL_FAILED_HTLC: case WIRE_CHANNEL_MALFORMED_HTLC: /* FIXME: Forward. */ diff --git a/lightningd/test/test-basic b/lightningd/test/test-basic index 286293e28..43f2199f7 100755 --- a/lightningd/test/test-basic +++ b/lightningd/test/test-basic @@ -74,11 +74,11 @@ check "lcli1 getlog debug | $FGREP 'Sending commit_sig with 1 htlc sigs'" check "lcli2 getlog debug | $FGREP 'Resolving invoice '\'testpayment1\'' with HTLC 1'" [ `lcli2 listinvoice testpayment1 | get_field complete` = true ] -#check "lcli1 getpeers | get_field msatoshi_to_us | $FGREP -w 900000000" -#[ `lcli1 getpeers | get_field msatoshi_to_us` = 900000000 ] -#[ `lcli1 getpeers | get_field msatoshi_to_them` = 100000000 ] -#[ `lcli2 getpeers | get_field msatoshi_to_them` = 900000000 ] -#[ `lcli2 getpeers | get_field msatoshi_to_us` = 100000000 ] +check "lcli1 getpeers | get_field msatoshi_to_us | $FGREP -w 900000000" +[ `lcli1 getpeers | get_field msatoshi_to_us` = 900000000 ] +[ `lcli1 getpeers | get_field msatoshi_to_them` = 100000000 ] +[ `lcli2 getpeers | get_field msatoshi_to_them` = 900000000 ] +[ `lcli2 getpeers | get_field msatoshi_to_us` = 100000000 ] lcli1 stop lcli2 stop