2020-01-04 21:44:43 +00:00
|
|
|
package dataformat
|
2019-11-24 11:32:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-01-04 22:10:26 +00:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-11-24 11:32:59 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 17:53:47 +00:00
|
|
|
type NumberString uint64
|
|
|
|
|
|
|
|
func (n *NumberString) UnmarshalJSON(b []byte) error {
|
|
|
|
if b[0] != '"' {
|
|
|
|
return json.Unmarshal(b, (*uint64)(n))
|
|
|
|
}
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(b, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*n = NumberString(i)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-24 11:32:59 +00:00
|
|
|
type InputFile interface {
|
|
|
|
AsSummaryEntries() ([]*SummaryEntry, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Input interface {
|
|
|
|
AsSummaryEntry() *SummaryEntry
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
type ListChannelsFile struct {
|
|
|
|
Channels []*ListChannelsChannel `json:"channels"`
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
func (f *ListChannelsFile) AsSummaryEntries() ([]*SummaryEntry, error) {
|
2019-11-24 11:32:59 +00:00
|
|
|
result := make([]*SummaryEntry, len(f.Channels))
|
|
|
|
for idx, entry := range f.Channels {
|
|
|
|
result[idx] = entry.AsSummaryEntry()
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
type ListChannelsChannel struct {
|
2019-12-06 17:53:47 +00:00
|
|
|
RemotePubkey string `json:"remote_pubkey"`
|
|
|
|
ChannelPoint string `json:"channel_point"`
|
|
|
|
Capacity NumberString `json:"capacity"`
|
|
|
|
Initiator bool `json:"initiator"`
|
|
|
|
LocalBalance NumberString `json:"local_balance"`
|
|
|
|
RemoteBalance NumberString `json:"remote_balance"`
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
func (c *ListChannelsChannel) AsSummaryEntry() *SummaryEntry {
|
2019-11-24 11:32:59 +00:00
|
|
|
return &SummaryEntry{
|
|
|
|
RemotePubkey: c.RemotePubkey,
|
|
|
|
ChannelPoint: c.ChannelPoint,
|
2020-01-04 23:33:43 +00:00
|
|
|
FundingTXID: FundingTXID(c.ChannelPoint),
|
|
|
|
FundingTXIndex: FundingTXIndex(c.ChannelPoint),
|
2019-12-06 17:53:47 +00:00
|
|
|
Capacity: uint64(c.Capacity),
|
2019-11-24 11:32:59 +00:00
|
|
|
Initiator: c.Initiator,
|
2019-12-06 17:53:47 +00:00
|
|
|
LocalBalance: uint64(c.LocalBalance),
|
|
|
|
RemoteBalance: uint64(c.RemoteBalance),
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
type PendingChannelsFile struct {
|
|
|
|
PendingOpen []*PendingChannelsChannel `json:"pending_open_channels"`
|
|
|
|
PendingClosing []*PendingChannelsChannel `json:"pending_closing_channels"`
|
|
|
|
PendingForceClosing []*PendingChannelsChannel `json:"pending_force_closing_channels"`
|
|
|
|
WaitingClose []*PendingChannelsChannel `json:"waiting_close_channels"`
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
func (f *PendingChannelsFile) AsSummaryEntries() ([]*SummaryEntry, error) {
|
2019-11-24 11:32:59 +00:00
|
|
|
numChannels := len(f.PendingOpen) + len(f.PendingClosing) +
|
|
|
|
len(f.PendingForceClosing) + len(f.WaitingClose)
|
|
|
|
result := make([]*SummaryEntry, numChannels)
|
|
|
|
idx := 0
|
|
|
|
for _, entry := range f.PendingOpen {
|
|
|
|
result[idx] = entry.AsSummaryEntry()
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, entry := range f.PendingClosing {
|
|
|
|
result[idx] = entry.AsSummaryEntry()
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, entry := range f.PendingForceClosing {
|
|
|
|
result[idx] = entry.AsSummaryEntry()
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, entry := range f.WaitingClose {
|
|
|
|
result[idx] = entry.AsSummaryEntry()
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
type PendingChannelsChannel struct {
|
2019-11-24 11:32:59 +00:00
|
|
|
Channel struct {
|
2019-12-06 17:53:47 +00:00
|
|
|
RemotePubkey string `json:"remote_node_pub"`
|
|
|
|
ChannelPoint string `json:"channel_point"`
|
|
|
|
Capacity NumberString `json:"capacity"`
|
|
|
|
LocalBalance NumberString `json:"local_balance"`
|
|
|
|
RemoteBalance NumberString `json:"remote_balance"`
|
2019-11-24 11:32:59 +00:00
|
|
|
} `json:"channel"`
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
func (c *PendingChannelsChannel) AsSummaryEntry() *SummaryEntry {
|
2019-11-24 11:32:59 +00:00
|
|
|
return &SummaryEntry{
|
|
|
|
RemotePubkey: c.Channel.RemotePubkey,
|
|
|
|
ChannelPoint: c.Channel.ChannelPoint,
|
2020-01-04 23:33:43 +00:00
|
|
|
FundingTXID: FundingTXID(c.Channel.ChannelPoint),
|
|
|
|
FundingTXIndex: FundingTXIndex(c.Channel.ChannelPoint),
|
2019-12-06 17:53:47 +00:00
|
|
|
Capacity: uint64(c.Channel.Capacity),
|
2019-11-24 11:32:59 +00:00
|
|
|
Initiator: false,
|
2019-12-06 17:53:47 +00:00
|
|
|
LocalBalance: uint64(c.Channel.LocalBalance),
|
|
|
|
RemoteBalance: uint64(c.Channel.RemoteBalance),
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
type ChannelDBFile struct {
|
2021-12-13 09:34:26 +00:00
|
|
|
DB *channeldb.ChannelStateDB
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 21:44:43 +00:00
|
|
|
func (c *ChannelDBFile) AsSummaryEntries() ([]*SummaryEntry, error) {
|
|
|
|
channels, err := c.DB.FetchAllChannels()
|
2019-11-24 11:32:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error fetching channels: %v", err)
|
|
|
|
}
|
|
|
|
result := make([]*SummaryEntry, len(channels))
|
|
|
|
for idx, channel := range channels {
|
|
|
|
result[idx] = &SummaryEntry{
|
|
|
|
RemotePubkey: hex.EncodeToString(
|
|
|
|
channel.IdentityPub.SerializeCompressed(),
|
|
|
|
),
|
|
|
|
ChannelPoint: channel.FundingOutpoint.String(),
|
|
|
|
FundingTXID: channel.FundingOutpoint.Hash.String(),
|
|
|
|
FundingTXIndex: channel.FundingOutpoint.Index,
|
|
|
|
Capacity: uint64(channel.Capacity),
|
|
|
|
Initiator: channel.IsInitiator,
|
2019-12-06 17:53:47 +00:00
|
|
|
LocalBalance: uint64(
|
2019-11-24 11:32:59 +00:00
|
|
|
channel.LocalCommitment.LocalBalance.ToSatoshis(),
|
|
|
|
),
|
2019-12-06 17:53:47 +00:00
|
|
|
RemoteBalance: uint64(
|
2019-11-24 11:32:59 +00:00
|
|
|
channel.LocalCommitment.RemoteBalance.ToSatoshis(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SummaryEntryFile) AsSummaryEntries() ([]*SummaryEntry, error) {
|
|
|
|
return f.Channels, nil
|
|
|
|
}
|
|
|
|
|
2020-01-04 23:33:43 +00:00
|
|
|
func FundingTXID(chanPoint string) string {
|
2019-11-24 11:32:59 +00:00
|
|
|
parts := strings.Split(chanPoint, ":")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
panic(fmt.Errorf("channel point not in format <txid>:<idx>: %s",
|
|
|
|
chanPoint))
|
|
|
|
}
|
|
|
|
return parts[0]
|
|
|
|
}
|
|
|
|
|
2020-01-04 23:33:43 +00:00
|
|
|
func FundingTXIndex(chanPoint string) uint32 {
|
2019-11-24 11:32:59 +00:00
|
|
|
parts := strings.Split(chanPoint, ":")
|
|
|
|
if len(parts) != 2 {
|
2020-01-04 22:58:10 +00:00
|
|
|
panic(fmt.Errorf("channel point %s not in format <txid>:<idx>",
|
2019-11-24 11:32:59 +00:00
|
|
|
chanPoint))
|
|
|
|
}
|
|
|
|
return uint32(parseInt(parts[1]))
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:53:47 +00:00
|
|
|
func parseInt(str string) uint64 {
|
2019-11-24 11:32:59 +00:00
|
|
|
index, err := strconv.Atoi(str)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("error parsing '%s' as int: %v", str, err))
|
|
|
|
}
|
2019-12-06 17:53:47 +00:00
|
|
|
return uint64(index)
|
2019-11-24 11:32:59 +00:00
|
|
|
}
|