Browse Source

htlc: keep rval (if known).

This makes struct htlc a complete object, containing its own information.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
3ba25dd994
  1. 2
      daemon/htlc.h
  2. 11
      daemon/pay.c
  3. 4
      daemon/pay.h
  4. 13
      daemon/peer.c

2
daemon/htlc.h

@ -19,6 +19,8 @@ struct htlc {
struct abs_locktime expiry; struct abs_locktime expiry;
/* The hash of the preimage which can redeem this HTLC */ /* The hash of the preimage which can redeem this HTLC */
struct sha256 rhash; struct sha256 rhash;
/* The preimage which hashes to rhash (if known) */
struct rval *r;
/* FIXME: We could union these together: */ /* FIXME: We could union these together: */
/* Routing information sent with this HTLC. */ /* Routing information sent with this HTLC. */

11
daemon/pay.c

@ -16,21 +16,19 @@ struct pay_command {
struct command *cmd; struct command *cmd;
}; };
void complete_pay_command(struct peer *peer, void complete_pay_command(struct peer *peer, struct htlc *htlc)
struct htlc *htlc,
const struct rval *rval)
{ {
struct pay_command *i; struct pay_command *i;
list_for_each(&peer->pay_commands, i, list) { list_for_each(&peer->pay_commands, i, list) {
if (i->htlc == htlc) { if (i->htlc == htlc) {
if (rval) { if (htlc->r) {
struct json_result *response; struct json_result *response;
response = new_json_result(i->cmd); response = new_json_result(i->cmd);
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_hex(response, "preimage", json_add_hex(response, "preimage",
rval->r, sizeof(rval->r)); htlc->r, sizeof(*htlc->r));
json_object_end(response); json_object_end(response);
command_success(i->cmd, response); command_success(i->cmd, response);
} else { } else {
@ -39,9 +37,10 @@ void complete_pay_command(struct peer *peer,
return; return;
} }
} }
/* Can happen if RPC connection goes away. */ /* Can happen if RPC connection goes away. */
log_unusual(peer->log, "No command for HTLC %"PRIu64" %s", 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) static void remove_from_list(struct pay_command *pc)

4
daemon/pay.h

@ -6,8 +6,6 @@ struct peer;
struct htlc; struct htlc;
struct rval; struct rval;
void complete_pay_command(struct peer *peer, void complete_pay_command(struct peer *peer, struct htlc *htlc);
struct htlc *htlc,
const struct rval *rval);
#endif /* LIGHTNING_DAEMON_PAY_H */ #endif /* LIGHTNING_DAEMON_PAY_H */

13
daemon/peer.c

@ -561,6 +561,9 @@ static bool command_htlc_fulfill(struct peer *peer,
struct htlc *htlc, struct htlc *htlc,
const struct rval *r) const struct rval *r)
{ {
assert(!htlc->r);
htlc->r = tal_dup(htlc, struct rval, r);
if (!state_can_remove_htlc(peer->state)) if (!state_can_remove_htlc(peer->state))
return false; return false;
@ -904,6 +907,7 @@ struct htlc *peer_new_htlc(struct peer *peer,
h->id = id; h->id = id;
h->msatoshis = msatoshis; h->msatoshis = msatoshis;
h->rhash = *rhash; h->rhash = *rhash;
h->r = NULL;
if (!blocks_to_abs_locktime(expiry, &h->expiry)) if (!blocks_to_abs_locktime(expiry, &h->expiry))
fatal("Invalid HTLC expiry %u", expiry); fatal("Invalid HTLC expiry %u", expiry);
h->routing = tal_dup_arr(h, u8, route, routelen, 0); 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) if (htlc->src)
command_htlc_fulfill(htlc->src->peer, htlc->src, preimage); command_htlc_fulfill(htlc->src->peer, htlc->src, preimage);
else else {
complete_pay_command(peer, htlc, preimage); assert(!htlc->r);
htlc->r = tal_dup(htlc, struct rval, preimage);
complete_pay_command(peer, htlc);
}
} }
static void their_htlc_depth(struct peer *peer, 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) if (htlc->src)
command_htlc_fail(htlc->src->peer, htlc->src); command_htlc_fail(htlc->src->peer, htlc->src);
else else
complete_pay_command(peer, htlc, NULL); complete_pay_command(peer, htlc);
} }
/* When changes are committed to. */ /* When changes are committed to. */

Loading…
Cancel
Save