Skip to content

Commit

Permalink
feat: save LDK static channel backups to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Sep 21, 2024
1 parent a8cc5d0 commit d90563d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// "github.com/getAlby/hub/ldk_node"

"encoding/hex"
"encoding/json"

decodepay "github.com/nbd-wtf/ln-decodepay"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -1302,7 +1303,7 @@ func (ls *LDKService) handleLdkEvent(event *ldk_node.Event) {
},
})

ls.publishChannelsBackupEvent()
ls.backupChannels()

if eventType.CounterpartyNodeId == nil {
logger.Logger.WithField("event", eventType).Error("channel ready event has no counterparty node ID")
Expand Down Expand Up @@ -1406,7 +1407,7 @@ func (ls *LDKService) handleLdkEvent(event *ldk_node.Event) {
}
}

func (ls *LDKService) publishChannelsBackupEvent() {
func (ls *LDKService) backupChannels() {
ldkChannels := ls.node.ListChannels()
channels := make([]events.ChannelBackupInfo, 0, len(ldkChannels))
for _, ldkChannel := range ldkChannels {
Expand All @@ -1427,6 +1428,28 @@ func (ls *LDKService) publishChannelsBackupEvent() {
})
}

func() {
backupDirectory := filepath.Join(ls.workdir, "static_channel_backups")
err := os.MkdirAll(backupDirectory, os.ModePerm)
if err != nil {
logger.Logger.WithError(err).Error("Failed to make static channel backup directory")
return
}

backupFilePath := filepath.Join(backupDirectory, time.Now().Format(time.RFC3339)+".json")
channelsBytes, err := json.Marshal(channels)
if err != nil {
logger.Logger.WithError(err).Error("Failed to serialize static channel backup to json")
return
}
err = os.WriteFile(backupFilePath, channelsBytes, 0644)
if err != nil {
logger.Logger.WithError(err).Error("Failed to write static channel backup to disk")
return
}
logger.Logger.WithField("backupPath", backupFilePath).Debug("Saved static channel backup to disk")
}()

ls.eventPublisher.Publish(&events.Event{
Event: "nwc_backup_channels",
Properties: &events.ChannelBackupEvent{
Expand Down

0 comments on commit d90563d

Please sign in to comment.