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

lightningnetwork / lnd / 13035292482

29 Jan 2025 03:59PM UTC coverage: 49.3% (-9.5%) from 58.777%
13035292482

Pull #9456

github

mohamedawnallah
docs: update release-notes-0.19.0.md

In this commit, we warn users about the removal
of RPCs `SendToRoute`, `SendToRouteSync`, `SendPayment`,
and `SendPaymentSync` in the next release 0.20.
Pull Request #9456: lnrpc+docs: deprecate warning `SendToRoute`, `SendToRouteSync`, `SendPayment`, and `SendPaymentSync` in Release 0.19

100634 of 204126 relevant lines covered (49.3%)

1.54 hits per line

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

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

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

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

13
var (
14
        // cSessionBkt is a top-level bucket storing:
15
        //   session-id => cSessionBody -> encoded ClientSessionBody
16
        //                 => cSessionDBID -> db-assigned-id
17
        //              => cSessionCommits => seqnum -> encoded CommittedUpdate
18
        //              => cSessionAckRangeIndex => chan-id => acked-index-range
19
        cSessionBkt = []byte("client-session-bucket")
20

21
        // cChanDetailsBkt is a top-level bucket storing:
22
        //   channel-id => cChannelSummary -> encoded ClientChanSummary.
23
        //                  => cChanDBID -> db-assigned-id
24
        //                 => cChanSessions => db-session-id -> 1
25
        cChanDetailsBkt = []byte("client-channel-detail-bucket")
26

27
        // cChannelSummary is a sub-bucket of cChanDetailsBkt which stores the
28
        // encoded body of ClientChanSummary.
29
        cChannelSummary = []byte("client-channel-summary")
30

31
        // cChanSessions is a sub-bucket of cChanDetailsBkt which stores:
32
        //    session-id -> 1
33
        cChanSessions = []byte("client-channel-sessions")
34

35
        // cSessionAckRangeIndex is a sub-bucket of cSessionBkt storing:
36
        //    chan-id => start -> end
37
        cSessionAckRangeIndex = []byte("client-session-ack-range-index")
38

39
        // cSessionDBID is a key used in the cSessionBkt to store the
40
        // db-assigned-d of a session.
41
        cSessionDBID = []byte("client-session-db-id")
42

43
        // cChanIDIndexBkt is a top-level bucket storing:
44
        //    db-assigned-id -> channel-ID
45
        cChanIDIndexBkt = []byte("client-channel-id-index")
46

47
        // ErrUninitializedDB signals that top-level buckets for the database
48
        // have not been initialized.
49
        ErrUninitializedDB = errors.New("db not initialized")
50

51
        // ErrCorruptClientSession signals that the client session's on-disk
52
        // structure deviates from what is expected.
53
        ErrCorruptClientSession = errors.New("client session corrupted")
54

55
        // byteOrder is the default endianness used when serializing integers.
56
        byteOrder = binary.BigEndian
57
)
58

59
// MigrateChannelToSessionIndex migrates the tower client DB to add an index
60
// from channel-to-session. This will make it easier in future to check which
61
// sessions have updates for which channels.
62
func MigrateChannelToSessionIndex(tx kvdb.RwTx) error {
×
63
        log.Infof("Migrating the tower client DB to build a new " +
×
64
                "channel-to-session index")
×
65

×
66
        sessionsBkt := tx.ReadBucket(cSessionBkt)
×
67
        if sessionsBkt == nil {
×
68
                return ErrUninitializedDB
×
69
        }
×
70

71
        chanDetailsBkt := tx.ReadWriteBucket(cChanDetailsBkt)
×
72
        if chanDetailsBkt == nil {
×
73
                return ErrUninitializedDB
×
74
        }
×
75

76
        chanIDsBkt := tx.ReadBucket(cChanIDIndexBkt)
×
77
        if chanIDsBkt == nil {
×
78
                return ErrUninitializedDB
×
79
        }
×
80

81
        // First gather all the new channel-to-session pairs that we want to
82
        // add.
83
        index, err := collectIndex(sessionsBkt)
×
84
        if err != nil {
×
85
                return err
×
86
        }
×
87

88
        // Then persist those pairs to the db.
89
        return persistIndex(chanDetailsBkt, chanIDsBkt, index)
×
90
}
91

92
// collectIndex iterates through all the sessions and uses the keys in the
93
// cSessionAckRangeIndex bucket to collect all the channels that the session
94
// has updates for. The function returns a map from channel ID to session ID
95
// (using the db-assigned IDs for both).
96
func collectIndex(sessionsBkt kvdb.RBucket) (map[uint64]map[uint64]bool,
97
        error) {
×
98

×
99
        index := make(map[uint64]map[uint64]bool)
×
100
        err := sessionsBkt.ForEach(func(sessID, _ []byte) error {
×
101
                sessionBkt := sessionsBkt.NestedReadBucket(sessID)
×
102
                if sessionBkt == nil {
×
103
                        return ErrCorruptClientSession
×
104
                }
×
105

106
                ackedRanges := sessionBkt.NestedReadBucket(
×
107
                        cSessionAckRangeIndex,
×
108
                )
×
109
                if ackedRanges == nil {
×
110
                        return nil
×
111
                }
×
112

113
                sessDBIDBytes := sessionBkt.Get(cSessionDBID)
×
114
                if sessDBIDBytes == nil {
×
115
                        return ErrCorruptClientSession
×
116
                }
×
117

118
                sessDBID, err := readUint64(sessDBIDBytes)
×
119
                if err != nil {
×
120
                        return err
×
121
                }
×
122

123
                return ackedRanges.ForEach(func(dbChanIDBytes, _ []byte) error {
×
124
                        dbChanID, err := readUint64(dbChanIDBytes)
×
125
                        if err != nil {
×
126
                                return err
×
127
                        }
×
128

129
                        if _, ok := index[dbChanID]; !ok {
×
130
                                index[dbChanID] = make(map[uint64]bool)
×
131
                        }
×
132

133
                        index[dbChanID][sessDBID] = true
×
134

×
135
                        return nil
×
136
                })
137
        })
138
        if err != nil {
×
139
                return nil, err
×
140
        }
×
141

142
        return index, nil
×
143
}
144

145
// persistIndex adds the channel-to-session mapping in each channel's details
146
// bucket.
147
func persistIndex(chanDetailsBkt kvdb.RwBucket, chanIDsBkt kvdb.RBucket,
148
        index map[uint64]map[uint64]bool) error {
×
149

×
150
        for dbChanID, sessIDs := range index {
×
151
                dbChanIDBytes, err := writeUint64(dbChanID)
×
152
                if err != nil {
×
153
                        return err
×
154
                }
×
155

156
                realChanID := chanIDsBkt.Get(dbChanIDBytes)
×
157

×
158
                chanBkt := chanDetailsBkt.NestedReadWriteBucket(realChanID)
×
159
                if chanBkt == nil {
×
160
                        return fmt.Errorf("channel not found")
×
161
                }
×
162

163
                sessIDsBkt, err := chanBkt.CreateBucket(cChanSessions)
×
164
                if err != nil {
×
165
                        return err
×
166
                }
×
167

168
                for id := range sessIDs {
×
169
                        sessID, err := writeUint64(id)
×
170
                        if err != nil {
×
171
                                return err
×
172
                        }
×
173

174
                        err = sessIDsBkt.Put(sessID, []byte{1})
×
175
                        if err != nil {
×
176
                                return err
×
177
                        }
×
178
                }
179
        }
180

181
        return nil
×
182
}
183

184
func writeUint64(i uint64) ([]byte, error) {
×
185
        var b bytes.Buffer
×
186
        err := tlv.WriteVarInt(&b, i, &[8]byte{})
×
187
        if err != nil {
×
188
                return nil, err
×
189
        }
×
190

191
        return b.Bytes(), nil
×
192
}
193

194
func readUint64(b []byte) (uint64, error) {
×
195
        r := bytes.NewReader(b)
×
196
        i, err := tlv.ReadVarInt(r, &[8]byte{})
×
197
        if err != nil {
×
198
                return 0, err
×
199
        }
×
200

201
        return i, nil
×
202
}
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