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

lightningnetwork / lnd / 13566028875

27 Feb 2025 12:09PM UTC coverage: 49.396% (-9.4%) from 58.748%
13566028875

Pull #9555

github

ellemouton
graph/db: populate the graph cache in Start instead of during construction

In this commit, we move the graph cache population logic out of the
ChannelGraph constructor and into its Start method instead.
Pull Request #9555: graph: extract cache from CRUD [6]

34 of 54 new or added lines in 4 files covered. (62.96%)

27464 existing lines in 436 files now uncovered.

101095 of 204664 relevant lines covered (49.4%)

1.54 hits per line

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

0.0
/watchtower/wtdb/migration3/client_db.go
1
package migration3
2

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

8
        "github.com/lightningnetwork/lnd/kvdb"
9
        "github.com/lightningnetwork/lnd/tlv"
10
)
11

12
var (
13
        // cChanDetailsBkt is a top-level bucket storing:
14
        //   channel-id => cChannelSummary -> encoded ClientChanSummary.
15
        //                 => cChanDBID -> db-assigned-id
16
        cChanDetailsBkt = []byte("client-channel-detail-bucket")
17

18
        // cChanDBID is a key used in the cChanDetailsBkt to store the
19
        // db-assigned-id of a channel.
20
        cChanDBID = []byte("client-channel-db-id")
21

22
        // cChanIDIndexBkt is a top-level bucket storing:
23
        //    db-assigned-id -> channel-ID
24
        cChanIDIndexBkt = []byte("client-channel-id-index")
25

26
        // cChannelSummary is a key used in cChanDetailsBkt to store the encoded
27
        // body of ClientChanSummary.
28
        cChannelSummary = []byte("client-channel-summary")
29

30
        // ErrUninitializedDB signals that top-level buckets for the database
31
        // have not been initialized.
32
        ErrUninitializedDB = errors.New("db not initialized")
33

34
        // ErrCorruptChanDetails signals that the clients channel detail's
35
        // on-disk structure deviates from what is expected.
36
        ErrCorruptChanDetails = errors.New("channel details corrupted")
37

38
        byteOrder = binary.BigEndian
39
)
40

41
// MigrateChannelIDIndex adds a new channel ID index to the tower client db.
42
// This index is a mapping from db-assigned ID (a uint64 encoded using BigSize
43
// encoding) to real channel ID (32 bytes). This mapping will allow us to
44
// persist channel pointers with fewer bytes in the future.
UNCOV
45
func MigrateChannelIDIndex(tx kvdb.RwTx) error {
×
UNCOV
46
        log.Infof("Migrating the tower client db to add a new channel ID " +
×
UNCOV
47
                "index which stores a mapping from db-assigned ID to real " +
×
UNCOV
48
                "channel ID")
×
UNCOV
49

×
UNCOV
50
        // Create a new top-level bucket for the new index.
×
UNCOV
51
        indexBkt, err := tx.CreateTopLevelBucket(cChanIDIndexBkt)
×
UNCOV
52
        if err != nil {
×
53
                return err
×
54
        }
×
55

56
        // Get the top-level channel-details bucket. The keys of this bucket
57
        // are the real channel IDs.
UNCOV
58
        chanDetailsBkt := tx.ReadWriteBucket(cChanDetailsBkt)
×
UNCOV
59
        if chanDetailsBkt == nil {
×
60
                return ErrUninitializedDB
×
61
        }
×
62

63
        // Iterate over the keys of the channel-details bucket.
UNCOV
64
        return chanDetailsBkt.ForEach(func(chanID, _ []byte) error {
×
UNCOV
65
                // Ask the db for a new, unique, ID for the index bucket.
×
UNCOV
66
                nextSeq, err := indexBkt.NextSequence()
×
UNCOV
67
                if err != nil {
×
68
                        return err
×
69
                }
×
70

71
                // Encode the sequence number using BigSize encoding.
UNCOV
72
                var newIndex bytes.Buffer
×
UNCOV
73
                err = tlv.WriteVarInt(&newIndex, nextSeq, &[8]byte{})
×
UNCOV
74
                if err != nil {
×
75
                        return err
×
76
                }
×
77

78
                // Add the mapping from the db-assigned ID to the channel ID
79
                // to the new index.
UNCOV
80
                newIndexBytes := newIndex.Bytes()
×
UNCOV
81
                err = indexBkt.Put(newIndexBytes, chanID)
×
UNCOV
82
                if err != nil {
×
83
                        return err
×
84
                }
×
85

UNCOV
86
                chanDetails := chanDetailsBkt.NestedReadWriteBucket(chanID)
×
UNCOV
87
                if chanDetails == nil {
×
88
                        return ErrCorruptChanDetails
×
89
                }
×
90

91
                // Here we ensure that the channel-details bucket includes a
92
                // channel summary. The only reason we do this is so that we can
93
                // simulate a migration fail in a test to ensure that a
94
                // migration fail results in an untouched db.
UNCOV
95
                chanSummaryBytes := chanDetails.Get(cChannelSummary)
×
UNCOV
96
                if chanSummaryBytes == nil {
×
UNCOV
97
                        return ErrCorruptChanDetails
×
UNCOV
98
                }
×
99

100
                // In the channel-details sub-bucket for this channel, add the
101
                // new DB-assigned ID for this channel under the cChanDBID key.
UNCOV
102
                return chanDetails.Put(cChanDBID, newIndexBytes)
×
103
        })
104
}
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