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"
|
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
"github.com/coreos/bbolt"
|
2019-03-19 17:09:27 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
|
|
|
)
|
|
|
|
|
2019-06-03 20:32:07 +02:00
|
|
|
var (
|
|
|
|
mcTestEdge = EdgeLocator{
|
2019-06-26 09:49:16 +02:00
|
|
|
ChannelID: 2,
|
2019-06-03 20:32:07 +02:00
|
|
|
}
|
2019-06-26 09:49:16 +02:00
|
|
|
|
|
|
|
mcTestRoute = &route.Route{
|
|
|
|
SourcePubKey: route.Vertex{10},
|
|
|
|
Hops: []*route.Hop{
|
|
|
|
{
|
|
|
|
ChannelID: 1,
|
|
|
|
PubKeyBytes: route.Vertex{11},
|
|
|
|
AmtToForward: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ChannelID: 2,
|
|
|
|
PubKeyBytes: route.Vertex{12},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mcTestTime = time.Date(2018, time.January, 9, 14, 00, 00, 0, time.UTC)
|
|
|
|
mcTestNode1 = mcTestRoute.Hops[0].PubKeyBytes
|
|
|
|
mcTestNode2 = mcTestRoute.Hops[1].PubKeyBytes
|
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
|
|
|
|
|
|
|
db *bbolt.DB
|
|
|
|
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()
|
|
|
|
|
|
|
|
ctx.db, err = bbolt.Open(ctx.dbPath, 0600, nil)
|
|
|
|
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 18:30:56 +02:00
|
|
|
&MissionControlConfig{
|
2019-05-22 11:56:04 +02:00
|
|
|
PenaltyHalfLife: 30 * time.Minute,
|
|
|
|
AprioriHopProbability: 0.8,
|
|
|
|
},
|
|
|
|
)
|
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.
|
|
|
|
func (ctx *mcTestContext) expectP(amt lnwire.MilliSatoshi,
|
|
|
|
expected float64) {
|
|
|
|
|
|
|
|
ctx.t.Helper()
|
|
|
|
|
2019-06-26 09:49:16 +02:00
|
|
|
p := ctx.mc.GetEdgeProbability(mcTestNode1, mcTestEdge, amt)
|
2019-06-03 20:32:07 +02:00
|
|
|
if p != expected {
|
|
|
|
ctx.t.Fatalf("unexpected probability %v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 09:49:16 +02:00
|
|
|
// reportFailure reports a failure by using a test route.
|
|
|
|
func (ctx *mcTestContext) reportFailure(t time.Time,
|
|
|
|
amt lnwire.MilliSatoshi, failure lnwire.FailureMessage) {
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
// Initial probability is expected to be 1.
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.expectP(1000, 0.8)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// Expect probability to be zero after reporting the edge as failed.
|
2019-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
|
|
|
testTime, 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
|
|
|
|
// should be unaffected.
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.expectP(500, 0.8)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
|
|
|
// Edge decay started.
|
2019-06-03 20:32:07 +02:00
|
|
|
ctx.now = testTime.Add(30 * time.Minute)
|
|
|
|
ctx.expectP(1000, 0.4)
|
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-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
|
|
|
ctx.now, 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)
|
|
|
|
ctx.expectP(1000, 0.4)
|
2019-03-19 17:09:27 +01:00
|
|
|
|
2019-06-26 13:00:35 +02:00
|
|
|
// Restart mission control to test persistence.
|
|
|
|
ctx.restartMc()
|
|
|
|
ctx.expectP(1000, 0.4)
|
|
|
|
|
2019-03-19 17:09:27 +01:00
|
|
|
// A node level failure should bring probability of every channel back
|
|
|
|
// to zero.
|
2019-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
|
|
|
ctx.now, 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
|
|
|
if len(history.Nodes) != 1 {
|
|
|
|
t.Fatal("unexpected number of nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(history.Nodes[0].Channels) != 1 {
|
|
|
|
t.Fatal("unexpected number of channels")
|
|
|
|
}
|
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)
|
2019-06-26 13:00:35 +02:00
|
|
|
defer ctx.cleanup()
|
2019-06-26 08:39:34 +02:00
|
|
|
|
|
|
|
// 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(
|
|
|
|
ctx.now, 0,
|
|
|
|
lnwire.NewFeeInsufficient(0, lnwire.ChannelUpdate{}),
|
|
|
|
)
|
2019-06-26 08:39:34 +02:00
|
|
|
ctx.expectP(0, 0.8)
|
|
|
|
|
|
|
|
// Report another failure for the same channel. We expect it to be
|
|
|
|
// pruned.
|
2019-06-26 09:49:16 +02:00
|
|
|
ctx.reportFailure(
|
|
|
|
ctx.now, 0,
|
|
|
|
lnwire.NewFeeInsufficient(0, lnwire.ChannelUpdate{}),
|
|
|
|
)
|
2019-06-26 08:39:34 +02:00
|
|
|
ctx.expectP(0, 0)
|
|
|
|
}
|