Skip to content

Commit c919551

Browse files
rustyrussellcdecker
authored andcommitted
onchaind: include htlc id in htlc_stub so we agree on what HTLC we're closing.
If there are two HTLCs with the same preimage, lightningd would always find the first one. By including the id in the `struct htlc_stub` it's both faster (normal HTLC lookup) and allows lightningd to detect that onchaind wants to fail both of them. Signed-off-by: Rusty Russell <[email protected]>
1 parent a0511a4 commit c919551

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

lightningd/onchain_control.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,12 @@ static bool tell_if_missing(const struct channel *channel,
343343
/* Keep valgrind happy. */
344344
*tell_immediate = false;
345345

346-
/* Is it a current HTLC? */
347-
hout = find_htlc_out_by_ripemd(channel, &stub->ripemd);
346+
/* Don't care about incoming HTLCs, just ones we offered. */
347+
if (stub->owner == REMOTE)
348+
return false;
349+
350+
/* Might not be a current HTLC. */
351+
hout = find_htlc_out(&channel->peer->ld->htlcs_out, channel, stub->id);
348352
if (!hout)
349353
return false;
350354

lightningd/peer_htlcs.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -823,36 +823,16 @@ static bool peer_failed_our_htlc(struct channel *channel,
823823
return true;
824824
}
825825

826-
/* FIXME: Crazy slow! */
827-
struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel,
828-
const struct ripemd160 *ripemd)
829-
{
830-
struct htlc_out_map_iter outi;
831-
struct htlc_out *hout;
832-
struct lightningd *ld = channel->peer->ld;
833-
834-
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
835-
hout;
836-
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
837-
struct ripemd160 hash;
838-
839-
if (hout->key.channel != channel)
840-
continue;
841-
842-
ripemd160(&hash,
843-
&hout->payment_hash, sizeof(hout->payment_hash));
844-
if (ripemd160_eq(&hash, ripemd))
845-
return hout;
846-
}
847-
return NULL;
848-
}
849-
850826
void onchain_failed_our_htlc(const struct channel *channel,
851827
const struct htlc_stub *htlc,
852828
const char *why)
853829
{
854830
struct lightningd *ld = channel->peer->ld;
855-
struct htlc_out *hout = find_htlc_out_by_ripemd(channel, &htlc->ripemd);
831+
struct htlc_out *hout;
832+
833+
hout = find_htlc_out(&ld->htlcs_out, channel, htlc->id);
834+
if (!hout)
835+
return;
856836

857837
/* Don't fail twice (or if already succeeded)! */
858838
if (hout->failuremsg || hout->failcode || hout->preimage)
@@ -863,7 +843,7 @@ void onchain_failed_our_htlc(const struct channel *channel,
863843
/* Force state to something which expects a failure, and save to db */
864844
hout->hstate = RCVD_REMOVE_HTLC;
865845
htlc_out_check(hout, __func__);
866-
wallet_htlc_update(channel->peer->ld->wallet, hout->dbid, hout->hstate,
846+
wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate,
867847
hout->preimage, hout->failcode, hout->failuremsg);
868848

869849
if (hout->am_origin) {

lightningd/peer_htlcs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ enum onion_type send_htlc_out(struct channel *out, u64 amount, u32 cltv,
5050
struct htlc_in *in,
5151
struct htlc_out **houtp);
5252

53-
struct htlc_out *find_htlc_out_by_ripemd(const struct channel *channel,
54-
const struct ripemd160 *ripemd160);
5553
void onchain_failed_our_htlc(const struct channel *channel,
5654
const struct htlc_stub *htlc,
5755
const char *why);

onchaind/onchain_wire.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void towire_htlc_stub(u8 **pptr, const struct htlc_stub *htlc_stub)
66
{
77
towire_side(pptr, htlc_stub->owner);
88
towire_u32(pptr, htlc_stub->cltv_expiry);
9+
towire_u64(pptr, htlc_stub->id);
910
towire_ripemd160(pptr, &htlc_stub->ripemd);
1011
}
1112

@@ -14,5 +15,6 @@ void fromwire_htlc_stub(const u8 **cursor, size_t *max,
1415
{
1516
htlc_stub->owner = fromwire_side(cursor, max);
1617
htlc_stub->cltv_expiry = fromwire_u32(cursor, max);
18+
htlc_stub->id = fromwire_u64(cursor, max);
1719
fromwire_ripemd160(cursor, max, &htlc_stub->ripemd);
1820
}

onchaind/onchain_wire.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
struct htlc_stub {
1010
enum side owner;
1111
u32 cltv_expiry;
12+
u64 id;
1213
struct ripemd160 ripemd;
1314
};
1415

wallet/wallet.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet,
15511551
struct htlc_stub *stubs;
15521552
struct sha256 payment_hash;
15531553
sqlite3_stmt *stmt = db_prepare(wallet->db,
1554-
"SELECT channel_id, direction, cltv_expiry, payment_hash "
1554+
"SELECT channel_id, direction, cltv_expiry, channel_htlc_id, payment_hash "
15551555
"FROM channel_htlcs WHERE channel_id = ?;");
15561556

15571557
sqlite3_bind_int64(stmt, 1, chan->dbid);
@@ -1566,8 +1566,9 @@ struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet,
15661566
/* FIXME: merge these two enums */
15671567
stub->owner = sqlite3_column_int(stmt, 1)==DIRECTION_INCOMING?REMOTE:LOCAL;
15681568
stub->cltv_expiry = sqlite3_column_int(stmt, 2);
1569+
stub->id = sqlite3_column_int(stmt, 3);
15691570

1570-
sqlite3_column_sha256(stmt, 3, &payment_hash);
1571+
sqlite3_column_sha256(stmt, 4, &payment_hash);
15711572
ripemd160(&stub->ripemd, payment_hash.u.u8, sizeof(payment_hash.u));
15721573
}
15731574
db_stmt_done(stmt);

0 commit comments

Comments
 (0)