2019-06-26 13:00:35 +02:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2024-03-13 16:54:10 +01:00
|
|
|
"fmt"
|
2019-06-26 13:00:35 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2024-03-13 16:54:10 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
2019-06-26 13:00:35 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2021-06-25 22:22:12 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-26 13:00:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const testMaxRecords = 2
|
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
var (
|
|
|
|
// mcStoreTestRoute is a test route for the mission control store tests.
|
|
|
|
mcStoreTestRoute = route.Route{
|
|
|
|
SourcePubKey: route.Vertex{1},
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{
|
|
|
|
PubKeyBytes: route.Vertex{2},
|
|
|
|
LegacyPayload: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// mcStoreTestHarness is the harness for a MissonControlStore test.
|
|
|
|
type mcStoreTestHarness struct {
|
|
|
|
db walletdb.DB
|
|
|
|
store *missionControlStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// newMCStoreTestHarness initializes a test mission control store.
|
|
|
|
func newMCStoreTestHarness(t testing.TB, maxRecords int,
|
|
|
|
flushInterval time.Duration) mcStoreTestHarness {
|
2019-12-20 11:25:08 +01:00
|
|
|
// Set time zone explicitly to keep test deterministic.
|
2019-06-26 13:00:35 +02:00
|
|
|
time.Local = time.UTC
|
|
|
|
|
2022-08-15 15:08:16 +02:00
|
|
|
file, err := os.CreateTemp("", "*.db")
|
2024-03-13 16:54:10 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
dbPath := file.Name()
|
2022-08-15 15:08:16 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, file.Close())
|
|
|
|
require.NoError(t, os.Remove(dbPath))
|
|
|
|
})
|
2019-06-26 13:00:35 +02:00
|
|
|
|
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout
* btcwallet: add DBTimeOut in config
* kvdb: add database timeout option for bbolt
This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.
* channeldb: add DBTimeout in db options
This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.
* contractcourt+routing: use DBTimeout in kvdb
This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.
* lncfg: add DBTimeout option in db config
The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.
* migration: add DBTimeout param in kvdb.Create/kvdb.Open
* keychain: update tests to use DBTimeout param
* htlcswitch+chainreg: add DBTimeout option
* macaroons: support DBTimeout config in creation
This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.
* walletunlocker: add dbTimeout option in UnlockerService
This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.
* watchtower/wtdb: add dbTimeout param during creation
This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.
* multi: add db timeout param for walletdb.Create
This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.
* lnd: pass DBTimeout config to relevant services
This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
- chainControlConfig
- walletunlocker
- wallet.NewLoader
- macaroons
- watchtower
In addition, the usage of wallet.Create is updated too.
* sample-config: add dbtimeout option
2020-12-08 00:31:49 +01:00
|
|
|
db, err := kvdb.Create(
|
|
|
|
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2024-03-13 16:54:10 +01:00
|
|
|
require.NoError(t, err)
|
2022-08-15 15:08:16 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, db.Close())
|
|
|
|
})
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
store, err := newMissionControlStore(db, maxRecords, flushInterval)
|
|
|
|
require.NoError(t, err)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
return mcStoreTestHarness{db: db, store: store}
|
|
|
|
}
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
// TestMissionControlStore tests the recording of payment failure events
|
|
|
|
// in mission control. It tests encoding and decoding of differing lnwire
|
|
|
|
// failures (FailIncorrectDetails and FailMppTimeout), pruning of results
|
|
|
|
// and idempotent writes.
|
|
|
|
func TestMissionControlStore(t *testing.T) {
|
|
|
|
h := newMCStoreTestHarness(t, testMaxRecords, time.Second)
|
|
|
|
db, store := h.db, h.store
|
|
|
|
|
|
|
|
results, err := store.fetchAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, results, 0)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
failureSourceIdx := 1
|
|
|
|
|
|
|
|
result1 := paymentResult{
|
2024-03-13 16:54:10 +01:00
|
|
|
route: &mcStoreTestRoute,
|
2019-06-12 12:18:58 +02:00
|
|
|
failure: lnwire.NewFailIncorrectDetails(100, 1000),
|
2019-06-26 13:00:35 +02:00
|
|
|
failureSourceIdx: &failureSourceIdx,
|
|
|
|
id: 99,
|
|
|
|
timeReply: testTime,
|
|
|
|
timeFwd: testTime.Add(-time.Minute),
|
|
|
|
}
|
|
|
|
|
|
|
|
result2 := result1
|
|
|
|
result2.timeReply = result1.timeReply.Add(time.Hour)
|
|
|
|
result2.timeFwd = result1.timeReply.Add(time.Hour)
|
|
|
|
result2.id = 2
|
|
|
|
|
|
|
|
// Store result.
|
2021-06-25 22:22:12 +02:00
|
|
|
store.AddResult(&result2)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
// Store again to test idempotency.
|
2021-06-25 22:22:12 +02:00
|
|
|
store.AddResult(&result2)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
// Store second result which has an earlier timestamp.
|
2021-06-25 22:22:12 +02:00
|
|
|
store.AddResult(&result1)
|
|
|
|
require.NoError(t, store.storeResults())
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
results, err = store.fetchAll()
|
2024-03-13 16:54:10 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, results, 2)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
// Check that results are stored in chronological order.
|
2024-03-13 16:54:10 +01:00
|
|
|
require.Equal(t, &result1, results[0])
|
|
|
|
require.Equal(t, &result2, results[1])
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
// Recreate store to test pruning.
|
2021-06-25 22:22:12 +02:00
|
|
|
store, err = newMissionControlStore(db, testMaxRecords, time.Second)
|
2024-03-13 16:54:10 +01:00
|
|
|
require.NoError(t, err)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2019-12-20 11:25:08 +01:00
|
|
|
// Add a newer result which failed due to mpp timeout.
|
2019-06-26 13:00:35 +02:00
|
|
|
result3 := result1
|
|
|
|
result3.timeReply = result1.timeReply.Add(2 * time.Hour)
|
|
|
|
result3.timeFwd = result1.timeReply.Add(2 * time.Hour)
|
|
|
|
result3.id = 3
|
2019-12-20 11:25:08 +01:00
|
|
|
result3.failure = &lnwire.FailMPPTimeout{}
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2021-06-25 22:22:12 +02:00
|
|
|
store.AddResult(&result3)
|
|
|
|
require.NoError(t, store.storeResults())
|
2019-06-26 13:00:35 +02:00
|
|
|
|
|
|
|
// Check that results are pruned.
|
|
|
|
results, err = store.fetchAll()
|
2024-03-13 16:54:10 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, results, 2)
|
|
|
|
|
|
|
|
require.Equal(t, &result2, results[0])
|
|
|
|
require.Equal(t, &result3, results[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMissionControlStoreFlushing asserts the periodic flushing of the store
|
|
|
|
// works correctly.
|
|
|
|
func TestMissionControlStoreFlushing(t *testing.T) {
|
|
|
|
const flushInterval = 500 * time.Millisecond
|
|
|
|
|
|
|
|
h := newMCStoreTestHarness(t, testMaxRecords, flushInterval)
|
|
|
|
db, store := h.db, h.store
|
|
|
|
|
|
|
|
var (
|
|
|
|
failureSourceIdx = 1
|
|
|
|
failureDetails = lnwire.NewFailIncorrectDetails(100, 1000)
|
|
|
|
lastID uint64
|
|
|
|
)
|
|
|
|
nextResult := func() *paymentResult {
|
|
|
|
lastID += 1
|
|
|
|
return &paymentResult{
|
|
|
|
route: &mcStoreTestRoute,
|
|
|
|
failure: failureDetails,
|
|
|
|
failureSourceIdx: &failureSourceIdx,
|
|
|
|
id: lastID,
|
|
|
|
timeReply: testTime,
|
|
|
|
timeFwd: testTime.Add(-time.Minute),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to assert the number of results is correct.
|
|
|
|
assertResults := func(wantCount int) {
|
|
|
|
t.Helper()
|
|
|
|
err := wait.NoError(func() error {
|
|
|
|
results, err := store.fetchAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if wantCount != len(results) {
|
|
|
|
return fmt.Errorf("wrong nb of results: got "+
|
|
|
|
"%d, want %d", len(results), wantCount)
|
|
|
|
}
|
|
|
|
if len(results) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gotLastID := results[len(results)-1].id
|
|
|
|
if len(results) > 0 && gotLastID != lastID {
|
|
|
|
return fmt.Errorf("wrong id for last item: "+
|
|
|
|
"got %d, want %d", gotLastID, lastID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}, flushInterval*5)
|
|
|
|
require.NoError(t, err)
|
2019-06-26 13:00:35 +02:00
|
|
|
}
|
2024-03-13 16:54:10 +01:00
|
|
|
|
|
|
|
// Run the store.
|
|
|
|
store.run()
|
|
|
|
time.Sleep(flushInterval)
|
|
|
|
|
|
|
|
// Wait for the flush interval. There should be no records.
|
|
|
|
assertResults(0)
|
|
|
|
|
|
|
|
// Store a result and check immediately. There still shouldn't be
|
|
|
|
// any results stored (flush interval has not elapsed).
|
|
|
|
store.AddResult(nextResult())
|
|
|
|
assertResults(0)
|
|
|
|
|
|
|
|
// Assert that eventually the result is stored after being flushed.
|
|
|
|
assertResults(1)
|
|
|
|
|
|
|
|
// Store enough results that fill the max number of results.
|
|
|
|
for i := 0; i < testMaxRecords; i++ {
|
|
|
|
store.AddResult(nextResult())
|
2019-06-26 13:00:35 +02:00
|
|
|
}
|
2024-03-13 16:54:10 +01:00
|
|
|
assertResults(testMaxRecords)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2024-03-13 16:54:10 +01:00
|
|
|
// Finally, stop the store to recreate it.
|
|
|
|
store.stop()
|
|
|
|
|
|
|
|
// Recreate store.
|
|
|
|
store, err := newMissionControlStore(db, testMaxRecords, flushInterval)
|
|
|
|
require.NoError(t, err)
|
|
|
|
store.run()
|
|
|
|
defer store.stop()
|
|
|
|
time.Sleep(flushInterval)
|
|
|
|
assertResults(testMaxRecords)
|
|
|
|
|
|
|
|
// Fill the store with results again.
|
|
|
|
for i := 0; i < testMaxRecords; i++ {
|
|
|
|
store.AddResult(nextResult())
|
2019-06-26 13:00:35 +02:00
|
|
|
}
|
2024-03-13 16:54:10 +01:00
|
|
|
assertResults(testMaxRecords)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkMissionControlStoreFlushing benchmarks the periodic storage of data
|
|
|
|
// from the mission control store when additional results are added between
|
|
|
|
// runs.
|
|
|
|
func BenchmarkMissionControlStoreFlushing(b *testing.B) {
|
|
|
|
var (
|
|
|
|
failureSourceIdx = 1
|
|
|
|
failureDetails = lnwire.NewFailIncorrectDetails(100, 1000)
|
|
|
|
testTimeFwd = testTime.Add(-time.Minute)
|
|
|
|
|
|
|
|
tests = []int{0, 1, 10, 100, 250, 500}
|
|
|
|
)
|
|
|
|
|
|
|
|
const testMaxRecords = 1000
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
name := fmt.Sprintf("%v additional results", tc)
|
|
|
|
b.Run(name, func(b *testing.B) {
|
|
|
|
h := newMCStoreTestHarness(
|
|
|
|
b, testMaxRecords, time.Second,
|
|
|
|
)
|
|
|
|
store := h.store
|
|
|
|
|
|
|
|
// Fill the store.
|
|
|
|
var lastID uint64
|
|
|
|
for i := 0; i < testMaxRecords; i++ {
|
|
|
|
lastID++
|
|
|
|
result := &paymentResult{
|
|
|
|
route: &mcStoreTestRoute,
|
|
|
|
failure: failureDetails,
|
|
|
|
failureSourceIdx: &failureSourceIdx,
|
|
|
|
id: lastID,
|
|
|
|
timeReply: testTime,
|
|
|
|
timeFwd: testTimeFwd,
|
|
|
|
}
|
|
|
|
store.AddResult(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the first flush.
|
|
|
|
err := store.storeResults()
|
|
|
|
require.NoError(b, err)
|
|
|
|
|
|
|
|
// Create the additional results.
|
|
|
|
results := make([]*paymentResult, tc)
|
|
|
|
for i := 0; i < len(results); i++ {
|
|
|
|
results[i] = &paymentResult{
|
|
|
|
route: &mcStoreTestRoute,
|
|
|
|
failure: failureDetails,
|
|
|
|
failureSourceIdx: &failureSourceIdx,
|
|
|
|
timeReply: testTime,
|
|
|
|
timeFwd: testTimeFwd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the actual benchmark.
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < len(results); j++ {
|
|
|
|
lastID++
|
|
|
|
results[j].id = lastID
|
|
|
|
store.AddResult(results[j])
|
|
|
|
}
|
|
|
|
err := store.storeResults()
|
|
|
|
require.NoError(b, err)
|
|
|
|
}
|
|
|
|
})
|
2019-06-26 13:00:35 +02:00
|
|
|
}
|
|
|
|
}
|