From 3a1a7eb93f3daef5b90cb3ccc9ac48a85b4a0fa3 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 9 Jul 2022 12:53:23 +0930 Subject: [PATCH] wallet: allow saving forwarding scid even if we don't have amount. They're not logically connected: we can know where they wanted to go, but we didn't send it. Where possible, it's the scid *they asked for*; otherwise, it's the scid or fallback to the alias, but do this in the *caller*, not by overriding inside wallet_forwarded_payment_add. Signed-off-by: Rusty Russell --- lightningd/peer_htlcs.c | 12 ++++++------ wallet/wallet.c | 32 +++++++++++++++++--------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 29a7de316..f747af9ea 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -702,7 +702,7 @@ static void forward_htlc(struct htlc_in *hin, local_fail_in_htlc(hin, take(towire_unknown_next_peer(NULL))); wallet_forwarded_payment_add(hin->key.channel->peer->ld->wallet, hin, get_onion_style(hin), - next ? next->scid : NULL, NULL, + scid, NULL, FORWARD_LOCAL_FAILED, WIRE_UNKNOWN_NEXT_PEER); return; @@ -797,7 +797,7 @@ static void forward_htlc(struct htlc_in *hin, fail: local_fail_in_htlc(hin, failmsg); wallet_forwarded_payment_add(ld->wallet, - hin, get_onion_style(hin), next->scid, hout, + hin, get_onion_style(hin), scid, hout, FORWARD_LOCAL_FAILED, fromwire_peektype(failmsg)); } @@ -1337,7 +1337,7 @@ static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout, fulfill_htlc(hout->in, preimage); wallet_forwarded_payment_add(ld->wallet, hout->in, get_onion_style(hout->in), - hout->key.channel->scid, hout, + channel_scid_or_local_alias(hout->key.channel), hout, FORWARD_SETTLED, 0); } } @@ -1465,7 +1465,7 @@ static bool peer_failed_our_htlc(struct channel *channel, if (hout->in) wallet_forwarded_payment_add(ld->wallet, hout->in, get_onion_style(hout->in), - channel->scid, + channel_scid_or_local_alias(channel), hout, FORWARD_FAILED, hout->failmsg ? fromwire_peektype(hout->failmsg) @@ -1628,7 +1628,7 @@ void onchain_failed_our_htlc(const struct channel *channel, take(towire_permanent_channel_failure(NULL))); wallet_forwarded_payment_add(hout->key.channel->peer->ld->wallet, hout->in, get_onion_style(hout->in), - channel->scid, hout, + channel_scid_or_local_alias(channel), hout, FORWARD_LOCAL_FAILED, hout->failmsg ? fromwire_peektype(hout->failmsg) @@ -1795,7 +1795,7 @@ static bool update_out_htlc(struct channel *channel, if (hout->in) { wallet_forwarded_payment_add(ld->wallet, hout->in, get_onion_style(hout->in), - channel->scid, hout, + channel_scid_or_local_alias(channel), hout, FORWARD_OFFERED, 0); } diff --git a/wallet/wallet.c b/wallet/wallet.c index 1cc1703e4..efd966d32 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -4361,23 +4361,17 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in, ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")); db_bind_u64(stmt, 0, in->dbid); - if (out) { - db_bind_u64(stmt, 1, out->dbid); - - /* If we forward we must have a name for this - * channel. It can be either a locally assigned alias, - * or the real scid. */ - db_bind_u64(stmt, 3, channel_scid_or_local_alias(out->key.channel)->u64); - db_bind_amount_msat(stmt, 5, &out->msat); - } else { - /* FORWARD_LOCAL_FAILED may occur before we get htlc_out */ - assert(failcode != 0); - assert(state == FORWARD_LOCAL_FAILED); - db_bind_null(stmt, 1); - db_bind_null(stmt, 3); - db_bind_null(stmt, 5); + /* FORWARD_LOCAL_FAILED may occur before we get htlc_out */ + if (!out || !scid_out) { + assert(failcode != 0); + assert(state == FORWARD_LOCAL_FAILED); } + if (out) + db_bind_u64(stmt, 1, out->dbid); + else + db_bind_null(stmt, 1); + /* We use the LOCAL alias, since that's under our control, and * we keep it stable, whereas the REMOTE alias is likely what * the sender used to specify the channel, but that's under @@ -4385,7 +4379,15 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in, assert(in->key.channel->scid != NULL || in->key.channel->alias[LOCAL]); db_bind_u64(stmt, 2, channel_scid_or_local_alias(in->key.channel)->u64); + if (scid_out) + db_bind_u64(stmt, 3, scid_out->u64); + else + db_bind_null(stmt, 3); db_bind_amount_msat(stmt, 4, &in->msat); + if (out) + db_bind_amount_msat(stmt, 5, &out->msat); + else + db_bind_null(stmt, 5); db_bind_int(stmt, 6, wallet_forward_status_in_db(state)); db_bind_timeabs(stmt, 7, in->received_time);