github.com/decred/dcrlnd@v0.7.6/routing/router.go (about) 1 package routing 2 3 import ( 4 "bytes" 5 goErrors "errors" 6 "fmt" 7 "runtime" 8 "strings" 9 "sync" 10 "sync/atomic" 11 "time" 12 13 "github.com/davecgh/go-spew/spew" 14 "github.com/decred/dcrd/dcrec/secp256k1/v4" 15 "github.com/decred/dcrd/dcrutil/v4" 16 "github.com/decred/dcrd/wire" 17 sphinx "github.com/decred/lightning-onion/v4" 18 "github.com/go-errors/errors" 19 20 "github.com/decred/dcrlnd/amp" 21 "github.com/decred/dcrlnd/batch" 22 "github.com/decred/dcrlnd/chainntnfs" 23 "github.com/decred/dcrlnd/channeldb" 24 "github.com/decred/dcrlnd/clock" 25 "github.com/decred/dcrlnd/htlcswitch" 26 "github.com/decred/dcrlnd/input" 27 "github.com/decred/dcrlnd/kvdb" 28 "github.com/decred/dcrlnd/lntypes" 29 "github.com/decred/dcrlnd/lnwallet" 30 "github.com/decred/dcrlnd/lnwallet/chanvalidate" 31 "github.com/decred/dcrlnd/lnwire" 32 "github.com/decred/dcrlnd/multimutex" 33 "github.com/decred/dcrlnd/record" 34 "github.com/decred/dcrlnd/routing/chainview" 35 "github.com/decred/dcrlnd/routing/route" 36 "github.com/decred/dcrlnd/routing/shards" 37 "github.com/decred/dcrlnd/ticker" 38 "github.com/decred/dcrlnd/zpay32" 39 ) 40 41 const ( 42 // DefaultPayAttemptTimeout is the default payment attempt timeout. The 43 // payment attempt timeout defines the duration after which we stop 44 // trying more routes for a payment. 45 DefaultPayAttemptTimeout = time.Second * 60 46 47 // DefaultChannelPruneExpiry is the default duration used to determine 48 // if a channel should be pruned or not. 49 DefaultChannelPruneExpiry = time.Hour * 24 * 14 50 51 // DefaultFirstTimePruneDelay is the time we'll wait after startup 52 // before attempting to prune the graph for zombie channels. We don't 53 // do it immediately after startup to allow lnd to start up without 54 // getting blocked by this job. 55 DefaultFirstTimePruneDelay = 30 * time.Second 56 57 // defaultStatInterval governs how often the router will log non-empty 58 // stats related to processing new channels, updates, or node 59 // announcements. 60 defaultStatInterval = time.Minute 61 62 // MinCLTVDelta is the minimum CLTV value accepted by LND for all 63 // timelock deltas. This includes both forwarding CLTV deltas set on 64 // channel updates, as well as final CLTV deltas used to create BOLT 11 65 // payment requests. 66 // 67 // NOTE: For payment requests, BOLT 11 stipulates that a final CLTV 68 // delta of 9 should be used when no value is decoded. This however 69 // leads to inflexiblity in upgrading this default parameter, since it 70 // can create inconsistencies around the assumed value between sender 71 // and receiver. Specifically, if the receiver assumes a higher value 72 // than the sender, the receiver will always see the received HTLCs as 73 // invalid due to their timelock not meeting the required delta. 74 // 75 // We skirt this by always setting an explicit CLTV delta when creating 76 // invoices. This allows LND nodes to freely update the minimum without 77 // creating incompatibilities during the upgrade process. For some time 78 // LND has used an explicit default final CLTV delta of 40 blocks for 79 // bitcoin (160 for litecoin), though we now clamp the lower end of this 80 // range for user-chosen deltas to 18 blocks to be conservative. 81 MinCLTVDelta = 18 82 ) 83 84 var ( 85 // ErrRouterShuttingDown is returned if the router is in the process of 86 // shutting down. 87 ErrRouterShuttingDown = fmt.Errorf("router shutting down") 88 ) 89 90 // ChannelGraphSource represents the source of information about the topology 91 // of the lightning network. It's responsible for the addition of nodes, edges, 92 // applying edge updates, and returning the current block height with which the 93 // topology is synchronized. 94 type ChannelGraphSource interface { 95 // AddNode is used to add information about a node to the router 96 // database. If the node with this pubkey is not present in an existing 97 // channel, it will be ignored. 98 AddNode(node *channeldb.LightningNode, op ...batch.SchedulerOption) error 99 100 // AddEdge is used to add edge/channel to the topology of the router, 101 // after all information about channel will be gathered this 102 // edge/channel might be used in construction of payment path. 103 AddEdge(edge *channeldb.ChannelEdgeInfo, op ...batch.SchedulerOption) error 104 105 // AddProof updates the channel edge info with proof which is needed to 106 // properly announce the edge to the rest of the network. 107 AddProof(chanID lnwire.ShortChannelID, proof *channeldb.ChannelAuthProof) error 108 109 // UpdateEdge is used to update edge information, without this message 110 // edge considered as not fully constructed. 111 UpdateEdge(policy *channeldb.ChannelEdgePolicy, op ...batch.SchedulerOption) error 112 113 // IsStaleNode returns true if the graph source has a node announcement 114 // for the target node with a more recent timestamp. This method will 115 // also return true if we don't have an active channel announcement for 116 // the target node. 117 IsStaleNode(node route.Vertex, timestamp time.Time) bool 118 119 // IsPublicNode determines whether the given vertex is seen as a public 120 // node in the graph from the graph's source node's point of view. 121 IsPublicNode(node route.Vertex) (bool, error) 122 123 // IsKnownEdge returns true if the graph source already knows of the 124 // passed channel ID either as a live or zombie edge. 125 IsKnownEdge(chanID lnwire.ShortChannelID) bool 126 127 // IsStaleEdgePolicy returns true if the graph source has a channel 128 // edge for the passed channel ID (and flags) that have a more recent 129 // timestamp. 130 IsStaleEdgePolicy(chanID lnwire.ShortChannelID, timestamp time.Time, 131 flags lnwire.ChanUpdateChanFlags) bool 132 133 // MarkEdgeLive clears an edge from our zombie index, deeming it as 134 // live. 135 MarkEdgeLive(chanID lnwire.ShortChannelID) error 136 137 // ForAllOutgoingChannels is used to iterate over all channels 138 // emanating from the "source" node which is the center of the 139 // star-graph. 140 ForAllOutgoingChannels(cb func(tx kvdb.RTx, 141 c *channeldb.ChannelEdgeInfo, 142 e *channeldb.ChannelEdgePolicy) error) error 143 144 // CurrentBlockHeight returns the block height from POV of the router 145 // subsystem. 146 CurrentBlockHeight() (uint32, error) 147 148 // GetChannelByID return the channel by the channel id. 149 GetChannelByID(chanID lnwire.ShortChannelID) (*channeldb.ChannelEdgeInfo, 150 *channeldb.ChannelEdgePolicy, *channeldb.ChannelEdgePolicy, error) 151 152 // FetchLightningNode attempts to look up a target node by its identity 153 // public key. channeldb.ErrGraphNodeNotFound is returned if the node 154 // doesn't exist within the graph. 155 FetchLightningNode(route.Vertex) (*channeldb.LightningNode, error) 156 157 // ForEachNode is used to iterate over every node in the known graph. 158 ForEachNode(func(node *channeldb.LightningNode) error) error 159 } 160 161 // PaymentAttemptDispatcher is used by the router to send payment attempts onto 162 // the network, and receive their results. 163 type PaymentAttemptDispatcher interface { 164 // SendHTLC is a function that directs a link-layer switch to 165 // forward a fully encoded payment to the first hop in the route 166 // denoted by its public key. A non-nil error is to be returned if the 167 // payment was unsuccessful. 168 SendHTLC(firstHop lnwire.ShortChannelID, 169 attemptID uint64, 170 htlcAdd *lnwire.UpdateAddHTLC) error 171 172 // GetPaymentResult returns the the result of the payment attempt with 173 // the given attemptID. The paymentHash should be set to the payment's 174 // overall hash, or in case of AMP payments the payment's unique 175 // identifier. 176 // 177 // The method returns a channel where the payment result will be sent 178 // when available, or an error is encountered during forwarding. When a 179 // result is received on the channel, the HTLC is guaranteed to no 180 // longer be in flight. The switch shutting down is signaled by 181 // closing the channel. If the attemptID is unknown, 182 // ErrPaymentIDNotFound will be returned. 183 GetPaymentResult(attemptID uint64, paymentHash lntypes.Hash, 184 deobfuscator htlcswitch.ErrorDecrypter) ( 185 <-chan *htlcswitch.PaymentResult, error) 186 187 // CleanStore calls the underlying result store, telling it is safe to 188 // delete all entries except the ones in the keepPids map. This should 189 // be called preiodically to let the switch clean up payment results 190 // that we have handled. 191 // NOTE: New payment attempts MUST NOT be made after the keepPids map 192 // has been created and this method has returned. 193 CleanStore(keepPids map[uint64]struct{}) error 194 } 195 196 // PaymentSessionSource is an interface that defines a source for the router to 197 // retrive new payment sessions. 198 type PaymentSessionSource interface { 199 // NewPaymentSession creates a new payment session that will produce 200 // routes to the given target. An optional set of routing hints can be 201 // provided in order to populate additional edges to explore when 202 // finding a path to the payment's destination. 203 NewPaymentSession(p *LightningPayment) (PaymentSession, error) 204 205 // NewPaymentSessionEmpty creates a new paymentSession instance that is 206 // empty, and will be exhausted immediately. Used for failure reporting 207 // to missioncontrol for resumed payment we don't want to make more 208 // attempts for. 209 NewPaymentSessionEmpty() PaymentSession 210 } 211 212 // MissionController is an interface that exposes failure reporting and 213 // probability estimation. 214 type MissionController interface { 215 // ReportPaymentFail reports a failed payment to mission control as 216 // input for future probability estimates. It returns a bool indicating 217 // whether this error is a final error and no further payment attempts 218 // need to be made. 219 ReportPaymentFail(attemptID uint64, rt *route.Route, 220 failureSourceIdx *int, failure lnwire.FailureMessage) ( 221 *channeldb.FailureReason, error) 222 223 // ReportPaymentSuccess reports a successful payment to mission control as input 224 // for future probability estimates. 225 ReportPaymentSuccess(attemptID uint64, rt *route.Route) error 226 227 // GetProbability is expected to return the success probability of a 228 // payment from fromNode along edge. 229 GetProbability(fromNode, toNode route.Vertex, 230 amt lnwire.MilliAtom) float64 231 } 232 233 // FeeSchema is the set fee configuration for a Lightning Node on the network. 234 // Using the coefficients described within the schema, the required fee to 235 // forward outgoing payments can be derived. 236 type FeeSchema struct { 237 // BaseFee is the base amount of milli-atoms that will be chained 238 // for ANY payment forwarded. 239 BaseFee lnwire.MilliAtom 240 241 // FeeRate is the rate that will be charged for forwarding payments. 242 // This value should be interpreted as the numerator for a fraction 243 // (fixed point arithmetic) whose denominator is 1 million. As a result 244 // the effective fee rate charged per milli-atoms will be: (amount * 245 // FeeRate/1,000,000). 246 FeeRate uint32 247 } 248 249 // ChannelPolicy holds the parameters that determine the policy we enforce 250 // when forwarding payments on a channel. These parameters are communicated 251 // to the rest of the network in ChannelUpdate messages. 252 type ChannelPolicy struct { 253 // FeeSchema holds the fee configuration for a channel. 254 FeeSchema 255 256 // TimeLockDelta is the required HTLC timelock delta to be used 257 // when forwarding payments. 258 TimeLockDelta uint32 259 260 // MaxHTLC is the maximum HTLC size including fees we are allowed to 261 // forward over this channel. 262 MaxHTLC lnwire.MilliAtom 263 264 // MinHTLC is the minimum HTLC size including fees we are allowed to 265 // forward over this channel. 266 MinHTLC *lnwire.MilliAtom 267 } 268 269 // Config defines the configuration for the ChannelRouter. ALL elements within 270 // the configuration MUST be non-nil for the ChannelRouter to carry out its 271 // duties. 272 type Config struct { 273 // Graph is the channel graph that the ChannelRouter will use to gather 274 // metrics from and also to carry out path finding queries. 275 // TODO(roasbeef): make into an interface 276 Graph *channeldb.ChannelGraph 277 278 // Chain is the router's source to the most up-to-date blockchain data. 279 // All incoming advertised channels will be checked against the chain 280 // to ensure that the channels advertised are still open. 281 Chain lnwallet.BlockChainIO 282 283 // ChainView is an instance of a FilteredChainView which is used to 284 // watch the sub-set of the UTXO set (the set of active channels) that 285 // we need in order to properly maintain the channel graph. 286 ChainView chainview.FilteredChainView 287 288 // Notifier is a reference to the ChainNotifier, used to grab 289 // the latest blocks if the router is missing any. 290 Notifier chainntnfs.ChainNotifier 291 292 // Payer is an instance of a PaymentAttemptDispatcher and is used by 293 // the router to send payment attempts onto the network, and receive 294 // their results. 295 Payer PaymentAttemptDispatcher 296 297 // Control keeps track of the status of ongoing payments, ensuring we 298 // can properly resume them across restarts. 299 Control ControlTower 300 301 // MissionControl is a shared memory of sorts that executions of 302 // payment path finding use in order to remember which vertexes/edges 303 // were pruned from prior attempts. During SendPayment execution, 304 // errors sent by nodes are mapped into a vertex or edge to be pruned. 305 // Each run will then take into account this set of pruned 306 // vertexes/edges to reduce route failure and pass on graph information 307 // gained to the next execution. 308 MissionControl MissionController 309 310 // SessionSource defines a source for the router to retrieve new payment 311 // sessions. 312 SessionSource PaymentSessionSource 313 314 // ChannelPruneExpiry is the duration used to determine if a channel 315 // should be pruned or not. If the delta between now and when the 316 // channel was last updated is greater than ChannelPruneExpiry, then 317 // the channel is marked as a zombie channel eligible for pruning. 318 ChannelPruneExpiry time.Duration 319 320 // GraphPruneInterval is used as an interval to determine how often we 321 // should examine the channel graph to garbage collect zombie channels. 322 GraphPruneInterval time.Duration 323 324 // FirstTimePruneDelay is the time we'll wait after startup before 325 // attempting to prune the graph for zombie channels. We don't do it 326 // immediately after startup to allow lnd to start up without getting 327 // blocked by this job. 328 FirstTimePruneDelay time.Duration 329 330 // QueryBandwidth is a method that allows the router to query the lower 331 // link layer to determine the up to date available bandwidth at a 332 // prospective link to be traversed. If the link isn't available, then 333 // a value of zero should be returned. Otherwise, the current up to 334 // date knowledge of the available bandwidth of the link should be 335 // returned. 336 GetLink getLinkQuery 337 338 // NextPaymentID is a method that guarantees to return a new, unique ID 339 // each time it is called. This is used by the router to generate a 340 // unique payment ID for each payment it attempts to send, such that 341 // the switch can properly handle the HTLC. 342 NextPaymentID func() (uint64, error) 343 344 // AssumeChannelValid toggles whether or not the router will check for 345 // spentness of channel outpoints. For neutrino, this saves long rescans 346 // from blocking initial usage of the daemon. 347 AssumeChannelValid bool 348 349 // PathFindingConfig defines global path finding parameters. 350 PathFindingConfig PathFindingConfig 351 352 // LocalOpenChanIDs should return a list of short channel IDs for all 353 // open channels. This is a dcrlnd-only feature. 354 LocalOpenChanIDs func() (map[uint64]struct{}, error) 355 356 // Clock is mockable time provider. 357 Clock clock.Clock 358 359 // StrictZombiePruning determines if we attempt to prune zombie 360 // channels according to a stricter criteria. If true, then we'll prune 361 // a channel if only *one* of the edges is considered a zombie. 362 // Otherwise, we'll only prune the channel when both edges have a very 363 // dated last update. 364 StrictZombiePruning bool 365 } 366 367 // EdgeLocator is a struct used to identify a specific edge. 368 type EdgeLocator struct { 369 // ChannelID is the channel of this edge. 370 ChannelID uint64 371 372 // Direction takes the value of 0 or 1 and is identical in definition to 373 // the channel direction flag. A value of 0 means the direction from the 374 // lower node pubkey to the higher. 375 Direction uint8 376 } 377 378 // String returns a human readable version of the edgeLocator values. 379 func (e *EdgeLocator) String() string { 380 return fmt.Sprintf("%v:%v", e.ChannelID, e.Direction) 381 } 382 383 // ChannelRouter is the layer 3 router within the Lightning stack. Below the 384 // ChannelRouter is the HtlcSwitch, and below that is the Bitcoin blockchain 385 // itself. The primary role of the ChannelRouter is to respond to queries for 386 // potential routes that can support a payment amount, and also general graph 387 // reachability questions. The router will prune the channel graph 388 // automatically as new blocks are discovered which spend certain known funding 389 // outpoints, thereby closing their respective channels. 390 type ChannelRouter struct { 391 ntfnClientCounter uint64 // To be used atomically. 392 393 started uint32 // To be used atomically. 394 stopped uint32 // To be used atomically. 395 396 bestHeight uint32 // To be used atomically. 397 398 // cfg is a copy of the configuration struct that the ChannelRouter was 399 // initialized with. 400 cfg *Config 401 402 // selfNode is the center of the star-graph centered around the 403 // ChannelRouter. The ChannelRouter uses this node as a starting point 404 // when doing any path finding. 405 selfNode *channeldb.LightningNode 406 407 // cachedGraph is an instance of routingGraph that caches the source node as 408 // well as the channel graph itself in memory. 409 cachedGraph routingGraph 410 411 // newBlocks is a channel in which new blocks connected to the end of 412 // the main chain are sent over, and blocks updated after a call to 413 // UpdateFilter. 414 newBlocks <-chan *chainview.FilteredBlock 415 416 // staleBlocks is a channel in which blocks disconnected fromt the end 417 // of our currently known best chain are sent over. 418 staleBlocks <-chan *chainview.FilteredBlock 419 420 // networkUpdates is a channel that carries new topology updates 421 // messages from outside the ChannelRouter to be processed by the 422 // networkHandler. 423 networkUpdates chan *routingMsg 424 425 // topologyClients maps a client's unique notification ID to a 426 // topologyClient client that contains its notification dispatch 427 // channel. 428 topologyClients map[uint64]*topologyClient 429 430 // ntfnClientUpdates is a channel that's used to send new updates to 431 // topology notification clients to the ChannelRouter. Updates either 432 // add a new notification client, or cancel notifications for an 433 // existing client. 434 ntfnClientUpdates chan *topologyClientUpdate 435 436 // channelEdgeMtx is a mutex we use to make sure we process only one 437 // ChannelEdgePolicy at a time for a given channelID, to ensure 438 // consistency between the various database accesses. 439 channelEdgeMtx *multimutex.Mutex 440 441 // statTicker is a resumable ticker that logs the router's progress as 442 // it discovers channels or receives updates. 443 statTicker ticker.Ticker 444 445 // stats tracks newly processed channels, updates, and node 446 // announcements over a window of defaultStatInterval. 447 stats *routerStats 448 449 // Tracking of startup pruning stats. 450 prunedMtx sync.Mutex 451 prunedTarget uint32 452 prunedHeight uint32 453 454 sync.RWMutex 455 456 quit chan struct{} 457 wg sync.WaitGroup 458 } 459 460 // A compile time check to ensure ChannelRouter implements the 461 // ChannelGraphSource interface. 462 var _ ChannelGraphSource = (*ChannelRouter)(nil) 463 464 // New creates a new instance of the ChannelRouter with the specified 465 // configuration parameters. As part of initialization, if the router detects 466 // that the channel graph isn't fully in sync with the latest UTXO (since the 467 // channel graph is a subset of the UTXO set) set, then the router will proceed 468 // to fully sync to the latest state of the UTXO set. 469 func New(cfg Config) (*ChannelRouter, error) { 470 selfNode, err := cfg.Graph.SourceNode() 471 if err != nil { 472 return nil, err 473 } 474 475 r := &ChannelRouter{ 476 cfg: &cfg, 477 cachedGraph: &CachedGraph{ 478 graph: cfg.Graph, 479 source: selfNode.PubKeyBytes, 480 }, 481 networkUpdates: make(chan *routingMsg), 482 topologyClients: make(map[uint64]*topologyClient), 483 ntfnClientUpdates: make(chan *topologyClientUpdate), 484 channelEdgeMtx: multimutex.NewMutex(), 485 selfNode: selfNode, 486 statTicker: ticker.New(defaultStatInterval), 487 stats: new(routerStats), 488 quit: make(chan struct{}), 489 } 490 491 return r, nil 492 } 493 494 // Start launches all the goroutines the ChannelRouter requires to carry out 495 // its duties. If the router has already been started, then this method is a 496 // noop. 497 func (r *ChannelRouter) Start() error { 498 if !atomic.CompareAndSwapUint32(&r.started, 0, 1) { 499 return nil 500 } 501 502 log.Tracef("Channel Router starting") 503 504 bestHash, bestHeight, err := r.cfg.Chain.GetBestBlock() 505 if err != nil { 506 return err 507 } 508 509 // If the graph has never been pruned, or hasn't fully been created yet, 510 // then we don't treat this as an explicit error. 511 if _, _, err := r.cfg.Graph.PruneTip(); err != nil { 512 switch { 513 case err == channeldb.ErrGraphNeverPruned: 514 fallthrough 515 case err == channeldb.ErrGraphNotFound: 516 // If the graph has never been pruned, then we'll set 517 // the prune height to the current best height of the 518 // chain backend. 519 _, err = r.cfg.Graph.PruneGraph( 520 nil, bestHash, uint32(bestHeight), 521 ) 522 if err != nil { 523 return err 524 } 525 default: 526 return err 527 } 528 } 529 530 // If AssumeChannelValid is present, then we won't rely on pruning 531 // channels from the graph based on their spentness, but whether they 532 // are considered zombies or not. We will start zombie pruning after a 533 // small delay, to avoid slowing down startup of lnd. 534 if r.cfg.AssumeChannelValid { 535 time.AfterFunc(r.cfg.FirstTimePruneDelay, func() { 536 select { 537 case <-r.quit: 538 return 539 default: 540 } 541 542 log.Info("Initial zombie prune starting") 543 if err := r.pruneZombieChans(); err != nil { 544 log.Errorf("Unable to prune zombies: %v", err) 545 } 546 }) 547 } else { 548 // Otherwise, we'll use our filtered chain view to prune 549 // channels as soon as they are detected as spent on-chain. 550 if err := r.cfg.ChainView.Start(); err != nil { 551 return err 552 } 553 554 // Once the instance is active, we'll fetch the channel we'll 555 // receive notifications over. 556 r.newBlocks = r.cfg.ChainView.FilteredBlocks() 557 r.staleBlocks = r.cfg.ChainView.DisconnectedBlocks() 558 559 // Before we perform our manual block pruning, we'll construct 560 // and apply a fresh chain filter to the active 561 // FilteredChainView instance. We do this before, as otherwise 562 // we may miss on-chain events as the filter hasn't properly 563 // been applied. 564 channelView, err := r.cfg.Graph.ChannelView() 565 if err != nil && err != channeldb.ErrGraphNoEdgesFound { 566 return err 567 } 568 569 log.Infof("Filtering chain using %v channels active", 570 len(channelView)) 571 572 if len(channelView) != 0 { 573 err = r.cfg.ChainView.UpdateFilter( 574 channelView, int64(bestHeight), 575 ) 576 if err != nil { 577 return err 578 } 579 } 580 581 // The graph pruning might have taken a while and there could be 582 // new blocks available. 583 _, bestHeight, err = r.cfg.Chain.GetBestBlock() 584 if err != nil { 585 return err 586 } 587 r.bestHeight = uint32(bestHeight) 588 589 // Before we begin normal operation of the router, we first need 590 // to synchronize the channel graph to the latest state of the 591 // UTXO set. 592 if err := r.syncGraphWithChain(); err != nil { 593 return err 594 } 595 596 // Finally, before we proceed, we'll prune any unconnected nodes 597 // from the graph in order to ensure we maintain a tight graph 598 // of "useful" nodes. 599 err = r.cfg.Graph.PruneGraphNodes() 600 if err != nil && err != channeldb.ErrGraphNodesNotFound { 601 return err 602 } 603 } 604 605 // If any payments are still in flight, we resume, to make sure their 606 // results are properly handled. 607 payments, err := r.cfg.Control.FetchInFlightPayments() 608 if err != nil { 609 return err 610 } 611 612 // Before we restart existing payments and start accepting more 613 // payments to be made, we clean the network result store of the 614 // Switch. We do this here at startup to ensure no more payments can be 615 // made concurrently, so we know the toKeep map will be up-to-date 616 // until the cleaning has finished. 617 toKeep := make(map[uint64]struct{}) 618 for _, p := range payments { 619 for _, a := range p.HTLCs { 620 toKeep[a.AttemptID] = struct{}{} 621 } 622 } 623 624 log.Debugf("Cleaning network result store.") 625 if err := r.cfg.Payer.CleanStore(toKeep); err != nil { 626 return err 627 } 628 629 for _, payment := range payments { 630 log.Infof("Resuming payment %v", payment.Info.PaymentIdentifier) 631 r.wg.Add(1) 632 go func(payment *channeldb.MPPayment) { 633 defer r.wg.Done() 634 635 // Get the hashes used for the outstanding HTLCs. 636 htlcs := make(map[uint64]lntypes.Hash) 637 for _, a := range payment.HTLCs { 638 a := a 639 640 // We check whether the individual attempts 641 // have their HTLC hash set, if not we'll fall 642 // back to the overall payment hash. 643 hash := payment.Info.PaymentIdentifier 644 if a.Hash != nil { 645 hash = *a.Hash 646 } 647 648 htlcs[a.AttemptID] = hash 649 } 650 651 // Since we are not supporting creating more shards 652 // after a restart (only receiving the result of the 653 // shards already outstanding), we create a simple 654 // shard tracker that will map the attempt IDs to 655 // hashes used for the HTLCs. This will be enough also 656 // for AMP payments, since we only need the hashes for 657 // the individual HTLCs to regenerate the circuits, and 658 // we don't currently persist the root share necessary 659 // to re-derive them. 660 shardTracker := shards.NewSimpleShardTracker( 661 payment.Info.PaymentIdentifier, htlcs, 662 ) 663 664 // We create a dummy, empty payment session such that 665 // we won't make another payment attempt when the 666 // result for the in-flight attempt is received. 667 paySession := r.cfg.SessionSource.NewPaymentSessionEmpty() 668 669 // We pass in a zero timeout value, to indicate we 670 // don't need it to timeout. It will stop immediately 671 // after the existing attempt has finished anyway. We 672 // also set a zero fee limit, as no more routes should 673 // be tried. 674 _, _, err := r.sendPayment( 675 payment.Info.Value, 0, 676 payment.Info.PaymentIdentifier, 0, paySession, 677 shardTracker, 678 ) 679 if err != nil { 680 log.Errorf("Resuming payment %v failed: %v.", 681 payment.Info.PaymentIdentifier, err) 682 return 683 } 684 685 log.Infof("Resumed payment %v completed.", 686 payment.Info.PaymentIdentifier) 687 }(payment) 688 } 689 690 r.wg.Add(1) 691 go r.networkHandler() 692 693 return nil 694 } 695 696 // Stop signals the ChannelRouter to gracefully halt all routines. This method 697 // will *block* until all goroutines have excited. If the channel router has 698 // already stopped then this method will return immediately. 699 func (r *ChannelRouter) Stop() error { 700 if !atomic.CompareAndSwapUint32(&r.stopped, 0, 1) { 701 return nil 702 } 703 704 log.Info("Channel Router shutting down") 705 706 // Our filtered chain view could've only been started if 707 // AssumeChannelValid isn't present. 708 if !r.cfg.AssumeChannelValid { 709 if err := r.cfg.ChainView.Stop(); err != nil { 710 return err 711 } 712 } 713 714 close(r.quit) 715 r.wg.Wait() 716 717 return nil 718 } 719 720 // StartupPruneProgress returns the progress (respectively, the target height 721 // and last checked height) of the graph pruning process that happens during 722 // startup. 723 func (r *ChannelRouter) StartupPruneProgress() (uint32, uint32) { 724 r.prunedMtx.Lock() 725 target, progress := r.prunedTarget, r.prunedHeight 726 r.prunedMtx.Unlock() 727 return target, progress 728 } 729 730 // syncGraphWithChain attempts to synchronize the current channel graph with 731 // the latest UTXO set state. This process involves pruning from the channel 732 // graph any channels which have been closed by spending their funding output 733 // since we've been down. 734 func (r *ChannelRouter) syncGraphWithChain() error { 735 // First, we'll need to check to see if we're already in sync with the 736 // latest state of the UTXO set. 737 bestHash, bestHeight, err := r.cfg.Chain.GetBestBlock() 738 if err != nil { 739 return err 740 } 741 r.bestHeight = uint32(bestHeight) 742 743 pruneHash, pruneHeight, err := r.cfg.Graph.PruneTip() 744 if err != nil { 745 switch { 746 // If the graph has never been pruned, or hasn't fully been 747 // created yet, then we don't treat this as an explicit error. 748 case err == channeldb.ErrGraphNeverPruned: 749 case err == channeldb.ErrGraphNotFound: 750 default: 751 return err 752 } 753 } 754 755 log.Infof("Prune tip for Channel Graph: height=%v, hash=%v", pruneHeight, 756 pruneHash) 757 758 switch { 759 760 // If the graph has never been pruned, then we can exit early as this 761 // entails it's being created for the first time and hasn't seen any 762 // block or created channels. 763 case pruneHeight == 0 || pruneHash == nil: 764 return nil 765 766 // If the block hashes and heights match exactly, then we don't need to 767 // prune the channel graph as we're already fully in sync. 768 case bestHash.IsEqual(pruneHash) && uint32(bestHeight) == pruneHeight: 769 return nil 770 } 771 772 // If the main chain blockhash at prune height is different from the 773 // prune hash, this might indicate the database is on a stale branch. 774 mainBlockHash, err := r.cfg.Chain.GetBlockHash(int64(pruneHeight)) 775 if err != nil { 776 return err 777 } 778 779 // While we are on a stale branch of the chain, walk backwards to find 780 // first common block. 781 for !pruneHash.IsEqual(mainBlockHash) { 782 log.Infof("channel graph is stale. Disconnecting block %v "+ 783 "(hash=%v)", pruneHeight, pruneHash) 784 // Prune the graph for every channel that was opened at height 785 // >= pruneHeight. 786 _, err := r.cfg.Graph.DisconnectBlockAtHeight(pruneHeight) 787 if err != nil { 788 return err 789 } 790 791 pruneHash, pruneHeight, err = r.cfg.Graph.PruneTip() 792 if err != nil { 793 switch { 794 // If at this point the graph has never been pruned, we 795 // can exit as this entails we are back to the point 796 // where it hasn't seen any block or created channels, 797 // alas there's nothing left to prune. 798 case err == channeldb.ErrGraphNeverPruned: 799 return nil 800 case err == channeldb.ErrGraphNotFound: 801 return nil 802 default: 803 return err 804 } 805 } 806 mainBlockHash, err = r.cfg.Chain.GetBlockHash(int64(pruneHeight)) 807 if err != nil { 808 return err 809 } 810 } 811 812 log.Infof("Syncing channel graph from height=%v (hash=%v) to height=%v "+ 813 "(hash=%v)", pruneHeight, pruneHash, bestHeight, bestHash) 814 815 r.prunedMtx.Lock() 816 r.prunedTarget = uint32(bestHeight) 817 r.prunedMtx.Unlock() 818 lastLogTime := time.Now() 819 lastLogHeight := pruneHeight 820 totalPruneBlocks := uint32(bestHeight) - pruneHeight 821 822 // If we're not yet caught up, then we'll walk forward in the chain 823 // pruning the channel graph with each new block that hasn't yet been 824 // consumed by the channel graph. 825 var spentOutputs []*wire.OutPoint 826 for nextHeight := pruneHeight + 1; nextHeight <= uint32(bestHeight); nextHeight++ { 827 // Break out of the rescan early if a shutdown has been 828 // requested, otherwise long rescans will block the daemon from 829 // shutting down promptly. 830 select { 831 case <-r.quit: 832 return ErrRouterShuttingDown 833 default: 834 } 835 836 // Using the next height, request a manual block pruning from 837 // the chainview for the particular block hash. 838 nextHash, err := r.cfg.Chain.GetBlockHash(int64(nextHeight)) 839 if err != nil { 840 return err 841 } 842 filterBlock, err := r.cfg.ChainView.FilterBlock(nextHash) 843 if err != nil { 844 return err 845 } 846 847 // We're only interested in all prior outputs that have been 848 // spent in the block, so collate all the referenced previous 849 // outpoints within each tx and input. 850 for _, tx := range filterBlock.Transactions { 851 for _, txIn := range tx.TxIn { 852 spentOutputs = append(spentOutputs, 853 &txIn.PreviousOutPoint) 854 } 855 } 856 857 r.prunedMtx.Lock() 858 r.prunedHeight = nextHeight 859 r.prunedMtx.Unlock() 860 861 // Log progress on startup pruning. 862 now := time.Now() 863 if now.After(lastLogTime.Add(5 * time.Second)) { 864 log.Infof("Checked %d blocks for closed channels in the "+ 865 "last %s (%.2f%% complete)", 866 nextHeight-lastLogHeight, now.Sub(lastLogTime).Truncate(time.Millisecond), 867 float64(nextHeight-pruneHeight)/float64(totalPruneBlocks)*100) 868 lastLogHeight = nextHeight 869 lastLogTime = now 870 } 871 } 872 873 // With the spent outputs gathered, attempt to prune the channel graph, 874 // also passing in the best hash+height so the prune tip can be updated. 875 closedChans, err := r.cfg.Graph.PruneGraph( 876 spentOutputs, bestHash, uint32(bestHeight), 877 ) 878 if err != nil { 879 return err 880 } 881 882 log.Infof("Graph pruning complete: %v channels were closed since "+ 883 "height %v", len(closedChans), pruneHeight) 884 return nil 885 } 886 887 // pruneZombieChans is a method that will be called periodically to prune out 888 // any "zombie" channels. We consider channels zombies if *both* edges haven't 889 // been updated since our zombie horizon. If AssumeChannelValid is present, 890 // we'll also consider channels zombies if *both* edges are disabled. This 891 // usually signals that a channel has been closed on-chain. We do this 892 // periodically to keep a healthy, lively routing table. 893 func (r *ChannelRouter) pruneZombieChans() error { 894 chansToPrune := make(map[uint64]struct{}) 895 chanExpiry := r.cfg.ChannelPruneExpiry 896 897 log.Infof("Examining channel graph for zombie channels") 898 899 // A helper method to detect if the channel belongs to this node 900 isSelfChannelEdge := func(info *channeldb.ChannelEdgeInfo) bool { 901 return info.NodeKey1Bytes == r.selfNode.PubKeyBytes || 902 info.NodeKey2Bytes == r.selfNode.PubKeyBytes 903 } 904 905 // First, we'll collect all the channels which are eligible for garbage 906 // collection due to being zombies. 907 filterPruneChans := func(info *channeldb.ChannelEdgeInfo, 908 e1, e2 *channeldb.ChannelEdgePolicy) error { 909 910 // Exit early in case this channel is already marked to be pruned 911 if _, markedToPrune := chansToPrune[info.ChannelID]; markedToPrune { 912 return nil 913 } 914 915 // We'll ensure that we don't attempt to prune our *own* 916 // channels from the graph, as in any case this should be 917 // re-advertised by the sub-system above us. 918 if isSelfChannelEdge(info) { 919 return nil 920 } 921 922 // If either edge hasn't been updated for a period of 923 // chanExpiry, then we'll mark the channel itself as eligible 924 // for graph pruning. 925 e1Zombie := e1 == nil || time.Since(e1.LastUpdate) >= chanExpiry 926 e2Zombie := e2 == nil || time.Since(e2.LastUpdate) >= chanExpiry 927 928 if e1Zombie { 929 log.Tracef("Node1 pubkey=%x of chan_id=%v is zombie", 930 info.NodeKey1Bytes, info.ChannelID) 931 } 932 if e2Zombie { 933 log.Tracef("Node2 pubkey=%x of chan_id=%v is zombie", 934 info.NodeKey2Bytes, info.ChannelID) 935 } 936 937 // If we're using strict zombie pruning, then a channel is only 938 // considered live if both edges have a recent update we know 939 // of. 940 var channelIsLive bool 941 switch { 942 case r.cfg.StrictZombiePruning: 943 channelIsLive = !e1Zombie && !e2Zombie 944 945 // Otherwise, if we're using the less strict variant, then a 946 // channel is considered live if either of the edges have a 947 // recent update. 948 default: 949 channelIsLive = !e1Zombie || !e2Zombie 950 } 951 952 // Return early if the channel is still considered to be live 953 // with the current set of configuration parameters. 954 if channelIsLive { 955 return nil 956 } 957 958 log.Debugf("ChannelID(%v) is a zombie, collecting to prune", 959 info.ChannelID) 960 961 // TODO(roasbeef): add ability to delete single directional edge 962 chansToPrune[info.ChannelID] = struct{}{} 963 964 return nil 965 } 966 967 // If AssumeChannelValid is present we'll look at the disabled bit for both 968 // edges. If they're both disabled, then we can interpret this as the 969 // channel being closed and can prune it from our graph. 970 if r.cfg.AssumeChannelValid { 971 disabledChanIDs, err := r.cfg.Graph.DisabledChannelIDs() 972 if err != nil { 973 return fmt.Errorf("unable to get disabled channels ids "+ 974 "chans: %v", err) 975 } 976 977 disabledEdges, err := r.cfg.Graph.FetchChanInfos(disabledChanIDs) 978 if err != nil { 979 return fmt.Errorf("unable to fetch disabled channels edges "+ 980 "chans: %v", err) 981 } 982 983 // Ensuring we won't prune our own channel from the graph. 984 for _, disabledEdge := range disabledEdges { 985 if !isSelfChannelEdge(disabledEdge.Info) { 986 chansToPrune[disabledEdge.Info.ChannelID] = struct{}{} 987 } 988 } 989 } 990 991 startTime := time.Unix(0, 0) 992 endTime := time.Now().Add(-1 * chanExpiry) 993 oldEdges, err := r.cfg.Graph.ChanUpdatesInHorizon(startTime, endTime) 994 if err != nil { 995 return fmt.Errorf("unable to fetch expired channel updates "+ 996 "chans: %v", err) 997 } 998 999 for _, u := range oldEdges { 1000 filterPruneChans(u.Info, u.Policy1, u.Policy2) 1001 } 1002 1003 log.Infof("Pruning %v zombie channels", len(chansToPrune)) 1004 if len(chansToPrune) == 0 { 1005 return nil 1006 } 1007 1008 // With the set of zombie-like channels obtained, we'll do another pass 1009 // to delete them from the channel graph. 1010 toPrune := make([]uint64, 0, len(chansToPrune)) 1011 for chanID := range chansToPrune { 1012 toPrune = append(toPrune, chanID) 1013 log.Tracef("Pruning zombie channel with ChannelID(%v)", chanID) 1014 } 1015 err = r.cfg.Graph.DeleteChannelEdges(r.cfg.StrictZombiePruning, toPrune...) 1016 if err != nil { 1017 return fmt.Errorf("unable to delete zombie channels: %v", err) 1018 } 1019 1020 // With the channels pruned, we'll also attempt to prune any nodes that 1021 // were a part of them. 1022 err = r.cfg.Graph.PruneGraphNodes() 1023 if err != nil && err != channeldb.ErrGraphNodesNotFound { 1024 return fmt.Errorf("unable to prune graph nodes: %v", err) 1025 } 1026 1027 return nil 1028 } 1029 1030 // networkHandler is the primary goroutine for the ChannelRouter. The roles of 1031 // this goroutine include answering queries related to the state of the 1032 // network, pruning the graph on new block notification, applying network 1033 // updates, and registering new topology clients. 1034 // 1035 // NOTE: This MUST be run as a goroutine. 1036 func (r *ChannelRouter) networkHandler() { 1037 defer r.wg.Done() 1038 1039 graphPruneTicker := time.NewTicker(r.cfg.GraphPruneInterval) 1040 defer graphPruneTicker.Stop() 1041 1042 defer r.statTicker.Stop() 1043 1044 r.stats.Reset() 1045 1046 // We'll use this validation barrier to ensure that we process all jobs 1047 // in the proper order during parallel validation. 1048 // 1049 // NOTE: For AssumeChannelValid, we bump up the maximum number of 1050 // concurrent validation requests since there are no blocks being 1051 // fetched. This significantly increases the performance of IGD for 1052 // neutrino nodes. 1053 // 1054 // However, we dial back to use multiple of the number of cores when 1055 // fully validating, to avoid fetching up to 1000 blocks from the 1056 // backend. On bitcoind, this will empirically cause massive latency 1057 // spikes when executing this many concurrent RPC calls. Critical 1058 // subsystems or basic rpc calls that rely on calls such as GetBestBlock 1059 // will hang due to excessive load. 1060 // 1061 // See https://github.com/decred/dcrlnd/issues/4892. 1062 var validationBarrier *ValidationBarrier 1063 if r.cfg.AssumeChannelValid { 1064 validationBarrier = NewValidationBarrier(1000, r.quit) 1065 } else { 1066 validationBarrier = NewValidationBarrier( 1067 4*runtime.NumCPU(), r.quit, 1068 ) 1069 } 1070 1071 for { 1072 1073 // If there are stats, resume the statTicker. 1074 if !r.stats.Empty() { 1075 r.statTicker.Resume() 1076 } 1077 1078 select { 1079 // A new fully validated network update has just arrived. As a 1080 // result we'll modify the channel graph accordingly depending 1081 // on the exact type of the message. 1082 case update := <-r.networkUpdates: 1083 // We'll set up any dependants, and wait until a free 1084 // slot for this job opens up, this allow us to not 1085 // have thousands of goroutines active. 1086 validationBarrier.InitJobDependencies(update.msg) 1087 1088 r.wg.Add(1) 1089 go func() { 1090 defer r.wg.Done() 1091 defer validationBarrier.CompleteJob() 1092 1093 // If this message has an existing dependency, 1094 // then we'll wait until that has been fully 1095 // validated before we proceed. 1096 err := validationBarrier.WaitForDependants( 1097 update.msg, 1098 ) 1099 if err != nil { 1100 switch { 1101 case IsError( 1102 err, ErrVBarrierShuttingDown, 1103 ): 1104 update.err <- err 1105 1106 case IsError( 1107 err, ErrParentValidationFailed, 1108 ): 1109 update.err <- newErrf( 1110 ErrIgnored, err.Error(), 1111 ) 1112 1113 default: 1114 log.Warnf("unexpected error "+ 1115 "during validation "+ 1116 "barrier shutdown: %v", 1117 err) 1118 update.err <- err 1119 } 1120 return 1121 } 1122 1123 // Process the routing update to determine if 1124 // this is either a new update from our PoV or 1125 // an update to a prior vertex/edge we 1126 // previously accepted. 1127 err = r.processUpdate(update.msg, update.op...) 1128 update.err <- err 1129 1130 // If this message had any dependencies, then 1131 // we can now signal them to continue. 1132 allowDependents := err == nil || 1133 IsError(err, ErrIgnored, ErrOutdated) 1134 validationBarrier.SignalDependants( 1135 update.msg, allowDependents, 1136 ) 1137 if err != nil { 1138 return 1139 } 1140 1141 // Send off a new notification for the newly 1142 // accepted update. 1143 topChange := &TopologyChange{} 1144 err = addToTopologyChange( 1145 r.cfg.Graph, topChange, update.msg, 1146 ) 1147 if err != nil { 1148 log.Errorf("unable to update topology "+ 1149 "change notification: %v", err) 1150 return 1151 } 1152 1153 if !topChange.isEmpty() { 1154 r.notifyTopologyChange(topChange) 1155 } 1156 }() 1157 1158 // TODO(roasbeef): remove all unconnected vertexes 1159 // after N blocks pass with no corresponding 1160 // announcements. 1161 1162 case chainUpdate, ok := <-r.staleBlocks: 1163 // If the channel has been closed, then this indicates 1164 // the daemon is shutting down, so we exit ourselves. 1165 if !ok { 1166 return 1167 } 1168 1169 // Since this block is stale, we update our best height 1170 // to the previous block. 1171 blockHeight := uint32(chainUpdate.Height) 1172 atomic.StoreUint32(&r.bestHeight, blockHeight-1) 1173 1174 // Update the channel graph to reflect that this block 1175 // was disconnected. 1176 _, err := r.cfg.Graph.DisconnectBlockAtHeight(blockHeight) 1177 if err != nil { 1178 log.Errorf("unable to prune graph with stale "+ 1179 "block: %v", err) 1180 continue 1181 } 1182 1183 // TODO(halseth): notify client about the reorg? 1184 1185 // A new block has arrived, so we can prune the channel graph 1186 // of any channels which were closed in the block. 1187 case chainUpdate, ok := <-r.newBlocks: 1188 // If the channel has been closed, then this indicates 1189 // the daemon is shutting down, so we exit ourselves. 1190 if !ok { 1191 return 1192 } 1193 1194 // We'll ensure that any new blocks received attach 1195 // directly to the end of our main chain. If not, then 1196 // we've somehow missed some blocks. Here we'll catch 1197 // up the chain with the latest blocks. 1198 currentHeight := int64(atomic.LoadUint32(&r.bestHeight)) 1199 switch { 1200 case chainUpdate.Height == currentHeight+1: 1201 err := r.updateGraphWithClosedChannels( 1202 chainUpdate, 1203 ) 1204 if err != nil { 1205 log.Errorf("unable to prune graph "+ 1206 "with closed channels: %v", err) 1207 } 1208 1209 case chainUpdate.Height > currentHeight+1: 1210 log.Errorf("out of order block: expecting "+ 1211 "height=%v, got height=%v", 1212 currentHeight+1, chainUpdate.Height) 1213 1214 err := r.getMissingBlocks(uint32(currentHeight), chainUpdate) 1215 if err != nil { 1216 log.Errorf("unable to retrieve missing"+ 1217 "blocks: %v", err) 1218 } 1219 1220 case chainUpdate.Height < currentHeight+1: 1221 log.Errorf("out of order block: expecting "+ 1222 "height=%v, got height=%v", 1223 currentHeight+1, chainUpdate.Height) 1224 1225 log.Infof("Skipping channel pruning since "+ 1226 "received block height %v was already"+ 1227 " processed.", chainUpdate.Height) 1228 } 1229 1230 // A new notification client update has arrived. We're either 1231 // gaining a new client, or cancelling notifications for an 1232 // existing client. 1233 case ntfnUpdate := <-r.ntfnClientUpdates: 1234 clientID := ntfnUpdate.clientID 1235 1236 if ntfnUpdate.cancel { 1237 r.RLock() 1238 client, ok := r.topologyClients[ntfnUpdate.clientID] 1239 r.RUnlock() 1240 if ok { 1241 r.Lock() 1242 delete(r.topologyClients, clientID) 1243 r.Unlock() 1244 1245 close(client.exit) 1246 client.wg.Wait() 1247 1248 close(client.ntfnChan) 1249 } 1250 1251 continue 1252 } 1253 1254 r.Lock() 1255 r.topologyClients[ntfnUpdate.clientID] = &topologyClient{ 1256 ntfnChan: ntfnUpdate.ntfnChan, 1257 exit: make(chan struct{}), 1258 } 1259 r.Unlock() 1260 1261 // The graph prune ticker has ticked, so we'll examine the 1262 // state of the known graph to filter out any zombie channels 1263 // for pruning. 1264 case <-graphPruneTicker.C: 1265 if err := r.pruneZombieChans(); err != nil { 1266 log.Errorf("Unable to prune zombies: %v", err) 1267 } 1268 1269 // Log any stats if we've processed a non-empty number of 1270 // channels, updates, or nodes. We'll only pause the ticker if 1271 // the last window contained no updates to avoid resuming and 1272 // pausing while consecutive windows contain new info. 1273 case <-r.statTicker.Ticks(): 1274 if !r.stats.Empty() { 1275 log.Infof(r.stats.String()) 1276 } else { 1277 r.statTicker.Pause() 1278 } 1279 r.stats.Reset() 1280 1281 // The router has been signalled to exit, to we exit our main 1282 // loop so the wait group can be decremented. 1283 case <-r.quit: 1284 return 1285 } 1286 } 1287 } 1288 1289 // getMissingBlocks walks through all missing blocks and updates the graph 1290 // closed channels accordingly. 1291 func (r *ChannelRouter) getMissingBlocks(currentHeight uint32, 1292 chainUpdate *chainview.FilteredBlock) error { 1293 1294 outdatedHash, err := r.cfg.Chain.GetBlockHash(int64(currentHeight)) 1295 if err != nil { 1296 return err 1297 } 1298 1299 outdatedBlock := &chainntnfs.BlockEpoch{ 1300 Height: int32(currentHeight), 1301 Hash: outdatedHash, 1302 } 1303 1304 epochClient, err := r.cfg.Notifier.RegisterBlockEpochNtfn( 1305 outdatedBlock, 1306 ) 1307 if err != nil { 1308 return err 1309 } 1310 defer epochClient.Cancel() 1311 1312 blockDifference := int(chainUpdate.Height - int64(currentHeight)) 1313 1314 // We'll walk through all the outdated blocks and make sure we're able 1315 // to update the graph with any closed channels from them. 1316 for i := 0; i < blockDifference; i++ { 1317 var ( 1318 missingBlock *chainntnfs.BlockEpoch 1319 ok bool 1320 ) 1321 1322 select { 1323 case missingBlock, ok = <-epochClient.Epochs: 1324 if !ok { 1325 return nil 1326 } 1327 1328 case <-r.quit: 1329 return nil 1330 } 1331 1332 filteredBlock, err := r.cfg.ChainView.FilterBlock( 1333 missingBlock.Hash, 1334 ) 1335 if err != nil { 1336 return err 1337 } 1338 1339 err = r.updateGraphWithClosedChannels( 1340 filteredBlock, 1341 ) 1342 if err != nil { 1343 return err 1344 } 1345 } 1346 1347 return nil 1348 } 1349 1350 // updateGraphWithClosedChannels prunes the channel graph of closed channels 1351 // that are no longer needed. 1352 func (r *ChannelRouter) updateGraphWithClosedChannels( 1353 chainUpdate *chainview.FilteredBlock) error { 1354 1355 // Once a new block arrives, we update our running track of the height 1356 // of the chain tip. 1357 blockHeight := uint32(chainUpdate.Height) 1358 1359 atomic.StoreUint32(&r.bestHeight, blockHeight) 1360 log.Infof("Pruning channel graph using block %v (height=%v)", 1361 chainUpdate.Hash, blockHeight) 1362 1363 // We're only interested in all prior outputs that have been spent in 1364 // the block, so collate all the referenced previous outpoints within 1365 // each tx and input. 1366 var spentOutputs []*wire.OutPoint 1367 for _, tx := range chainUpdate.Transactions { 1368 for _, txIn := range tx.TxIn { 1369 spentOutputs = append(spentOutputs, 1370 &txIn.PreviousOutPoint) 1371 } 1372 } 1373 1374 // With the spent outputs gathered, attempt to prune the channel graph, 1375 // also passing in the hash+height of the block being pruned so the 1376 // prune tip can be updated. 1377 chansClosed, err := r.cfg.Graph.PruneGraph(spentOutputs, 1378 &chainUpdate.Hash, uint32(chainUpdate.Height)) 1379 if err != nil { 1380 log.Errorf("unable to prune routing table: %v", err) 1381 return err 1382 } 1383 1384 log.Infof("Block %v (height=%v) closed %v channels", chainUpdate.Hash, 1385 blockHeight, len(chansClosed)) 1386 1387 if len(chansClosed) == 0 { 1388 return err 1389 } 1390 1391 // Notify all currently registered clients of the newly closed channels. 1392 closeSummaries := createCloseSummaries(blockHeight, chansClosed...) 1393 r.notifyTopologyChange(&TopologyChange{ 1394 ClosedChannels: closeSummaries, 1395 }) 1396 1397 return nil 1398 } 1399 1400 // assertNodeAnnFreshness returns a non-nil error if we have an announcement in 1401 // the database for the passed node with a timestamp newer than the passed 1402 // timestamp. ErrIgnored will be returned if we already have the node, and 1403 // ErrOutdated will be returned if we have a timestamp that's after the new 1404 // timestamp. 1405 func (r *ChannelRouter) assertNodeAnnFreshness(node route.Vertex, 1406 msgTimestamp time.Time) error { 1407 1408 // If we are not already aware of this node, it means that we don't 1409 // know about any channel using this node. To avoid a DoS attack by 1410 // node announcements, we will ignore such nodes. If we do know about 1411 // this node, check that this update brings info newer than what we 1412 // already have. 1413 lastUpdate, exists, err := r.cfg.Graph.HasLightningNode(node) 1414 if err != nil { 1415 return errors.Errorf("unable to query for the "+ 1416 "existence of node: %v", err) 1417 } 1418 if !exists { 1419 return newErrf(ErrIgnored, "Ignoring node announcement"+ 1420 " for node not found in channel graph (%x)", 1421 node[:]) 1422 } 1423 1424 // If we've reached this point then we're aware of the vertex being 1425 // advertised. So we now check if the new message has a new time stamp, 1426 // if not then we won't accept the new data as it would override newer 1427 // data. 1428 if !lastUpdate.Before(msgTimestamp) { 1429 return newErrf(ErrOutdated, "Ignoring outdated "+ 1430 "announcement for %x", node[:]) 1431 } 1432 1433 return nil 1434 } 1435 1436 // addZombieEdge adds a channel that failed complete validation into the zombie 1437 // index so we can avoid having to re-validate it in the future. 1438 func (r *ChannelRouter) addZombieEdge(chanID uint64) error { 1439 // If the edge fails validation we'll mark the edge itself as a zombie 1440 // so we don't continue to request it. We use the "zero key" for both 1441 // node pubkeys so this edge can't be resurrected. 1442 var zeroKey [33]byte 1443 err := r.cfg.Graph.MarkEdgeZombie(chanID, zeroKey, zeroKey) 1444 if err != nil { 1445 return fmt.Errorf("unable to mark spent chan(id=%v) as a "+ 1446 "zombie: %w", chanID, err) 1447 } 1448 1449 return nil 1450 } 1451 1452 // processUpdate processes a new relate authenticated channel/edge, node or 1453 // channel/edge update network update. If the update didn't affect the internal 1454 // state of the draft due to either being out of date, invalid, or redundant, 1455 // then error is returned. 1456 func (r *ChannelRouter) processUpdate(msg interface{}, 1457 op ...batch.SchedulerOption) error { 1458 1459 switch msg := msg.(type) { 1460 case *channeldb.LightningNode: 1461 // Before we add the node to the database, we'll check to see 1462 // if the announcement is "fresh" or not. If it isn't, then 1463 // we'll return an error. 1464 err := r.assertNodeAnnFreshness(msg.PubKeyBytes, msg.LastUpdate) 1465 if err != nil { 1466 return err 1467 } 1468 1469 if err := r.cfg.Graph.AddLightningNode(msg, op...); err != nil { 1470 return errors.Errorf("unable to add node %v to the "+ 1471 "graph: %v", msg.PubKeyBytes, err) 1472 } 1473 1474 log.Tracef("Updated vertex data for node=%x", msg.PubKeyBytes) 1475 r.stats.incNumNodeUpdates() 1476 1477 case *channeldb.ChannelEdgeInfo: 1478 // Prior to processing the announcement we first check if we 1479 // already know of this channel, if so, then we can exit early. 1480 _, _, exists, isZombie, err := r.cfg.Graph.HasChannelEdge( 1481 msg.ChannelID, 1482 ) 1483 if err != nil && err != channeldb.ErrGraphNoEdgesFound { 1484 return errors.Errorf("unable to check for edge "+ 1485 "existence: %v", err) 1486 } 1487 if isZombie { 1488 return newErrf(ErrIgnored, "ignoring msg for zombie "+ 1489 "chan_id=%v", msg.ChannelID) 1490 } 1491 if exists { 1492 return newErrf(ErrIgnored, "ignoring msg for known "+ 1493 "chan_id=%v", msg.ChannelID) 1494 } 1495 knownSpent, err := r.cfg.Graph.IsKnownSpent(msg.ChannelID) 1496 if err != nil { 1497 return errors.Errorf("unable to check if edge known spent: %v", err) 1498 } 1499 if knownSpent { 1500 return errors.Errorf("ignoring msg for known spent "+ 1501 "chan_id=%v", msg.ChannelID) 1502 } 1503 1504 // If AssumeChannelValid is present, then we are unable to 1505 // perform any of the expensive checks below, so we'll 1506 // short-circuit our path straight to adding the edge to our 1507 // graph. 1508 if r.cfg.AssumeChannelValid { 1509 if err := r.cfg.Graph.AddChannelEdge(msg, op...); err != nil { 1510 return fmt.Errorf("unable to add edge: %v", err) 1511 } 1512 log.Tracef("New channel discovered! Link "+ 1513 "connects %x and %x with ChannelID(%v)", 1514 msg.NodeKey1Bytes, msg.NodeKey2Bytes, 1515 msg.ChannelID) 1516 r.stats.incNumEdgesDiscovered() 1517 1518 break 1519 } 1520 1521 // Before we can add the channel to the channel graph, we need 1522 // to obtain the full funding outpoint that's encoded within 1523 // the channel ID. 1524 channelID := lnwire.NewShortChanIDFromInt(msg.ChannelID) 1525 fundingTx, err := r.fetchFundingTx(&channelID) 1526 if err != nil { 1527 // In order to ensure we don't erroneously mark a 1528 // channel as a zombie due to an RPC failure, we'll 1529 // attempt to string match for the relevant errors. 1530 // 1531 // * btcd: 1532 // * https://github.com/decred/dcrd/blob/master/rpcserver.go#L1316 1533 // * https://github.com/decred/dcrd/blob/master/rpcserver.go#L1086 1534 // * bitcoind: 1535 // * https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/rpc/blockchain.cpp#L770 1536 // * https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/rpc/blockchain.cpp#L954 1537 switch { 1538 case strings.Contains(err.Error(), "not found"): 1539 fallthrough 1540 1541 case strings.Contains(err.Error(), "out of range"): 1542 // If the funding transaction isn't found at 1543 // all, then we'll mark the edge itself as a 1544 // zombie so we don't continue to request it. 1545 // We use the "zero key" for both node pubkeys 1546 // so this edge can't be resurrected. 1547 zErr := r.addZombieEdge(msg.ChannelID) 1548 if zErr != nil { 1549 return zErr 1550 } 1551 1552 default: 1553 } 1554 1555 return newErrf(ErrNoFundingTransaction, "unable to "+ 1556 "locate funding tx: %v", err) 1557 } 1558 1559 // Recreate witness output to be sure that declared in channel 1560 // edge bitcoin keys and channel value corresponds to the 1561 // reality. 1562 witnessScript, err := input.GenMultiSigScript( 1563 msg.DecredKey1Bytes[:], msg.DecredKey2Bytes[:], 1564 ) 1565 if err != nil { 1566 return err 1567 } 1568 pkScript, err := input.ScriptHashPkScript(witnessScript) 1569 if err != nil { 1570 return err 1571 } 1572 1573 // Next we'll validate that this channel is actually well 1574 // formed. If this check fails, then this channel either 1575 // doesn't exist, or isn't the one that was meant to be created 1576 // according to the passed channel proofs. 1577 fundingPoint, err := chanvalidate.Validate(&chanvalidate.Context{ 1578 Locator: &chanvalidate.ShortChanIDChanLocator{ 1579 ID: channelID, 1580 }, 1581 MultiSigPkScript: pkScript, 1582 FundingTx: fundingTx, 1583 }) 1584 if err != nil { 1585 // Mark the edge as a zombie so we won't try to 1586 // re-validate it on start up. 1587 if err := r.addZombieEdge(msg.ChannelID); err != nil { 1588 return err 1589 } 1590 1591 return newErrf(ErrInvalidFundingOutput, "output "+ 1592 "failed validation: %w", err) 1593 } 1594 1595 // Now that we have the funding outpoint of the channel, ensure 1596 // that it hasn't yet been spent. If so, then this channel has 1597 // been closed so we'll ignore it. 1598 fundingPkScript, err := input.ScriptHashPkScript(witnessScript) 1599 if err != nil { 1600 return err 1601 } 1602 chanUtxo, err := r.cfg.Chain.GetUtxo( 1603 fundingPoint, fundingPkScript, channelID.BlockHeight, 1604 r.quit, 1605 ) 1606 if err != nil { 1607 // If we fail validation of the UTXO here, then we'll 1608 // mark the channel as a zombie as otherwise, we may 1609 // continue to continue to request it. We use the "zero 1610 // key" for both node pubkeys so this edge can't be 1611 // resurrected. 1612 if errors.Is(err, lnwallet.ErrUtxoAlreadySpent{}) { 1613 // Received a channel announcement for a 1614 // known-spent channel. Mark it as known spent 1615 // in the DB to speed up the lookup if we 1616 // receive other announcements. 1617 if err := r.cfg.Graph.MarkKnownSpent(msg.ChannelID); err != nil { 1618 log.Warnf("Unable to mark channel edge %s spent: %v", 1619 channelID, err) 1620 } else { 1621 log.Infof("Marked channel edge %s spent", 1622 channelID) 1623 } 1624 1625 zErr := r.addZombieEdge(msg.ChannelID) 1626 if zErr != nil { 1627 return zErr 1628 } 1629 } 1630 1631 return newErrf(ErrChannelSpent, "unable to fetch utxo "+ 1632 "for chan_id=%v, chan_point=%v: %v", 1633 msg.ChannelID, fundingPoint, err) 1634 } 1635 1636 // TODO(roasbeef): this is a hack, needs to be removed 1637 // after commitment fees are dynamic. 1638 msg.Capacity = dcrutil.Amount(chanUtxo.Value) 1639 msg.ChannelPoint = *fundingPoint 1640 if err := r.cfg.Graph.AddChannelEdge(msg, op...); err != nil { 1641 return errors.Errorf("unable to add edge: %v", err) 1642 } 1643 1644 log.Tracef("New channel discovered! Link "+ 1645 "connects %x and %x with ChannelPoint(%v): "+ 1646 "chan_id=%v, capacity=%v", 1647 msg.NodeKey1Bytes, msg.NodeKey2Bytes, 1648 fundingPoint, msg.ChannelID, msg.Capacity) 1649 r.stats.incNumEdgesDiscovered() 1650 1651 // As a new edge has been added to the channel graph, we'll 1652 // update the current UTXO filter within our active 1653 // FilteredChainView so we are notified if/when this channel is 1654 // closed. 1655 filterUpdate := []channeldb.EdgePoint{ 1656 { 1657 FundingPkScript: fundingPkScript, 1658 OutPoint: *fundingPoint, 1659 }, 1660 } 1661 err = r.cfg.ChainView.UpdateFilter( 1662 filterUpdate, int64(atomic.LoadUint32(&r.bestHeight)), 1663 ) 1664 if err != nil { 1665 return errors.Errorf("unable to update chain "+ 1666 "view: %v", err) 1667 } 1668 1669 case *channeldb.ChannelEdgePolicy: 1670 // We make sure to hold the mutex for this channel ID, 1671 // such that no other goroutine is concurrently doing 1672 // database accesses for the same channel ID. 1673 r.channelEdgeMtx.Lock(msg.ChannelID) 1674 defer r.channelEdgeMtx.Unlock(msg.ChannelID) 1675 1676 edge1Timestamp, edge2Timestamp, exists, isZombie, err := 1677 r.cfg.Graph.HasChannelEdge(msg.ChannelID) 1678 if err != nil && err != channeldb.ErrGraphNoEdgesFound { 1679 return errors.Errorf("unable to check for edge "+ 1680 "existence: %v", err) 1681 1682 } 1683 1684 // If the channel is marked as a zombie in our database, and 1685 // we consider this a stale update, then we should not apply the 1686 // policy. 1687 isStaleUpdate := time.Since(msg.LastUpdate) > r.cfg.ChannelPruneExpiry 1688 if isZombie && isStaleUpdate { 1689 return newErrf(ErrIgnored, "ignoring stale update "+ 1690 "(flags=%v|%v) for zombie chan_id=%v", 1691 msg.MessageFlags, msg.ChannelFlags, 1692 msg.ChannelID) 1693 } 1694 1695 // If the channel doesn't exist in our database, we cannot 1696 // apply the updated policy. 1697 if !exists { 1698 return newErrf(ErrIgnored, "ignoring update "+ 1699 "(flags=%v|%v) for unknown chan_id=%v", 1700 msg.MessageFlags, msg.ChannelFlags, 1701 msg.ChannelID) 1702 } 1703 1704 // As edges are directional edge node has a unique policy for 1705 // the direction of the edge they control. Therefore we first 1706 // check if we already have the most up to date information for 1707 // that edge. If this message has a timestamp not strictly 1708 // newer than what we already know of we can exit early. 1709 switch { 1710 1711 // A flag set of 0 indicates this is an announcement for the 1712 // "first" node in the channel. 1713 case msg.ChannelFlags&lnwire.ChanUpdateDirection == 0: 1714 1715 // Ignore outdated message. 1716 if !edge1Timestamp.Before(msg.LastUpdate) { 1717 return newErrf(ErrOutdated, "Ignoring "+ 1718 "outdated update (flags=%v|%v) for "+ 1719 "known chan_id=%v", msg.MessageFlags, 1720 msg.ChannelFlags, msg.ChannelID) 1721 } 1722 1723 // Similarly, a flag set of 1 indicates this is an announcement 1724 // for the "second" node in the channel. 1725 case msg.ChannelFlags&lnwire.ChanUpdateDirection == 1: 1726 1727 // Ignore outdated message. 1728 if !edge2Timestamp.Before(msg.LastUpdate) { 1729 return newErrf(ErrOutdated, "Ignoring "+ 1730 "outdated update (flags=%v|%v) for "+ 1731 "known chan_id=%v", msg.MessageFlags, 1732 msg.ChannelFlags, msg.ChannelID) 1733 } 1734 } 1735 1736 // Now that we know this isn't a stale update, we'll apply the 1737 // new edge policy to the proper directional edge within the 1738 // channel graph. 1739 if err = r.cfg.Graph.UpdateEdgePolicy(msg, op...); err != nil { 1740 err := errors.Errorf("unable to add channel: %v", err) 1741 log.Error(err) 1742 return err 1743 } 1744 1745 log.Tracef("New channel update applied: %v", 1746 newLogClosure(func() string { return spew.Sdump(msg) })) 1747 r.stats.incNumChannelUpdates() 1748 1749 default: 1750 return errors.Errorf("wrong routing update message type") 1751 } 1752 1753 return nil 1754 } 1755 1756 // fetchFundingTx returns the funding transaction identified by the passed 1757 // short channel ID. 1758 // 1759 // TODO(roasbeef): replace with call to GetBlockTransaction? (would allow to 1760 // later use getblocktxn) 1761 func (r *ChannelRouter) fetchFundingTx( 1762 chanID *lnwire.ShortChannelID) (*wire.MsgTx, error) { 1763 1764 // First fetch the block hash by the block number encoded, then use 1765 // that hash to fetch the block itself. 1766 blockNum := int64(chanID.BlockHeight) 1767 blockHash, err := r.cfg.Chain.GetBlockHash(blockNum) 1768 if err != nil { 1769 return nil, err 1770 } 1771 fundingBlock, err := r.cfg.Chain.GetBlock(blockHash) 1772 if err != nil { 1773 return nil, err 1774 } 1775 1776 // As a sanity check, ensure that the advertised transaction index is 1777 // within the bounds of the total number of transactions within a 1778 // block. 1779 numTxns := uint32(len(fundingBlock.Transactions)) 1780 if chanID.TxIndex > numTxns-1 { 1781 return nil, fmt.Errorf("tx_index=#%v "+ 1782 "is out of range (max_index=%v), network_chan_id=%v", 1783 chanID.TxIndex, numTxns-1, chanID) 1784 } 1785 1786 return fundingBlock.Transactions[chanID.TxIndex], nil 1787 } 1788 1789 // routingMsg couples a routing related routing topology update to the 1790 // error channel. 1791 type routingMsg struct { 1792 msg interface{} 1793 op []batch.SchedulerOption 1794 err chan error 1795 } 1796 1797 // FindRoute attempts to query the ChannelRouter for the optimum path to a 1798 // particular target destination to which it is able to send `amt` after 1799 // factoring in channel capacities and cumulative fees along the route. 1800 func (r *ChannelRouter) FindRoute(source, target route.Vertex, 1801 amt lnwire.MilliAtom, restrictions *RestrictParams, 1802 destCustomRecords record.CustomSet, 1803 routeHints map[route.Vertex][]*channeldb.CachedEdgePolicy, 1804 finalExpiry uint16) (*route.Route, error) { 1805 1806 log.Debugf("Searching for path to %v, sending %v", target, amt) 1807 1808 // We'll attempt to obtain a set of bandwidth hints that can help us 1809 // eliminate certain routes early on in the path finding process. 1810 bandwidthHints, err := newBandwidthManager( 1811 r.cachedGraph, r.selfNode.PubKeyBytes, r.cfg.GetLink, 1812 ) 1813 if err != nil { 1814 return nil, err 1815 } 1816 1817 // We'll fetch the current block height so we can properly calculate the 1818 // required HTLC time locks within the route. 1819 _, currentHeight, err := r.cfg.Chain.GetBestBlock() 1820 if err != nil { 1821 return nil, err 1822 } 1823 1824 // Now that we know the destination is reachable within the graph, we'll 1825 // execute our path finding algorithm. 1826 finalHtlcExpiry := currentHeight + int32(finalExpiry) 1827 1828 path, err := findPath( 1829 &graphParams{ 1830 additionalEdges: routeHints, 1831 bandwidthHints: bandwidthHints, 1832 graph: r.cachedGraph, 1833 }, 1834 restrictions, 1835 &r.cfg.PathFindingConfig, 1836 source, target, amt, finalHtlcExpiry, 1837 ) 1838 if err != nil { 1839 return nil, err 1840 } 1841 1842 // Create the route with absolute time lock values. 1843 route, err := newRoute( 1844 source, path, uint32(currentHeight), 1845 finalHopParams{ 1846 amt: amt, 1847 totalAmt: amt, 1848 cltvDelta: finalExpiry, 1849 records: destCustomRecords, 1850 }, 1851 ) 1852 if err != nil { 1853 return nil, err 1854 } 1855 1856 go log.Tracef("Obtained path to send %v to %x: %v", 1857 amt, target, newLogClosure(func() string { 1858 return spew.Sdump(route) 1859 }), 1860 ) 1861 1862 return route, nil 1863 } 1864 1865 // generateNewSessionKey generates a new ephemeral private key to be used for a 1866 // payment attempt. 1867 func generateNewSessionKey() (*secp256k1.PrivateKey, error) { 1868 // Generate a new random session key to ensure that we don't trigger 1869 // any replay. 1870 // 1871 // TODO(roasbeef): add more sources of randomness? 1872 return secp256k1.GeneratePrivateKey() 1873 } 1874 1875 // generateSphinxPacket generates then encodes a sphinx packet which encodes 1876 // the onion route specified by the passed layer 3 route. The blob returned 1877 // from this function can immediately be included within an HTLC add packet to 1878 // be sent to the first hop within the route. 1879 func generateSphinxPacket(rt *route.Route, paymentHash []byte, 1880 sessionKey *secp256k1.PrivateKey) ([]byte, *sphinx.Circuit, error) { 1881 1882 // Now that we know we have an actual route, we'll map the route into a 1883 // sphinx payument path which includes per-hop paylods for each hop 1884 // that give each node within the route the necessary information 1885 // (fees, CLTV value, etc) to properly forward the payment. 1886 sphinxPath, err := rt.ToSphinxPath() 1887 if err != nil { 1888 return nil, nil, err 1889 } 1890 1891 log.Tracef("Constructed per-hop payloads for payment_hash=%x: %v", 1892 paymentHash[:], newLogClosure(func() string { 1893 path := make([]sphinx.OnionHop, sphinxPath.TrueRouteLength()) 1894 for i := range path { 1895 hopCopy := sphinxPath[i] 1896 path[i] = hopCopy 1897 } 1898 return spew.Sdump(path) 1899 }), 1900 ) 1901 1902 // Next generate the onion routing packet which allows us to perform 1903 // privacy preserving source routing across the network. 1904 sphinxPacket, err := sphinx.NewOnionPacket( 1905 sphinxPath, sessionKey, paymentHash, 1906 sphinx.DeterministicPacketFiller, 1907 ) 1908 if err != nil { 1909 return nil, nil, err 1910 } 1911 1912 // Finally, encode Sphinx packet using its wire representation to be 1913 // included within the HTLC add packet. 1914 var onionBlob bytes.Buffer 1915 if err := sphinxPacket.Encode(&onionBlob); err != nil { 1916 return nil, nil, err 1917 } 1918 1919 log.Tracef("Generated sphinx packet: %v", 1920 newLogClosure(func() string { 1921 // We make a copy of the ephemeral key and unset the 1922 // internal curve here in order to keep the logs from 1923 // getting noisy. 1924 key := *sphinxPacket.EphemeralKey 1925 packetCopy := *sphinxPacket 1926 packetCopy.EphemeralKey = &key 1927 return spew.Sdump(packetCopy) 1928 }), 1929 ) 1930 1931 return onionBlob.Bytes(), &sphinx.Circuit{ 1932 SessionKey: sessionKey, 1933 PaymentPath: sphinxPath.NodeKeys(), 1934 }, nil 1935 } 1936 1937 // LightningPayment describes a payment to be sent through the network to the 1938 // final destination. 1939 type LightningPayment struct { 1940 // Target is the node in which the payment should be routed towards. 1941 Target route.Vertex 1942 1943 // Amount is the value of the payment to send through the network in 1944 // milli-atoms. 1945 Amount lnwire.MilliAtom 1946 1947 // FeeLimit is the maximum fee in MilliAtoms that the payment should 1948 // accept when sending it through the network. The payment will fail 1949 // if there isn't a route with lower fees than this limit. 1950 FeeLimit lnwire.MilliAtom 1951 1952 // CltvLimit is the maximum time lock that is allowed for attempts to 1953 // complete this payment. 1954 CltvLimit uint32 1955 1956 // paymentHash is the r-hash value to use within the HTLC extended to 1957 // the first hop. This won't be set for AMP payments. 1958 paymentHash *lntypes.Hash 1959 1960 // amp is an optional field that is set if and only if this is am AMP 1961 // payment. 1962 amp *AMPOptions 1963 1964 // FinalCLTVDelta is the CTLV expiry delta to use for the _final_ hop 1965 // in the route. This means that the final hop will have a CLTV delta 1966 // of at least: currentHeight + FinalCLTVDelta. 1967 FinalCLTVDelta uint16 1968 1969 // PayAttemptTimeout is a timeout value that we'll use to determine 1970 // when we should should abandon the payment attempt after consecutive 1971 // payment failure. This prevents us from attempting to send a payment 1972 // indefinitely. A zero value means the payment will never time out. 1973 // 1974 // TODO(halseth): make wallclock time to allow resume after startup. 1975 PayAttemptTimeout time.Duration 1976 1977 // RouteHints represents the different routing hints that can be used to 1978 // assist a payment in reaching its destination successfully. These 1979 // hints will act as intermediate hops along the route. 1980 // 1981 // NOTE: This is optional unless required by the payment. When providing 1982 // multiple routes, ensure the hop hints within each route are chained 1983 // together and sorted in forward order in order to reach the 1984 // destination successfully. 1985 RouteHints [][]zpay32.HopHint 1986 1987 // OutgoingChannelIDs is the list of channels that are allowed for the 1988 // first hop. If nil, any channel may be used. 1989 OutgoingChannelIDs []uint64 1990 1991 // LastHop is the pubkey of the last node before the final destination 1992 // is reached. If nil, any node may be used. 1993 LastHop *route.Vertex 1994 1995 // DestFeatures specifies the set of features we assume the final node 1996 // has for pathfinding. Typically these will be taken directly from an 1997 // invoice, but they can also be manually supplied or assumed by the 1998 // sender. If a nil feature vector is provided, the router will try to 1999 // fallback to the graph in order to load a feature vector for a node in 2000 // the public graph. 2001 DestFeatures *lnwire.FeatureVector 2002 2003 // PaymentAddr is the payment address specified by the receiver. This 2004 // field should be a random 32-byte nonce presented in the receiver's 2005 // invoice to prevent probing of the destination. 2006 PaymentAddr *[32]byte 2007 2008 // PaymentRequest is an optional payment request that this payment is 2009 // attempting to complete. 2010 PaymentRequest []byte 2011 2012 // DestCustomRecords are TLV records that are to be sent to the final 2013 // hop in the new onion payload format. If the destination does not 2014 // understand this new onion payload format, then the payment will 2015 // fail. 2016 DestCustomRecords record.CustomSet 2017 2018 // MaxParts is the maximum number of partial payments that may be used 2019 // to complete the full amount. 2020 MaxParts uint32 2021 2022 // MaxShardAmt is the largest shard that we'll attempt to split using. 2023 // If this field is set, and we need to split, rather than attempting 2024 // half of the original payment amount, we'll use this value if half 2025 // the payment amount is greater than it. 2026 // 2027 // NOTE: This field is _optional_. 2028 MaxShardAmt *lnwire.MilliAtom 2029 } 2030 2031 // AMPOptions houses information that must be known in order to send an AMP 2032 // payment. 2033 type AMPOptions struct { 2034 SetID [32]byte 2035 RootShare [32]byte 2036 } 2037 2038 // SetPaymentHash sets the given hash as the payment's overall hash. This 2039 // should only be used for non-AMP payments. 2040 func (l *LightningPayment) SetPaymentHash(hash lntypes.Hash) error { 2041 if l.amp != nil { 2042 return fmt.Errorf("cannot set payment hash for AMP payment") 2043 } 2044 2045 l.paymentHash = &hash 2046 return nil 2047 } 2048 2049 // SetAMP sets the given AMP options for the payment. 2050 func (l *LightningPayment) SetAMP(amp *AMPOptions) error { 2051 if l.paymentHash != nil { 2052 return fmt.Errorf("cannot set amp options for payment " + 2053 "with payment hash") 2054 } 2055 2056 l.amp = amp 2057 return nil 2058 } 2059 2060 // Identifier returns a 32-byte slice that uniquely identifies this single 2061 // payment. For non-AMP payments this will be the payment hash, for AMP 2062 // payments this will be the used SetID. 2063 func (l *LightningPayment) Identifier() [32]byte { 2064 if l.amp != nil { 2065 return l.amp.SetID 2066 } 2067 2068 return *l.paymentHash 2069 } 2070 2071 // SendPayment attempts to send a payment as described within the passed 2072 // LightningPayment. This function is blocking and will return either: when the 2073 // payment is successful, or all candidates routes have been attempted and 2074 // resulted in a failed payment. If the payment succeeds, then a non-nil Route 2075 // will be returned which describes the path the successful payment traversed 2076 // within the network to reach the destination. Additionally, the payment 2077 // preimage will also be returned. 2078 func (r *ChannelRouter) SendPayment(payment *LightningPayment) ([32]byte, 2079 *route.Route, error) { 2080 2081 paySession, shardTracker, err := r.preparePayment(payment) 2082 if err != nil { 2083 return [32]byte{}, nil, err 2084 } 2085 2086 log.Tracef("Dispatching SendPayment for lightning payment: %v", 2087 spewPayment(payment)) 2088 2089 // Since this is the first time this payment is being made, we pass nil 2090 // for the existing attempt. 2091 return r.sendPayment( 2092 payment.Amount, payment.FeeLimit, payment.Identifier(), 2093 payment.PayAttemptTimeout, paySession, shardTracker, 2094 ) 2095 } 2096 2097 // SendPaymentAsync is the non-blocking version of SendPayment. The payment 2098 // result needs to be retrieved via the control tower. 2099 func (r *ChannelRouter) SendPaymentAsync(payment *LightningPayment) error { 2100 paySession, shardTracker, err := r.preparePayment(payment) 2101 if err != nil { 2102 return err 2103 } 2104 2105 // Since this is the first time this payment is being made, we pass nil 2106 // for the existing attempt. 2107 r.wg.Add(1) 2108 go func() { 2109 defer r.wg.Done() 2110 2111 log.Tracef("Dispatching SendPayment for lightning payment: %v", 2112 spewPayment(payment)) 2113 2114 _, _, err := r.sendPayment( 2115 payment.Amount, payment.FeeLimit, payment.Identifier(), 2116 payment.PayAttemptTimeout, paySession, shardTracker, 2117 ) 2118 if err != nil { 2119 log.Errorf("Payment %x failed: %v", 2120 payment.Identifier(), err) 2121 } 2122 }() 2123 2124 return nil 2125 } 2126 2127 // spewPayment returns a log closures that provides a spewed string 2128 // representation of the passed payment. 2129 func spewPayment(payment *LightningPayment) logClosure { 2130 return newLogClosure(func() string { 2131 // Make a copy of the payment with a nilled Curve 2132 // before spewing. 2133 var routeHints [][]zpay32.HopHint 2134 for _, routeHint := range payment.RouteHints { 2135 var hopHints []zpay32.HopHint 2136 for _, hopHint := range routeHint { 2137 h := hopHint.Copy() 2138 hopHints = append(hopHints, h) 2139 } 2140 routeHints = append(routeHints, hopHints) 2141 } 2142 p := *payment 2143 p.RouteHints = routeHints 2144 return spew.Sdump(p) 2145 }) 2146 } 2147 2148 // preparePayment creates the payment session and registers the payment with the 2149 // control tower. 2150 func (r *ChannelRouter) preparePayment(payment *LightningPayment) ( 2151 PaymentSession, shards.ShardTracker, error) { 2152 2153 // Before starting the HTLC routing attempt, we'll create a fresh 2154 // payment session which will report our errors back to mission 2155 // control. 2156 paySession, err := r.cfg.SessionSource.NewPaymentSession(payment) 2157 if err != nil { 2158 return nil, nil, err 2159 } 2160 2161 // Record this payment hash with the ControlTower, ensuring it is not 2162 // already in-flight. 2163 // 2164 // TODO(roasbeef): store records as part of creation info? 2165 info := &channeldb.PaymentCreationInfo{ 2166 PaymentIdentifier: payment.Identifier(), 2167 Value: payment.Amount, 2168 CreationTime: r.cfg.Clock.Now(), 2169 PaymentRequest: payment.PaymentRequest, 2170 } 2171 2172 // Create a new ShardTracker that we'll use during the life cycle of 2173 // this payment. 2174 var shardTracker shards.ShardTracker 2175 switch { 2176 2177 // If this is an AMP payment, we'll use the AMP shard tracker. 2178 case payment.amp != nil: 2179 shardTracker = amp.NewShardTracker( 2180 payment.amp.RootShare, payment.amp.SetID, 2181 *payment.PaymentAddr, payment.Amount, 2182 ) 2183 2184 // Otherwise we'll use the simple tracker that will map each attempt to 2185 // the same payment hash. 2186 default: 2187 shardTracker = shards.NewSimpleShardTracker( 2188 payment.Identifier(), nil, 2189 ) 2190 } 2191 2192 err = r.cfg.Control.InitPayment(payment.Identifier(), info) 2193 if err != nil { 2194 return nil, nil, err 2195 } 2196 2197 return paySession, shardTracker, nil 2198 } 2199 2200 // SendToRoute attempts to send a payment with the given hash through the 2201 // provided route. This function is blocking and will return the attempt 2202 // information as it is stored in the database. For a successful htlc, this 2203 // information will contain the preimage. If an error occurs after the attempt 2204 // was initiated, both return values will be non-nil. 2205 func (r *ChannelRouter) SendToRoute(htlcHash lntypes.Hash, rt *route.Route) ( 2206 *channeldb.HTLCAttempt, error) { 2207 2208 // Calculate amount paid to receiver. 2209 amt := rt.ReceiverAmt() 2210 2211 // If this is meant as a MP payment shard, we set the amount 2212 // for the creating info to the total amount of the payment. 2213 finalHop := rt.Hops[len(rt.Hops)-1] 2214 mpp := finalHop.MPP 2215 if mpp != nil { 2216 amt = mpp.TotalMAtoms() 2217 } 2218 2219 // For non-AMP payments the overall payment identifier will be the same 2220 // hash as used for this HTLC. 2221 paymentIdentifier := htlcHash 2222 2223 // For AMP-payments, we'll use the setID as the unique ID for the 2224 // overall payment. 2225 amp := finalHop.AMP 2226 if amp != nil { 2227 paymentIdentifier = amp.SetID() 2228 } 2229 2230 // Record this payment hash with the ControlTower, ensuring it is not 2231 // already in-flight. 2232 info := &channeldb.PaymentCreationInfo{ 2233 PaymentIdentifier: paymentIdentifier, 2234 Value: amt, 2235 CreationTime: r.cfg.Clock.Now(), 2236 PaymentRequest: nil, 2237 } 2238 2239 err := r.cfg.Control.InitPayment(paymentIdentifier, info) 2240 switch { 2241 // If this is an MPP attempt and the hash is already registered with 2242 // the database, we can go on to launch the shard. 2243 case err == channeldb.ErrPaymentInFlight && mpp != nil: 2244 2245 // Any other error is not tolerated. 2246 case err != nil: 2247 return nil, err 2248 } 2249 2250 log.Tracef("Dispatching SendToRoute for HTLC hash %v: %v", 2251 htlcHash, newLogClosure(func() string { 2252 return spew.Sdump(rt) 2253 }), 2254 ) 2255 2256 // Since the HTLC hashes and preimages are specified manually over the 2257 // RPC for SendToRoute requests, we don't have to worry about creating 2258 // a ShardTracker that can generate hashes for AMP payments. Instead we 2259 // create a simple tracker that can just return the hash for the single 2260 // shard we'll now launch. 2261 shardTracker := shards.NewSimpleShardTracker(htlcHash, nil) 2262 2263 // Launch a shard along the given route. 2264 sh := &shardHandler{ 2265 router: r, 2266 identifier: paymentIdentifier, 2267 shardTracker: shardTracker, 2268 } 2269 2270 var shardError error 2271 attempt, outcome, err := sh.launchShard(rt, false) 2272 2273 // With SendToRoute, it can happen that the route exceeds protocol 2274 // constraints. Mark the payment as failed with an internal error. 2275 if err == route.ErrMaxRouteHopsExceeded || 2276 err == sphinx.ErrMaxRoutingInfoSizeExceeded { 2277 2278 log.Debugf("Invalid route provided for payment %x: %v", 2279 paymentIdentifier, err) 2280 2281 controlErr := r.cfg.Control.Fail( 2282 paymentIdentifier, channeldb.FailureReasonError, 2283 ) 2284 if controlErr != nil { 2285 return nil, controlErr 2286 } 2287 } 2288 2289 // In any case, don't continue if there is an error. 2290 if err != nil { 2291 return nil, err 2292 } 2293 2294 var htlcAttempt *channeldb.HTLCAttempt 2295 switch { 2296 // Failed to launch shard. 2297 case outcome.err != nil: 2298 shardError = outcome.err 2299 htlcAttempt = outcome.attempt 2300 2301 // Shard successfully launched, wait for the result to be available. 2302 default: 2303 result, err := sh.collectResult(attempt) 2304 if err != nil { 2305 return nil, err 2306 } 2307 2308 // We got a successful result. 2309 if result.err == nil { 2310 return result.attempt, nil 2311 } 2312 2313 // The shard failed, break switch to handle it. 2314 shardError = result.err 2315 htlcAttempt = result.attempt 2316 } 2317 2318 // Since for SendToRoute we won't retry in case the shard fails, we'll 2319 // mark the payment failed with the control tower immediately. Process 2320 // the error to check if it maps into a terminal error code, if not use 2321 // a generic NO_ROUTE error. 2322 var failureReason *channeldb.FailureReason 2323 err = sh.handleSendError(attempt, shardError) 2324 2325 switch { 2326 // If we weren't able to extract a proper failure reason (which can 2327 // happen if the second chance logic is triggered), then we'll use the 2328 // normal no route error. 2329 case err == nil: 2330 err = r.cfg.Control.Fail( 2331 paymentIdentifier, channeldb.FailureReasonNoRoute, 2332 ) 2333 2334 // If this is a failure reason, then we'll apply the failure directly 2335 // to the control tower, and return the normal response to the caller. 2336 case goErrors.As(err, &failureReason): 2337 err = r.cfg.Control.Fail(paymentIdentifier, *failureReason) 2338 } 2339 if err != nil { 2340 return nil, err 2341 } 2342 2343 return htlcAttempt, shardError 2344 } 2345 2346 // sendPayment attempts to send a payment to the passed payment hash. This 2347 // function is blocking and will return either: when the payment is successful, 2348 // or all candidates routes have been attempted and resulted in a failed 2349 // payment. If the payment succeeds, then a non-nil Route will be returned 2350 // which describes the path the successful payment traversed within the network 2351 // to reach the destination. Additionally, the payment preimage will also be 2352 // returned. 2353 // 2354 // The existing attempt argument should be set to nil if this is a payment that 2355 // haven't had any payment attempt sent to the switch yet. If it has had an 2356 // attempt already, it should be passed such that the result can be retrieved. 2357 // 2358 // This method relies on the ControlTower's internal payment state machine to 2359 // carry out its execution. After restarts it is safe, and assumed, that the 2360 // router will call this method for every payment still in-flight according to 2361 // the ControlTower. 2362 func (r *ChannelRouter) sendPayment( 2363 totalAmt, feeLimit lnwire.MilliAtom, identifier lntypes.Hash, 2364 timeout time.Duration, paySession PaymentSession, 2365 shardTracker shards.ShardTracker) ([32]byte, *route.Route, error) { 2366 2367 // We'll also fetch the current block height so we can properly 2368 // calculate the required HTLC time locks within the route. 2369 _, currentHeight, err := r.cfg.Chain.GetBestBlock() 2370 if err != nil { 2371 return [32]byte{}, nil, err 2372 } 2373 2374 // Now set up a paymentLifecycle struct with these params, such that we 2375 // can resume the payment from the current state. 2376 p := &paymentLifecycle{ 2377 router: r, 2378 totalAmount: totalAmt, 2379 feeLimit: feeLimit, 2380 identifier: identifier, 2381 paySession: paySession, 2382 shardTracker: shardTracker, 2383 currentHeight: currentHeight, 2384 } 2385 2386 // If a timeout is specified, create a timeout channel. If no timeout is 2387 // specified, the channel is left nil and will never abort the payment 2388 // loop. 2389 if timeout != 0 { 2390 p.timeoutChan = time.After(timeout) 2391 } 2392 2393 return p.resumePayment() 2394 2395 } 2396 2397 // extractChannelUpdate examines the error and extracts the channel update. 2398 func (r *ChannelRouter) extractChannelUpdate( 2399 failure lnwire.FailureMessage) *lnwire.ChannelUpdate { 2400 2401 var update *lnwire.ChannelUpdate 2402 switch onionErr := failure.(type) { 2403 case *lnwire.FailExpiryTooSoon: 2404 update = &onionErr.Update 2405 case *lnwire.FailAmountBelowMinimum: 2406 update = &onionErr.Update 2407 case *lnwire.FailFeeInsufficient: 2408 update = &onionErr.Update 2409 case *lnwire.FailIncorrectCltvExpiry: 2410 update = &onionErr.Update 2411 case *lnwire.FailChannelDisabled: 2412 update = &onionErr.Update 2413 case *lnwire.FailTemporaryChannelFailure: 2414 update = onionErr.Update 2415 } 2416 2417 return update 2418 } 2419 2420 // applyChannelUpdate validates a channel update and if valid, applies it to 2421 // the database. It returns a bool indicating whether the updates was 2422 // successful. 2423 func (r *ChannelRouter) applyChannelUpdate(msg *lnwire.ChannelUpdate, 2424 pubKey *secp256k1.PublicKey) bool { 2425 2426 ch, _, _, err := r.GetChannelByID(msg.ShortChannelID) 2427 if err != nil { 2428 log.Errorf("Unable to retrieve channel by id: %v", err) 2429 return false 2430 } 2431 2432 if err := ValidateChannelUpdateAnn(pubKey, ch.Capacity, msg); err != nil { 2433 log.Errorf("Unable to validate channel update: %v", err) 2434 return false 2435 } 2436 2437 err = r.UpdateEdge(&channeldb.ChannelEdgePolicy{ 2438 SigBytes: msg.Signature.ToSignatureBytes(), 2439 ChannelID: msg.ShortChannelID.ToUint64(), 2440 LastUpdate: time.Unix(int64(msg.Timestamp), 0), 2441 MessageFlags: msg.MessageFlags, 2442 ChannelFlags: msg.ChannelFlags, 2443 TimeLockDelta: msg.TimeLockDelta, 2444 MinHTLC: msg.HtlcMinimumMAtoms, 2445 MaxHTLC: msg.HtlcMaximumMAtoms, 2446 FeeBaseMAtoms: lnwire.MilliAtom(msg.BaseFee), 2447 FeeProportionalMillionths: lnwire.MilliAtom(msg.FeeRate), 2448 }) 2449 if err != nil && !IsError(err, ErrIgnored, ErrOutdated) { 2450 log.Errorf("Unable to apply channel update: %v", err) 2451 return false 2452 } 2453 2454 return true 2455 } 2456 2457 // AddNode is used to add information about a node to the router database. If 2458 // the node with this pubkey is not present in an existing channel, it will 2459 // be ignored. 2460 // 2461 // NOTE: This method is part of the ChannelGraphSource interface. 2462 func (r *ChannelRouter) AddNode(node *channeldb.LightningNode, 2463 op ...batch.SchedulerOption) error { 2464 2465 rMsg := &routingMsg{ 2466 msg: node, 2467 op: op, 2468 err: make(chan error, 1), 2469 } 2470 2471 select { 2472 case r.networkUpdates <- rMsg: 2473 select { 2474 case err := <-rMsg.err: 2475 return err 2476 case <-r.quit: 2477 return ErrRouterShuttingDown 2478 } 2479 case <-r.quit: 2480 return ErrRouterShuttingDown 2481 } 2482 } 2483 2484 // AddEdge is used to add edge/channel to the topology of the router, after all 2485 // information about channel will be gathered this edge/channel might be used 2486 // in construction of payment path. 2487 // 2488 // NOTE: This method is part of the ChannelGraphSource interface. 2489 func (r *ChannelRouter) AddEdge(edge *channeldb.ChannelEdgeInfo, 2490 op ...batch.SchedulerOption) error { 2491 2492 rMsg := &routingMsg{ 2493 msg: edge, 2494 op: op, 2495 err: make(chan error, 1), 2496 } 2497 2498 select { 2499 case r.networkUpdates <- rMsg: 2500 select { 2501 case err := <-rMsg.err: 2502 return err 2503 case <-r.quit: 2504 return ErrRouterShuttingDown 2505 } 2506 case <-r.quit: 2507 return ErrRouterShuttingDown 2508 } 2509 } 2510 2511 // UpdateEdge is used to update edge information, without this message edge 2512 // considered as not fully constructed. 2513 // 2514 // NOTE: This method is part of the ChannelGraphSource interface. 2515 func (r *ChannelRouter) UpdateEdge(update *channeldb.ChannelEdgePolicy, 2516 op ...batch.SchedulerOption) error { 2517 2518 rMsg := &routingMsg{ 2519 msg: update, 2520 op: op, 2521 err: make(chan error, 1), 2522 } 2523 2524 select { 2525 case r.networkUpdates <- rMsg: 2526 select { 2527 case err := <-rMsg.err: 2528 return err 2529 case <-r.quit: 2530 return ErrRouterShuttingDown 2531 } 2532 case <-r.quit: 2533 return ErrRouterShuttingDown 2534 } 2535 } 2536 2537 // CurrentBlockHeight returns the block height from POV of the router subsystem. 2538 // 2539 // NOTE: This method is part of the ChannelGraphSource interface. 2540 func (r *ChannelRouter) CurrentBlockHeight() (uint32, error) { 2541 _, height, err := r.cfg.Chain.GetBestBlock() 2542 return uint32(height), err 2543 } 2544 2545 // SyncedHeight returns the block height to which the router subsystem currently 2546 // is synced to. This can differ from the above chain height if the goroutine 2547 // responsible for processing the blocks isn't yet up to speed. 2548 func (r *ChannelRouter) SyncedHeight() uint32 { 2549 return atomic.LoadUint32(&r.bestHeight) 2550 } 2551 2552 // GetChannelByID return the channel by the channel id. 2553 // 2554 // NOTE: This method is part of the ChannelGraphSource interface. 2555 func (r *ChannelRouter) GetChannelByID(chanID lnwire.ShortChannelID) ( 2556 *channeldb.ChannelEdgeInfo, 2557 *channeldb.ChannelEdgePolicy, 2558 *channeldb.ChannelEdgePolicy, error) { 2559 2560 return r.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64()) 2561 } 2562 2563 // FetchLightningNode attempts to look up a target node by its identity public 2564 // key. channeldb.ErrGraphNodeNotFound is returned if the node doesn't exist 2565 // within the graph. 2566 // 2567 // NOTE: This method is part of the ChannelGraphSource interface. 2568 func (r *ChannelRouter) FetchLightningNode( 2569 node route.Vertex) (*channeldb.LightningNode, error) { 2570 2571 return r.cfg.Graph.FetchLightningNode(node) 2572 } 2573 2574 // ForEachNode is used to iterate over every node in router topology. 2575 // 2576 // NOTE: This method is part of the ChannelGraphSource interface. 2577 func (r *ChannelRouter) ForEachNode(cb func(*channeldb.LightningNode) error) error { 2578 return r.cfg.Graph.ForEachNode(func(_ kvdb.RTx, n *channeldb.LightningNode) error { 2579 return cb(n) 2580 }) 2581 } 2582 2583 // ForAllOutgoingChannels is used to iterate over all outgoing channels owned by 2584 // the router. 2585 // 2586 // NOTE: This method is part of the ChannelGraphSource interface. 2587 func (r *ChannelRouter) ForAllOutgoingChannels(cb func(kvdb.RTx, 2588 *channeldb.ChannelEdgeInfo, *channeldb.ChannelEdgePolicy) error) error { 2589 2590 openChans, err := r.cfg.LocalOpenChanIDs() 2591 if err != nil { 2592 return fmt.Errorf("unable to query local open chan IDs: %v", err) 2593 } 2594 2595 return r.selfNode.ForEachChannel(nil, func(tx kvdb.RTx, 2596 c *channeldb.ChannelEdgeInfo, 2597 e, _ *channeldb.ChannelEdgePolicy) error { 2598 2599 if e == nil { 2600 return fmt.Errorf("channel from self node has no policy") 2601 } 2602 2603 // If the channel is not actually open in the DB, skip it. Some 2604 // unindentified bug causes a node to have old, already closed 2605 // channels in the graph but not the DB, so this check prevents 2606 // re-generating announcements for those channels. 2607 if _, ok := openChans[c.ChannelID]; !ok { 2608 log.Warnf("Seeing local channel %s (%s) in graph when channel "+ 2609 "is not open in the DB", lnwire.NewShortChanIDFromInt(c.ChannelID), 2610 c.ChannelPoint) 2611 return nil 2612 } 2613 2614 return cb(tx, c, e) 2615 }) 2616 } 2617 2618 // AddProof updates the channel edge info with proof which is needed to 2619 // properly announce the edge to the rest of the network. 2620 // 2621 // NOTE: This method is part of the ChannelGraphSource interface. 2622 func (r *ChannelRouter) AddProof(chanID lnwire.ShortChannelID, 2623 proof *channeldb.ChannelAuthProof) error { 2624 2625 info, _, _, err := r.cfg.Graph.FetchChannelEdgesByID(chanID.ToUint64()) 2626 if err != nil { 2627 return err 2628 } 2629 2630 info.AuthProof = proof 2631 return r.cfg.Graph.UpdateChannelEdge(info) 2632 } 2633 2634 // IsStaleNode returns true if the graph source has a node announcement for the 2635 // target node with a more recent timestamp. 2636 // 2637 // NOTE: This method is part of the ChannelGraphSource interface. 2638 func (r *ChannelRouter) IsStaleNode(node route.Vertex, timestamp time.Time) bool { 2639 // If our attempt to assert that the node announcement is fresh fails, 2640 // then we know that this is actually a stale announcement. 2641 return r.assertNodeAnnFreshness(node, timestamp) != nil 2642 } 2643 2644 // IsPublicNode determines whether the given vertex is seen as a public node in 2645 // the graph from the graph's source node's point of view. 2646 // 2647 // NOTE: This method is part of the ChannelGraphSource interface. 2648 func (r *ChannelRouter) IsPublicNode(node route.Vertex) (bool, error) { 2649 return r.cfg.Graph.IsPublicNode(node) 2650 } 2651 2652 // IsKnownEdge returns true if the graph source already knows of the passed 2653 // channel ID either as a live or zombie edge. 2654 // 2655 // NOTE: This method is part of the ChannelGraphSource interface. 2656 func (r *ChannelRouter) IsKnownEdge(chanID lnwire.ShortChannelID) bool { 2657 _, _, exists, isZombie, _ := r.cfg.Graph.HasChannelEdge(chanID.ToUint64()) 2658 return exists || isZombie 2659 } 2660 2661 // IsStaleEdgePolicy returns true if the graph source has a channel edge for 2662 // the passed channel ID (and flags) that have a more recent timestamp. 2663 // 2664 // NOTE: This method is part of the ChannelGraphSource interface. 2665 func (r *ChannelRouter) IsStaleEdgePolicy(chanID lnwire.ShortChannelID, 2666 timestamp time.Time, flags lnwire.ChanUpdateChanFlags) bool { 2667 2668 // If the channel is known to be spent, this can't possibly be a useful 2669 // ChannelUpdate. 2670 if isSpent, _ := r.cfg.Graph.IsKnownSpent(chanID.ToUint64()); isSpent { 2671 return false 2672 } 2673 2674 edge1Timestamp, edge2Timestamp, exists, isZombie, err := 2675 r.cfg.Graph.HasChannelEdge(chanID.ToUint64()) 2676 if err != nil { 2677 return false 2678 2679 } 2680 2681 // If we know of the edge as a zombie, then we'll make some additional 2682 // checks to determine if the new policy is fresh. 2683 if isZombie { 2684 // When running with AssumeChannelValid, we also prune channels 2685 // if both of their edges are disabled. We'll mark the new 2686 // policy as stale if it remains disabled. 2687 if r.cfg.AssumeChannelValid { 2688 isDisabled := flags&lnwire.ChanUpdateDisabled == 2689 lnwire.ChanUpdateDisabled 2690 if isDisabled { 2691 return true 2692 } 2693 } 2694 2695 // Otherwise, we'll fall back to our usual ChannelPruneExpiry. 2696 return time.Since(timestamp) > r.cfg.ChannelPruneExpiry 2697 } 2698 2699 // If we don't know of the edge, then it means it's fresh (thus not 2700 // stale). 2701 if !exists { 2702 return false 2703 } 2704 2705 // As edges are directional edge node has a unique policy for the 2706 // direction of the edge they control. Therefore we first check if we 2707 // already have the most up to date information for that edge. If so, 2708 // then we can exit early. 2709 switch { 2710 // A flag set of 0 indicates this is an announcement for the "first" 2711 // node in the channel. 2712 case flags&lnwire.ChanUpdateDirection == 0: 2713 return !edge1Timestamp.Before(timestamp) 2714 2715 // Similarly, a flag set of 1 indicates this is an announcement for the 2716 // "second" node in the channel. 2717 case flags&lnwire.ChanUpdateDirection == 1: 2718 return !edge2Timestamp.Before(timestamp) 2719 } 2720 2721 return false 2722 } 2723 2724 // MarkEdgeLive clears an edge from our zombie index, deeming it as live. 2725 // 2726 // NOTE: This method is part of the ChannelGraphSource interface. 2727 func (r *ChannelRouter) MarkEdgeLive(chanID lnwire.ShortChannelID) error { 2728 return r.cfg.Graph.MarkEdgeLive(chanID.ToUint64()) 2729 } 2730 2731 // ErrNoChannel is returned when a route cannot be built because there are no 2732 // channels that satisfy all requirements. 2733 type ErrNoChannel struct { 2734 position int 2735 fromNode route.Vertex 2736 } 2737 2738 // Error returns a human readable string describing the error. 2739 func (e ErrNoChannel) Error() string { 2740 return fmt.Sprintf("no matching outgoing channel available for "+ 2741 "node %v (%v)", e.position, e.fromNode) 2742 } 2743 2744 // BuildRoute returns a fully specified route based on a list of pubkeys. If 2745 // amount is nil, the minimum routable amount is used. To force a specific 2746 // outgoing channel, use the outgoingChan parameter. 2747 func (r *ChannelRouter) BuildRoute(amt *lnwire.MilliAtom, 2748 hops []route.Vertex, outgoingChan *uint64, 2749 finalCltvDelta int32, payAddr *[32]byte) (*route.Route, error) { 2750 2751 log.Tracef("BuildRoute called: hopsCount=%v, amt=%v", 2752 len(hops), amt) 2753 2754 var outgoingChans map[uint64]struct{} 2755 if outgoingChan != nil { 2756 outgoingChans = map[uint64]struct{}{ 2757 *outgoingChan: {}, 2758 } 2759 } 2760 2761 // If no amount is specified, we need to build a route for the minimum 2762 // amount that this route can carry. 2763 useMinAmt := amt == nil 2764 2765 // We'll attempt to obtain a set of bandwidth hints that helps us select 2766 // the best outgoing channel to use in case no outgoing channel is set. 2767 bandwidthHints, err := newBandwidthManager( 2768 r.cachedGraph, r.selfNode.PubKeyBytes, r.cfg.GetLink, 2769 ) 2770 if err != nil { 2771 return nil, err 2772 } 2773 2774 // Fetch the current block height outside the routing transaction, to 2775 // prevent the rpc call blocking the database. 2776 _, height, err := r.cfg.Chain.GetBestBlock() 2777 if err != nil { 2778 return nil, err 2779 } 2780 2781 // Allocate a list that will contain the unified policies for this 2782 // route. 2783 edges := make([]*unifiedPolicy, len(hops)) 2784 2785 var runningAmt lnwire.MilliAtom 2786 if useMinAmt { 2787 // For minimum amount routes, aim to deliver at least 1 msat to 2788 // the destination. There are nodes in the wild that have a 2789 // min_htlc channel policy of zero, which could lead to a zero 2790 // amount payment being made. 2791 runningAmt = 1 2792 } else { 2793 // If an amount is specified, we need to build a route that 2794 // delivers exactly this amount to the final destination. 2795 runningAmt = *amt 2796 } 2797 2798 // Traverse hops backwards to accumulate fees in the running amounts. 2799 source := r.selfNode.PubKeyBytes 2800 for i := len(hops) - 1; i >= 0; i-- { 2801 toNode := hops[i] 2802 2803 var fromNode route.Vertex 2804 if i == 0 { 2805 fromNode = source 2806 } else { 2807 fromNode = hops[i-1] 2808 } 2809 2810 localChan := i == 0 2811 2812 // Build unified policies for this hop based on the channels 2813 // known in the graph. 2814 u := newUnifiedPolicies(source, toNode, outgoingChans) 2815 2816 err := u.addGraphPolicies(r.cachedGraph) 2817 if err != nil { 2818 return nil, err 2819 } 2820 2821 // Exit if there are no channels. 2822 unifiedPolicy, ok := u.policies[fromNode] 2823 if !ok { 2824 return nil, ErrNoChannel{ 2825 fromNode: fromNode, 2826 position: i, 2827 } 2828 } 2829 2830 // If using min amt, increase amt if needed. 2831 if useMinAmt { 2832 min := unifiedPolicy.minAmt() 2833 if min > runningAmt { 2834 runningAmt = min 2835 } 2836 } 2837 2838 // Get a forwarding policy for the specific amount that we want 2839 // to forward. 2840 policy := unifiedPolicy.getPolicy(runningAmt, bandwidthHints) 2841 if policy == nil { 2842 return nil, ErrNoChannel{ 2843 fromNode: fromNode, 2844 position: i, 2845 } 2846 } 2847 2848 // Add fee for this hop. 2849 if !localChan { 2850 runningAmt += policy.ComputeFee(runningAmt) 2851 } 2852 2853 log.Tracef("Select channel %v at position %v", policy.ChannelID, i) 2854 2855 edges[i] = unifiedPolicy 2856 } 2857 2858 // Now that we arrived at the start of the route and found out the route 2859 // total amount, we make a forward pass. Because the amount may have 2860 // been increased in the backward pass, fees need to be recalculated and 2861 // amount ranges re-checked. 2862 var pathEdges []*channeldb.CachedEdgePolicy 2863 receiverAmt := runningAmt 2864 for i, edge := range edges { 2865 policy := edge.getPolicy(receiverAmt, bandwidthHints) 2866 if policy == nil { 2867 return nil, ErrNoChannel{ 2868 fromNode: hops[i-1], 2869 position: i, 2870 } 2871 } 2872 2873 if i > 0 { 2874 // Decrease the amount to send while going forward. 2875 receiverAmt -= policy.ComputeFeeFromIncoming( 2876 receiverAmt, 2877 ) 2878 } 2879 2880 pathEdges = append(pathEdges, policy) 2881 } 2882 2883 // Build and return the final route. 2884 return newRoute( 2885 source, pathEdges, uint32(height), 2886 finalHopParams{ 2887 amt: receiverAmt, 2888 totalAmt: receiverAmt, 2889 cltvDelta: uint16(finalCltvDelta), 2890 records: nil, 2891 paymentAddr: payAddr, 2892 }, 2893 ) 2894 }