2019-03-19 17:09:27 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2019-06-26 13:00:35 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-03-19 17:09:27 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2019-03-19 17:09:27 +01: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-03-19 17:09:27 +01:00
|
|
|
)
|
|
|
|
|
2019-06-03 20:32:07 +02:00
|
|
|
var (
|
2019-06-26 09:49:16 +02:00
|
|
|
mcTestRoute = &route.Route{
|
2019-11-07 11:24:38 +01:00
|
|
|
SourcePubKey: mcTestSelf,
|
2019-06-26 09:49:16 +02:00
|
|
|
Hops: []*route.Hop{
|
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: route.Vertex{11},
|
|
|
|
AmtToForward: 1000,
|
|
|
|
LegacyPayload: true,
|
2019-06-26 09:49:16 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-31 06:41:58 +02:00
|
|
|
ChannelID: 2,
|
|
|
|
PubKeyBytes: route.Vertex{12},
|
|
|
|
LegacyPayload: true,
|
2019-06-26 09:49:16 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mcTestTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
2019-11-07 11:24:38 +01:00
|
|
|
mcTestSelf = route.Vertex{10}
|
2019-06-26 09:49:16 +02:00
|
|
|
mcTestNode1 = mcTestRoute.Hops[0].PubKeyBytes
|
|
|
|
mcTestNode2 = mcTestRoute.Hops[1].PubKeyBytes
|
2019-09-04 17:54:29 +02:00
|
|
|
|
|
|
|
testPenaltyHalfLife = 30 * time.Minute
|
2019-09-04 17:40:14 +02:00
|
|
|
testAprioriHopProbability = 0.9
|
|
|
|
testAprioriWeight = 0.5
|
2019-06-03 20:32:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type mcTestContext struct {
|
|
|
|
t *testing.T
|
|
|
|
mc *MissionControl
|
|
|
|
now time.Time
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2020-01-10 03:44:43 +01:00
|
|
|
db kvdb.Backend
|
2019-06-26 13:00:35 +02:00
|
|
|
dbPath string
|
|
|
|
|
2019-06-26 11:48:59 +02:00
|
|
|
pid uint64
|
2019-06-03 20:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMcTestContext(t *testing.T) *mcTestContext {
|
|
|
|
ctx := &mcTestContext{
|
|
|
|
t: t,
|
|
|
|
now: mcTestTime,
|
|
|
|
}
|
2019-03-19 17:09:27 +01:00
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
file, err := ioutil.TempFile("", "*.db")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.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 00:31:49 +01:00
|
|
|
ctx.db, err = kvdb.Open(
|
|
|
|
kvdb.BoltBackendName, ctx.dbPath, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2019-06-26 13:00:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.restartMc()
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// restartMc creates a new instances of mission control on the same database.
|
|
|
|
func (ctx *mcTestContext) restartMc() {
|
2021-06-25 22:22:12 +02:00
|
|
|
// Since we don't run a timer to store results in unit tests, we store
|
|
|
|
// them here before fetching back everything in NewMissionControl.
|
|
|
|
if ctx.mc != nil {
|
|
|
|
require.NoError(ctx.t, ctx.mc.store.storeResults())
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
mc, err := NewMissionControl(
|
2021-01-19 09:57:10 +01:00
|
|
|
ctx.db, mcTestSelf,
|
2019-06-18 18:30:56 +02:00
|
|
|
&MissionControlConfig{
|
2021-01-19 09:57:13 +01:00
|
|
|
ProbabilityEstimatorCfg: ProbabilityEstimatorCfg{
|
|
|
|
PenaltyHalfLife: testPenaltyHalfLife,
|
|
|
|
AprioriHopProbability: testAprioriHopProbability,
|
|
|
|
AprioriWeight: testAprioriWeight,
|
|
|
|
},
|
2019-05-22 11:56:04 +02:00
|
|
|
},
|
|
|
|
)
|
2019-06-26 13:00:35 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.t.Fatal(err)
|
|
|
|
}
|
2019-06-03 20:32:07 +02:00
|
|
|
|
|
|
|
mc.now = func() time.Time { return ctx.now }
|
|
|
|
ctx.mc = mc
|
2019-06-26 13:00:35 +02:00
|
|
|
}
|
2019-06-03 20:32:07 +02:00
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
// cleanup closes the database and removes the temp file.
|
|
|
|
func (ctx *mcTestContext) cleanup() {
|
|
|
|
ctx.db.Close()
|
|
|
|
os.Remove(ctx.dbPath)
|
2019-06-03 20:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that mission control returns a probability for an edge.
|
2019-07-29 15:10:58 +02:00
|
|
|
func (ctx *mcTestContext) expectP(amt lnwire.MilliSatoshi, expected float64) {
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.t.Helper()
|
|
|
|
|
2019-07-29 15:10:58 +02:00
|
|
|
p := ctx.mc.GetProbability(mcTestNode1, mcTestNode2, amt)
|
2019-06-03 20:32:07 +02:00
|
|
|
if p != expected {
|
2019-07-29 15:10:58 +02:00
|
|
|
ctx.t.Fatalf("expected probability %v but got %v", expected, p)
|
2019-06-03 20:32:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:49:16 +02:00
|
|
|
// reportFailure reports a failure by using a test route.
|
2019-08-14 14:06:53 +02:00
|
|
|
func (ctx *mcTestContext) reportFailure(amt lnwire.MilliSatoshi,
|
|
|
|
failure lnwire.FailureMessage) {
|
2019-06-26 09:49:16 +02:00
|
|
|
|
|
|
|
mcTestRoute.Hops[0].AmtToForward = amt
|
|
|
|
|
|
|
|
errorSourceIdx := 1
|
|
|
|
ctx.mc.ReportPaymentFail(
|
2019-06-26 11:48:59 +02:00
|
|
|
ctx.pid, mcTestRoute, &errorSourceIdx, failure,
|
2019-06-26 09:49:16 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:20:06 +02:00
|
|
|
// reportSuccess reports a success by using a test route.
|
|
|
|
func (ctx *mcTestContext) reportSuccess() {
|
|
|
|
err := ctx.mc.ReportPaymentSuccess(ctx.pid, mcTestRoute)
|
|
|
|
if err != nil {
|
|
|
|
ctx.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.pid++
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:32:07 +02:00
|
|
|
// TestMissionControl tests mission control probability estimation.
|
|
|
|
func TestMissionControl(t *testing.T) {
|
|
|
|
ctx := createMcTestContext(t)
|
2019-06-26 13:00:35 +02:00
|
|
|
defer ctx.cleanup()
|
2019-06-03 20:32:07 +02:00
|
|
|
|
|
|
|
ctx.now = testTime
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
testTime := time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
|
|
|
|
2019-11-07 11:24:38 +01:00
|
|
|
// For local channels, we expect a higher probability than our a prior
|
|
|
|
// test probability.
|
|
|
|
selfP := ctx.mc.GetProbability(mcTestSelf, mcTestNode1, 100)
|
|
|
|
if selfP != prevSuccessProbability {
|
|
|
|
t.Fatalf("expected prev success prob for untried local chans")
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:40:14 +02:00
|
|
|
// Initial probability is expected to be the a priori.
|
|
|
|
ctx.expectP(1000, testAprioriHopProbability)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// Expect probability to be zero after reporting the edge as failed.
|
2019-08-14 14:06:53 +02:00
|
|
|
ctx.reportFailure(1000, lnwire.NewTemporaryChannelFailure(nil))
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.expectP(1000, 0)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// As we reported with a min penalization amt, a lower amt than reported
|
2019-09-04 17:40:14 +02:00
|
|
|
// should return the node probability, which is the a priori
|
|
|
|
// probability.
|
2019-09-04 17:54:29 +02:00
|
|
|
ctx.expectP(500, testAprioriHopProbability)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
2019-09-04 17:40:14 +02:00
|
|
|
// Edge decay started. The node probability weighted average should now
|
|
|
|
// have shifted from 1:1 to 1:0.5 -> 60%. The connection probability is
|
|
|
|
// half way through the recovery, so we expect 30% here.
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.now = testTime.Add(30 * time.Minute)
|
2019-09-04 17:40:14 +02:00
|
|
|
ctx.expectP(1000, 0.3)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// Edge fails again, this time without a min penalization amt. The edge
|
|
|
|
// should be penalized regardless of amount.
|
2019-08-14 14:06:53 +02:00
|
|
|
ctx.reportFailure(0, lnwire.NewTemporaryChannelFailure(nil))
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.expectP(1000, 0)
|
|
|
|
ctx.expectP(500, 0)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// Edge decay started.
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.now = testTime.Add(60 * time.Minute)
|
2019-09-04 17:40:14 +02:00
|
|
|
ctx.expectP(1000, 0.3)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
// Restart mission control to test persistence.
|
|
|
|
ctx.restartMc()
|
2019-09-04 17:40:14 +02:00
|
|
|
ctx.expectP(1000, 0.3)
|
2019-06-26 13:00:35 +02:00
|
|
|
|
2019-09-04 16:26:18 +02:00
|
|
|
// A node level failure should bring probability of all known channels
|
|
|
|
// back to zero.
|
2019-08-14 14:06:53 +02:00
|
|
|
ctx.reportFailure(0, lnwire.NewExpiryTooSoon(lnwire.ChannelUpdate{}))
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.expectP(1000, 0)
|
2019-05-10 10:38:31 +02:00
|
|
|
|
|
|
|
// Check whether history snapshot looks sane.
|
2019-06-03 20:32:07 +02:00
|
|
|
history := ctx.mc.GetHistorySnapshot()
|
2019-05-10 10:38:31 +02:00
|
|
|
|
2020-01-21 09:02:25 +01:00
|
|
|
if len(history.Pairs) != 4 {
|
|
|
|
t.Fatalf("expected 4 pairs, but got %v", len(history.Pairs))
|
2019-05-10 10:38:31 +02:00
|
|
|
}
|
2019-07-29 14:20:06 +02:00
|
|
|
|
|
|
|
// Test reporting a success.
|
|
|
|
ctx.reportSuccess()
|
2019-03-19 17:09:27 +01:00
|
|
|
}
|
2019-06-26 08:39:34 +02:00
|
|
|
|
|
|
|
// TestMissionControlChannelUpdate tests that the first channel update is not
|
|
|
|
// penalizing the channel yet.
|
|
|
|
func TestMissionControlChannelUpdate(t *testing.T) {
|
|
|
|
ctx := createMcTestContext(t)
|
|
|
|
|
|
|
|
// Report a policy related failure. Because it is the first, we don't
|
|
|
|
// expect a penalty.
|
2019-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
2019-08-14 14:06:53 +02:00
|
|
|
0, lnwire.NewFeeInsufficient(0, lnwire.ChannelUpdate{}),
|
2019-06-26 09:49:16 +02:00
|
|
|
)
|
2019-09-26 17:04:02 +02:00
|
|
|
ctx.expectP(100, testAprioriHopProbability)
|
2019-06-26 08:39:34 +02:00
|
|
|
|
|
|
|
// Report another failure for the same channel. We expect it to be
|
|
|
|
// pruned.
|
2019-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
2019-08-14 14:06:53 +02:00
|
|
|
0, lnwire.NewFeeInsufficient(0, lnwire.ChannelUpdate{}),
|
2019-06-26 09:49:16 +02:00
|
|
|
)
|
2019-09-26 17:04:02 +02:00
|
|
|
ctx.expectP(100, 0)
|
2019-06-26 08:39:34 +02:00
|
|
|
}
|