Skip to content

Commit fc4d01c

Browse files
rustyrussellniftynei
authored andcommitted
db: put scids in forwards even if we didn't actually send.
If the peer is not connected, or other error which means we don't actually create an outgoing HTLC, we don't record the short_channel_id. This is unhelpful! Pass the scid down to the wallet code, and explicitly hand the scid and amount down to the notification code rather than handing it the htlc_out (which it doesn't need). Changelog-Changed: JSON API: `listforwards` now shows `out_channel` even if we couldn't forward. Signed-off-by: Rusty Russell <[email protected]>
1 parent 5e2053f commit fc4d01c

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This release named by Vasil Dimov @vasild.
2626

2727
- JSON API: The hooks `db_write`, `invoice_payment`, and `rpc_command` now accept `{ "result": "continue" }` to mean "do default action". ([3475](https://github.com/ElementsProject/lightning/pull/3475))
2828
- Plugin: Multiple plugins can now register for the htlc_accepted hook. ([3489](https://github.com/ElementsProject/lightning/pull/3489))
29+
- JSON API: `listforwards` now shows `out_channel` even if we couldn't forward.
2930
- JSON API: `funchannel_cancel`: only the opener of a fundchannel can cancel the channel open ([3336](https://github.com/ElementsProject/lightning/pull/3336))
3031
- JSON API: `sendpay` optional `msatoshi` param for non-MPP (if set), must be the exact amount sent to the final recipient. ([3470](https://github.com/ElementsProject/lightning/pull/3470))
3132
- JSON API: `waitinvoice` now returns error code 903 to designate that the invoice expired during wait, instead of the previous -2 ([3441](https://github.com/ElementsProject/lightning/pull/3441))

lightningd/notification.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ void notify_channel_opened(struct lightningd *ld, struct node_id *node_id,
176176

177177
static void forward_event_notification_serialize(struct json_stream *stream,
178178
const struct htlc_in *in,
179-
const struct htlc_out *out,
179+
const struct short_channel_id *scid_out,
180+
const struct amount_msat *amount_out,
180181
enum forward_status state,
181182
enum onion_type failcode,
182183
struct timeabs *resolved_time)
@@ -186,10 +187,16 @@ static void forward_event_notification_serialize(struct json_stream *stream,
186187
struct forwarding *cur = tal(tmpctx, struct forwarding);
187188
cur->channel_in = *in->key.channel->scid;
188189
cur->msat_in = in->msat;
189-
if (out) {
190-
cur->channel_out = *out->key.channel->scid;
191-
cur->msat_out = out->msat;
192-
assert(amount_msat_sub(&cur->fee, in->msat, out->msat));
190+
if (scid_out) {
191+
cur->channel_out = *scid_out;
192+
if (amount_out) {
193+
cur->msat_out = *amount_out;
194+
assert(amount_msat_sub(&cur->fee,
195+
in->msat, *amount_out));
196+
} else {
197+
cur->msat_out = AMOUNT_MSAT(0);
198+
cur->fee = AMOUNT_MSAT(0);
199+
}
193200
} else {
194201
cur->channel_out.u64 = 0;
195202
cur->msat_out = AMOUNT_MSAT(0);
@@ -209,21 +216,23 @@ REGISTER_NOTIFICATION(forward_event,
209216

210217
void notify_forward_event(struct lightningd *ld,
211218
const struct htlc_in *in,
212-
const struct htlc_out *out,
219+
const struct short_channel_id *scid_out,
220+
const struct amount_msat *amount_out,
213221
enum forward_status state,
214222
enum onion_type failcode,
215223
struct timeabs *resolved_time)
216224
{
217225
void (*serialize)(struct json_stream *,
218226
const struct htlc_in *,
219-
const struct htlc_out *,
227+
const struct short_channel_id *,
228+
const struct amount_msat *,
220229
enum forward_status,
221230
enum onion_type,
222231
struct timeabs *) = forward_event_notification_gen.serialize;
223232

224233
struct jsonrpc_notification *n
225234
= jsonrpc_notification_start(NULL, forward_event_notification_gen.topic);
226-
serialize(n->stream, in, out, state, failcode, resolved_time);
235+
serialize(n->stream, in, scid_out, amount_out, state, failcode, resolved_time);
227236
jsonrpc_notification_end(n);
228237
plugins_notify(ld->plugins, take(n));
229238
}

lightningd/notification.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ void notify_channel_opened(struct lightningd *ld, struct node_id *node_id,
5353

5454
void notify_forward_event(struct lightningd *ld,
5555
const struct htlc_in *in,
56-
const struct htlc_out *out,
56+
/* May be NULL if we don't know. */
57+
const struct short_channel_id *scid_out,
58+
/* May be NULL. */
59+
const struct amount_msat *amount_out,
5760
enum forward_status state,
5861
enum onion_type failcode,
5962
struct timeabs *resolved_time);

lightningd/peer_htlcs.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds UNU
404404
/* here we haven't called connect_htlc_out(),
405405
* so set htlc field with NULL */
406406
wallet_forwarded_payment_add(ld->wallet,
407-
hout->in, NULL,
407+
hout->in, NULL, NULL,
408408
FORWARD_LOCAL_FAILED,
409409
failure_code);
410410
}
@@ -514,7 +514,7 @@ static void forward_htlc(struct htlc_in *hin,
514514
if (!next || !next->scid) {
515515
local_fail_htlc(hin, WIRE_UNKNOWN_NEXT_PEER, NULL);
516516
wallet_forwarded_payment_add(hin->key.channel->peer->ld->wallet,
517-
hin, NULL,
517+
hin, next ? next->scid : NULL, NULL,
518518
FORWARD_LOCAL_FAILED,
519519
hin->failcode);
520520
return;
@@ -603,7 +603,7 @@ static void forward_htlc(struct htlc_in *hin,
603603
fail:
604604
local_fail_htlc(hin, failcode, next->scid);
605605
wallet_forwarded_payment_add(ld->wallet,
606-
hin, hout,
606+
hin, next->scid, hout,
607607
FORWARD_LOCAL_FAILED,
608608
hin->failcode);
609609
}
@@ -637,7 +637,7 @@ static void channel_resolve_reply(struct subd *gossip, const u8 *msg,
637637
if (!peer_id) {
638638
local_fail_htlc(gr->hin, WIRE_UNKNOWN_NEXT_PEER, NULL);
639639
wallet_forwarded_payment_add(gr->hin->key.channel->peer->ld->wallet,
640-
gr->hin, NULL,
640+
gr->hin, &gr->next_channel, NULL,
641641
FORWARD_LOCAL_FAILED,
642642
gr->hin->failcode);
643643
tal_free(gr);
@@ -1009,7 +1009,8 @@ static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout,
10091009
payment_succeeded(ld, hout, preimage);
10101010
else if (hout->in) {
10111011
fulfill_htlc(hout->in, preimage);
1012-
wallet_forwarded_payment_add(ld->wallet, hout->in, hout,
1012+
wallet_forwarded_payment_add(ld->wallet, hout->in,
1013+
hout->key.channel->scid, hout,
10131014
FORWARD_SETTLED, 0);
10141015
}
10151016
}
@@ -1102,6 +1103,7 @@ static bool peer_failed_our_htlc(struct channel *channel,
11021103

11031104
if (hout->in)
11041105
wallet_forwarded_payment_add(ld->wallet, hout->in,
1106+
channel->scid,
11051107
hout, FORWARD_FAILED, hout->failcode);
11061108

11071109
return true;
@@ -1141,7 +1143,7 @@ void onchain_failed_our_htlc(const struct channel *channel,
11411143
local_fail_htlc(hout->in, WIRE_PERMANENT_CHANNEL_FAILURE,
11421144
hout->key.channel->scid);
11431145
wallet_forwarded_payment_add(hout->key.channel->peer->ld->wallet,
1144-
hout->in, hout,
1146+
hout->in, channel->scid, hout,
11451147
FORWARD_LOCAL_FAILED,
11461148
hout->failcode);
11471149
}
@@ -1266,7 +1268,8 @@ static bool update_out_htlc(struct channel *channel,
12661268
hout->msat);
12671269

12681270
if (hout->in) {
1269-
wallet_forwarded_payment_add(ld->wallet, hout->in, hout,
1271+
wallet_forwarded_payment_add(ld->wallet, hout->in,
1272+
channel->scid, hout,
12701273
FORWARD_OFFERED, 0);
12711274
}
12721275

@@ -1760,7 +1763,7 @@ void peer_got_revoke(struct channel *channel, const u8 *msg)
17601763
local_fail_htlc(hin, failcodes[i], NULL);
17611764
// in fact, now we don't know if this htlc is a forward or localpay!
17621765
wallet_forwarded_payment_add(ld->wallet,
1763-
hin, NULL,
1766+
hin, NULL, NULL,
17641767
FORWARD_LOCAL_FAILED,
17651768
hin->failcode);
17661769
}

wallet/test/run-wallet.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@ void notify_disconnect(struct lightningd *ld UNNEEDED, struct node_id *nodeid UN
388388
/* Generated stub for notify_forward_event */
389389
void notify_forward_event(struct lightningd *ld UNNEEDED,
390390
const struct htlc_in *in UNNEEDED,
391-
const struct htlc_out *out UNNEEDED,
391+
/* May be NULL if we don't know. */
392+
const struct short_channel_id *scid_out UNNEEDED,
393+
/* May be NULL. */
394+
const struct amount_msat *amount_out UNNEEDED,
392395
enum forward_status state UNNEEDED,
393396
enum onion_type failcode UNNEEDED,
394397
struct timeabs *resolved_time UNNEEDED)

wallet/wallet.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,7 @@ static bool wallet_forwarded_payment_update(struct wallet *w,
33653365
}
33663366

33673367
void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
3368+
const struct short_channel_id *scid_out,
33683369
const struct htlc_out *out,
33693370
enum forward_status state,
33703371
enum onion_type failcode)
@@ -3432,7 +3433,8 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
34323433
db_exec_prepared_v2(take(stmt));
34333434

34343435
notify:
3435-
notify_forward_event(w->ld, in, out, state, failcode, resolved_time);
3436+
notify_forward_event(w->ld, in, scid_out, out ? &out->msat : NULL,
3437+
state, failcode, resolved_time);
34363438
}
34373439

34383440
struct amount_msat wallet_total_forward_fees(struct wallet *w)

wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx,
11731173
* Add of update a forwarded_payment
11741174
*/
11751175
void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
1176+
const struct short_channel_id *scid_out,
11761177
const struct htlc_out *out,
11771178
enum forward_status state,
11781179
enum onion_type failcode);

0 commit comments

Comments
 (0)