Skip to content

Commit

Permalink
lnd+chanbackup: add lnd config flag
Browse files Browse the repository at this point in the history
In this commit, we add the --disable-backup-archive with a default
as false. When set to true then previous channel backup file will
not be archived but replaced.
  • Loading branch information
Abdulkbk committed Nov 24, 2024
1 parent a4d6827 commit 71ee590
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
36 changes: 21 additions & 15 deletions chanbackup/backupfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@ type MultiFile struct {

// archiveDir is the directory where we'll store old channel backups.
archiveDir string

// deleteOldBackup indicates whether old backups should be deleted
// rather than archived.
deleteOldBackup bool
}

// NewMultiFile create a new multi-file instance at the target location on the
// file system.
func NewMultiFile(fileName string) *MultiFile {

func NewMultiFile(fileName string, deleteOldBackup bool) *MultiFile {
// We'll our temporary backup file in the very same directory as the
// main backup file.
backupFileDir := filepath.Dir(fileName)
Expand All @@ -70,9 +73,10 @@ func NewMultiFile(fileName string) *MultiFile {
)

return &MultiFile{
fileName: fileName,
tempFileName: tempFileName,
archiveDir: archiveDir,
fileName: fileName,
tempFileName: tempFileName,
archiveDir: archiveDir,
deleteOldBackup: deleteOldBackup,
}
}

Expand Down Expand Up @@ -130,16 +134,18 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
return fmt.Errorf("unable to close file: %w", err)
}

// We check if the main backup file exists, if it does we archive it
// before replacing it with the new backup.
if _, err := os.Stat(b.fileName); err == nil {
log.Infof("Archiving old backup file at %v", b.fileName)

if err := createArchiveFile(
b.archiveDir, b.fileName,
); err != nil {
return fmt.Errorf("unable to archive old backup file:"+
" %w", err)
if !b.deleteOldBackup {
// We check if the main backup file exists, if it does we
// archive it before replacing it with the new backup.
if _, err := os.Stat(b.fileName); err == nil {
log.Infof("Archiving old backup file at %v", b.fileName)

if err := createArchiveFile(
b.archiveDir, b.fileName,
); err != nil {
return fmt.Errorf("unable to archive old "+
"backup file: %w", err)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions chanbackup/backupfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestUpdateAndSwap(t *testing.T) {
},
}
for i, testCase := range testCases {
backupFile := NewMultiFile(testCase.fileName)
backupFile := NewMultiFile(testCase.fileName, false)

// To start with, we'll make a random byte slice that'll pose
// as our packed multi backup.
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestExtractMulti(t *testing.T) {
}
for i, testCase := range testCases {
// First, we'll make our backup file with the specified name.
backupFile := NewMultiFile(testCase.fileName)
backupFile := NewMultiFile(testCase.fileName, false)

// With our file made, we'll now attempt to read out the
// multi-file.
Expand Down
15 changes: 12 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ const (
bitcoindBackendName = "bitcoind"
btcdBackendName = "btcd"
neutrinoBackendName = "neutrino"

// defaultDisableBackupArchive indicates whether to disable archiving of
// channel backups. When false (default), old backups are archived to a
// designated location. When true, old backups are simply deleted or
// replaced.
defaultDisableBackupArchive = false
)

var (
Expand Down Expand Up @@ -360,6 +366,8 @@ type Config struct {
MaxPendingChannels int `long:"maxpendingchannels" description:"The maximum number of incoming pending channels permitted per peer."`
BackupFilePath string `long:"backupfilepath" description:"The target location of the channel backup file"`

DisableBackupArchive bool `long:"disable-backup-archive" description:"If set to true, channel backups will be deleted or replaced rather than being archived to a separate location."`

FeeURL string `long:"feeurl" description:"DEPRECATED: Use 'fee.url' option. Optional URL for external fee estimation. If no URL is specified, the method for fee estimation will depend on the chosen backend and network. Must be set for neutrino on mainnet." hidden:"true"`

Bitcoin *lncfg.Chain `group:"Bitcoin" namespace:"bitcoin"`
Expand Down Expand Up @@ -732,9 +740,10 @@ func DefaultConfig() Config {
ServerPingTimeout: defaultGrpcServerPingTimeout,
ClientPingMinWait: defaultGrpcClientPingMinWait,
},
LogConfig: build.DefaultLogConfig(),
WtClient: lncfg.DefaultWtClientCfg(),
HTTPHeaderTimeout: DefaultHTTPHeaderTimeout,
LogConfig: build.DefaultLogConfig(),
WtClient: lncfg.DefaultWtClientCfg(),
HTTPHeaderTimeout: DefaultHTTPHeaderTimeout,
DisableBackupArchive: defaultDisableBackupArchive,
}
}

Expand Down
4 changes: 4 additions & 0 deletions sample-lnd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@
; Example:
; backupfilepath=~/.lnd/data/chain/bitcoin/mainnet/channel.backup

; When false (default), old channel backups are archived to a designated location.
; When true, old backups are simply deleted or replaced.
; disable-backup-archive=true

; The maximum capacity of the block cache in bytes. Increasing this will result
; in more blocks being kept in memory but will increase performance when the
; same block is required multiple times.
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,9 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
chanNotifier: s.channelNotifier,
addrs: dbs.ChanStateDB,
}
backupFile := chanbackup.NewMultiFile(cfg.BackupFilePath)
backupFile := chanbackup.NewMultiFile(
cfg.BackupFilePath, cfg.DisableBackupArchive,
)
startingChans, err := chanbackup.FetchStaticChanBackups(
s.chanStateDB, s.addrSource,
)
Expand Down

0 comments on commit 71ee590

Please sign in to comment.