• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/channeldb/migration24/migration.go
1
package migration24
2

3
import (
4
        "bytes"
5
        "encoding/binary"
6
        "io"
7

8
        mig "github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
9
        "github.com/lightningnetwork/lnd/kvdb"
10
)
11

12
var (
13
        // closedChannelBucket stores summarization information concerning
14
        // previously open, but now closed channels.
15
        closedChannelBucket = []byte("closed-chan-bucket")
16

17
        // fwdPackagesKey is the root-level bucket that all forwarding packages
18
        // are written. This bucket is further subdivided based on the short
19
        // channel ID of each channel.
20
        fwdPackagesKey = []byte("fwd-packages")
21
)
22

23
// MigrateFwdPkgCleanup deletes all the forwarding packages of closed channels.
24
// It determines the closed channels by iterating closedChannelBucket. The
25
// ShortChanID found in the ChannelCloseSummary is then used as a key to query
26
// the forwarding packages bucket. If a match is found, it will be deleted.
UNCOV
27
func MigrateFwdPkgCleanup(tx kvdb.RwTx) error {
×
UNCOV
28
        log.Infof("Deleting forwarding packages for closed channels")
×
UNCOV
29

×
UNCOV
30
        // Find all closed channel summaries, which are stored in the
×
UNCOV
31
        // closeBucket.
×
UNCOV
32
        closeBucket := tx.ReadBucket(closedChannelBucket)
×
UNCOV
33
        if closeBucket == nil {
×
UNCOV
34
                return nil
×
UNCOV
35
        }
×
36

UNCOV
37
        var chanSummaries []*mig.ChannelCloseSummary
×
UNCOV
38

×
UNCOV
39
        // appendSummary is a function closure to help put deserialized close
×
UNCOV
40
        // summeries into chanSummaries.
×
UNCOV
41
        appendSummary := func(_ []byte, summaryBytes []byte) error {
×
UNCOV
42
                summaryReader := bytes.NewReader(summaryBytes)
×
UNCOV
43
                chanSummary, err := deserializeCloseChannelSummary(
×
UNCOV
44
                        summaryReader,
×
UNCOV
45
                )
×
UNCOV
46
                if err != nil {
×
47
                        return err
×
48
                }
×
49

50
                // Skip pending channels
UNCOV
51
                if chanSummary.IsPending {
×
UNCOV
52
                        return nil
×
UNCOV
53
                }
×
54

UNCOV
55
                chanSummaries = append(chanSummaries, chanSummary)
×
UNCOV
56
                return nil
×
57
        }
58

UNCOV
59
        if err := closeBucket.ForEach(appendSummary); err != nil {
×
60
                return err
×
61
        }
×
62

63
        // Now we will load the forwarding packages bucket, delete all the
64
        // nested buckets whose source matches the ShortChanID found in the
65
        // closed channel summeraries.
UNCOV
66
        fwdPkgBkt := tx.ReadWriteBucket(fwdPackagesKey)
×
UNCOV
67
        if fwdPkgBkt == nil {
×
68
                return nil
×
69
        }
×
70

71
        // Iterate over all close channels and remove their forwarding packages.
UNCOV
72
        for _, summery := range chanSummaries {
×
UNCOV
73
                sourceBytes := MakeLogKey(summery.ShortChanID.ToUint64())
×
UNCOV
74

×
UNCOV
75
                // First, we will try to find the corresponding bucket. If there
×
UNCOV
76
                // is not a nested bucket matching the ShortChanID, we will skip
×
UNCOV
77
                // it.
×
UNCOV
78
                if fwdPkgBkt.NestedReadBucket(sourceBytes[:]) == nil {
×
UNCOV
79
                        continue
×
80
                }
81

82
                // Otherwise, wipe all the forwarding packages.
UNCOV
83
                if err := fwdPkgBkt.DeleteNestedBucket(
×
UNCOV
84
                        sourceBytes[:],
×
UNCOV
85
                ); err != nil {
×
86
                        return err
×
87
                }
×
88
        }
89

UNCOV
90
        log.Infof("Deletion of forwarding packages of closed channels " +
×
UNCOV
91
                "complete! DB compaction is recommended to free up the" +
×
UNCOV
92
                "disk space.")
×
UNCOV
93
        return nil
×
94
}
95

96
// deserializeCloseChannelSummary will decode a CloseChannelSummary with no
97
// optional fields.
98
func deserializeCloseChannelSummary(
UNCOV
99
        r io.Reader) (*mig.ChannelCloseSummary, error) {
×
UNCOV
100

×
UNCOV
101
        c := &mig.ChannelCloseSummary{}
×
UNCOV
102
        err := mig.ReadElements(
×
UNCOV
103
                r,
×
UNCOV
104
                &c.ChanPoint, &c.ShortChanID, &c.ChainHash, &c.ClosingTXID,
×
UNCOV
105
                &c.CloseHeight, &c.RemotePub, &c.Capacity, &c.SettledBalance,
×
UNCOV
106
                &c.TimeLockedBalance, &c.CloseType, &c.IsPending,
×
UNCOV
107
        )
×
UNCOV
108
        if err != nil {
×
109
                return nil, err
×
110
        }
×
UNCOV
111
        return c, nil
×
112
}
113

114
// makeLogKey converts a uint64 into an 8 byte array.
UNCOV
115
func MakeLogKey(updateNum uint64) [8]byte {
×
UNCOV
116
        var key [8]byte
×
UNCOV
117
        binary.BigEndian.PutUint64(key[:], updateNum)
×
UNCOV
118
        return key
×
UNCOV
119
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc