-
Notifications
You must be signed in to change notification settings - Fork 18
/
db_test.go
156 lines (124 loc) · 3.96 KB
/
db_test.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
package main
import (
"context"
"os"
"testing"
"time"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/require"
)
func setupTestDb(t *testing.T, fwdingHistoryLimit int) (*Db, func()) {
file, err := os.CreateTemp("", "test_db_")
require.NoError(t, err)
db, err := NewDb(context.Background(), file.Name(), fwdingHistoryLimit)
require.NoError(t, err)
return db, func() {
os.Remove(file.Name())
}
}
func TestDb(t *testing.T) {
ctx := context.Background()
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
expectedDefaultLimit := Limit{
MaxPending: 5,
MaxHourlyRate: 3600,
}
limits, err := db.GetLimits(ctx)
require.NoError(t, err)
require.Equal(t, expectedDefaultLimit, limits.Default)
require.Len(t, limits.PerPeer, 0)
peer := route.Vertex{1}
limit := Limit{
MaxHourlyRate: 1,
MaxPending: 2,
Mode: ModeQueue,
}
require.NoError(t, db.UpdateLimit(ctx, peer, limit))
limits, err = db.GetLimits(ctx)
require.NoError(t, err)
require.Equal(t, expectedDefaultLimit, limits.Default)
require.Equal(t, map[route.Vertex]Limit{peer: limit}, limits.PerPeer)
require.NoError(t, db.UpdateLimit(ctx, defaultNodeKey, limit))
limits, err = db.GetLimits(ctx)
require.NoError(t, err)
require.Equal(t, limit, limits.Default)
require.NoError(t, db.ClearLimit(ctx, peer))
limits, err = db.GetLimits(ctx)
require.NoError(t, err)
require.Len(t, limits.PerPeer, 0)
require.Error(t, db.ClearLimit(ctx, defaultNodeKey))
defer db.Close()
}
func TestDbForwardingHistory(t *testing.T) {
limit := 20
// Create a test DB that will limit to 10 forwarding history records.
ctx := context.Background()
db, cleanup := setupTestDb(t, limit)
defer cleanup()
// Insert HTLCs just up until our limit.
for i := 1; i < limit; i++ {
htlc := testHtlc(uint64(i))
require.NoError(t, db.RecordHtlcResolution(ctx, htlc))
}
endTime := time.Unix(100000, 0)
fwds, err := db.ListForwardingHistory(ctx, time.Time{}, endTime)
require.NoError(t, err)
require.Len(t, fwds, limit-1)
// Insert a HTLC that reaches the limit and assert that we've removed old records
// but we still have the newest HTLC and we've culled the old one.
limitHtlc := testHtlc(20)
require.NoError(t, db.RecordHtlcResolution(ctx, limitHtlc))
limitCount := limit - (limit / 10)
fwds, err = db.ListForwardingHistory(ctx, time.Time{}, endTime)
require.NoError(t, err)
require.Len(t, fwds, limitCount)
require.Equal(t, limitHtlc, fwds[len(fwds)-1])
}
func testHtlc(i uint64) *HtlcInfo {
return &HtlcInfo{
addTime: time.Unix(int64(i), 0),
resolveTime: time.Unix(int64(i), 0),
settled: true,
incomingMsat: 50,
outgoingMsat: 45,
incomingCircuit: circuitKey{
channel: 1,
htlc: i,
},
outgoingCircuit: circuitKey{
channel: 2,
htlc: i,
},
}
}
func TestDbNoForwardingHistory(t *testing.T) {
ctx := context.Background()
db, cleanup := setupTestDb(t, 0)
defer cleanup()
htlc := testHtlc(1)
require.NoError(t, db.RecordHtlcResolution(ctx, htlc))
fwds, err := db.ListForwardingHistory(ctx, time.Time{}, time.Unix(1000000, 0))
require.NoError(t, err)
require.Len(t, fwds, 0)
}
func TestForwadingHistoryDelete(t *testing.T) {
// Create a db that will store HTLCs.
ctx := context.Background()
db, cleanup := setupTestDb(t, 5)
defer cleanup()
// Write a test HTLC and assert that it's stored.
htlc := testHtlc(1)
require.NoError(t, db.RecordHtlcResolution(ctx, htlc))
fwds, err := db.ListForwardingHistory(ctx, time.Time{}, time.Unix(1000000, 0))
require.NoError(t, err)
require.Len(t, fwds, 1)
// Modify the db to have a zero limit on forwarding history. We don't recreate
// the test db because it would re-create the file. Run limitHTLCRecords once
// (as we would on NewDb) to assert that we clean up our records.
db.fwdHistoryLimit = 0
require.NoError(t, db.limitHTLCRecords(ctx))
fwds, err = db.ListForwardingHistory(ctx, time.Time{}, time.Unix(1000000, 0))
require.NoError(t, err)
require.Len(t, fwds, 0)
}