2020-01-27 15:40:33 +01:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2020-01-28 16:07:34 +01:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-01-27 15:40:33 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-06-26 04:58:57 +02:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2024-04-19 13:49:45 +02:00
|
|
|
"github.com/lightningnetwork/lnd/fn"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2020-01-27 15:40:33 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2024-04-19 13:49:45 +02:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2022-09-13 01:58:58 +02:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2023-01-20 11:25:53 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-27 15:40:33 +01:00
|
|
|
)
|
|
|
|
|
2020-03-17 17:07:52 +01:00
|
|
|
const (
|
|
|
|
sourceNodeID = 1
|
|
|
|
targetNodeID = 2
|
|
|
|
)
|
|
|
|
|
2021-10-19 09:37:44 +02:00
|
|
|
type mockBandwidthHints struct {
|
|
|
|
hints map[uint64]lnwire.MilliSatoshi
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:37:47 +02:00
|
|
|
func (m *mockBandwidthHints) availableChanBandwidth(channelID uint64,
|
|
|
|
_ lnwire.MilliSatoshi) (lnwire.MilliSatoshi, bool) {
|
2021-10-19 09:37:44 +02:00
|
|
|
|
|
|
|
if m.hints == nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
balance, ok := m.hints[channelID]
|
|
|
|
return balance, ok
|
|
|
|
}
|
|
|
|
|
2024-04-19 13:49:45 +02:00
|
|
|
func (m *mockBandwidthHints) firstHopCustomBlob() fn.Option[tlv.Blob] {
|
|
|
|
return fn.None[tlv.Blob]()
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:40:33 +01:00
|
|
|
// integratedRoutingContext defines the context in which integrated routing
|
|
|
|
// tests run.
|
|
|
|
type integratedRoutingContext struct {
|
|
|
|
graph *mockGraph
|
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
source *mockNode
|
|
|
|
target *mockNode
|
|
|
|
|
|
|
|
amt lnwire.MilliSatoshi
|
2021-02-12 03:05:13 +01:00
|
|
|
maxShardAmt *lnwire.MilliSatoshi
|
2020-01-27 15:40:33 +01:00
|
|
|
finalExpiry int32
|
|
|
|
|
|
|
|
mcCfg MissionControlConfig
|
|
|
|
pathFindingCfg PathFindingConfig
|
2022-09-13 01:58:58 +02:00
|
|
|
routeHints [][]zpay32.HopHint
|
2020-01-27 15:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newIntegratedRoutingContext instantiates a new integrated routing test
|
|
|
|
// context with a source and a target node.
|
|
|
|
func newIntegratedRoutingContext(t *testing.T) *integratedRoutingContext {
|
|
|
|
// Instantiate a mock graph.
|
2020-03-17 17:07:52 +01:00
|
|
|
source := newMockNode(sourceNodeID)
|
|
|
|
target := newMockNode(targetNodeID)
|
2020-01-27 15:40:33 +01:00
|
|
|
|
|
|
|
graph := newMockGraph(t)
|
|
|
|
graph.addNode(source)
|
|
|
|
graph.addNode(target)
|
|
|
|
graph.source = source
|
|
|
|
|
|
|
|
// Initiate the test context with a set of default configuration values.
|
|
|
|
// We don't use the lnd defaults here, because otherwise changing the
|
|
|
|
// defaults would break the unit tests. The actual values picked aren't
|
|
|
|
// critical to excite certain behavior, but do need to be aligned with
|
|
|
|
// the test case assertions.
|
2023-01-20 11:25:53 +01:00
|
|
|
aCfg := AprioriConfig{
|
|
|
|
PenaltyHalfLife: 30 * time.Minute,
|
|
|
|
AprioriHopProbability: 0.6,
|
|
|
|
AprioriWeight: 0.5,
|
2023-02-24 11:59:44 +01:00
|
|
|
CapacityFraction: testCapacityFraction,
|
2023-01-20 11:25:53 +01:00
|
|
|
}
|
|
|
|
estimator, err := NewAprioriEstimator(aCfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-01-27 15:40:33 +01:00
|
|
|
ctx := integratedRoutingContext{
|
|
|
|
t: t,
|
|
|
|
graph: graph,
|
|
|
|
amt: 100000,
|
|
|
|
finalExpiry: 40,
|
|
|
|
|
|
|
|
mcCfg: MissionControlConfig{
|
2023-01-20 11:25:53 +01:00
|
|
|
Estimator: estimator,
|
2020-01-27 15:40:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
pathFindingCfg: PathFindingConfig{
|
2020-09-08 13:07:35 +02:00
|
|
|
AttemptCost: 1000,
|
|
|
|
MinProbability: 0.01,
|
2020-01-27 15:40:33 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
source: source,
|
|
|
|
target: target,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:51:42 +01:00
|
|
|
// htlcAttempt records the route and outcome of an attempted htlc.
|
|
|
|
type htlcAttempt struct {
|
|
|
|
route *route.Route
|
|
|
|
success bool
|
|
|
|
}
|
|
|
|
|
2020-01-28 16:07:34 +01:00
|
|
|
func (h htlcAttempt) String() string {
|
|
|
|
return fmt.Sprintf("success=%v, route=%v", h.success, h.route)
|
|
|
|
}
|
|
|
|
|
2020-01-27 15:40:33 +01:00
|
|
|
// testPayment launches a test payment and asserts that it is completed after
|
|
|
|
// the expected number of attempts.
|
2021-02-04 02:52:08 +01:00
|
|
|
func (c *integratedRoutingContext) testPayment(maxParts uint32,
|
|
|
|
destFeatureBits ...lnwire.FeatureBit) ([]htlcAttempt, error) {
|
|
|
|
|
|
|
|
// We start out with the base set of MPP feature bits. If the caller
|
|
|
|
// overrides this set of bits, then we'll use their feature bits
|
|
|
|
// entirely.
|
|
|
|
baseFeatureBits := mppFeatures
|
|
|
|
if len(destFeatureBits) != 0 {
|
|
|
|
baseFeatureBits = lnwire.NewRawFeatureVector(destFeatureBits...)
|
|
|
|
}
|
2020-01-28 16:07:34 +01:00
|
|
|
|
2020-03-17 16:51:42 +01:00
|
|
|
var (
|
|
|
|
nextPid uint64
|
|
|
|
attempts []htlcAttempt
|
|
|
|
)
|
2020-01-27 15:40:33 +01:00
|
|
|
|
|
|
|
// Create temporary database for mission control.
|
2022-08-15 15:08:16 +02:00
|
|
|
file, err := os.CreateTemp("", "*.db")
|
2020-01-27 15:40:33 +01:00
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbPath := file.Name()
|
2022-08-15 15:08:16 +02:00
|
|
|
c.t.Cleanup(func() {
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.Remove(dbPath); err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2020-01-27 15:40:33 +01: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.Open(
|
|
|
|
kvdb.BoltBackendName, dbPath, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2020-01-27 15:40:33 +01:00
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
2022-08-15 15:08:16 +02:00
|
|
|
c.t.Cleanup(func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
2020-01-27 15:40:33 +01:00
|
|
|
|
2024-08-13 18:52:16 +02:00
|
|
|
// Instantiate a new mission controller with the current configuration
|
2020-01-27 15:40:33 +01:00
|
|
|
// values.
|
2024-08-13 18:52:16 +02:00
|
|
|
mcController, err := NewMissionController(db, c.source.pubkey, &c.mcCfg)
|
|
|
|
require.NoError(c.t, err)
|
|
|
|
|
2024-08-13 19:19:06 +02:00
|
|
|
mc, err := mcController.GetNamespacedStore(
|
|
|
|
DefaultMissionControlNamespace,
|
|
|
|
)
|
|
|
|
require.NoError(c.t, err)
|
2020-01-27 15:40:33 +01:00
|
|
|
|
2024-06-26 04:22:00 +02:00
|
|
|
getBandwidthHints := func(_ Graph) (bandwidthHints, error) {
|
2020-03-17 11:53:29 +01:00
|
|
|
// Create bandwidth hints based on local channel balances.
|
|
|
|
bandwidthHints := map[uint64]lnwire.MilliSatoshi{}
|
|
|
|
for _, ch := range c.graph.nodes[c.source.pubkey].channels {
|
|
|
|
bandwidthHints[ch.id] = ch.balance
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:37:44 +02:00
|
|
|
return &mockBandwidthHints{
|
|
|
|
hints: bandwidthHints,
|
|
|
|
}, nil
|
2020-03-17 11:53:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 16:07:34 +01:00
|
|
|
var paymentAddr [32]byte
|
2020-03-17 11:53:29 +01:00
|
|
|
payment := LightningPayment{
|
|
|
|
FinalCLTVDelta: uint16(c.finalExpiry),
|
|
|
|
FeeLimit: lnwire.MaxMilliSatoshi,
|
|
|
|
Target: c.target.pubkey,
|
2024-09-24 05:36:15 +02:00
|
|
|
PaymentAddr: fn.Some(paymentAddr),
|
2023-12-14 00:56:33 +01:00
|
|
|
DestFeatures: lnwire.NewFeatureVector(
|
|
|
|
baseFeatureBits, lnwire.Features,
|
|
|
|
),
|
|
|
|
Amount: c.amt,
|
|
|
|
CltvLimit: math.MaxUint32,
|
|
|
|
MaxParts: maxParts,
|
|
|
|
RouteHints: c.routeHints,
|
2020-03-17 11:53:29 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:23:08 +02:00
|
|
|
var paymentHash [32]byte
|
|
|
|
if err := payment.SetPaymentHash(paymentHash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-12 03:05:13 +01:00
|
|
|
if c.maxShardAmt != nil {
|
|
|
|
payment.MaxShardAmt = c.maxShardAmt
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:20:23 +02:00
|
|
|
session, err := newPaymentSession(
|
2024-06-15 00:47:15 +02:00
|
|
|
&payment, c.graph.source.pubkey, getBandwidthHints,
|
2024-06-26 04:58:57 +02:00
|
|
|
newMockGraphSessionFactory(c.graph), mc, c.pathFindingCfg,
|
2020-04-16 15:20:23 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
2020-01-27 15:40:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:20:23 +02:00
|
|
|
// Override default minimum shard amount.
|
|
|
|
session.minShardAmt = lnwire.NewMSatFromSatoshis(5000)
|
|
|
|
|
2020-01-27 15:40:33 +01:00
|
|
|
// Now the payment control loop starts. It will keep trying routes until
|
|
|
|
// the payment succeeds.
|
2020-01-28 16:07:34 +01:00
|
|
|
var (
|
|
|
|
amtRemaining = payment.Amount
|
|
|
|
inFlightHtlcs uint32
|
|
|
|
)
|
2020-01-27 15:40:33 +01:00
|
|
|
for {
|
|
|
|
// Create bandwidth hints based on local channel balances.
|
|
|
|
bandwidthHints := map[uint64]lnwire.MilliSatoshi{}
|
|
|
|
for _, ch := range c.graph.nodes[c.source.pubkey].channels {
|
|
|
|
bandwidthHints[ch.id] = ch.balance
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a route.
|
2020-03-17 11:53:29 +01:00
|
|
|
route, err := session.RequestRoute(
|
2020-01-28 16:07:34 +01:00
|
|
|
amtRemaining, lnwire.MaxMilliSatoshi, inFlightHtlcs, 0,
|
2024-04-19 13:49:45 +02:00
|
|
|
lnwire.CustomRecords{
|
|
|
|
lnwire.MinCustomRecordsTlvType: []byte{1, 2, 3},
|
|
|
|
},
|
2020-01-27 15:40:33 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-01-28 16:07:34 +01:00
|
|
|
return attempts, err
|
2020-01-27 15:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send out the htlc on the mock graph.
|
|
|
|
pid := nextPid
|
|
|
|
nextPid++
|
|
|
|
htlcResult, err := c.graph.sendHtlc(route)
|
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-17 16:51:42 +01:00
|
|
|
success := htlcResult.failure == nil
|
|
|
|
attempts = append(attempts, htlcAttempt{
|
|
|
|
route: route,
|
|
|
|
success: success,
|
|
|
|
})
|
|
|
|
|
2020-01-28 16:07:34 +01:00
|
|
|
// Process the result. In normal Lightning operations, the
|
|
|
|
// sender doesn't get an acknowledgement from the recipient that
|
|
|
|
// the htlc arrived. In integrated routing tests, this
|
|
|
|
// acknowledgement is available. It is a simplification of
|
|
|
|
// reality that still allows certain classes of tests to be
|
|
|
|
// performed.
|
2020-03-17 16:51:42 +01:00
|
|
|
if success {
|
2020-01-28 16:07:34 +01:00
|
|
|
inFlightHtlcs++
|
|
|
|
|
2020-01-27 15:40:33 +01:00
|
|
|
err := mc.ReportPaymentSuccess(pid, route)
|
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-28 16:07:34 +01:00
|
|
|
amtRemaining -= route.ReceiverAmt()
|
|
|
|
|
|
|
|
// If the full amount has been paid, the payment is
|
|
|
|
// successful and the control loop can be terminated.
|
|
|
|
if amtRemaining == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise try to send the remaining amount.
|
|
|
|
continue
|
2020-01-27 15:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Failure, update mission control and retry.
|
|
|
|
finalResult, err := mc.ReportPaymentFail(
|
|
|
|
pid, route,
|
|
|
|
getNodeIndex(route, htlcResult.failureSource),
|
|
|
|
htlcResult.failure,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if finalResult != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:51:07 +01:00
|
|
|
return attempts, nil
|
2020-01-27 15:40:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNodeIndex returns the zero-based index of the given node in the route.
|
|
|
|
func getNodeIndex(route *route.Route, failureSource route.Vertex) *int {
|
|
|
|
if failureSource == route.SourcePubKey {
|
|
|
|
idx := 0
|
|
|
|
return &idx
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, h := range route.Hops {
|
|
|
|
if h.PubKeyBytes == failureSource {
|
|
|
|
idx := i + 1
|
|
|
|
return &idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-26 04:58:57 +02:00
|
|
|
|
|
|
|
type mockGraphSessionFactory struct {
|
|
|
|
Graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockGraphSessionFactory(graph Graph) GraphSessionFactory {
|
|
|
|
return &mockGraphSessionFactory{Graph: graph}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockGraphSessionFactory) NewGraphSession() (Graph, func() error,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
return m, func() error {
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ GraphSessionFactory = (*mockGraphSessionFactory)(nil)
|
|
|
|
var _ Graph = (*mockGraphSessionFactory)(nil)
|
|
|
|
|
|
|
|
type mockGraphSessionFactoryChanDB struct {
|
|
|
|
graph *channeldb.ChannelGraph
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockGraphSessionFactoryFromChanDB(
|
|
|
|
graph *channeldb.ChannelGraph) *mockGraphSessionFactoryChanDB {
|
|
|
|
|
|
|
|
return &mockGraphSessionFactoryChanDB{
|
|
|
|
graph: graph,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *mockGraphSessionFactoryChanDB) NewGraphSession() (Graph, func() error,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
tx, err := g.graph.NewPathFindTx()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
session := &mockGraphSessionChanDB{
|
|
|
|
graph: g.graph,
|
|
|
|
tx: tx,
|
|
|
|
}
|
|
|
|
|
|
|
|
return session, session.close, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ GraphSessionFactory = (*mockGraphSessionFactoryChanDB)(nil)
|
|
|
|
|
|
|
|
type mockGraphSessionChanDB struct {
|
|
|
|
graph *channeldb.ChannelGraph
|
|
|
|
tx kvdb.RTx
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockGraphSessionChanDB(graph *channeldb.ChannelGraph) Graph {
|
|
|
|
return &mockGraphSessionChanDB{
|
|
|
|
graph: graph,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *mockGraphSessionChanDB) close() error {
|
|
|
|
if g.tx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := g.tx.Rollback()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error closing db tx: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *mockGraphSessionChanDB) ForEachNodeChannel(nodePub route.Vertex,
|
|
|
|
cb func(channel *channeldb.DirectedChannel) error) error {
|
|
|
|
|
|
|
|
return g.graph.ForEachNodeDirectedChannel(g.tx, nodePub, cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *mockGraphSessionChanDB) FetchNodeFeatures(nodePub route.Vertex) (
|
|
|
|
*lnwire.FeatureVector, error) {
|
|
|
|
|
|
|
|
return g.graph.FetchNodeFeatures(nodePub)
|
|
|
|
}
|