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

lightningnetwork / lnd / 14193549836

01 Apr 2025 10:40AM UTC coverage: 69.046% (+0.007%) from 69.039%
14193549836

Pull #9665

github

web-flow
Merge e8825f209 into b01f4e514
Pull Request #9665: kvdb: bump etcd libs to v3.5.12

133439 of 193262 relevant lines covered (69.05%)

22119.45 hits per line

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

85.0
/watchtower/wtdb/migration5/client_db.go
1
package migration5
2

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

7
        "github.com/lightningnetwork/lnd/kvdb"
8
)
9

10
var (
11
        // cTowerBkt is a top-level bucket storing:
12
        //    tower-id -> encoded Tower.
13
        cTowerBkt = []byte("client-tower-bucket")
14

15
        // cTowerIDToSessionIDIndexBkt is a top-level bucket storing:
16
        //         tower-id -> session-id -> 1
17
        cTowerIDToSessionIDIndexBkt = []byte(
18
                "client-tower-to-session-index-bucket",
19
        )
20

21
        // ErrUninitializedDB signals that top-level buckets for the database
22
        // have not been initialized.
23
        ErrUninitializedDB = errors.New("db not initialized")
24

25
        // byteOrder is the default endianness used when serializing integers.
26
        byteOrder = binary.BigEndian
27
)
28

29
// MigrateCompleteTowerToSessionIndex ensures that the tower-to-session index
30
// contains entries for all towers in the db. This is necessary because
31
// migration1 only created entries in the index for towers that the client had
32
// at least one session with. This migration thus makes sure that there is
33
// always a tower-to-sessions index entry for a tower even if there are no
34
// sessions with that tower.
35
func MigrateCompleteTowerToSessionIndex(tx kvdb.RwTx) error {
4✔
36
        log.Infof("Migrating the tower client db to ensure that there is an " +
4✔
37
                "entry in the towerID-to-sessionID index for every tower in " +
4✔
38
                "the db")
4✔
39

4✔
40
        // First, we collect all the towers that we should add an entry for in
4✔
41
        // the index.
4✔
42
        towerIDs, err := listTowerIDs(tx)
4✔
43
        if err != nil {
5✔
44
                return err
1✔
45
        }
1✔
46

47
        // Create a new top-level bucket for the index if it does not yet exist.
48
        indexBkt, err := tx.CreateTopLevelBucket(cTowerIDToSessionIDIndexBkt)
3✔
49
        if err != nil {
3✔
50
                return err
×
51
        }
×
52

53
        // Finally, ensure that there is an entry in the tower-to-session index
54
        // for each of our towers.
55
        for _, id := range towerIDs {
8✔
56
                // Create a sub-bucket using the tower ID.
5✔
57
                _, err := indexBkt.CreateBucketIfNotExists(id.Bytes())
5✔
58
                if err != nil {
5✔
59
                        return err
×
60
                }
×
61
        }
62

63
        return nil
3✔
64
}
65

66
// listTowerIDs iterates through the cTowerBkt and collects a list of all the
67
// TowerIDs.
68
func listTowerIDs(tx kvdb.RTx) ([]*TowerID, error) {
4✔
69
        var ids []*TowerID
4✔
70
        towerBucket := tx.ReadBucket(cTowerBkt)
4✔
71
        if towerBucket == nil {
4✔
72
                return nil, ErrUninitializedDB
×
73
        }
×
74

75
        err := towerBucket.ForEach(func(towerIDBytes, _ []byte) error {
10✔
76
                id, err := TowerIDFromBytes(towerIDBytes)
6✔
77
                if err != nil {
7✔
78
                        return err
1✔
79
                }
1✔
80

81
                ids = append(ids, &id)
5✔
82

5✔
83
                return nil
5✔
84
        })
85
        if err != nil {
5✔
86
                return nil, err
1✔
87
        }
1✔
88

89
        return ids, nil
3✔
90
}
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