Skip to content

Commit 3134e5c

Browse files
committed
root+dataformat: add fromchanneldump as input format
1 parent 7150f71 commit 3134e5c

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

cmd/chantools/root.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ type inputFlags struct {
231231
PendingChannels string
232232
FromSummary string
233233
FromChannelDB string
234+
FromChannelDump string
234235
}
235236

236237
func newInputFlags(cmd *cobra.Command) *inputFlags {
@@ -250,6 +251,10 @@ func newInputFlags(cmd *cobra.Command) *inputFlags {
250251
cmd.Flags().StringVar(&f.FromChannelDB, "fromchanneldb", "", "channel "+
251252
"input is in the format of an lnd channel.db file",
252253
)
254+
cmd.Flags().StringVar(
255+
&f.FromChannelDump, "fromchanneldump", "", "channel "+
256+
"input is in the format of a channel dump file",
257+
)
253258

254259
return f
255260
}
@@ -283,6 +288,15 @@ func (f *inputFlags) parseInputType() ([]*dataformat.SummaryEntry, error) {
283288
target = &dataformat.ChannelDBFile{DB: db.ChannelStateDB()}
284289
return target.AsSummaryEntries()
285290

291+
case f.FromChannelDump != "":
292+
content, err = readInput(f.FromChannelDump)
293+
if err != nil {
294+
return nil, fmt.Errorf("error reading channel dump: %w",
295+
err)
296+
}
297+
298+
return dataformat.ExtractSummaryFromDump(string(content))
299+
286300
default:
287301
return nil, errors.New("an input file must be specified")
288302
}

dataformat/summary.go

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package dataformat
33
import (
44
"encoding/hex"
55
"fmt"
6+
"regexp"
7+
"strconv"
8+
"strings"
69

710
"github.com/btcsuite/btcd/btcec/v2"
811
"github.com/lightningnetwork/lnd/keychain"
@@ -62,18 +65,19 @@ type ForceClose struct {
6265
}
6366

6467
type SummaryEntry struct {
65-
RemotePubkey string `json:"remote_pubkey"`
66-
ChannelPoint string `json:"channel_point"`
67-
FundingTXID string `json:"funding_txid"`
68-
FundingTXIndex uint32 `json:"funding_tx_index"`
69-
Capacity uint64 `json:"capacity"`
70-
Initiator bool `json:"initiator"`
71-
LocalBalance uint64 `json:"local_balance"`
72-
RemoteBalance uint64 `json:"remote_balance"`
73-
ChanExists bool `json:"chan_exists_onchain"`
74-
HasPotential bool `json:"has_potential_funds"`
75-
ClosingTX *ClosingTX `json:"closing_tx,omitempty"`
76-
ForceClose *ForceClose `json:"force_close"`
68+
RemotePubkey string `json:"remote_pubkey"`
69+
ChannelPoint string `json:"channel_point"`
70+
FundingTXID string `json:"funding_txid"`
71+
FundingTXIndex uint32 `json:"funding_tx_index"`
72+
Capacity uint64 `json:"capacity"`
73+
Initiator bool `json:"initiator"`
74+
LocalBalance uint64 `json:"local_balance"`
75+
RemoteBalance uint64 `json:"remote_balance"`
76+
ChanExists bool `json:"chan_exists_onchain"`
77+
HasPotential bool `json:"has_potential_funds"`
78+
LocalUnrevokedCommitPoint string `json:"local_unrevoked_commit_point"`
79+
ClosingTX *ClosingTX `json:"closing_tx,omitempty"`
80+
ForceClose *ForceClose `json:"force_close"`
7781
}
7882

7983
type SummaryEntryFile struct {
@@ -91,3 +95,48 @@ type SummaryEntryFile struct {
9195
FundsForceClose uint64 `json:"funds_force_closed_maybe_ours"`
9296
FundsCoopClose uint64 `json:"funds_coop_closed_maybe_ours"`
9397
}
98+
99+
func ExtractSummaryFromDump(data string) ([]*SummaryEntry, error) {
100+
// Regex to match the data pattern.
101+
pattern := `(?ms) ChanPoint: \(string\) \(len=\d+\) "(.*?)",.*? ` +
102+
`Capacity: \(btcutil\.Amount\) ([\d\.]+) BTC,.*? ` +
103+
`LocalUnrevokedCommitPoint: \(string\) \(len=66\) "(.*?)"`
104+
re := regexp.MustCompile(pattern)
105+
106+
var results []*SummaryEntry
107+
matches := re.FindAllStringSubmatch(data, -1)
108+
for _, match := range matches {
109+
if len(match) == 4 {
110+
chanPoint := strings.TrimSpace(match[1])
111+
chanPointParts := strings.Split(chanPoint, ":")
112+
if len(chanPointParts) != 2 {
113+
return nil, fmt.Errorf("invalid chanPoint: %s",
114+
chanPoint)
115+
}
116+
txid := chanPointParts[0]
117+
index, err := strconv.Atoi(chanPointParts[1])
118+
if err != nil {
119+
return nil, fmt.Errorf("unable to parse "+
120+
"index: %w", err)
121+
}
122+
123+
capacity, err := strconv.ParseFloat(match[2], 64)
124+
if err != nil {
125+
return nil, fmt.Errorf("unable to parse "+
126+
"capacity: %w", err)
127+
}
128+
129+
results = append(results, &SummaryEntry{
130+
ChannelPoint: chanPoint,
131+
FundingTXID: txid,
132+
FundingTXIndex: uint32(index),
133+
Capacity: uint64(
134+
capacity * 1e8,
135+
),
136+
LocalUnrevokedCommitPoint: match[3],
137+
})
138+
}
139+
}
140+
141+
return results, nil
142+
}

0 commit comments

Comments
 (0)