Skip to content

Commit 42e40c1

Browse files
niftyneicdecker
authored andcommitted
htlcs: add flag to 'fail immediately'
If we're over the dust limit, we fail it immediatey *after* commiting it, but we need a way to signal this throughout the lifecycle, so we add it to htlc_in struct and persist it through to the database. If it's supposed to be failed, we fail after the commit cycle is completed.
1 parent 208b161 commit 42e40c1

File tree

11 files changed

+53
-14
lines changed

11 files changed

+53
-14
lines changed

channeld/channeld.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,11 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
738738
#endif
739739
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
740740
cltv_expiry, &payment_hash,
741-
onion_routing_packet, blinding, &htlc, NULL);
741+
onion_routing_packet, blinding, &htlc, NULL,
742+
/* We don't immediately fail incoming htlcs,
743+
* instead we wait and fail them after
744+
* they've been committed */
745+
false);
742746
if (add_err != CHANNEL_ERR_ADD_OK)
743747
peer_failed_warn(peer->pps, &peer->channel_id,
744748
"Bad peer_add_htlc: %s",
@@ -1468,6 +1472,7 @@ static void marshall_htlc_info(const tal_t *ctx,
14681472
ecdh(a.blinding, &a.blinding_ss);
14691473
} else
14701474
a.blinding = NULL;
1475+
a.fail_immediate = htlc->fail_immediate;
14711476
tal_arr_expand(added, a);
14721477
} else if (htlc->state == RCVD_REMOVE_COMMIT) {
14731478
if (htlc->r) {
@@ -3299,7 +3304,8 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
32993304

33003305
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
33013306
amount, cltv_expiry, &payment_hash,
3302-
onion_routing_packet, take(blinding), NULL, &htlc_fee);
3307+
onion_routing_packet, take(blinding), NULL,
3308+
&htlc_fee, true);
33033309
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s",
33043310
peer->htlc_id,
33053311
type_to_string(tmpctx, struct amount_msat, &amount),

channeld/channeld_htlc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct htlc {
2828

2929
/* Blinding (optional). */
3030
struct pubkey *blinding;
31+
32+
/* Should we immediately fail this htlc? */
33+
bool fail_immediate;
3134
};
3235

3336
static inline bool htlc_has(const struct htlc *h, int flag)

channeld/full_channel.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ static enum channel_add_err add_htlc(struct channel *channel,
493493
const struct pubkey *blinding TAKES,
494494
struct htlc **htlcp,
495495
bool enforce_aggregate_limits,
496-
struct amount_sat *htlc_fee)
496+
struct amount_sat *htlc_fee,
497+
bool err_immediate_failures)
497498
{
498499
struct htlc *htlc, *old;
499500
struct amount_msat msat_in_htlcs, committed_msat, adding_msat, removing_msat;
@@ -508,6 +509,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
508509
htlc->id = id;
509510
htlc->amount = amount;
510511
htlc->state = state;
512+
htlc->fail_immediate = false;
511513

512514
htlc->rhash = *payment_hash;
513515
if (blinding)
@@ -768,7 +770,8 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
768770
const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
769771
const struct pubkey *blinding TAKES,
770772
struct htlc **htlcp,
771-
struct amount_sat *htlc_fee)
773+
struct amount_sat *htlc_fee,
774+
bool err_immediate_failures)
772775
{
773776
enum htlc_state state;
774777

@@ -786,7 +789,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
786789

787790
return add_htlc(channel, state, id, amount, cltv_expiry,
788791
payment_hash, routing, blinding,
789-
htlcp, true, htlc_fee);
792+
htlcp, true, htlc_fee, err_immediate_failures);
790793
}
791794

792795
struct htlc *channel_get_htlc(struct channel *channel, enum side sender, u64 id)
@@ -1392,7 +1395,7 @@ bool channel_force_htlcs(struct channel *channel,
13921395
&htlcs[i]->payment_hash,
13931396
htlcs[i]->onion_routing_packet,
13941397
htlcs[i]->blinding,
1395-
&htlc, false, NULL);
1398+
&htlc, false, NULL, false);
13961399
if (e != CHANNEL_ERR_ADD_OK) {
13971400
status_broken("%s HTLC %"PRIu64" failed error %u",
13981401
htlc_state_owner(htlcs[i]->state) == LOCAL

channeld/full_channel.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ u32 actual_feerate(const struct channel *channel,
103103
* @routing: routing information (copied)
104104
* @blinding: optional blinding information for this HTLC.
105105
* @htlcp: optional pointer for resulting htlc: filled in if and only if CHANNEL_ERR_NONE.
106+
* @err_immediate_failures: in some cases (dusty htlcs) we want to immediately
107+
* fail the htlc; for peer incoming don't want to
108+
* error, but rather mark it as failed and fail after
109+
* it's been committed to (so set this to false)
106110
*
107111
* If this returns CHANNEL_ERR_NONE, the fee htlc was added and
108112
* the output amounts adjusted accordingly. Otherwise nothing
@@ -117,7 +121,8 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
117121
const u8 routing[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)],
118122
const struct pubkey *blinding TAKES,
119123
struct htlc **htlcp,
120-
struct amount_sat *htlc_fee);
124+
struct amount_sat *htlc_fee,
125+
bool err_immediate_failures);
121126

122127
/**
123128
* channel_get_htlc: find an HTLC

channeld/test/run-full_channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side
165165
memset(&preimage, i, sizeof(preimage));
166166
sha256(&hash, &preimage, sizeof(preimage));
167167
e = channel_add_htlc(channel, sender, i, msatoshi, 500+i, &hash,
168-
dummy_routing, NULL, NULL, NULL);
168+
dummy_routing, NULL, NULL, NULL, true);
169169
assert(e == CHANNEL_ERR_ADD_OK);
170170
htlcs[i] = channel_get_htlc(channel, sender, i);
171171
}
@@ -257,7 +257,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
257257
sha256(&rhash, &r, sizeof(r));
258258

259259
assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, &rhash,
260-
dummy_routing, NULL, NULL, NULL)
260+
dummy_routing, NULL, NULL, NULL, true)
261261
== CHANNEL_ERR_ADD_OK);
262262

263263
changed_htlcs = tal_arr(channel, const struct htlc *, 0);

common/htlc_wire.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
7878
towire_secret(pptr, &added->blinding_ss);
7979
} else
8080
towire_bool(pptr, false);
81+
towire_bool(pptr, added->fail_immediate);
8182
}
8283

8384
void towire_existing_htlc(u8 **pptr, const struct existing_htlc *existing)
@@ -171,6 +172,7 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
171172
fromwire_secret(cursor, max, &added->blinding_ss);
172173
} else
173174
added->blinding = NULL;
175+
added->fail_immediate = fromwire_bool(cursor, max);
174176
}
175177

176178
struct existing_htlc *fromwire_existing_htlc(const tal_t *ctx,

common/htlc_wire.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct added_htlc {
1515
struct sha256 payment_hash;
1616
u32 cltv_expiry;
1717
u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
18+
bool fail_immediate;
1819

1920
/* If this is non-NULL, secret is the resulting shared secret */
2021
struct pubkey *blinding;

lightningd/htlc_end.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
130130
const struct secret *shared_secret TAKES,
131131
const struct pubkey *blinding TAKES,
132132
const struct secret *blinding_ss,
133-
const u8 *onion_routing_packet)
133+
const u8 *onion_routing_packet,
134+
bool fail_immediate)
134135
{
135136
struct htlc_in *hin = tal(ctx, struct htlc_in);
136137

@@ -141,6 +142,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
141142
hin->cltv_expiry = cltv_expiry;
142143
hin->payment_hash = *payment_hash;
143144
hin->status = NULL;
145+
hin->fail_immediate = fail_immediate;
144146
if (shared_secret)
145147
hin->shared_secret = tal_dup(hin, struct secret, shared_secret);
146148
else

lightningd/htlc_end.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ struct htlc_in {
5252
struct secret blinding_ss;
5353
/* true if we supplied the preimage */
5454
bool *we_filled;
55+
/* true if we immediately fail the htlc (too much dust) */
56+
bool fail_immediate;
5557

5658
/* A simple text annotation shown in `listpeers` */
5759
char *status;
@@ -154,7 +156,8 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
154156
const struct secret *shared_secret TAKES,
155157
const struct pubkey *blinding TAKES,
156158
const struct secret *blinding_ss,
157-
const u8 *onion_routing_packet);
159+
const u8 *onion_routing_packet,
160+
bool fail_immediate);
158161

159162
/* You need to set the ID, then connect_htlc_out this! */
160163
struct htlc_out *new_htlc_out(const tal_t *ctx,

lightningd/peer_htlcs.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,13 @@ static bool peer_accepted_htlc(const tal_t *ctx,
11711171
goto fail;
11721172
}
11731173

1174+
if (hin->fail_immediate && htlc_in_update_state(channel, hin, RCVD_ADD_ACK_REVOCATION)) {
1175+
log_debug(channel->log, "failing immediately, as requested");
1176+
/* Failing the htlc, typically done because of htlc dust */
1177+
*failmsg = towire_temporary_node_failure(ctx);
1178+
goto fail;
1179+
}
1180+
11741181
if (!replay && !htlc_in_update_state(channel, hin, RCVD_ADD_ACK_REVOCATION)) {
11751182
*failmsg = towire_temporary_node_failure(ctx);
11761183
goto fail;
@@ -1863,7 +1870,8 @@ static bool channel_added_their_htlc(struct channel *channel,
18631870
added->cltv_expiry, &added->payment_hash,
18641871
op ? &shared_secret : NULL,
18651872
added->blinding, &added->blinding_ss,
1866-
added->onion_routing_packet);
1873+
added->onion_routing_packet,
1874+
added->fail_immediate);
18671875

18681876
/* Save an incoming htlc to the wallet */
18691877
wallet_htlc_save_in(ld->wallet, channel, hin);

0 commit comments

Comments
 (0)