From ebc8ef5073ee87519b2c96da33d089dd29814a13 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 9 Oct 2023 15:15:50 +1030 Subject: [PATCH] lightningd: save peer's commitment anchor info into db. We actually only need to remember the last two, worst case. Signed-off-by: Rusty Russell --- lightningd/channel_control.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 3ec0c3b8d2f7..ab79133582b8 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1210,6 +1210,34 @@ static void handle_channel_upgrade(struct channel *channel, wallet_channel_save(channel->peer->ld->wallet, channel); } +static void handle_local_anchors(struct channel *channel, const u8 *msg) +{ + u64 remote_commitnum; + struct local_anchor_info *anchors; + + if (!fromwire_channeld_local_anchor_info(msg, msg, &remote_commitnum, + &anchors)) { + channel_internal_error(channel, + "bad channeld_local_anchor_info %s", + tal_hex(channel, msg)); + return; + } + + /* Update all these anchors */ + for (size_t i = 0; i < tal_count(anchors); i++) { + wallet_set_local_anchor(channel->peer->ld->wallet, + channel->dbid, + anchors + i, + remote_commitnum); + } + /* Now safe to forget old ones */ + if (remote_commitnum > 2) { + wallet_remove_local_anchors(channel->peer->ld->wallet, + channel->dbid, + remote_commitnum - 2); + } +} + static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) { enum channeld_wire t = fromwire_peektype(msg); @@ -1219,7 +1247,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) peer_sending_commitsig(sd->channel, msg); break; case WIRE_CHANNELD_LOCAL_ANCHOR_INFO: - /* FIXME */ + handle_local_anchors(sd->channel, msg); break; case WIRE_CHANNELD_GOT_COMMITSIG: peer_got_commitsig(sd->channel, msg);