2
0
mirror of https://github.com/guggero/chantools synced 2024-11-15 06:12:49 +00:00
chantools/cmd/chantools/dumpchannels.go

67 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-01-04 22:31:38 +00:00
package main
2019-12-28 15:04:36 +00:00
import (
2020-01-04 22:31:38 +00:00
"fmt"
"path"
2019-12-28 15:04:36 +00:00
"github.com/davecgh/go-spew/spew"
2020-01-04 21:54:03 +00:00
"github.com/guggero/chantools/dump"
2019-12-28 15:04:36 +00:00
"github.com/lightningnetwork/lnd/channeldb"
)
2020-01-04 22:31:38 +00:00
type dumpChannelsCommand struct {
ChannelDB string `long:"channeldb" description:"The lnd channel.db file to dump the channels from."`
Closed bool `long:"closed" description:"Dump all closed channels instead of all open channels."`
2020-01-04 22:31:38 +00:00
}
func (c *dumpChannelsCommand) Execute(_ []string) error {
2020-02-10 10:25:32 +00:00
setupChainParams(cfg)
2020-01-04 22:31:38 +00:00
// Check that we have a channel DB.
if c.ChannelDB == "" {
return fmt.Errorf("channel DB is required")
}
2020-03-07 10:27:04 +00:00
db, err := channeldb.Open(
path.Dir(c.ChannelDB), path.Base(c.ChannelDB),
channeldb.OptionSetSyncFreelist(true),
2020-03-07 10:27:04 +00:00
channeldb.OptionReadOnly(true),
)
2020-01-04 22:31:38 +00:00
if err != nil {
return fmt.Errorf("error opening rescue DB: %v", err)
}
if c.Closed {
return dumpClosedChannelInfo(db)
}
return dumpOpenChannelInfo(db)
2020-01-04 22:31:38 +00:00
}
func dumpOpenChannelInfo(chanDb *channeldb.DB) error {
2019-12-28 15:04:36 +00:00
channels, err := chanDb.FetchAllChannels()
if err != nil {
return err
}
dumpChannels, err := dump.OpenChannelDump(channels, chainParams)
2020-03-30 16:33:00 +00:00
if err != nil {
return fmt.Errorf("error converting to dump format: %v", err)
2019-12-28 15:04:36 +00:00
}
spew.Dump(dumpChannels)
return nil
}
func dumpClosedChannelInfo(chanDb *channeldb.DB) error {
channels, err := chanDb.FetchClosedChannels(false)
if err != nil {
return err
}
dumpChannels, err := dump.ClosedChannelDump(channels, chainParams)
if err != nil {
return fmt.Errorf("error converting to dump format: %v", err)
}
spew.Dump(dumpChannels)
return nil
}