From 8ed68179a5bc133162066a998d5cb46fdd188ceb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 30 Mar 2016 16:55:03 +1030 Subject: [PATCH] funding: add 64-bit id to HTLCs. This gives us a reliable way to distinguish HTLCs, even in the face of duplicate R values. Signed-off-by: Rusty Russell --- daemon/packets.c | 2 +- daemon/peer.c | 2 +- funding.c | 22 +++++++++++++++++----- funding.h | 16 ++++++++++++++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/daemon/packets.c b/daemon/packets.c index 38a7e8a25..91ac80077 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -449,7 +449,7 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx, if (!funding_b_add_htlc(cur->cstate, cur->stage.add.htlc.msatoshis, &cur->stage.add.htlc.expiry, - &cur->stage.add.htlc.rhash)) { + &cur->stage.add.htlc.rhash, 0)) { err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis", cur->stage.add.htlc.msatoshis); goto fail; diff --git a/daemon/peer.c b/daemon/peer.c index af4205a95..ee471c852 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -1473,7 +1473,7 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc) * execute. */ cstate = copy_funding(newhtlc, peer->cstate); if (!funding_a_add_htlc(cstate, newhtlc->htlc.msatoshis, - &newhtlc->htlc.expiry, &newhtlc->htlc.rhash)) { + &newhtlc->htlc.expiry, &newhtlc->htlc.rhash, 0)) { command_fail(newhtlc->jsoncmd, "Cannot afford %"PRIu64" milli-satoshis", newhtlc->htlc.msatoshis); diff --git a/funding.c b/funding.c index 5abd5f8b3..d67d6b44e 100644 --- a/funding.c +++ b/funding.c @@ -224,7 +224,7 @@ static bool add_htlc(const struct channel_state *cstate, struct channel_oneside *creator, struct channel_oneside *recipient, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash) + const struct sha256 *rhash, uint64_t id) { size_t n, nondust; @@ -246,6 +246,7 @@ static bool add_htlc(const struct channel_state *cstate, creator->htlcs[n].msatoshis = msatoshis; creator->htlcs[n].expiry = *expiry; creator->htlcs[n].rhash = *rhash; + creator->htlcs[n].id = id; memcheck(&creator->htlcs[n].msatoshis, sizeof(creator->htlcs[n].msatoshis)); memcheck(&creator->htlcs[n].rhash, sizeof(creator->htlcs[n].rhash)); @@ -288,18 +289,18 @@ static void remove_htlc(const struct channel_state *cstate, bool funding_a_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash) + const struct sha256 *rhash, uint64_t id) { return add_htlc(cstate, &cstate->a, &cstate->b, - msatoshis, expiry, rhash); + msatoshis, expiry, rhash, id); } bool funding_b_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash) + const struct sha256 *rhash, uint64_t id) { return add_htlc(cstate, &cstate->b, &cstate->a, - msatoshis, expiry, rhash); + msatoshis, expiry, rhash, id); } void funding_a_fail_htlc(struct channel_state *cstate, size_t index) @@ -334,6 +335,17 @@ size_t funding_find_htlc(struct channel_oneside *creator, return i; } +size_t funding_htlc_by_id(struct channel_oneside *creator, uint64_t id) +{ + size_t i; + + for (i = 0; i < tal_count(creator->htlcs); i++) { + if (creator->htlcs[i].id == id) + break; + } + return i; +} + struct channel_state *copy_funding(const tal_t *ctx, const struct channel_state *cstate) { diff --git a/funding.h b/funding.h index 09fc328a0..6385ea51a 100644 --- a/funding.h +++ b/funding.h @@ -7,6 +7,7 @@ #include struct channel_htlc { + u64 id; u64 msatoshis; struct abs_locktime expiry; struct sha256 rhash; @@ -55,6 +56,7 @@ struct channel_state *copy_funding(const tal_t *ctx, * @msatoshis: Millisatoshi A is putting into a HTLC * @expiry: time it expires * @rhash: hash of redeem secret + * @id: 64-bit ID for htlc * * If A can't afford the HTLC (or still owes its half of the fees), * this will return false and leave @cstate unchanged. Otherwise @@ -63,11 +65,11 @@ struct channel_state *copy_funding(const tal_t *ctx, */ bool funding_a_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash); + const struct sha256 *rhash, uint64_t id); bool funding_b_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash); + const struct sha256 *rhash, uint64_t id); /** * funding_a_fail_htlc: remove an HTLC from A's side of cstate, funds to A @@ -126,6 +128,16 @@ void invert_cstate(struct channel_state *cstate); size_t funding_find_htlc(struct channel_oneside *creator, const struct sha256 *rhash); +/** + * funding_htlc_by_id: find an HTLC on this side of the channel by ID. + * @creator: channel_state->a or channel_state->b, whichever originated htlc + * @id: id for HTLC. + * + * Returns a number < tal_count(creator->htlcs), or == tal_count(creator->htlcs) + * on fail. + */ +size_t funding_htlc_by_id(struct channel_oneside *creator, uint64_t id); + /** * fee_for_feerate: calculate the fee (in satoshi) for a given fee_rate. * @txsize: transaction size in bytes.