Skip to content

Commit 4849a69

Browse files
committed
summary: add ancient channel stats
1 parent a872fe2 commit 4849a69

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

cmd/chantools/summary.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
type summaryCommand struct {
1717
APIURL string
1818

19-
Ancient bool
19+
Ancient bool
20+
AncientStats string
2021

2122
inputs *inputFlags
2223
cmd *cobra.Command
@@ -43,13 +44,22 @@ chantools summary --fromchanneldb ~/.lnd/data/graph/mainnet/channel.db`,
4344
&cc.Ancient, "ancient", false, "Create summary of ancient "+
4445
"channel closes with un-swept outputs",
4546
)
47+
cc.cmd.Flags().StringVar(
48+
&cc.AncientStats, "ancientstats", "", "Create summary of "+
49+
"ancient channel closes with un-swept outputs and "+
50+
"print stats for the given list of channels",
51+
)
4652

4753
cc.inputs = newInputFlags(cc.cmd)
4854

4955
return cc.cmd
5056
}
5157

5258
func (c *summaryCommand) Execute(_ *cobra.Command, _ []string) error {
59+
if c.AncientStats != "" {
60+
return summarizeAncientChannelOutputs(c.APIURL, c.AncientStats)
61+
}
62+
5363
// Parse channel entries from any of the possible input files.
5464
entries, err := c.inputs.parseInputType()
5565
if err != nil {
@@ -185,3 +195,48 @@ func summarizeAncientChannels(apiURL string,
185195
log.Infof("Writing result to %s", fileName)
186196
return os.WriteFile(fileName, summaryBytes, 0644)
187197
}
198+
199+
func summarizeAncientChannelOutputs(apiURL, ancientFile string) error {
200+
jsonBytes, err := os.ReadFile(ancientFile)
201+
if err != nil {
202+
return fmt.Errorf("error reading file %s: %w", ancientFile, err)
203+
}
204+
205+
var ancients []ancientChannel
206+
err = json.Unmarshal(jsonBytes, &ancients)
207+
if err != nil {
208+
return fmt.Errorf("error unmarshalling ancient channels: %w",
209+
err)
210+
}
211+
212+
var (
213+
api = newExplorerAPI(apiURL)
214+
numUnspents uint32
215+
unspentSats uint64
216+
)
217+
for _, channel := range ancients {
218+
unspents, err := api.Unspent(channel.Addr)
219+
if err != nil {
220+
return fmt.Errorf("error fetching unspents for %s: %w",
221+
channel.Addr, err)
222+
}
223+
224+
if len(unspents) > 1 {
225+
log.Infof("Address %s has multiple unspents",
226+
channel.Addr)
227+
}
228+
for _, unspent := range unspents {
229+
if unspent.Outspend.Spent {
230+
continue
231+
}
232+
233+
numUnspents++
234+
unspentSats += unspent.Value
235+
}
236+
}
237+
238+
log.Infof("Found %d unspent outputs with %d sats", numUnspents,
239+
unspentSats)
240+
241+
return nil
242+
}

0 commit comments

Comments
 (0)