2017-10-17 04:57:30 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
2021-01-19 11:57:13 +03:00
|
|
|
"errors"
|
2021-01-19 11:57:11 +03:00
|
|
|
"fmt"
|
2017-10-17 04:57:30 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-10-18 05:41:46 +03:00
|
|
|
|
2019-06-26 10:49:16 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2020-01-10 05:44:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2018-03-27 06:53:46 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-04-05 18:36:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing/route"
|
2017-10-17 04:57:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-22 12:56:04 +03:00
|
|
|
// DefaultPenaltyHalfLife is the default half-life duration. The
|
2019-03-19 19:09:27 +03:00
|
|
|
// half-life duration defines after how much time a penalized node or
|
|
|
|
// channel is back at 50% probability.
|
2019-05-22 12:56:04 +03:00
|
|
|
DefaultPenaltyHalfLife = time.Hour
|
2019-06-26 09:39:34 +03:00
|
|
|
|
|
|
|
// minSecondChanceInterval is the minimum time required between
|
|
|
|
// second-chance failures.
|
|
|
|
//
|
|
|
|
// If nodes return a channel policy related failure, they may get a
|
|
|
|
// second chance to forward the payment. It could be that the channel
|
|
|
|
// policy that we are aware of is not up to date. This is especially
|
|
|
|
// important in case of mobile apps that are mostly offline.
|
|
|
|
//
|
|
|
|
// However, we don't want to give nodes the option to endlessly return
|
|
|
|
// new channel updates so that we are kept busy trying to route through
|
|
|
|
// that node until the payment loop times out.
|
|
|
|
//
|
|
|
|
// Therefore we only grant a second chance to a node if the previous
|
|
|
|
// second chance is sufficiently long ago. This is what
|
|
|
|
// minSecondChanceInterval defines. If a second policy failure comes in
|
|
|
|
// within that interval, we will apply a penalty.
|
|
|
|
//
|
|
|
|
// Second chances granted are tracked on the level of node pairs. This
|
|
|
|
// means that if a node has multiple channels to the same peer, they
|
|
|
|
// will only get a single second chance to route to that peer again.
|
|
|
|
// Nodes forward non-strict, so it isn't necessary to apply a less
|
|
|
|
// restrictive channel level tracking scheme here.
|
|
|
|
minSecondChanceInterval = time.Minute
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
// DefaultMaxMcHistory is the default maximum history size.
|
|
|
|
DefaultMaxMcHistory = 1000
|
2019-07-29 15:20:06 +03:00
|
|
|
|
|
|
|
// prevSuccessProbability is the assumed probability for node pairs that
|
|
|
|
// successfully relayed the previous attempt.
|
|
|
|
prevSuccessProbability = 0.95
|
2019-09-04 18:40:14 +03:00
|
|
|
|
|
|
|
// DefaultAprioriWeight is the default a priori weight. See
|
|
|
|
// MissionControlConfig for further explanation.
|
|
|
|
DefaultAprioriWeight = 0.5
|
2020-03-23 21:59:16 +03:00
|
|
|
|
|
|
|
// DefaultMinFailureRelaxInterval is the default minimum time that must
|
|
|
|
// have passed since the previously recorded failure before the failure
|
|
|
|
// amount may be raised.
|
|
|
|
DefaultMinFailureRelaxInterval = time.Minute
|
2017-10-17 04:57:30 +03:00
|
|
|
)
|
|
|
|
|
2021-01-19 11:57:13 +03:00
|
|
|
var (
|
|
|
|
// ErrInvalidMcHistory is returned if we get a negative mission control
|
|
|
|
// history count.
|
|
|
|
ErrInvalidMcHistory = errors.New("mission control history must be " +
|
|
|
|
">= 0")
|
|
|
|
|
|
|
|
// ErrInvalidFailureInterval is returned if we get an invalid failure
|
|
|
|
// interval.
|
|
|
|
ErrInvalidFailureInterval = errors.New("failure interval must be >= 0")
|
|
|
|
)
|
|
|
|
|
2019-09-03 18:36:32 +03:00
|
|
|
// NodeResults contains previous results from a node to its peers.
|
2019-09-26 16:31:24 +03:00
|
|
|
type NodeResults map[route.Vertex]TimedPairResult
|
2019-09-03 18:36:32 +03:00
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
// MissionControl contains state which summarizes the past attempts of HTLC
|
2019-03-19 19:09:27 +03:00
|
|
|
// routing by external callers when sending payments throughout the network. It
|
|
|
|
// acts as a shared memory during routing attempts with the goal to optimize the
|
|
|
|
// payment attempt success rate.
|
|
|
|
//
|
|
|
|
// Failed payment attempts are reported to mission control. These reports are
|
|
|
|
// used to track the time of the last node or channel level failure. The time
|
|
|
|
// since the last failure is used to estimate a success probability that is fed
|
|
|
|
// into the path finding process for subsequent payment attempts.
|
2019-05-23 21:05:30 +03:00
|
|
|
type MissionControl struct {
|
2020-03-24 18:26:48 +03:00
|
|
|
// state is the internal mission control state that is input for
|
|
|
|
// probability estimation.
|
|
|
|
state *missionControlState
|
2019-06-26 09:39:34 +03:00
|
|
|
|
2019-03-19 19:09:27 +03:00
|
|
|
// now is expected to return the current time. It is supplied as an
|
|
|
|
// external function to enable deterministic unit tests.
|
|
|
|
now func() time.Time
|
|
|
|
|
2021-01-19 11:57:10 +03:00
|
|
|
// selfNode is our pubkey.
|
|
|
|
selfNode route.Vertex
|
2019-03-19 19:09:27 +03:00
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
store *missionControlStore
|
|
|
|
|
2019-09-04 18:40:14 +03:00
|
|
|
// estimator is the probability estimator that is used with the payment
|
|
|
|
// results that mission control collects.
|
|
|
|
estimator *probabilityEstimator
|
|
|
|
|
2017-10-17 04:57:30 +03:00
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// TODO(roasbeef): further counters, if vertex continually unavailable,
|
|
|
|
// add to another generation
|
|
|
|
|
|
|
|
// TODO(roasbeef): also add favorable metrics for nodes
|
|
|
|
}
|
|
|
|
|
2019-05-22 12:56:04 +03:00
|
|
|
// MissionControlConfig defines parameters that control mission control
|
|
|
|
// behaviour.
|
|
|
|
type MissionControlConfig struct {
|
2021-01-19 11:57:13 +03:00
|
|
|
// ProbabilityEstimatorConfig is the config we will use for probability
|
|
|
|
// calculations.
|
|
|
|
ProbabilityEstimatorCfg
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
// MaxMcHistory defines the maximum number of payment results that are
|
|
|
|
// held on disk.
|
|
|
|
MaxMcHistory int
|
2019-09-04 18:40:14 +03:00
|
|
|
|
2020-03-23 21:59:16 +03:00
|
|
|
// MinFailureRelaxInterval is the minimum time that must have passed
|
|
|
|
// since the previously recorded failure before the failure amount may
|
|
|
|
// be raised.
|
|
|
|
MinFailureRelaxInterval time.Duration
|
2019-05-22 12:56:04 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:13 +03:00
|
|
|
func (c *MissionControlConfig) validate() error {
|
|
|
|
if err := c.ProbabilityEstimatorCfg.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MaxMcHistory < 0 {
|
|
|
|
return ErrInvalidMcHistory
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MinFailureRelaxInterval < 0 {
|
|
|
|
return ErrInvalidFailureInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:11 +03:00
|
|
|
// String returns a string representation of a mission control config.
|
|
|
|
func (c *MissionControlConfig) String() string {
|
|
|
|
return fmt.Sprintf("Penalty Half Life: %v, Apriori Hop "+
|
|
|
|
"Probablity: %v, Maximum History: %v, Apriori Weight: %v, "+
|
|
|
|
"Minimum Failure Relax Interval: %v", c.PenaltyHalfLife,
|
|
|
|
c.AprioriHopProbability, c.MaxMcHistory, c.AprioriWeight,
|
|
|
|
c.MinFailureRelaxInterval)
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:31:24 +03:00
|
|
|
// TimedPairResult describes a timestamped pair result.
|
|
|
|
type TimedPairResult struct {
|
2019-09-26 18:04:02 +03:00
|
|
|
// FailTime is the time of the last failure.
|
|
|
|
FailTime time.Time
|
|
|
|
|
|
|
|
// FailAmt is the amount of the last failure. This amount may be pushed
|
|
|
|
// up if a later success is higher than the last failed amount.
|
|
|
|
FailAmt lnwire.MilliSatoshi
|
|
|
|
|
|
|
|
// SuccessTime is the time of the last success.
|
|
|
|
SuccessTime time.Time
|
|
|
|
|
|
|
|
// SuccessAmt is the highest amount that successfully forwarded. This
|
|
|
|
// isn't necessarily the last success amount. The value of this field
|
|
|
|
// may also be pushed down if a later failure is lower than the highest
|
|
|
|
// success amount. Because of this, SuccessAmt may not match
|
|
|
|
// SuccessTime.
|
|
|
|
SuccessAmt lnwire.MilliSatoshi
|
2017-10-18 05:41:46 +03:00
|
|
|
}
|
|
|
|
|
2019-05-10 11:38:31 +03:00
|
|
|
// MissionControlSnapshot contains a snapshot of the current state of mission
|
|
|
|
// control.
|
|
|
|
type MissionControlSnapshot struct {
|
2019-07-29 16:10:58 +03:00
|
|
|
// Pairs is a list of channels for which specific information is
|
|
|
|
// logged.
|
|
|
|
Pairs []MissionControlPairSnapshot
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
// MissionControlPairSnapshot contains a snapshot of the current node pair
|
2019-05-10 11:38:31 +03:00
|
|
|
// state in mission control.
|
2019-07-29 16:10:58 +03:00
|
|
|
type MissionControlPairSnapshot struct {
|
|
|
|
// Pair is the node pair of which the state is described.
|
|
|
|
Pair DirectedNodePair
|
2019-05-10 11:38:31 +03:00
|
|
|
|
2019-09-30 10:20:55 +03:00
|
|
|
// TimedPairResult contains the data for this pair.
|
|
|
|
TimedPairResult
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:25:23 +03:00
|
|
|
// paymentResult is the information that becomes available when a payment
|
|
|
|
// attempt completes.
|
|
|
|
type paymentResult struct {
|
|
|
|
id uint64
|
|
|
|
timeFwd, timeReply time.Time
|
|
|
|
route *route.Route
|
|
|
|
success bool
|
|
|
|
failureSourceIdx *int
|
|
|
|
failure lnwire.FailureMessage
|
|
|
|
}
|
|
|
|
|
2019-03-19 19:09:27 +03:00
|
|
|
// NewMissionControl returns a new instance of missionControl.
|
2021-01-19 11:57:10 +03:00
|
|
|
func NewMissionControl(db kvdb.Backend, self route.Vertex,
|
|
|
|
cfg *MissionControlConfig) (*MissionControl, error) {
|
2019-06-26 14:00:35 +03:00
|
|
|
|
2021-01-19 11:57:11 +03:00
|
|
|
log.Debugf("Instantiating mission control with config: %v", cfg)
|
2017-10-17 04:57:30 +03:00
|
|
|
|
2021-01-19 11:57:13 +03:00
|
|
|
if err := cfg.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
store, err := newMissionControlStore(db, cfg.MaxMcHistory)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-04 18:40:14 +03:00
|
|
|
estimator := &probabilityEstimator{
|
2021-01-19 11:57:13 +03:00
|
|
|
ProbabilityEstimatorCfg: cfg.ProbabilityEstimatorCfg,
|
|
|
|
prevSuccessProbability: prevSuccessProbability,
|
2019-09-04 18:40:14 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
mc := &MissionControl{
|
2020-03-23 21:59:16 +03:00
|
|
|
state: newMissionControlState(cfg.MinFailureRelaxInterval),
|
2020-03-24 18:26:48 +03:00
|
|
|
now: time.Now,
|
2021-01-19 11:57:10 +03:00
|
|
|
selfNode: self,
|
2020-03-24 18:26:48 +03:00
|
|
|
store: store,
|
|
|
|
estimator: estimator,
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := mc.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// init initializes mission control with historical data.
|
|
|
|
func (m *MissionControl) init() error {
|
|
|
|
log.Debugf("Mission control state reconstruction started")
|
|
|
|
|
2021-01-19 11:57:12 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
results, err := m.store.fetchAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
m.applyPaymentResult(result)
|
2019-05-23 21:05:29 +03:00
|
|
|
}
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
log.Debugf("Mission control state reconstruction finished: "+
|
2020-04-09 21:42:27 +03:00
|
|
|
"n=%v, time=%v", len(results), time.Since(start))
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
return nil
|
2019-05-23 21:05:29 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:14 +03:00
|
|
|
// GetConfig returns the config that mission control is currently configured
|
|
|
|
// with. All fields are copied by value, so we do not need to worry about
|
|
|
|
// mutation.
|
|
|
|
func (m *MissionControl) GetConfig() *MissionControlConfig {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
return &MissionControlConfig{
|
|
|
|
ProbabilityEstimatorCfg: m.estimator.ProbabilityEstimatorCfg,
|
|
|
|
MaxMcHistory: m.store.maxRecords,
|
|
|
|
MinFailureRelaxInterval: m.state.minFailureRelaxInterval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig validates the config provided and updates mission control's config
|
|
|
|
// if it is valid.
|
|
|
|
func (m *MissionControl) SetConfig(cfg *MissionControlConfig) error {
|
|
|
|
if cfg == nil {
|
|
|
|
return errors.New("nil mission control config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cfg.validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
log.Infof("Updating mission control cfg: %v", cfg)
|
|
|
|
|
|
|
|
m.store.maxRecords = cfg.MaxMcHistory
|
|
|
|
m.state.minFailureRelaxInterval = cfg.MinFailureRelaxInterval
|
|
|
|
m.estimator.ProbabilityEstimatorCfg = cfg.ProbabilityEstimatorCfg
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:05:30 +03:00
|
|
|
// ResetHistory resets the history of MissionControl returning it to a state as
|
2017-10-17 04:57:30 +03:00
|
|
|
// if no payment attempts have been made.
|
2019-06-26 14:00:35 +03:00
|
|
|
func (m *MissionControl) ResetHistory() error {
|
2017-10-17 04:57:30 +03:00
|
|
|
m.Lock()
|
2019-03-19 19:09:27 +03:00
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
if err := m.store.clear(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
m.state.resetHistory()
|
2019-03-19 19:09:27 +03:00
|
|
|
|
|
|
|
log.Debugf("Mission control history cleared")
|
2019-06-26 14:00:35 +03:00
|
|
|
|
|
|
|
return nil
|
2019-03-19 19:09:27 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 16:10:58 +03:00
|
|
|
// GetProbability is expected to return the success probability of a payment
|
2019-03-19 19:09:27 +03:00
|
|
|
// from fromNode along edge.
|
2019-07-29 16:10:58 +03:00
|
|
|
func (m *MissionControl) GetProbability(fromNode, toNode route.Vertex,
|
|
|
|
amt lnwire.MilliSatoshi) float64 {
|
2019-03-19 19:09:27 +03:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-09-04 18:40:14 +03:00
|
|
|
now := m.now()
|
2020-03-24 18:26:48 +03:00
|
|
|
results, _ := m.state.getLastPairResult(fromNode)
|
2019-03-19 19:09:27 +03:00
|
|
|
|
2019-11-07 13:24:38 +03:00
|
|
|
// Use a distinct probability estimation function for local channels.
|
2021-01-19 11:57:10 +03:00
|
|
|
if fromNode == m.selfNode {
|
2019-11-07 13:24:38 +03:00
|
|
|
return m.estimator.getLocalPairProbability(now, results, toNode)
|
|
|
|
}
|
|
|
|
|
2019-09-04 18:40:14 +03:00
|
|
|
return m.estimator.getPairProbability(now, results, toNode, amt)
|
2019-09-03 18:36:32 +03:00
|
|
|
}
|
|
|
|
|
2019-05-10 11:38:31 +03:00
|
|
|
// GetHistorySnapshot takes a snapshot from the current mission control state
|
|
|
|
// and actual probability estimates.
|
|
|
|
func (m *MissionControl) GetHistorySnapshot() *MissionControlSnapshot {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
log.Debugf("Requesting history snapshot from mission control")
|
2019-05-10 11:38:31 +03:00
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
return m.state.getSnapshot()
|
2019-05-10 11:38:31 +03:00
|
|
|
}
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2021-03-18 11:46:45 +03:00
|
|
|
// ImportHistory imports the set of mission control results provided to our
|
|
|
|
// in-memory state. These results are not persisted, so will not survive
|
|
|
|
// restarts.
|
|
|
|
func (m *MissionControl) ImportHistory(history *MissionControlSnapshot) error {
|
|
|
|
if history == nil {
|
|
|
|
return errors.New("cannot import nil history")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
log.Infof("Importing history snapshot with %v pairs to mission control",
|
|
|
|
len(history.Pairs))
|
|
|
|
|
|
|
|
imported := m.state.importSnapshot(history)
|
|
|
|
|
|
|
|
log.Infof("Imported %v results to mission control", imported)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:43:12 +03:00
|
|
|
// GetPairHistorySnapshot returns the stored history for a given node pair.
|
|
|
|
func (m *MissionControl) GetPairHistorySnapshot(
|
|
|
|
fromNode, toNode route.Vertex) TimedPairResult {
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
results, ok := m.state.getLastPairResult(fromNode)
|
2019-09-27 12:43:12 +03:00
|
|
|
if !ok {
|
|
|
|
return TimedPairResult{}
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := results[toNode]
|
|
|
|
if !ok {
|
|
|
|
return TimedPairResult{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-06-26 10:49:16 +03:00
|
|
|
// ReportPaymentFail reports a failed payment to mission control as input for
|
2019-06-26 12:34:25 +03:00
|
|
|
// future probability estimates. The failureSourceIdx argument indicates the
|
|
|
|
// failure source. If it is nil, the failure source is unknown. This function
|
2019-08-05 13:13:58 +03:00
|
|
|
// returns a reason if this failure is a final failure. In that case no further
|
|
|
|
// payment attempts need to be made.
|
2019-06-26 12:48:59 +03:00
|
|
|
func (m *MissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
|
2019-08-05 13:13:58 +03:00
|
|
|
failureSourceIdx *int, failure lnwire.FailureMessage) (
|
|
|
|
*channeldb.FailureReason, error) {
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2021-01-19 11:57:12 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-07-02 12:29:44 +03:00
|
|
|
timestamp := m.now()
|
|
|
|
|
2019-06-26 13:25:23 +03:00
|
|
|
result := &paymentResult{
|
|
|
|
success: false,
|
|
|
|
timeFwd: timestamp,
|
|
|
|
timeReply: timestamp,
|
|
|
|
id: paymentID,
|
|
|
|
failureSourceIdx: failureSourceIdx,
|
|
|
|
failure: failure,
|
|
|
|
route: rt,
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:20:06 +03:00
|
|
|
return m.processPaymentResult(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportPaymentSuccess reports a successful payment to mission control as input
|
|
|
|
// for future probability estimates.
|
|
|
|
func (m *MissionControl) ReportPaymentSuccess(paymentID uint64,
|
|
|
|
rt *route.Route) error {
|
|
|
|
|
2021-01-19 11:57:12 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2019-07-29 15:20:06 +03:00
|
|
|
timestamp := m.now()
|
|
|
|
|
|
|
|
result := &paymentResult{
|
|
|
|
timeFwd: timestamp,
|
|
|
|
timeReply: timestamp,
|
|
|
|
id: paymentID,
|
|
|
|
success: true,
|
|
|
|
route: rt,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := m.processPaymentResult(result)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// processPaymentResult stores a payment result in the mission control store and
|
|
|
|
// updates mission control's in-memory state.
|
|
|
|
func (m *MissionControl) processPaymentResult(result *paymentResult) (
|
|
|
|
*channeldb.FailureReason, error) {
|
|
|
|
|
2019-06-26 14:00:35 +03:00
|
|
|
// Store complete result in database.
|
|
|
|
if err := m.store.AddResult(result); err != nil {
|
2019-08-05 13:13:58 +03:00
|
|
|
return nil, err
|
2019-06-26 14:00:35 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 12:29:44 +03:00
|
|
|
// Apply result to update mission control state.
|
2019-08-05 13:29:16 +03:00
|
|
|
reason := m.applyPaymentResult(result)
|
2019-08-05 13:13:58 +03:00
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
return reason, nil
|
2019-07-02 12:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// applyPaymentResult applies a payment result as input for future probability
|
|
|
|
// estimates. It returns a bool indicating whether this error is a final error
|
|
|
|
// and no further payment attempts need to be made.
|
2019-08-05 13:29:16 +03:00
|
|
|
func (m *MissionControl) applyPaymentResult(
|
|
|
|
result *paymentResult) *channeldb.FailureReason {
|
2019-07-02 12:29:44 +03:00
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
// Interpret result.
|
|
|
|
i := interpretResult(
|
2019-07-29 15:20:06 +03:00
|
|
|
result.route, result.success, result.failureSourceIdx,
|
|
|
|
result.failure,
|
2019-07-02 12:29:44 +03:00
|
|
|
)
|
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
if i.policyFailure != nil {
|
2020-03-24 18:26:48 +03:00
|
|
|
if m.state.requestSecondChance(
|
2019-08-05 13:29:16 +03:00
|
|
|
result.timeReply,
|
|
|
|
i.policyFailure.From, i.policyFailure.To,
|
|
|
|
) {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:40:14 +03:00
|
|
|
// If there is a node-level failure, record a failure for every tried
|
|
|
|
// connection of that node. A node-level failure can be considered as a
|
|
|
|
// failure that would have occurred with any of the node's channels.
|
|
|
|
//
|
|
|
|
// Ideally we'd also record the failure for the untried connections of
|
|
|
|
// the node. Unfortunately this would require access to the graph and
|
|
|
|
// adding this dependency and db calls does not outweigh the benefits.
|
|
|
|
//
|
|
|
|
// Untried connections will fall back to the node probability. After the
|
|
|
|
// call to setAllPairResult below, the node probability will be equal to
|
|
|
|
// the probability of the tried channels except that the a priori
|
|
|
|
// probability is mixed in too. This effect is controlled by the
|
|
|
|
// aprioriWeight parameter. If that parameter isn't set to an extreme
|
|
|
|
// and there are a few known connections, there shouldn't be much of a
|
|
|
|
// difference. The largest difference occurs when aprioriWeight is 1. In
|
|
|
|
// that case, a node-level failure would not be applied to untried
|
|
|
|
// channels.
|
2019-08-05 13:29:16 +03:00
|
|
|
if i.nodeFailure != nil {
|
|
|
|
log.Debugf("Reporting node failure to Mission Control: "+
|
|
|
|
"node=%v", *i.nodeFailure)
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
m.state.setAllFail(*i.nodeFailure, result.timeReply)
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:20:06 +03:00
|
|
|
for pair, pairResult := range i.pairResults {
|
2019-09-26 18:04:02 +03:00
|
|
|
pairResult := pairResult
|
|
|
|
|
2019-07-29 15:20:06 +03:00
|
|
|
if pairResult.success {
|
|
|
|
log.Debugf("Reporting pair success to Mission "+
|
2020-03-18 15:06:05 +03:00
|
|
|
"Control: pair=%v, amt=%v",
|
|
|
|
pair, pairResult.amt)
|
2019-07-29 15:20:06 +03:00
|
|
|
} else {
|
|
|
|
log.Debugf("Reporting pair failure to Mission "+
|
2019-09-26 17:33:08 +03:00
|
|
|
"Control: pair=%v, amt=%v",
|
|
|
|
pair, pairResult.amt)
|
2019-07-29 15:20:06 +03:00
|
|
|
}
|
2019-06-26 10:49:16 +03:00
|
|
|
|
2020-03-24 18:26:48 +03:00
|
|
|
m.state.setLastPairResult(
|
2019-09-26 18:04:02 +03:00
|
|
|
pair.From, pair.To, result.timeReply, &pairResult,
|
2019-09-26 16:27:41 +03:00
|
|
|
)
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 13:29:16 +03:00
|
|
|
return i.finalFailureReason
|
2019-06-26 10:49:16 +03:00
|
|
|
}
|