multi: make forwarding history limit configurable

This commit is contained in:
Carla Kirk-Cohen 2023-10-02 13:06:28 -04:00
parent 5632bab2fe
commit e43aa41121
No known key found for this signature in database
GPG key ID: 4CA7FE54A6213C91
5 changed files with 37 additions and 22 deletions

13
db.go
View file

@ -96,7 +96,7 @@ type Db struct {
fwdHistoryLimit int
}
func NewDb(dbPath string, opts ...func(*Db)) (*Db, error) {
func NewDb(dbPath string, fwdHistoryLimit int) (*Db, error) {
const busyTimeoutMs = 5000
dsn := dbPath + fmt.Sprintf("?_pragma=busy_timeout=%d", busyTimeoutMs)
@ -116,10 +116,7 @@ func NewDb(dbPath string, opts ...func(*Db)) (*Db, error) {
database := &Db{
db: db,
fwdHistoryLimit: defaultFwdHistoryLimit,
}
for _, opt := range opts {
opt(database)
fwdHistoryLimit: fwdHistoryLimit,
}
return database, nil
@ -244,6 +241,12 @@ type HtlcInfo struct {
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
}

View file

@ -10,11 +10,11 @@ import (
"github.com/stretchr/testify/require"
)
func setupTestDb(t *testing.T, dbOpts ...func(*Db)) (*Db, func()) {
func setupTestDb(t *testing.T, fwdingHistoryLimit int) (*Db, func()) {
file, err := os.CreateTemp("", "test_db_")
require.NoError(t, err)
db, err := NewDb(file.Name(), dbOpts...)
db, err := NewDb(file.Name(), fwdingHistoryLimit)
require.NoError(t, err)
return db, func() {
@ -24,7 +24,7 @@ func setupTestDb(t *testing.T, dbOpts ...func(*Db)) (*Db, func()) {
func TestDb(t *testing.T) {
ctx := context.Background()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
expectedDefaultLimit := Limit{
@ -68,18 +68,12 @@ func TestDb(t *testing.T) {
defer db.Close()
}
func dbWithCustomForwardingHistoryLimit(limit int) func(d *Db) {
return func(d *Db) {
d.fwdHistoryLimit = limit
}
}
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, dbWithCustomForwardingHistoryLimit(limit))
db, cleanup := setupTestDb(t, limit)
defer cleanup()
// Insert HTLCs just up until our limit.
@ -122,3 +116,16 @@ func testHtlc(i uint64) *HtlcInfo {
},
}
}
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)
}

View file

@ -129,6 +129,11 @@ func main() {
Value: "127.0.0.1:9234",
Usage: "grpc server listen address",
},
cli.Uint64Flag{
Name: "fwdhistorylimit",
Usage: "limit the number of htlc forwards that are persisted",
Value: defaultFwdHistoryLimit,
},
httpListenFlag,
stubFlag,
}

View file

@ -37,7 +37,7 @@ func testProcess(t *testing.T, event resolveEvent) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
log := zaptest.NewLogger(t).Sugar()
@ -114,7 +114,7 @@ func TestLimits(t *testing.T) {
func testRateLimit(t *testing.T, mode Mode) {
defer Timeout()()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
cfg := &Limits{
@ -204,7 +204,7 @@ func testRateLimit(t *testing.T, mode Mode) {
func testMaxPending(t *testing.T, mode Mode) {
defer Timeout()()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
cfg := &Limits{
@ -283,7 +283,7 @@ func TestNewPeer(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
log := zaptest.NewLogger(t).Sugar()
@ -323,7 +323,7 @@ func TestNewPeer(t *testing.T) {
func TestBlocked(t *testing.T) {
defer Timeout()()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
cfg := &Limits{
@ -375,7 +375,7 @@ func TestChannelNotFound(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
db, cleanup := setupTestDb(t)
db, cleanup := setupTestDb(t, defaultFwdHistoryLimit)
defer cleanup()
log := zaptest.NewLogger(t).Sugar()

2
run.go
View file

@ -43,7 +43,7 @@ func run(c *cli.Context) error {
log.Infow("Opening database", "path", dbPath)
// Open database.
db, err := NewDb(dbPath)
db, err := NewDb(dbPath, c.Int("fwdhistorylimit"))
if err != nil {
return err
}