Skip to content

Commit 729e71f

Browse files
committed
cmd: add migratedb command
1 parent fa62a57 commit 729e71f

File tree

9 files changed

+135
-5
lines changed

9 files changed

+135
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ Your BIP32 HD root key is: xprv9s21ZrQH1...
241241
## Command overview
242242

243243
```text
244+
This tool provides helper functions that can be used rescue
245+
funds locked in lnd channels in case lnd itself cannot run properly anymore.
246+
Complete documentation is available at https://github.com/guggero/chantools/.
247+
244248
Usage:
245249
chantools [command]
246250
@@ -255,6 +259,7 @@ Available Commands:
255259
forceclose Force-close the last state that is in the channel.db provided
256260
genimportscript Generate a script containing the on-chain keys of an lnd wallet that can be imported into other software like bitcoind
257261
help Help about any command
262+
migratedb Apply all recent lnd channel database migrations
258263
removechannel Remove a single channel from the given channel DB
259264
rescueclosed Try finding the private keys for funds that are in outputs of remotely force-closed channels
260265
rescuefunding Rescue funds locked in a funding multisig output that never resulted in a proper channel; this is the command the initiator of the channel needs to run
@@ -288,6 +293,7 @@ Quick access:
288293
+ [filterbackup](doc/chantools_filterbackup.md)
289294
+ [fixoldbackup](doc/chantools_fixoldbackup.md)
290295
+ [genimportscript](doc/chantools_genimportscript.md)
296+
+ [migratedb](doc/chantools_migratedb.md)
291297
+ [forceclose](doc/chantools_forceclose.md)
292298
+ [removechannel](doc/chantools_removechannel.md)
293299
+ [rescueclosed](doc/chantools_rescueclosed.md)

cmd/chantools/migratedb.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/guggero/chantools/lnd"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type migrateDBCommand struct {
11+
ChannelDB string
12+
13+
cmd *cobra.Command
14+
}
15+
16+
func newMigrateDBCommand() *cobra.Command {
17+
cc := &migrateDBCommand{}
18+
cc.cmd = &cobra.Command{
19+
Use: "migratedb",
20+
Short: "Apply all recent lnd channel database migrations",
21+
Long: `This command opens an lnd channel database in write mode
22+
and applies all recent database migrations to it. This can be used to update
23+
an old database file to be compatible with the current version that chantools
24+
needs to read the database content.
25+
26+
CAUTION: Running this command will make it impossible to use the channel DB
27+
with an older version of lnd. Downgrading is not possible and you'll need to
28+
run lnd v0.12.0-beta or later after using this command!'`,
29+
Example: `chantools migratedb \
30+
--channeldb ~/.lnd/data/graph/mainnet/channel.db`,
31+
RunE: cc.Execute,
32+
}
33+
cc.cmd.Flags().StringVar(
34+
&cc.ChannelDB, "channeldb", "", "lnd channel.db file to "+
35+
"migrate",
36+
)
37+
38+
return cc.cmd
39+
}
40+
41+
func (c *migrateDBCommand) Execute(_ *cobra.Command, _ []string) error {
42+
// Check that we have a channel DB.
43+
if c.ChannelDB == "" {
44+
return fmt.Errorf("channel DB is required")
45+
}
46+
db, err := lnd.OpenDB(c.ChannelDB, false)
47+
if err != nil {
48+
return fmt.Errorf("error opening DB: %v", err)
49+
}
50+
51+
return db.Close()
52+
}

cmd/chantools/removechannel.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ func newRemoveChannelCommand() *cobra.Command {
2424
cc.cmd = &cobra.Command{
2525
Use: "removechannel",
2626
Short: "Remove a single channel from the given channel DB",
27-
Example: `chantools --channeldb ~/.lnd/data/graph/mainnet/channel.db \
27+
Long: `Opens the given channel DB in write mode and removes one
28+
single channel from it. This means giving up on any state (and therefore coins)
29+
of that channel and should only be used if the funding transaction of the
30+
channel was never confirmed on chain!
31+
32+
CAUTION: Running this command will make it impossible to use the channel DB
33+
with an older version of lnd. Downgrading is not possible and you'll need to
34+
run lnd v0.12.0-beta or later after using this command!`,
35+
Example: `chantools removechannel \
36+
--channeldb ~/.lnd/data/graph/mainnet/channel.db \
2837
--channel 3149764effbe82718b280de425277e5e7b245a4573aa4a0203ac12cee1c37816:0`,
2938
RunE: cc.Execute,
3039
}

cmd/chantools/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const (
2828
defaultAPIURL = "https://blockstream.info/api"
29-
version = "0.7.1"
29+
version = "0.8.0"
3030

3131
Commit = ""
3232
)
@@ -88,6 +88,7 @@ func init() {
8888
newFixOldBackupCommand(),
8989
newForceCloseCommand(),
9090
newGenImportScriptCommand(),
91+
newMigrateDBCommand(),
9192
newRemoveChannelCommand(),
9293
newRescueClosedCommand(),
9394
newRescueFundingCommand(),
@@ -146,7 +147,7 @@ func (r *rootKey) readWithBirthday() (*hdkeychain.ExtendedKey, time.Time,
146147
case r.BIP39:
147148
extendedKey, err := btc.ReadMnemonicFromTerminal(chainParams)
148149
return extendedKey, time.Unix(0, 0), err
149-
150+
150151
default:
151152
return lnd.ReadAezeed(chainParams)
152153
}

doc/chantools.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Complete documentation is available at https://github.com/guggero/chantools/.
2727
* [chantools fixoldbackup](chantools_fixoldbackup.md) - Fixes an old channel.backup file that is affected by the lnd issue #3881 (unable to derive shachain root key)
2828
* [chantools forceclose](chantools_forceclose.md) - Force-close the last state that is in the channel.db provided
2929
* [chantools genimportscript](chantools_genimportscript.md) - Generate a script containing the on-chain keys of an lnd wallet that can be imported into other software like bitcoind
30+
* [chantools migratedb](chantools_migratedb.md) - Apply all recent lnd channel database migrations
3031
* [chantools removechannel](chantools_removechannel.md) - Remove a single channel from the given channel DB
3132
* [chantools rescueclosed](chantools_rescueclosed.md) - Try finding the private keys for funds that are in outputs of remotely force-closed channels
3233
* [chantools rescuefunding](chantools_rescuefunding.md) - Rescue funds locked in a funding multisig output that never resulted in a proper channel; this is the command the initiator of the channel needs to run

doc/chantools_migratedb.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## chantools migratedb
2+
3+
Apply all recent lnd channel database migrations
4+
5+
### Synopsis
6+
7+
This command opens an lnd channel database in write mode
8+
and applies all recent database migrations to it. This can be used to update
9+
an old database file to be compatible with the current version that chantools
10+
needs to read the database content.
11+
12+
CAUTION: Running this command will make it impossible to use the channel DB
13+
with an older version of lnd. Downgrading is not possible and you'll need to
14+
run lnd v0.12.0-beta or later after using this command!'
15+
16+
```
17+
chantools migratedb [flags]
18+
```
19+
20+
### Examples
21+
22+
```
23+
chantools migratedb \
24+
--channeldb ~/.lnd/data/graph/mainnet/channel.db
25+
```
26+
27+
### Options
28+
29+
```
30+
--channeldb string lnd channel.db file to migrate
31+
-h, --help help for migratedb
32+
```
33+
34+
### Options inherited from parent commands
35+
36+
```
37+
-r, --regtest Indicates if regtest parameters should be used
38+
-t, --testnet Indicates if testnet parameters should be used
39+
```
40+
41+
### SEE ALSO
42+
43+
* [chantools](chantools.md) - Chantools helps recover funds from lightning channels
44+

doc/chantools_removechannel.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22

33
Remove a single channel from the given channel DB
44

5+
### Synopsis
6+
7+
Opens the given channel DB in write mode and removes one
8+
single channel from it. This means giving up on any state (and therefore coins)
9+
of that channel and should only be used if the funding transaction of the
10+
channel was never confirmed on chain!
11+
12+
CAUTION: Running this command will make it impossible to use the channel DB
13+
with an older version of lnd. Downgrading is not possible and you'll need to
14+
run lnd v0.12.0-beta or later after using this command!
15+
516
```
617
chantools removechannel [flags]
718
```
819

920
### Examples
1021

1122
```
12-
chantools --channeldb ~/.lnd/data/graph/mainnet/channel.db \
23+
chantools removechannel \
24+
--channeldb ~/.lnd/data/graph/mainnet/channel.db \
1325
--channel 3149764effbe82718b280de425277e5e7b245a4573aa4a0203ac12cee1c37816:0
1426
```
1527

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ require (
2222
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
2323
)
2424

25-
replace github.com/lightningnetwork/lnd => github.com/guggero/lnd v0.11.1-beta.rc5.0.20201214225837-5a6d8ff78da4
25+
replace github.com/lightningnetwork/lnd => github.com/guggero/lnd v0.11.0-beta.rc4.0.20210101121045-2ae6e51765fe
2626

2727
go 1.13

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
306306
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
307307
github.com/grpc-ecosystem/grpc-gateway v1.14.3 h1:OCJlWkOUoTnl0neNGlf4fUm3TmbEtguw7vR+nGtnDjY=
308308
github.com/grpc-ecosystem/grpc-gateway v1.14.3/go.mod h1:6CwZWGDSPRJidgKAtJVvND6soZe6fT7iteq8wDPdhb0=
309+
github.com/guggero/lnd v0.11.0-beta.rc4.0.20210101121045-2ae6e51765fe h1:jALvgjIGF31xvEInOdMZgyM3BdwkLlUUHdz6H11cWp8=
310+
github.com/guggero/lnd v0.11.0-beta.rc4.0.20210101121045-2ae6e51765fe/go.mod h1:2GyP1IG1kXV5Af/LOCxnXfux1OP3fAGr8zptS5PB2YI=
309311
github.com/guggero/lnd v0.11.1-beta.rc5.0.20201214225837-5a6d8ff78da4 h1:jdsGb7+DPYJnMTx6/HmOVbUFXWls39ZhsasrLRbMyJY=
310312
github.com/guggero/lnd v0.11.1-beta.rc5.0.20201214225837-5a6d8ff78da4/go.mod h1:PGIgxy8aH70Li33YVYkHSaCM8m8LjEevk5h1Dpldrr4=
311313
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
@@ -397,10 +399,13 @@ github.com/lightninglabs/neutrino v0.11.0 h1:lPpYFCtsfJX2W5zI4pWycPmbbBdr7zU+Baf
397399
github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg=
398400
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200 h1:j4iZ1XlUAPQmW6oSzMcJGILYsRHNs+4O3Gk+2Ms5Dww=
399401
github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200/go.mod h1:MlZmoKa7CJP3eR1s5yB7Rm5aSyadpKkxqAwLQmog7N0=
402+
github.com/lightninglabs/neutrino v0.11.1-0.20201210023533-e1978372d15e h1:K5LCCnSAk3NVT/aCy8wNPv0I5JfyLgijg1VX8Gz306E=
403+
github.com/lightninglabs/neutrino v0.11.1-0.20201210023533-e1978372d15e/go.mod h1:KDWfQDKp+CFBxO1t2NRmWuagTY2sYIjpHB1k5vrojTI=
400404
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d/go.mod h1:KDb67YMzoh4eudnzClmvs2FbiLG9vxISmLApUkCa4uI=
401405
github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea h1:oCj48NQ8u7Vz+MmzHqt0db6mxcFZo3Ho7M5gCJauY/k=
402406
github.com/lightningnetwork/lightning-onion v1.0.2-0.20200501022730-3c8c8d0b89ea/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
403407
github.com/lightningnetwork/lnd/cert v1.0.2/go.mod h1:fmtemlSMf5t4hsQmcprSoOykypAPp+9c+0d0iqTScMo=
408+
github.com/lightningnetwork/lnd/cert v1.0.3/go.mod h1:3MWXVLLPI0Mg0XETm9fT4N9Vyy/8qQLmaM5589bEggM=
404409
github.com/lightningnetwork/lnd/clock v1.0.1 h1:QQod8+m3KgqHdvVMV+2DRNNZS1GRFir8mHZYA+Z2hFo=
405410
github.com/lightningnetwork/lnd/clock v1.0.1/go.mod h1:KnQudQ6w0IAMZi1SgvecLZQZ43ra2vpDNj7H/aasemg=
406411
github.com/lightningnetwork/lnd/queue v1.0.1 h1:jzJKcTy3Nj5lQrooJ3aaw9Lau3I0IwvQR5sqtjdv2R0=

0 commit comments

Comments
 (0)