From e43aa4112184c6facb2aad43652ff7bd483f5dcd Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Mon, 2 Oct 2023 13:06:28 -0400 Subject: [PATCH] multi: make forwarding history limit configurable --- db.go | 13 ++++++++----- db_test.go | 27 +++++++++++++++++---------- main.go | 5 +++++ process_test.go | 12 ++++++------ run.go | 2 +- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/db.go b/db.go index 491f343..e5f837e 100644 --- a/db.go +++ b/db.go @@ -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 } diff --git a/db_test.go b/db_test.go index b6e1406..94f0435 100644 --- a/db_test.go +++ b/db_test.go @@ -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) +} diff --git a/main.go b/main.go index 75d715e..2a417ef 100644 --- a/main.go +++ b/main.go @@ -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, } diff --git a/process_test.go b/process_test.go index e48a476..0feb159 100644 --- a/process_test.go +++ b/process_test.go @@ -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() diff --git a/run.go b/run.go index e25a849..0dccd87 100644 --- a/run.go +++ b/run.go @@ -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 }