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