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

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

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

85.29
/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.
27
func MigrateFwdPkgCleanup(tx kvdb.RwTx) error {
5✔
28
        log.Infof("Deleting forwarding packages for closed channels")
5✔
29

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

37
        var chanSummaries []*mig.ChannelCloseSummary
4✔
38

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

50
                // Skip pending channels
51
                if chanSummary.IsPending {
7✔
52
                        return nil
1✔
53
                }
1✔
54

55
                chanSummaries = append(chanSummaries, chanSummary)
5✔
56
                return nil
5✔
57
        }
58

59
        if err := closeBucket.ForEach(appendSummary); err != nil {
4✔
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.
66
        fwdPkgBkt := tx.ReadWriteBucket(fwdPackagesKey)
4✔
67
        if fwdPkgBkt == nil {
4✔
68
                return nil
×
69
        }
×
70

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

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

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

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

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

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

114
// makeLogKey converts a uint64 into an 8 byte array.
115
func MakeLogKey(updateNum uint64) [8]byte {
12,817✔
116
        var key [8]byte
12,817✔
117
        binary.BigEndian.PutUint64(key[:], updateNum)
12,817✔
118
        return key
12,817✔
119
}
12,817✔
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