2019-06-26 14:00:35 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-31 07:41:58 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2020-01-10 05:44:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-06-26 14:00:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
const testMaxRecords = 2
|
|
|
|
|
2019-12-20 13:25:08 +03: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.
|
2019-06-26 14:00:35 +03:00
|
|
|
func TestMissionControlStore(t *testing.T) {
|
2019-12-20 13:25:08 +03:00
|
|
|
// Set time zone explicitly to keep test deterministic.
|
2019-06-26 14:00:35 +03:00
|
|
|
time.Local = time.UTC
|
|
|
|
|
|
|
|
file, err := ioutil.TempFile("", "*.db")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbPath := file.Name()
|
|
|
|
|
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 02:31:49 +03:00
|
|
|
db, err := kvdb.Create(
|
|
|
|
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2019-06-26 14:00:35 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
defer os.Remove(dbPath)
|
|
|
|
|
|
|
|
store, err := newMissionControlStore(db, testMaxRecords)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results, err := store.fetchAll()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(results) != 0 {
|
|
|
|
t.Fatal("expected no results")
|
|
|
|
}
|
|
|
|
|
|
|
|
testRoute := route.Route{
|
|
|
|
SourcePubKey: route.Vertex{1},
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{
|
2019-07-31 07:41:58 +03:00
|
|
|
PubKeyBytes: route.Vertex{2},
|
|
|
|
LegacyPayload: true,
|
2019-06-26 14:00:35 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
failureSourceIdx := 1
|
|
|
|
|
|
|
|
result1 := paymentResult{
|
|
|
|
route: &testRoute,
|
2019-06-12 13:18:58 +03:00
|
|
|
failure: lnwire.NewFailIncorrectDetails(100, 1000),
|
2019-06-26 14:00:35 +03: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.
|
|
|
|
err = store.AddResult(&result2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store again to test idempotency.
|
|
|
|
err = store.AddResult(&result2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store second result which has an earlier timestamp.
|
|
|
|
err = store.AddResult(&result1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
results, err = store.fetchAll()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(results) != 2 {
|
|
|
|
t.Fatal("expected two results")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that results are stored in chronological order.
|
|
|
|
if !reflect.DeepEqual(&result1, results[0]) {
|
2019-07-31 07:41:58 +03:00
|
|
|
t.Fatalf("the results differ: %v vs %v", spew.Sdump(&result1),
|
|
|
|
spew.Sdump(results[0]))
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&result2, results[1]) {
|
2019-07-31 07:41:58 +03:00
|
|
|
t.Fatalf("the results differ: %v vs %v", spew.Sdump(&result2),
|
|
|
|
spew.Sdump(results[1]))
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate store to test pruning.
|
|
|
|
store, err = newMissionControlStore(db, testMaxRecords)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:08 +03:00
|
|
|
// Add a newer result which failed due to mpp timeout.
|
2019-06-26 14:00:35 +03: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 13:25:08 +03:00
|
|
|
result3.failure = &lnwire.FailMPPTimeout{}
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
err = store.AddResult(&result3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that results are pruned.
|
|
|
|
results, err = store.fetchAll()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(results) != 2 {
|
|
|
|
t.Fatal("expected two results")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&result2, results[0]) {
|
2019-07-31 07:41:58 +03:00
|
|
|
t.Fatalf("the results differ: %v vs %v", spew.Sdump(&result2),
|
|
|
|
spew.Sdump(results[0]))
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&result3, results[1]) {
|
2019-07-31 07:41:58 +03:00
|
|
|
t.Fatalf("the results differ: %v vs %v", spew.Sdump(&result3),
|
|
|
|
spew.Sdump(results[1]))
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
}
|