diff --git a/daemon/htlc.h b/daemon/htlc.h index da879f1f6..da1225520 100644 --- a/daemon/htlc.h +++ b/daemon/htlc.h @@ -19,6 +19,8 @@ struct htlc { struct abs_locktime expiry; /* The hash of the preimage which can redeem this HTLC */ struct sha256 rhash; + /* The preimage which hashes to rhash (if known) */ + struct rval *r; /* FIXME: We could union these together: */ /* Routing information sent with this HTLC. */ diff --git a/daemon/pay.c b/daemon/pay.c index 7bf139185..d303b1c6c 100644 --- a/daemon/pay.c +++ b/daemon/pay.c @@ -16,21 +16,19 @@ struct pay_command { struct command *cmd; }; -void complete_pay_command(struct peer *peer, - struct htlc *htlc, - const struct rval *rval) +void complete_pay_command(struct peer *peer, struct htlc *htlc) { struct pay_command *i; list_for_each(&peer->pay_commands, i, list) { if (i->htlc == htlc) { - if (rval) { + if (htlc->r) { struct json_result *response; response = new_json_result(i->cmd); json_object_start(response, NULL); json_add_hex(response, "preimage", - rval->r, sizeof(rval->r)); + htlc->r, sizeof(*htlc->r)); json_object_end(response); command_success(i->cmd, response); } else { @@ -39,10 +37,11 @@ void complete_pay_command(struct peer *peer, return; } } + /* Can happen if RPC connection goes away. */ log_unusual(peer->log, "No command for HTLC %"PRIu64" %s", - htlc->id, rval ? "fulfill" : "fail"); -} + htlc->id, htlc->r ? "fulfill" : "fail"); +} static void remove_from_list(struct pay_command *pc) { diff --git a/daemon/pay.h b/daemon/pay.h index 85f28a202..2b117d6e3 100644 --- a/daemon/pay.h +++ b/daemon/pay.h @@ -6,8 +6,6 @@ struct peer; struct htlc; struct rval; -void complete_pay_command(struct peer *peer, - struct htlc *htlc, - const struct rval *rval); +void complete_pay_command(struct peer *peer, struct htlc *htlc); #endif /* LIGHTNING_DAEMON_PAY_H */ diff --git a/daemon/peer.c b/daemon/peer.c index 2d31878b0..1f7db0ba4 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -561,6 +561,9 @@ static bool command_htlc_fulfill(struct peer *peer, struct htlc *htlc, const struct rval *r) { + assert(!htlc->r); + htlc->r = tal_dup(htlc, struct rval, r); + if (!state_can_remove_htlc(peer->state)) return false; @@ -904,6 +907,7 @@ struct htlc *peer_new_htlc(struct peer *peer, h->id = id; h->msatoshis = msatoshis; h->rhash = *rhash; + h->r = NULL; if (!blocks_to_abs_locktime(expiry, &h->expiry)) fatal("Invalid HTLC expiry %u", expiry); h->routing = tal_dup_arr(h, u8, route, routelen, 0); @@ -1633,8 +1637,11 @@ void our_htlc_fulfilled(struct peer *peer, struct htlc *htlc, { if (htlc->src) command_htlc_fulfill(htlc->src->peer, htlc->src, preimage); - else - complete_pay_command(peer, htlc, preimage); + else { + assert(!htlc->r); + htlc->r = tal_dup(htlc, struct rval, preimage); + complete_pay_command(peer, htlc); + } } static void their_htlc_depth(struct peer *peer, @@ -2449,7 +2456,7 @@ static void our_htlc_failed(struct peer *peer, struct htlc *htlc) if (htlc->src) command_htlc_fail(htlc->src->peer, htlc->src); else - complete_pay_command(peer, htlc, NULL); + complete_pay_command(peer, htlc); } /* When changes are committed to. */