Skip to content

Commit

Permalink
Add closed channels to dumpchannels command
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Aug 26, 2020
1 parent 3a8d95c commit a95f475
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cmd/chantools/chanbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func (c *chanBackupCommand) Execute(_ []string) error {
return fmt.Errorf("channel DB is required")
}
db, err := channeldb.Open(
path.Dir(c.ChannelDB), channeldb.OptionSetSyncFreelist(true),
path.Dir(c.ChannelDB), path.Base(c.ChannelDB),
channeldb.OptionSetSyncFreelist(true),
channeldb.OptionReadOnly(true),
)
if err != nil {
Expand Down
29 changes: 25 additions & 4 deletions cmd/chantools/dumpchannels.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

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."`
}

func (c *dumpChannelsCommand) Execute(_ []string) error {
Expand All @@ -21,26 +22,46 @@ func (c *dumpChannelsCommand) Execute(_ []string) error {
return fmt.Errorf("channel DB is required")
}
db, err := channeldb.Open(
path.Dir(c.ChannelDB), channeldb.OptionSetSyncFreelist(true),
path.Dir(c.ChannelDB), path.Base(c.ChannelDB),
channeldb.OptionSetSyncFreelist(true),
channeldb.OptionReadOnly(true),
)
if err != nil {
return fmt.Errorf("error opening rescue DB: %v", err)
}
return dumpChannelInfo(db)

if c.Closed {
return dumpClosedChannelInfo(db)
}
return dumpOpenChannelInfo(db)
}

func dumpChannelInfo(chanDb *channeldb.DB) error {
func dumpOpenChannelInfo(chanDb *channeldb.DB) error {
channels, err := chanDb.FetchAllChannels()
if err != nil {
return err
}

dumpChannels, err := dump.ChannelDump(channels, chainParams)
dumpChannels, err := dump.OpenChannelDump(channels, chainParams)
if err != nil {
return fmt.Errorf("error converting to dump format: %v", err)
}

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
}
3 changes: 2 additions & 1 deletion cmd/chantools/forceclose.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (c *forceCloseCommand) Execute(_ []string) error {
return fmt.Errorf("rescue DB is required")
}
db, err := channeldb.Open(
path.Dir(c.ChannelDB), channeldb.OptionSetSyncFreelist(true),
path.Dir(c.ChannelDB), path.Base(c.ChannelDB),
channeldb.OptionSetSyncFreelist(true),
channeldb.OptionReadOnly(true),
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/chantools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

const (
defaultAPIURL = "https://blockstream.info/api"
version = "0.2.0"
version = "0.3.0"
)

var (
Expand Down Expand Up @@ -170,7 +170,7 @@ func parseInputType(cfg *config) ([]*dataformat.SummaryEntry, error) {

case cfg.FromChannelDB != "":
db, err := channeldb.Open(
path.Dir(cfg.FromChannelDB),
path.Dir(cfg.FromChannelDB), path.Base(cfg.FromChannelDB),
channeldb.OptionSetSyncFreelist(true),
channeldb.OptionReadOnly(true),
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/chantools/rescueclosed.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func (c *rescueClosedCommand) Execute(_ []string) error {
return fmt.Errorf("rescue DB is required")
}
db, err := channeldb.Open(
path.Dir(c.ChannelDB), channeldb.OptionSetSyncFreelist(true),
path.Dir(c.ChannelDB), path.Base(c.ChannelDB),
channeldb.OptionSetSyncFreelist(true),
channeldb.OptionReadOnly(true),
)
if err != nil {
Expand Down
63 changes: 59 additions & 4 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ type OpenChannel struct {
RemoteShutdownScript lnwire.DeliveryAddress
}

// ClosedChannel is the information we want to dump from a closed channel in
// lnd's channel DB. See `channeldb.ChannelCloseSummary` for information about
// the fields.
type ClosedChannel struct {
ChanPoint string
ShortChanID lnwire.ShortChannelID
ChainHash chainhash.Hash
ClosingTXID string
RemotePub string
Capacity btcutil.Amount
CloseHeight uint32
SettledBalance btcutil.Amount
TimeLockedBalance btcutil.Amount
CloseType string
IsPending bool
RemoteCurrentRevocation string
RemoteNextRevocation string
LocalChanConfig ChannelConfig
}

// ChannelConfig is the information we want to dump from a channel
// configuration. See `channeldb.ChannelConfig` for more information about the
// fields.
Expand All @@ -92,10 +112,10 @@ type KeyDescriptor struct {
PubKey string
}

// ChannelDump converts the channels in the given channel DB into a dumpable
// format.
func ChannelDump(channels []*channeldb.OpenChannel, params *chaincfg.Params) (
[]OpenChannel, error) {
// OpenChannelDump converts the open channels in the given channel DB into a
// dumpable format.
func OpenChannelDump(channels []*channeldb.OpenChannel,
params *chaincfg.Params) ([]OpenChannel, error) {

dumpChannels := make([]OpenChannel, len(channels))
for idx, channel := range channels {
Expand Down Expand Up @@ -154,6 +174,41 @@ func ChannelDump(channels []*channeldb.OpenChannel, params *chaincfg.Params) (
return dumpChannels, nil
}

// ClosedChannelDump converts the closed channels in the given channel DB into a
// dumpable format.
func ClosedChannelDump(channels []*channeldb.ChannelCloseSummary,
params *chaincfg.Params) ([]ClosedChannel, error) {

dumpChannels := make([]ClosedChannel, len(channels))
for idx, channel := range channels {
dumpChannels[idx] = ClosedChannel{
ChanPoint: channel.ChanPoint.String(),
ShortChanID: channel.ShortChanID,
ChainHash: channel.ChainHash,
ClosingTXID: channel.ClosingTXID.String(),
RemotePub: PubKeyToString(channel.RemotePub),
Capacity: channel.Capacity,
CloseHeight: channel.CloseHeight,
SettledBalance: channel.SettledBalance,
TimeLockedBalance: channel.TimeLockedBalance,
CloseType: fmt.Sprintf(
"%d", channel.CloseType,
),
IsPending: channel.IsPending,
RemoteCurrentRevocation: PubKeyToString(
channel.RemoteCurrentRevocation,
),
RemoteNextRevocation: PubKeyToString(
channel.RemoteNextRevocation,
),
LocalChanConfig: ToChannelConfig(
params, channel.LocalChanConfig,
),
}
}
return dumpChannels, nil
}

// BackupDump converts the given multi backup into a dumpable format.
func BackupDump(multi *chanbackup.Multi, params *chaincfg.Params) []BackupSingle {

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ require (
gopkg.in/yaml.v2 v2.2.3 // indirect
)

replace github.com/lightningnetwork/lnd => github.com/guggero/lnd v0.9.0-beta-rc1.0.20200307101759-2650bff06031
replace github.com/lightningnetwork/lnd => github.com/guggero/lnd v0.9.0-beta-rc4.0.20200826102054-8c9171307182

go 1.13
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92Bcuy
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.8.6 h1:XvND7+MPP7Jp+JpqSZ7naSl5nVZf6k0LbL1V3EKh0zc=
github.com/grpc-ecosystem/grpc-gateway v1.8.6/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/guggero/lnd v0.9.0-beta-rc1.0.20200307101759-2650bff06031 h1:G7UpjWLXdmFi1gYVidq6c/EJaH/eX0HixAOVxxAT/K0=
github.com/guggero/lnd v0.9.0-beta-rc1.0.20200307101759-2650bff06031/go.mod h1:bMBXSbO0hwk9HmZSbI04SDTHxTK/iEOvzfzQkNSIJmU=
github.com/guggero/lnd v0.9.0-beta-rc4.0.20200826102054-8c9171307182 h1:VMQ3vCjVGhT1k1agfGMEDbtqtw6xpXGfgO3xaQ/TyLM=
github.com/guggero/lnd v0.9.0-beta-rc4.0.20200826102054-8c9171307182/go.mod h1:bMBXSbO0hwk9HmZSbI04SDTHxTK/iEOvzfzQkNSIJmU=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jackpal/gateway v1.0.5 h1:qzXWUJfuMdlLMtt0a3Dgt+xkWQiA5itDEITVJtuSwMc=
Expand Down

0 comments on commit a95f475

Please sign in to comment.