mirror of
https://github.com/lightningequipment/circuitbreaker.git
synced 2026-08-13 12:33:02 +02:00
db: once off clean up forwarding records on db creation
This commit is contained in:
parent
e43aa41121
commit
85a1d48bf4
3 changed files with 34 additions and 3 deletions
8
db.go
8
db.go
|
|
@ -96,7 +96,7 @@ type Db struct {
|
|||
fwdHistoryLimit int
|
||||
}
|
||||
|
||||
func NewDb(dbPath string, fwdHistoryLimit int) (*Db, error) {
|
||||
func NewDb(ctx context.Context, dbPath string, fwdHistoryLimit int) (*Db, error) {
|
||||
const busyTimeoutMs = 5000
|
||||
|
||||
dsn := dbPath + fmt.Sprintf("?_pragma=busy_timeout=%d", busyTimeoutMs)
|
||||
|
|
@ -119,6 +119,12 @@ func NewDb(dbPath string, fwdHistoryLimit int) (*Db, error) {
|
|||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
27
db_test.go
27
db_test.go
|
|
@ -14,7 +14,7 @@ func setupTestDb(t *testing.T, fwdingHistoryLimit int) (*Db, func()) {
|
|||
file, err := os.CreateTemp("", "test_db_")
|
||||
require.NoError(t, err)
|
||||
|
||||
db, err := NewDb(file.Name(), fwdingHistoryLimit)
|
||||
db, err := NewDb(context.Background(), file.Name(), fwdingHistoryLimit)
|
||||
require.NoError(t, err)
|
||||
|
||||
return db, func() {
|
||||
|
|
@ -129,3 +129,28 @@ func TestDbNoForwardingHistory(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
|
|
|
|||
2
run.go
2
run.go
|
|
@ -43,7 +43,7 @@ func run(c *cli.Context) error {
|
|||
log.Infow("Opening database", "path", dbPath)
|
||||
|
||||
// Open database.
|
||||
db, err := NewDb(dbPath, c.Int("fwdhistorylimit"))
|
||||
db, err := NewDb(ctx, dbPath, c.Int("fwdhistorylimit"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue