/
db.go
406 lines (340 loc) · 10.6 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"context"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
migrate "github.com/rubenv/sql-migrate"
_ "modernc.org/sqlite"
)
var migrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
{
Id: "1",
Up: []string{
`
CREATE TABLE IF NOT EXISTS limits (
peer TEXT PRIMARY KEY NOT NULL,
htlc_max_pending INTEGER NOT NULL,
htlc_max_hourly_rate INTEGER NOT NULL,
mode TEXT CHECK(mode IN ('FAIL', 'QUEUE', 'QUEUE_PEER_INITIATED')) NOT NULL DEFAULT 'FAIL'
);
INSERT OR IGNORE INTO limits(peer, htlc_max_pending, htlc_max_hourly_rate)
VALUES('000000000000000000000000000000000000000000000000000000000000000000', 5, 3600);
`,
},
},
{
Id: "2",
Up: []string{
`
ALTER TABLE limits RENAME TO limits_old;
CREATE TABLE IF NOT EXISTS limits (
peer TEXT PRIMARY KEY NOT NULL,
htlc_max_pending INTEGER NOT NULL,
htlc_max_hourly_rate INTEGER NOT NULL,
mode TEXT CHECK(mode IN ('FAIL', 'QUEUE', 'QUEUE_PEER_INITIATED', 'BLOCK')) NOT NULL DEFAULT 'FAIL'
);
INSERT INTO limits(peer, htlc_max_pending, htlc_max_hourly_rate, mode)
SELECT peer, htlc_max_pending, htlc_max_hourly_rate, mode FROM limits_old;
DROP TABLE limits_old;
`,
},
},
{
Id: "3",
Up: []string{
`CREATE TABLE IF NOT EXISTS forwarding_history (
add_time TIMESTAMP NOT NULL,
resolved_time TIMESTAMP NOT NULL,
settled BOOLEAN NOT NULL,
incoming_amt_msat INTEGER NOT NULL CHECK (incoming_amt_msat > 0),
outgoing_amt_msat INTEGER NOT NULL CHECK (outgoing_amt_msat > 0),
incoming_peer TEXT NOT NULL,
incoming_channel INTEGER NOT NULL,
incoming_htlc_index INTEGER NOT NULL,
outgoing_peer TEXT NOT NULL,
outgoing_channel INTEGER NOT NULL,
outgoing_htlc_index INTEGER NOT NULL,
CONSTRAINT unique_incoming_circuit UNIQUE (incoming_channel, incoming_htlc_index),
CONSTRAINT unique_outgoing_circuit UNIQUE (outgoing_channel, outgoing_htlc_index)
);`,
`CREATE INDEX add_time_index ON forwarding_history (add_time);`,
},
},
},
}
const (
// defaultFwdHistoryLimit is the default limit we place on the forwarding_history table
// to prevent creation of an ever-growing table.
//
// Justification for value:
// * ~130 bytes per row in the table.
// * Help ourselves to 14MB of disk space
// -> 100_000 entries = 13 MB, plus ~0.8MB for add_time_index.
defaultFwdHistoryLimit = 100_000
)
var defaultNodeKey = route.Vertex{}
type Db struct {
db *sql.DB
fwdHistoryLimit int
}
func NewDb(ctx context.Context, dbPath string, fwdHistoryLimit int) (*Db, error) {
const busyTimeoutMs = 5000
dsn := dbPath + fmt.Sprintf("?_pragma=busy_timeout=%d", busyTimeoutMs)
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, err
}
n, err := migrate.Exec(db, "sqlite3", migrations, migrate.Up)
if err != nil {
return nil, fmt.Errorf("migration error: %w", err)
}
if n > 0 {
log.Infow("Applied migrations", "count", n)
}
database := &Db{
db: db,
fwdHistoryLimit: fwdHistoryLimit,
}
// Perform a once-off cleanup of the records in the db to update to a potential
// change in limit value.
if err := database.limitHTLCRecords(ctx); err != nil {
return nil, err
}
return database, nil
}
func (d *Db) Close() error {
return d.db.Close()
}
type Limit struct {
MaxHourlyRate int64
MaxPending int64
Mode Mode
}
type Limits struct {
Default Limit
PerPeer map[route.Vertex]Limit
}
func (d *Db) UpdateLimit(ctx context.Context, peer route.Vertex,
limit Limit) error {
peerHex := hex.EncodeToString(peer[:])
const replace string = `REPLACE INTO limits(peer, htlc_max_pending, htlc_max_hourly_rate, mode) VALUES(?, ?, ?, ?);`
_, err := d.db.ExecContext(
ctx, replace, peerHex,
limit.MaxPending, limit.MaxHourlyRate,
limit.Mode.String(),
)
return err
}
func (d *Db) ClearLimit(ctx context.Context, peer route.Vertex) error {
if peer == defaultNodeKey {
return errors.New("cannot clear default limit")
}
const query string = `DELETE FROM limits WHERE peer = ?;`
_, err := d.db.ExecContext(
ctx, query, hex.EncodeToString(peer[:]),
)
return err
}
func (d *Db) GetLimits(ctx context.Context) (*Limits, error) {
const query string = `
SELECT peer, htlc_max_pending, htlc_max_hourly_rate, mode from limits;`
rows, err := d.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
var limits = Limits{
PerPeer: make(map[route.Vertex]Limit),
}
for rows.Next() {
var (
limit Limit
peerHex string
modeStr string
)
err := rows.Scan(
&peerHex, &limit.MaxPending, &limit.MaxHourlyRate, &modeStr,
)
if err != nil {
return nil, err
}
switch modeStr {
case "FAIL":
limit.Mode = ModeFail
case "QUEUE":
limit.Mode = ModeQueue
case "QUEUE_PEER_INITIATED":
limit.Mode = ModeQueuePeerInitiated
case "BLOCK":
limit.Mode = ModeBlock
default:
return nil, errors.New("unknown mode")
}
key, err := route.NewVertexFromStr(peerHex)
if err != nil {
return nil, err
}
if key == defaultNodeKey {
limits.Default = limit
} else {
limits.PerPeer[key] = limit
}
}
return &limits, nil
}
type HtlcInfo struct {
addTime time.Time
resolveTime time.Time
settled bool
incomingMsat lnwire.MilliSatoshi
outgoingMsat lnwire.MilliSatoshi
incomingPeer route.Vertex
outgoingPeer route.Vertex
incomingCircuit circuitKey
outgoingCircuit circuitKey
}
// RecordHtlcResolution records a HTLC that has been resolved and deletes the oldest rows from
// the forwarding history table if the total row count has exceeded the configured limit.
func (d *Db) RecordHtlcResolution(ctx context.Context,
htlc *HtlcInfo) error {
// If the database is configured to not store any records, save the hassle of
// writing and deleting a record by returning early.
if d.fwdHistoryLimit == 0 {
return nil
}
if err := d.insertHtlcResolution(ctx, htlc); err != nil {
return err
}
return d.limitHTLCRecords(ctx)
}
func (d *Db) insertHtlcResolution(ctx context.Context, htlc *HtlcInfo) error {
insert := `INSERT INTO forwarding_history (
add_time,
resolved_time,
settled,
incoming_amt_msat,
outgoing_amt_msat,
incoming_peer,
incoming_channel,
incoming_htlc_index,
outgoing_peer,
outgoing_channel,
outgoing_htlc_index)
VALUES (?,?,?,?,?,?,?,?,?,?,?);`
_, err := d.db.ExecContext(
ctx, insert,
htlc.addTime.UnixNano(),
htlc.resolveTime.UnixNano(),
htlc.settled,
uint64(htlc.incomingMsat),
uint64(htlc.outgoingMsat),
hex.EncodeToString(htlc.incomingPeer[:]),
htlc.incomingCircuit.channel,
htlc.incomingCircuit.htlc,
hex.EncodeToString(htlc.outgoingPeer[:]),
htlc.outgoingCircuit.channel,
htlc.outgoingCircuit.htlc,
)
return err
}
// limitHTLCRecords counts the number of forwarding history records in the database and
// preemptively deletes records to fall 10% below the forwarding history limit if it has
// been reached.
//
// Note that the count and deletion of records is *not* atomic, so this function may
// not delete precisely 10% of the limit if other operations take place between count
// and deletion.
func (d *Db) limitHTLCRecords(ctx context.Context) error {
query := `SELECT COUNT(add_time) from forwarding_history`
var rowCount int
err := d.db.QueryRow(query).Scan(&rowCount)
if err != nil {
return err
}
if rowCount < d.fwdHistoryLimit {
return nil
}
// If we've hit our row count, delete oldest entries over the row limit plus an
// extra 10% of the limit to free up space so that we don't need to constantly
// delete on each
// insert.
//
// Note: if fwdHistoryLimit < 10 the additional 10% will be zero, so we'll just
// clear the rows beyond our limit. For such a small limit, we're expecting to
// be deleting all the time anyway, so this isn't a big performance hit.
offset := d.fwdHistoryLimit - (d.fwdHistoryLimit / 10)
query = `DELETE FROM forwarding_history
WHERE add_time <= (
SELECT add_time
FROM forwarding_history
ORDER BY add_time DESC
LIMIT 1 OFFSET ?
);`
_, err = d.db.ExecContext(ctx, query, offset)
return err
}
// ListForwardingHistory returns a list of htlcs that were resolved within the
// time range provided (start time is inclusive, end time is exclusive)
func (d *Db) ListForwardingHistory(ctx context.Context, start, end time.Time) (
[]*HtlcInfo, error) {
list := `SELECT
add_time,
resolved_time,
settled,
incoming_amt_msat,
outgoing_amt_msat,
incoming_peer,
incoming_channel,
incoming_htlc_index,
outgoing_peer,
outgoing_channel,
outgoing_htlc_index
FROM forwarding_history
WHERE add_time >= ? AND add_time < ?;`
rows, err := d.db.QueryContext(ctx, list, start.UnixNano(), end.UnixNano())
if err != nil {
return nil, err
}
defer rows.Close()
var htlcs []*HtlcInfo
for rows.Next() {
var (
incomingPeer, outgoingPeer string
addTime, resolveTime uint64
htlc HtlcInfo
)
err := rows.Scan(
&addTime,
&resolveTime,
&htlc.settled,
&htlc.incomingMsat,
&htlc.outgoingMsat,
&incomingPeer,
&htlc.incomingCircuit.channel,
&htlc.incomingCircuit.htlc,
&outgoingPeer,
&htlc.outgoingCircuit.channel,
&htlc.outgoingCircuit.htlc,
)
if err != nil {
return nil, err
}
htlc.addTime = time.Unix(0, int64(addTime))
htlc.resolveTime = time.Unix(0, int64(resolveTime))
htlc.incomingPeer, err = route.NewVertexFromStr(incomingPeer)
if err != nil {
return nil, err
}
htlc.outgoingPeer, err = route.NewVertexFromStr(outgoingPeer)
if err != nil {
return nil, err
}
htlcs = append(htlcs, &htlc)
}
return htlcs, nil
}