github.com/decred/dcrlnd@v0.7.6/htlcswitch/switch.go (about) 1 package htlcswitch 2 3 import ( 4 "bytes" 5 "errors" 6 "fmt" 7 "math/rand" 8 "sync" 9 "sync/atomic" 10 "time" 11 12 "github.com/davecgh/go-spew/spew" 13 "github.com/decred/dcrd/dcrutil/v4" 14 "github.com/decred/dcrd/wire" 15 "github.com/decred/dcrlnd/chainntnfs" 16 "github.com/decred/dcrlnd/channeldb" 17 "github.com/decred/dcrlnd/clock" 18 "github.com/decred/dcrlnd/contractcourt" 19 "github.com/decred/dcrlnd/htlcswitch/hop" 20 "github.com/decred/dcrlnd/kvdb" 21 "github.com/decred/dcrlnd/lntypes" 22 "github.com/decred/dcrlnd/lnwallet" 23 "github.com/decred/dcrlnd/lnwallet/chainfee" 24 "github.com/decred/dcrlnd/lnwire" 25 "github.com/decred/dcrlnd/ticker" 26 ) 27 28 const ( 29 // DefaultFwdEventInterval is the duration between attempts to flush 30 // pending forwarding events to disk. 31 DefaultFwdEventInterval = 15 * time.Second 32 33 // DefaultLogInterval is the duration between attempts to log statistics 34 // about forwarding events. 35 DefaultLogInterval = 10 * time.Second 36 37 // DefaultAckInterval is the duration between attempts to ack any settle 38 // fails in a forwarding package. 39 DefaultAckInterval = 15 * time.Second 40 41 // DefaultHTLCExpiry is the duration after which Adds will be cancelled 42 // if they could not get added to an outgoing commitment. 43 DefaultHTLCExpiry = time.Minute 44 ) 45 46 var ( 47 // ErrChannelLinkNotFound is used when channel link hasn't been found. 48 ErrChannelLinkNotFound = errors.New("channel link not found") 49 50 // ErrDuplicateAdd signals that the ADD htlc was already forwarded 51 // through the switch and is locked into another commitment txn. 52 ErrDuplicateAdd = errors.New("duplicate add HTLC detected") 53 54 // ErrUnknownErrorDecryptor signals that we were unable to locate the 55 // error decryptor for this payment. This is likely due to restarting 56 // the daemon. 57 ErrUnknownErrorDecryptor = errors.New("unknown error decryptor") 58 59 // ErrSwitchExiting signaled when the switch has received a shutdown 60 // request. 61 ErrSwitchExiting = errors.New("htlcswitch shutting down") 62 63 // ErrNoLinksFound is an error returned when we attempt to retrieve the 64 // active links in the switch for a specific destination. 65 ErrNoLinksFound = errors.New("no channel links found") 66 67 // ErrUnreadableFailureMessage is returned when the failure message 68 // cannot be decrypted. 69 ErrUnreadableFailureMessage = errors.New("unreadable failure message") 70 71 // ErrLocalAddFailed signals that the ADD htlc for a local payment 72 // failed to be processed. 73 ErrLocalAddFailed = errors.New("local add HTLC failed") 74 75 // errDustThresholdExceeded is only surfaced to callers of SendHTLC and 76 // signals that sending the HTLC would exceed the outgoing link's dust 77 // threshold. 78 errDustThresholdExceeded = errors.New("dust threshold exceeded") 79 80 // DefaultDustThreshold is the default threshold after which we'll fail 81 // payments if they are dust. This is currently set to 500k atoms. 82 DefaultDustThreshold = lnwire.MilliAtom(500_000_000) 83 ) 84 85 // plexPacket encapsulates switch packet and adds error channel to receive 86 // error from request handler. 87 type plexPacket struct { 88 pkt *htlcPacket 89 err chan error 90 } 91 92 // ChanClose represents a request which close a particular channel specified by 93 // its id. 94 type ChanClose struct { 95 // CloseType is a variable which signals the type of channel closure the 96 // peer should execute. 97 CloseType contractcourt.ChannelCloseType 98 99 // ChanPoint represent the id of the channel which should be closed. 100 ChanPoint *wire.OutPoint 101 102 // TargetFeePerKB is the ideal fee that was specified by the caller. 103 // This value is only utilized if the closure type is CloseRegular. 104 // This will be the starting offered fee when the fee negotiation 105 // process for the cooperative closure transaction kicks off. 106 TargetFeePerKB chainfee.AtomPerKByte 107 108 // DeliveryScript is an optional delivery script to pay funds out to. 109 DeliveryScript lnwire.DeliveryAddress 110 111 // Updates is used by request creator to receive the notifications about 112 // execution of the close channel request. 113 Updates chan interface{} 114 115 // Err is used by request creator to receive request execution error. 116 Err chan error 117 } 118 119 // Config defines the configuration for the service. ALL elements within the 120 // configuration MUST be non-nil for the service to carry out its duties. 121 type Config struct { 122 // FwdingLog is an interface that will be used by the switch to log 123 // forwarding events. A forwarding event happens each time a payment 124 // circuit is successfully completed. So when we forward an HTLC, and a 125 // settle is eventually received. 126 FwdingLog ForwardingLog 127 128 // LocalChannelClose kicks-off the workflow to execute a cooperative or 129 // forced unilateral closure of the channel initiated by a local 130 // subsystem. 131 LocalChannelClose func(pubKey []byte, request *ChanClose) 132 133 // DB is the database backend that will be used to back the switch's 134 // persistent circuit map. 135 DB kvdb.Backend 136 137 // FetchAllOpenChannels is a function that fetches all currently open 138 // channels from the channel database. 139 FetchAllOpenChannels func() ([]*channeldb.OpenChannel, error) 140 141 // FetchClosedChannels is a function that fetches all closed channels 142 // from the channel database. 143 FetchClosedChannels func( 144 pendingOnly bool) ([]*channeldb.ChannelCloseSummary, error) 145 146 // SwitchPackager provides access to the forwarding packages of all 147 // active channels. This gives the switch the ability to read arbitrary 148 // forwarding packages, and ack settles and fails contained within them. 149 SwitchPackager channeldb.FwdOperator 150 151 // ExtractErrorEncrypter is an interface allowing switch to reextract 152 // error encrypters stored in the circuit map on restarts, since they 153 // are not stored directly within the database. 154 ExtractErrorEncrypter hop.ErrorEncrypterExtracter 155 156 // FetchLastChannelUpdate retrieves the latest routing policy for a 157 // target channel. This channel will typically be the outgoing channel 158 // specified when we receive an incoming HTLC. This will be used to 159 // provide payment senders our latest policy when sending encrypted 160 // error messages. 161 FetchLastChannelUpdate func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate, error) 162 163 // Notifier is an instance of a chain notifier that we'll use to signal 164 // the switch when a new block has arrived. 165 Notifier chainntnfs.ChainNotifier 166 167 // HtlcNotifier is an instance of a htlcNotifier which we will pipe htlc 168 // events through. 169 HtlcNotifier htlcNotifier 170 171 // FwdEventTicker is a signal that instructs the htlcswitch to flush any 172 // pending forwarding events. 173 FwdEventTicker ticker.Ticker 174 175 // LogEventTicker is a signal instructing the htlcswitch to log 176 // aggregate stats about it's forwarding during the last interval. 177 LogEventTicker ticker.Ticker 178 179 // AckEventTicker is a signal instructing the htlcswitch to ack any settle 180 // fails in forwarding packages. 181 AckEventTicker ticker.Ticker 182 183 // AllowCircularRoute is true if the user has configured their node to 184 // allow forwards that arrive and depart our node over the same channel. 185 AllowCircularRoute bool 186 187 // RejectHTLC is a flag that instructs the htlcswitch to reject any 188 // HTLCs that are not from the source hop. 189 RejectHTLC bool 190 191 // Clock is a time source for the switch. 192 Clock clock.Clock 193 194 // HTLCExpiry is the interval after which Adds will be cancelled if they 195 // have not been yet been delivered to a link. The computed deadline 196 // will expiry this long after the Adds are added to a mailbox via 197 // AddPacket. 198 HTLCExpiry time.Duration 199 200 // DustThreshold is the threshold in milli-satoshis after which we'll 201 // fail incoming or outgoing dust payments for a particular channel. 202 DustThreshold lnwire.MilliAtom 203 } 204 205 // Switch is the central messaging bus for all incoming/outgoing HTLCs. 206 // Connected peers with active channels are treated as named interfaces which 207 // refer to active channels as links. A link is the switch's message 208 // communication point with the goroutine that manages an active channel. New 209 // links are registered each time a channel is created, and unregistered once 210 // the channel is closed. The switch manages the hand-off process for multi-hop 211 // HTLCs, forwarding HTLCs initiated from within the daemon, and finally 212 // notifies users local-systems concerning their outstanding payment requests. 213 type Switch struct { 214 started int32 // To be used atomically. 215 shutdown int32 // To be used atomically. 216 217 // bestHeight is the best known height of the main chain. The links will 218 // be used this information to govern decisions based on HTLC timeouts. 219 // This will be retrieved by the registered links atomically. 220 bestHeight uint32 221 222 wg sync.WaitGroup 223 quit chan struct{} 224 225 // cfg is a copy of the configuration struct that the htlc switch 226 // service was initialized with. 227 cfg *Config 228 229 // networkResults stores the results of payments initiated by the user. 230 // The store is used to later look up the payments and notify the 231 // user of the result when they are complete. Each payment attempt 232 // should be given a unique integer ID when it is created, otherwise 233 // results might be overwritten. 234 networkResults *networkResultStore 235 236 // circuits is storage for payment circuits which are used to 237 // forward the settle/fail htlc updates back to the add htlc initiator. 238 circuits CircuitMap 239 240 // mailOrchestrator manages the lifecycle of mailboxes used throughout 241 // the switch, and facilitates delayed delivery of packets to links that 242 // later come online. 243 mailOrchestrator *mailOrchestrator 244 245 // indexMtx is a read/write mutex that protects the set of indexes 246 // below. 247 indexMtx sync.RWMutex 248 249 // pendingLinkIndex holds links that have not had their final, live 250 // short_chan_id assigned. These links can be transitioned into the 251 // primary linkIndex by using UpdateShortChanID to load their live id. 252 pendingLinkIndex map[lnwire.ChannelID]ChannelLink 253 254 // links is a map of channel id and channel link which manages 255 // this channel. 256 linkIndex map[lnwire.ChannelID]ChannelLink 257 258 // forwardingIndex is an index which is consulted by the switch when it 259 // needs to locate the next hop to forward an incoming/outgoing HTLC 260 // update to/from. 261 // 262 // TODO(roasbeef): eventually add a NetworkHop mapping before the 263 // ChannelLink 264 forwardingIndex map[lnwire.ShortChannelID]ChannelLink 265 266 // interfaceIndex maps the compressed public key of a peer to all the 267 // channels that the switch maintains with that peer. 268 interfaceIndex map[[33]byte]map[lnwire.ChannelID]ChannelLink 269 270 // htlcPlex is the channel which all connected links use to coordinate 271 // the setup/teardown of Sphinx (onion routing) payment circuits. 272 // Active links forward any add/settle messages over this channel each 273 // state transition, sending new adds/settles which are fully locked 274 // in. 275 htlcPlex chan *plexPacket 276 277 // chanCloseRequests is used to transfer the channel close request to 278 // the channel close handler. 279 chanCloseRequests chan *ChanClose 280 281 // resolutionMsgs is the channel that all external contract resolution 282 // messages will be sent over. 283 resolutionMsgs chan *resolutionMsg 284 285 // pendingFwdingEvents is the set of forwarding events which have been 286 // collected during the current interval, but hasn't yet been written 287 // to the forwarding log. 288 fwdEventMtx sync.Mutex 289 pendingFwdingEvents []channeldb.ForwardingEvent 290 291 // blockEpochStream is an active block epoch event stream backed by an 292 // active ChainNotifier instance. This will be used to retrieve the 293 // lastest height of the chain. 294 blockEpochStream *chainntnfs.BlockEpochEvent 295 296 // pendingSettleFails is the set of settle/fail entries that we need to 297 // ack in the forwarding package of the outgoing link. This was added to 298 // make pipelining settles more efficient. 299 pendingSettleFails []channeldb.SettleFailRef 300 } 301 302 // New creates the new instance of htlc switch. 303 func New(cfg Config, currentHeight uint32) (*Switch, error) { 304 circuitMap, err := NewCircuitMap(&CircuitMapConfig{ 305 DB: cfg.DB, 306 FetchAllOpenChannels: cfg.FetchAllOpenChannels, 307 FetchClosedChannels: cfg.FetchClosedChannels, 308 ExtractErrorEncrypter: cfg.ExtractErrorEncrypter, 309 }) 310 if err != nil { 311 return nil, err 312 } 313 314 s := &Switch{ 315 bestHeight: currentHeight, 316 cfg: &cfg, 317 circuits: circuitMap, 318 linkIndex: make(map[lnwire.ChannelID]ChannelLink), 319 forwardingIndex: make(map[lnwire.ShortChannelID]ChannelLink), 320 interfaceIndex: make(map[[33]byte]map[lnwire.ChannelID]ChannelLink), 321 pendingLinkIndex: make(map[lnwire.ChannelID]ChannelLink), 322 networkResults: newNetworkResultStore(cfg.DB), 323 htlcPlex: make(chan *plexPacket), 324 chanCloseRequests: make(chan *ChanClose), 325 resolutionMsgs: make(chan *resolutionMsg), 326 quit: make(chan struct{}), 327 } 328 329 s.mailOrchestrator = newMailOrchestrator(&mailOrchConfig{ 330 fetchUpdate: s.cfg.FetchLastChannelUpdate, 331 forwardPackets: s.ForwardPackets, 332 clock: s.cfg.Clock, 333 expiry: s.cfg.HTLCExpiry, 334 }) 335 336 return s, nil 337 } 338 339 // resolutionMsg is a struct that wraps an existing ResolutionMsg with a done 340 // channel. We'll use this channel to synchronize delivery of the message with 341 // the caller. 342 type resolutionMsg struct { 343 contractcourt.ResolutionMsg 344 345 doneChan chan struct{} 346 } 347 348 // ProcessContractResolution is called by active contract resolvers once a 349 // contract they are watching over has been fully resolved. The message carries 350 // an external signal that *would* have been sent if the outgoing channel 351 // didn't need to go to the chain in order to fulfill a contract. We'll process 352 // this message just as if it came from an active outgoing channel. 353 func (s *Switch) ProcessContractResolution(msg contractcourt.ResolutionMsg) error { 354 355 done := make(chan struct{}) 356 357 select { 358 case s.resolutionMsgs <- &resolutionMsg{ 359 ResolutionMsg: msg, 360 doneChan: done, 361 }: 362 case <-s.quit: 363 return ErrSwitchExiting 364 } 365 366 select { 367 case <-done: 368 case <-s.quit: 369 return ErrSwitchExiting 370 } 371 372 return nil 373 } 374 375 // GetPaymentResult returns the the result of the payment attempt with the 376 // given attemptID. The paymentHash should be set to the payment's overall 377 // hash, or in case of AMP payments the payment's unique identifier. 378 // 379 // The method returns a channel where the payment result will be sent when 380 // available, or an error is encountered during forwarding. When a result is 381 // received on the channel, the HTLC is guaranteed to no longer be in flight. 382 // The switch shutting down is signaled by closing the channel. If the 383 // attemptID is unknown, ErrPaymentIDNotFound will be returned. 384 func (s *Switch) GetPaymentResult(attemptID uint64, paymentHash lntypes.Hash, 385 deobfuscator ErrorDecrypter) (<-chan *PaymentResult, error) { 386 387 var ( 388 nChan <-chan *networkResult 389 err error 390 inKey = CircuitKey{ 391 ChanID: hop.Source, 392 HtlcID: attemptID, 393 } 394 ) 395 396 // If the payment is not found in the circuit map, check whether a 397 // result is already available. 398 // Assumption: no one will add this payment ID other than the caller. 399 if s.circuits.LookupCircuit(inKey) == nil { 400 res, err := s.networkResults.getResult(attemptID) 401 if err != nil { 402 return nil, err 403 } 404 c := make(chan *networkResult, 1) 405 c <- res 406 nChan = c 407 } else { 408 // The payment was committed to the circuits, subscribe for a 409 // result. 410 nChan, err = s.networkResults.subscribeResult(attemptID) 411 if err != nil { 412 return nil, err 413 } 414 } 415 416 resultChan := make(chan *PaymentResult, 1) 417 418 // Since the payment was known, we can start a goroutine that can 419 // extract the result when it is available, and pass it on to the 420 // caller. 421 s.wg.Add(1) 422 go func() { 423 defer s.wg.Done() 424 425 var n *networkResult 426 select { 427 case n = <-nChan: 428 case <-s.quit: 429 // We close the result channel to signal a shutdown. We 430 // don't send any result in this case since the HTLC is 431 // still in flight. 432 close(resultChan) 433 return 434 } 435 436 log.Debugf("Received network result %T for attemptID=%v", n.msg, 437 attemptID) 438 439 // Extract the result and pass it to the result channel. 440 result, err := s.extractResult( 441 deobfuscator, n, attemptID, paymentHash, 442 ) 443 if err != nil { 444 e := fmt.Errorf("unable to extract result: %v", err) 445 log.Error(e) 446 resultChan <- &PaymentResult{ 447 Error: e, 448 } 449 return 450 } 451 resultChan <- result 452 }() 453 454 return resultChan, nil 455 } 456 457 // CleanStore calls the underlying result store, telling it is safe to delete 458 // all entries except the ones in the keepPids map. This should be called 459 // preiodically to let the switch clean up payment results that we have 460 // handled. 461 func (s *Switch) CleanStore(keepPids map[uint64]struct{}) error { 462 return s.networkResults.cleanStore(keepPids) 463 } 464 465 // SendHTLC is used by other subsystems which aren't belong to htlc switch 466 // package in order to send the htlc update. The attemptID used MUST be unique 467 // for this HTLC, and MUST be used only once, otherwise the switch might reject 468 // it. 469 func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, attemptID uint64, 470 htlc *lnwire.UpdateAddHTLC) error { 471 472 // Generate and send new update packet, if error will be received on 473 // this stage it means that packet haven't left boundaries of our 474 // system and something wrong happened. 475 packet := &htlcPacket{ 476 incomingChanID: hop.Source, 477 incomingHTLCID: attemptID, 478 outgoingChanID: firstHop, 479 htlc: htlc, 480 } 481 482 // Attempt to fetch the target link before creating a circuit so that 483 // we don't leave dangling circuits. The getLocalLink method does not 484 // require the circuit variable to be set on the *htlcPacket. 485 link, linkErr := s.getLocalLink(packet, htlc) 486 if linkErr != nil { 487 // Notify the htlc notifier of a link failure on our outgoing 488 // link. Incoming timelock/amount values are not set because 489 // they are not present for local sends. 490 s.cfg.HtlcNotifier.NotifyLinkFailEvent( 491 newHtlcKey(packet), 492 HtlcInfo{ 493 OutgoingTimeLock: htlc.Expiry, 494 OutgoingAmt: htlc.Amount, 495 }, 496 HtlcEventTypeSend, 497 linkErr, 498 false, 499 ) 500 501 return linkErr 502 } 503 504 // Evaluate whether this HTLC would increase our exposure to dust. If 505 // it does, don't send it out and instead return an error. 506 if s.evaluateDustThreshold(link, htlc.Amount, false) { 507 // Notify the htlc notifier of a link failure on our outgoing 508 // link. We use the FailTemporaryChannelFailure in place of a 509 // more descriptive error message. 510 linkErr := NewLinkError( 511 &lnwire.FailTemporaryChannelFailure{}, 512 ) 513 s.cfg.HtlcNotifier.NotifyLinkFailEvent( 514 newHtlcKey(packet), 515 HtlcInfo{ 516 OutgoingTimeLock: htlc.Expiry, 517 OutgoingAmt: htlc.Amount, 518 }, 519 HtlcEventTypeSend, 520 linkErr, 521 false, 522 ) 523 524 return errDustThresholdExceeded 525 } 526 527 circuit := newPaymentCircuit(&htlc.PaymentHash, packet) 528 actions, err := s.circuits.CommitCircuits(circuit) 529 if err != nil { 530 log.Errorf("unable to commit circuit in switch: %v", err) 531 return err 532 } 533 534 // Drop duplicate packet if it has already been seen. 535 switch { 536 case len(actions.Drops) == 1: 537 return ErrDuplicateAdd 538 539 case len(actions.Fails) == 1: 540 return ErrLocalAddFailed 541 } 542 543 // Send packet to link. 544 packet.circuit = circuit 545 546 return link.handleLocalAddPacket(packet) 547 } 548 549 // UpdateForwardingPolicies sends a message to the switch to update the 550 // forwarding policies for the set of target channels, keyed in chanPolicies. 551 // 552 // NOTE: This function is synchronous and will block until either the 553 // forwarding policies for all links have been updated, or the switch shuts 554 // down. 555 func (s *Switch) UpdateForwardingPolicies( 556 chanPolicies map[wire.OutPoint]ForwardingPolicy) { 557 558 log.Tracef("Updating link policies: %v", newLogClosure(func() string { 559 return spew.Sdump(chanPolicies) 560 })) 561 562 s.indexMtx.RLock() 563 564 // Update each link in chanPolicies. 565 for targetLink, policy := range chanPolicies { 566 cid := lnwire.NewChanIDFromOutPoint(&targetLink) 567 568 link, ok := s.linkIndex[cid] 569 if !ok { 570 log.Debugf("Unable to find ChannelPoint(%v) to update "+ 571 "link policy", targetLink) 572 continue 573 } 574 575 link.UpdateForwardingPolicy(policy) 576 } 577 578 s.indexMtx.RUnlock() 579 } 580 581 // IsForwardedHTLC checks for a given channel and htlc index if it is related 582 // to an opened circuit that represents a forwarded payment. 583 func (s *Switch) IsForwardedHTLC(chanID lnwire.ShortChannelID, 584 htlcIndex uint64) bool { 585 586 circuit := s.circuits.LookupOpenCircuit(channeldb.CircuitKey{ 587 ChanID: chanID, 588 HtlcID: htlcIndex, 589 }) 590 return circuit != nil && circuit.Incoming.ChanID != hop.Source 591 } 592 593 // ForwardPackets adds a list of packets to the switch for processing. Fails 594 // and settles are added on a first past, simultaneously constructing circuits 595 // for any adds. After persisting the circuits, another pass of the adds is 596 // given to forward them through the router. The sending link's quit channel is 597 // used to prevent deadlocks when the switch stops a link in the midst of 598 // forwarding. 599 func (s *Switch) ForwardPackets(linkQuit chan struct{}, 600 packets ...*htlcPacket) error { 601 602 var ( 603 // fwdChan is a buffered channel used to receive err msgs from 604 // the htlcPlex when forwarding this batch. 605 fwdChan = make(chan error, len(packets)) 606 607 // numSent keeps a running count of how many packets are 608 // forwarded to the switch, which determines how many responses 609 // we will wait for on the fwdChan.. 610 numSent int 611 ) 612 613 // No packets, nothing to do. 614 if len(packets) == 0 { 615 return nil 616 } 617 618 // Setup a barrier to prevent the background tasks from processing 619 // responses until this function returns to the user. 620 var wg sync.WaitGroup 621 wg.Add(1) 622 defer wg.Done() 623 624 // Before spawning the following goroutine to proxy our error responses, 625 // check to see if we have already been issued a shutdown request. If 626 // so, we exit early to avoid incrementing the switch's waitgroup while 627 // it is already in the process of shutting down. 628 select { 629 case <-linkQuit: 630 return nil 631 case <-s.quit: 632 return nil 633 default: 634 // Spawn a goroutine to log the errors returned from failed packets. 635 s.wg.Add(1) 636 go s.logFwdErrs(&numSent, &wg, fwdChan) 637 } 638 639 // Make a first pass over the packets, forwarding any settles or fails. 640 // As adds are found, we create a circuit and append it to our set of 641 // circuits to be written to disk. 642 var circuits []*PaymentCircuit 643 var addBatch []*htlcPacket 644 for _, packet := range packets { 645 switch htlc := packet.htlc.(type) { 646 case *lnwire.UpdateAddHTLC: 647 circuit := newPaymentCircuit(&htlc.PaymentHash, packet) 648 packet.circuit = circuit 649 circuits = append(circuits, circuit) 650 addBatch = append(addBatch, packet) 651 default: 652 err := s.routeAsync(packet, fwdChan, linkQuit) 653 if err != nil { 654 return fmt.Errorf("failed to forward packet %v", err) 655 } 656 numSent++ 657 } 658 } 659 660 // If this batch did not contain any circuits to commit, we can return 661 // early. 662 if len(circuits) == 0 { 663 return nil 664 } 665 666 // Write any circuits that we found to disk. 667 actions, err := s.circuits.CommitCircuits(circuits...) 668 if err != nil { 669 log.Errorf("unable to commit circuits in switch: %v", err) 670 } 671 672 // Split the htlc packets by comparing an in-order seek to the head of 673 // the added, dropped, or failed circuits. 674 // 675 // NOTE: This assumes each list is guaranteed to be a subsequence of the 676 // circuits, and that the union of the sets results in the original set 677 // of circuits. 678 var addedPackets, failedPackets []*htlcPacket 679 for _, packet := range addBatch { 680 switch { 681 case len(actions.Adds) > 0 && packet.circuit == actions.Adds[0]: 682 addedPackets = append(addedPackets, packet) 683 actions.Adds = actions.Adds[1:] 684 685 case len(actions.Drops) > 0 && packet.circuit == actions.Drops[0]: 686 actions.Drops = actions.Drops[1:] 687 688 case len(actions.Fails) > 0 && packet.circuit == actions.Fails[0]: 689 failedPackets = append(failedPackets, packet) 690 actions.Fails = actions.Fails[1:] 691 } 692 } 693 694 // Now, forward any packets for circuits that were successfully added to 695 // the switch's circuit map. 696 for _, packet := range addedPackets { 697 err := s.routeAsync(packet, fwdChan, linkQuit) 698 if err != nil { 699 return fmt.Errorf("failed to forward packet %v", err) 700 } 701 numSent++ 702 } 703 704 // Lastly, for any packets that failed, this implies that they were 705 // left in a half added state, which can happen when recovering from 706 // failures. 707 if len(failedPackets) > 0 { 708 var failure lnwire.FailureMessage 709 update, err := s.cfg.FetchLastChannelUpdate( 710 failedPackets[0].incomingChanID, 711 ) 712 if err != nil { 713 failure = &lnwire.FailTemporaryNodeFailure{} 714 } else { 715 failure = lnwire.NewTemporaryChannelFailure(update) 716 } 717 linkError := NewDetailedLinkError( 718 failure, OutgoingFailureIncompleteForward, 719 ) 720 721 for _, packet := range failedPackets { 722 // We don't handle the error here since this method 723 // always returns an error. 724 _ = s.failAddPacket(packet, linkError) 725 } 726 } 727 728 return nil 729 } 730 731 // logFwdErrs logs any errors received on `fwdChan` 732 func (s *Switch) logFwdErrs(num *int, wg *sync.WaitGroup, fwdChan chan error) { 733 defer s.wg.Done() 734 735 // Wait here until the outer function has finished persisting 736 // and routing the packets. This guarantees we don't read from num until 737 // the value is accurate. 738 wg.Wait() 739 740 numSent := *num 741 for i := 0; i < numSent; i++ { 742 select { 743 case err := <-fwdChan: 744 if err != nil { 745 log.Errorf("Unhandled error while reforwarding htlc "+ 746 "settle/fail over htlcswitch: %v", err) 747 } 748 case <-s.quit: 749 log.Errorf("unable to forward htlc packet " + 750 "htlc switch was stopped") 751 return 752 } 753 } 754 } 755 756 // routeAsync sends a packet through the htlc switch, using the provided err 757 // chan to propagate errors back to the caller. The link's quit channel is 758 // provided so that the send can be canceled if either the link or the switch 759 // receive a shutdown requuest. This method does not wait for a response from 760 // the htlcForwarder before returning. 761 func (s *Switch) routeAsync(packet *htlcPacket, errChan chan error, 762 linkQuit chan struct{}) error { 763 764 command := &plexPacket{ 765 pkt: packet, 766 err: errChan, 767 } 768 769 select { 770 case s.htlcPlex <- command: 771 return nil 772 case <-linkQuit: 773 return ErrLinkShuttingDown 774 case <-s.quit: 775 return errors.New("htlc switch was stopped") 776 } 777 } 778 779 // getLocalLink handles the addition of a htlc for a send that originates from 780 // our node. It returns the link that the htlc should be forwarded outwards on, 781 // and a link error if the htlc cannot be forwarded. 782 func (s *Switch) getLocalLink(pkt *htlcPacket, htlc *lnwire.UpdateAddHTLC) ( 783 ChannelLink, *LinkError) { 784 785 // Try to find links by node destination. 786 s.indexMtx.RLock() 787 link, err := s.getLinkByShortID(pkt.outgoingChanID) 788 s.indexMtx.RUnlock() 789 if err != nil { 790 log.Errorf("Link %v not found", pkt.outgoingChanID) 791 return nil, NewLinkError(&lnwire.FailUnknownNextPeer{}) 792 } 793 794 if !link.EligibleToForward() { 795 log.Errorf("Link %v is not available to forward", 796 pkt.outgoingChanID) 797 798 // The update does not need to be populated as the error 799 // will be returned back to the router. 800 return nil, NewDetailedLinkError( 801 lnwire.NewTemporaryChannelFailure(nil), 802 OutgoingFailureLinkNotEligible, 803 ) 804 } 805 806 // Ensure that the htlc satisfies the outgoing channel policy. 807 currentHeight := atomic.LoadUint32(&s.bestHeight) 808 htlcErr := link.CheckHtlcTransit( 809 htlc.PaymentHash, htlc.Amount, htlc.Expiry, currentHeight, 810 ) 811 if htlcErr != nil { 812 log.Errorf("Link %v policy for local forward not "+ 813 "satisfied", pkt.outgoingChanID) 814 return nil, htlcErr 815 } 816 return link, nil 817 } 818 819 // handleLocalResponse processes a Settle or Fail responding to a 820 // locally-initiated payment. This is handled asynchronously to avoid blocking 821 // the main event loop within the switch, as these operations can require 822 // multiple db transactions. The guarantees of the circuit map are stringent 823 // enough such that we are able to tolerate reordering of these operations 824 // without side effects. The primary operations handled are: 825 // 1. Save the payment result to the pending payment store. 826 // 2. Notify subscribers about the payment result. 827 // 3. Ack settle/fail references, to avoid resending this response internally 828 // 4. Teardown the closing circuit in the circuit map 829 // 830 // NOTE: This method MUST be spawned as a goroutine. 831 func (s *Switch) handleLocalResponse(pkt *htlcPacket) { 832 defer s.wg.Done() 833 834 attemptID := pkt.incomingHTLCID 835 836 // The error reason will be unencypted in case this a local 837 // failure or a converted error. 838 unencrypted := pkt.localFailure || pkt.convertedError 839 n := &networkResult{ 840 msg: pkt.htlc, 841 unencrypted: unencrypted, 842 isResolution: pkt.isResolution, 843 } 844 845 // Store the result to the db. This will also notify subscribers about 846 // the result. 847 if err := s.networkResults.storeResult(attemptID, n); err != nil { 848 log.Errorf("Unable to complete payment for pid=%v: %v", 849 attemptID, err) 850 return 851 } 852 853 // First, we'll clean up any fwdpkg references, circuit entries, and 854 // mark in our db that the payment for this payment hash has either 855 // succeeded or failed. 856 // 857 // If this response is contained in a forwarding package, we'll start by 858 // acking the settle/fail so that we don't continue to retransmit the 859 // HTLC internally. 860 if pkt.destRef != nil { 861 if err := s.ackSettleFail(*pkt.destRef); err != nil { 862 log.Warnf("Unable to ack settle/fail reference: %s: %v", 863 *pkt.destRef, err) 864 return 865 } 866 } 867 868 // Next, we'll remove the circuit since we are about to complete an 869 // fulfill/fail of this HTLC. Since we've already removed the 870 // settle/fail fwdpkg reference, the response from the peer cannot be 871 // replayed internally if this step fails. If this happens, this logic 872 // will be executed when a provided resolution message comes through. 873 // This can only happen if the circuit is still open, which is why this 874 // ordering is chosen. 875 if err := s.teardownCircuit(pkt); err != nil { 876 log.Warnf("Unable to teardown circuit %s: %v", 877 pkt.inKey(), err) 878 return 879 } 880 881 // Finally, notify on the htlc failure or success that has been handled. 882 key := newHtlcKey(pkt) 883 eventType := getEventType(pkt) 884 885 switch htlc := pkt.htlc.(type) { 886 case *lnwire.UpdateFulfillHTLC: 887 s.cfg.HtlcNotifier.NotifySettleEvent(key, htlc.PaymentPreimage, 888 eventType) 889 890 case *lnwire.UpdateFailHTLC: 891 s.cfg.HtlcNotifier.NotifyForwardingFailEvent(key, eventType) 892 } 893 } 894 895 // extractResult uses the given deobfuscator to extract the payment result from 896 // the given network message. 897 func (s *Switch) extractResult(deobfuscator ErrorDecrypter, n *networkResult, 898 attemptID uint64, paymentHash lntypes.Hash) (*PaymentResult, error) { 899 900 switch htlc := n.msg.(type) { 901 902 // We've received a settle update which means we can finalize the user 903 // payment and return successful response. 904 case *lnwire.UpdateFulfillHTLC: 905 return &PaymentResult{ 906 Preimage: htlc.PaymentPreimage, 907 }, nil 908 909 // We've received a fail update which means we can finalize the 910 // user payment and return fail response. 911 case *lnwire.UpdateFailHTLC: 912 paymentErr := s.parseFailedPayment( 913 deobfuscator, attemptID, paymentHash, n.unencrypted, 914 n.isResolution, htlc, 915 ) 916 917 return &PaymentResult{ 918 Error: paymentErr, 919 }, nil 920 921 default: 922 return nil, fmt.Errorf("received unknown response type: %T", 923 htlc) 924 } 925 } 926 927 // parseFailedPayment determines the appropriate failure message to return to 928 // a user initiated payment. The three cases handled are: 929 // 1. An unencrypted failure, which should already plaintext. 930 // 2. A resolution from the chain arbitrator, which possibly has no failure 931 // reason attached. 932 // 3. A failure from the remote party, which will need to be decrypted using 933 // the payment deobfuscator. 934 func (s *Switch) parseFailedPayment(deobfuscator ErrorDecrypter, 935 attemptID uint64, paymentHash lntypes.Hash, unencrypted, 936 isResolution bool, htlc *lnwire.UpdateFailHTLC) error { 937 938 switch { 939 940 // The payment never cleared the link, so we don't need to 941 // decrypt the error, simply decode it them report back to the 942 // user. 943 case unencrypted: 944 r := bytes.NewReader(htlc.Reason) 945 failureMsg, err := lnwire.DecodeFailure(r, 0) 946 if err != nil { 947 // If we could not decode the failure reason, return a link 948 // error indicating that we failed to decode the onion. 949 linkError := NewDetailedLinkError( 950 // As this didn't even clear the link, we don't 951 // need to apply an update here since it goes 952 // directly to the router. 953 lnwire.NewTemporaryChannelFailure(nil), 954 OutgoingFailureDecodeError, 955 ) 956 957 log.Errorf("%v: (hash=%v, pid=%d): %v", 958 linkError.FailureDetail.FailureString(), 959 paymentHash, attemptID, err) 960 961 return linkError 962 } 963 964 // If we successfully decoded the failure reason, return it. 965 return NewLinkError(failureMsg) 966 967 // A payment had to be timed out on chain before it got past 968 // the first hop. In this case, we'll report a permanent 969 // channel failure as this means us, or the remote party had to 970 // go on chain. 971 case isResolution && htlc.Reason == nil: 972 linkError := NewDetailedLinkError( 973 &lnwire.FailPermanentChannelFailure{}, 974 OutgoingFailureOnChainTimeout, 975 ) 976 977 log.Infof("%v: hash=%v, pid=%d", 978 linkError.FailureDetail.FailureString(), 979 paymentHash, attemptID) 980 981 return linkError 982 983 // A regular multi-hop payment error that we'll need to 984 // decrypt. 985 default: 986 // We'll attempt to fully decrypt the onion encrypted 987 // error. If we're unable to then we'll bail early. 988 failure, err := deobfuscator.DecryptError(htlc.Reason) 989 if err != nil { 990 log.Errorf("unable to de-obfuscate onion failure "+ 991 "(hash=%v, pid=%d): %v", 992 paymentHash, attemptID, err) 993 994 return ErrUnreadableFailureMessage 995 } 996 997 return failure 998 } 999 } 1000 1001 // handlePacketForward is used in cases when we need forward the htlc update 1002 // from one channel link to another and be able to propagate the settle/fail 1003 // updates back. This behaviour is achieved by creation of payment circuits. 1004 func (s *Switch) handlePacketForward(packet *htlcPacket) error { 1005 switch htlc := packet.htlc.(type) { 1006 1007 // Channel link forwarded us a new htlc, therefore we initiate the 1008 // payment circuit within our internal state so we can properly forward 1009 // the ultimate settle message back latter. 1010 case *lnwire.UpdateAddHTLC: 1011 // Check if the node is set to reject all onward HTLCs and also make 1012 // sure that HTLC is not from the source node. 1013 if s.cfg.RejectHTLC { 1014 failure := NewDetailedLinkError( 1015 &lnwire.FailChannelDisabled{}, 1016 OutgoingFailureForwardsDisabled, 1017 ) 1018 1019 return s.failAddPacket(packet, failure) 1020 } 1021 1022 // Before we attempt to find a non-strict forwarding path for 1023 // this htlc, check whether the htlc is being routed over the 1024 // same incoming and outgoing channel. If our node does not 1025 // allow forwards of this nature, we fail the htlc early. This 1026 // check is in place to disallow inefficiently routed htlcs from 1027 // locking up our balance. 1028 linkErr := checkCircularForward( 1029 packet.incomingChanID, packet.outgoingChanID, 1030 s.cfg.AllowCircularRoute, htlc.PaymentHash, 1031 ) 1032 if linkErr != nil { 1033 return s.failAddPacket(packet, linkErr) 1034 } 1035 1036 s.indexMtx.RLock() 1037 targetLink, err := s.getLinkByShortID(packet.outgoingChanID) 1038 if err != nil { 1039 s.indexMtx.RUnlock() 1040 1041 log.Debugf("unable to find link with "+ 1042 "destination %v", packet.outgoingChanID) 1043 1044 // If packet was forwarded from another channel link 1045 // than we should notify this link that some error 1046 // occurred. 1047 linkError := NewLinkError( 1048 &lnwire.FailUnknownNextPeer{}, 1049 ) 1050 1051 return s.failAddPacket(packet, linkError) 1052 } 1053 targetPeerKey := targetLink.Peer().PubKey() 1054 interfaceLinks, _ := s.getLinks(targetPeerKey) 1055 s.indexMtx.RUnlock() 1056 1057 // We'll keep track of any HTLC failures during the link 1058 // selection process. This way we can return the error for 1059 // precise link that the sender selected, while optimistically 1060 // trying all links to utilize our available bandwidth. 1061 linkErrs := make(map[lnwire.ShortChannelID]*LinkError) 1062 1063 // Find all destination channel links with appropriate 1064 // bandwidth. 1065 var destinations []ChannelLink 1066 for _, link := range interfaceLinks { 1067 var failure *LinkError 1068 1069 // We'll skip any links that aren't yet eligible for 1070 // forwarding. 1071 if !link.EligibleToForward() { 1072 failure = NewDetailedLinkError( 1073 &lnwire.FailUnknownNextPeer{}, 1074 OutgoingFailureLinkNotEligible, 1075 ) 1076 } else { 1077 // We'll ensure that the HTLC satisfies the 1078 // current forwarding conditions of this target 1079 // link. 1080 currentHeight := atomic.LoadUint32(&s.bestHeight) 1081 failure = link.CheckHtlcForward( 1082 htlc.PaymentHash, packet.incomingAmount, 1083 packet.amount, packet.incomingTimeout, 1084 packet.outgoingTimeout, currentHeight, 1085 ) 1086 } 1087 1088 // If this link can forward the htlc, add it to the set 1089 // of destinations. 1090 if failure == nil { 1091 destinations = append(destinations, link) 1092 continue 1093 } 1094 1095 linkErrs[link.ShortChanID()] = failure 1096 } 1097 1098 // If we had a forwarding failure due to the HTLC not 1099 // satisfying the current policy, then we'll send back an 1100 // error, but ensure we send back the error sourced at the 1101 // *target* link. 1102 if len(destinations) == 0 { 1103 // At this point, some or all of the links rejected the 1104 // HTLC so we couldn't forward it. So we'll try to look 1105 // up the error that came from the source. 1106 linkErr, ok := linkErrs[packet.outgoingChanID] 1107 if !ok { 1108 // If we can't find the error of the source, 1109 // then we'll return an unknown next peer, 1110 // though this should never happen. 1111 linkErr = NewLinkError( 1112 &lnwire.FailUnknownNextPeer{}, 1113 ) 1114 log.Warnf("unable to find err source for "+ 1115 "outgoing_link=%v, errors=%v", 1116 packet.outgoingChanID, newLogClosure(func() string { 1117 return spew.Sdump(linkErrs) 1118 })) 1119 } 1120 1121 log.Tracef("incoming HTLC(%x) violated "+ 1122 "target outgoing link (id=%v) policy: %v", 1123 htlc.PaymentHash[:], packet.outgoingChanID, 1124 linkErr) 1125 1126 return s.failAddPacket(packet, linkErr) 1127 } 1128 1129 // Choose a random link out of the set of links that can forward 1130 // this htlc. The reason for randomization is to evenly 1131 // distribute the htlc load without making assumptions about 1132 // what the best channel is. 1133 destination := destinations[rand.Intn(len(destinations))] 1134 1135 // Retrieve the incoming link by its ShortChannelID. Note that 1136 // the incomingChanID is never set to hop.Source here. 1137 s.indexMtx.RLock() 1138 incomingLink, err := s.getLinkByShortID(packet.incomingChanID) 1139 s.indexMtx.RUnlock() 1140 if err != nil { 1141 // If we couldn't find the incoming link, we can't 1142 // evaluate the incoming's exposure to dust, so we just 1143 // fail the HTLC back. 1144 linkErr := NewLinkError( 1145 &lnwire.FailTemporaryChannelFailure{}, 1146 ) 1147 1148 return s.failAddPacket(packet, linkErr) 1149 } 1150 1151 // Evaluate whether this HTLC would increase our exposure to 1152 // dust on the incoming link. If it does, fail it backwards. 1153 if s.evaluateDustThreshold( 1154 incomingLink, packet.incomingAmount, true, 1155 ) { 1156 // The incoming dust exceeds the threshold, so we fail 1157 // the add back. 1158 linkErr := NewLinkError( 1159 &lnwire.FailTemporaryChannelFailure{}, 1160 ) 1161 1162 return s.failAddPacket(packet, linkErr) 1163 } 1164 1165 // Also evaluate whether this HTLC would increase our exposure 1166 // to dust on the destination link. If it does, fail it back. 1167 if s.evaluateDustThreshold( 1168 destination, packet.amount, false, 1169 ) { 1170 // The outgoing dust exceeds the threshold, so we fail 1171 // the add back. 1172 linkErr := NewLinkError( 1173 &lnwire.FailTemporaryChannelFailure{}, 1174 ) 1175 1176 return s.failAddPacket(packet, linkErr) 1177 } 1178 1179 // Send the packet to the destination channel link which 1180 // manages the channel. 1181 packet.outgoingChanID = destination.ShortChanID() 1182 return destination.handleSwitchPacket(packet) 1183 1184 case *lnwire.UpdateFailHTLC, *lnwire.UpdateFulfillHTLC: 1185 // If the source of this packet has not been set, use the 1186 // circuit map to lookup the origin. 1187 circuit, err := s.closeCircuit(packet) 1188 if err != nil { 1189 return err 1190 } 1191 1192 // closeCircuit returns a nil circuit when a settle packet returns an 1193 // ErrUnknownCircuit error upon the inner call to CloseCircuit. 1194 if circuit == nil { 1195 return nil 1196 } 1197 1198 fail, isFail := htlc.(*lnwire.UpdateFailHTLC) 1199 if isFail && !packet.hasSource { 1200 switch { 1201 // No message to encrypt, locally sourced payment. 1202 case circuit.ErrorEncrypter == nil: 1203 1204 // If this is a resolution message, then we'll need to 1205 // encrypt it as it's actually internally sourced. 1206 case packet.isResolution: 1207 var err error 1208 // TODO(roasbeef): don't need to pass actually? 1209 failure := &lnwire.FailPermanentChannelFailure{} 1210 fail.Reason, err = circuit.ErrorEncrypter.EncryptFirstHop( 1211 failure, 1212 ) 1213 if err != nil { 1214 err = fmt.Errorf("unable to obfuscate "+ 1215 "error: %v", err) 1216 log.Error(err) 1217 } 1218 1219 // Alternatively, if the remote party send us an 1220 // UpdateFailMalformedHTLC, then we'll need to convert 1221 // this into a proper well formatted onion error as 1222 // there's no HMAC currently. 1223 case packet.convertedError: 1224 log.Infof("Converting malformed HTLC error "+ 1225 "for circuit for Circuit(%x: "+ 1226 "(%s, %d) <-> (%s, %d))", packet.circuit.PaymentHash, 1227 packet.incomingChanID, packet.incomingHTLCID, 1228 packet.outgoingChanID, packet.outgoingHTLCID) 1229 1230 fail.Reason = circuit.ErrorEncrypter.EncryptMalformedError( 1231 fail.Reason, 1232 ) 1233 1234 default: 1235 // Otherwise, it's a forwarded error, so we'll perform a 1236 // wrapper encryption as normal. 1237 fail.Reason = circuit.ErrorEncrypter.IntermediateEncrypt( 1238 fail.Reason, 1239 ) 1240 } 1241 } else if !isFail && circuit.Outgoing != nil { 1242 // If this is an HTLC settle, and it wasn't from a 1243 // locally initiated HTLC, then we'll log a forwarding 1244 // event so we can flush it to disk later. 1245 // 1246 // TODO(roasbeef): only do this once link actually 1247 // fully settles? 1248 localHTLC := packet.incomingChanID == hop.Source 1249 if !localHTLC { 1250 log.Infof("Forwarded HTLC(%x) of %v (fee: %v) "+ 1251 "from IncomingChanID(%v) to OutgoingChanID(%v)", 1252 circuit.PaymentHash[:], circuit.OutgoingAmount, 1253 circuit.IncomingAmount-circuit.OutgoingAmount, 1254 circuit.Incoming.ChanID, circuit.Outgoing.ChanID) 1255 s.fwdEventMtx.Lock() 1256 s.pendingFwdingEvents = append( 1257 s.pendingFwdingEvents, 1258 channeldb.ForwardingEvent{ 1259 Timestamp: time.Now(), 1260 IncomingChanID: circuit.Incoming.ChanID, 1261 OutgoingChanID: circuit.Outgoing.ChanID, 1262 AmtIn: circuit.IncomingAmount, 1263 AmtOut: circuit.OutgoingAmount, 1264 }, 1265 ) 1266 s.fwdEventMtx.Unlock() 1267 } 1268 } 1269 1270 // A blank IncomingChanID in a circuit indicates that it is a pending 1271 // user-initiated payment. 1272 if packet.incomingChanID == hop.Source { 1273 s.wg.Add(1) 1274 go s.handleLocalResponse(packet) 1275 return nil 1276 } 1277 1278 // Check to see that the source link is online before removing 1279 // the circuit. 1280 return s.mailOrchestrator.Deliver(packet.incomingChanID, packet) 1281 1282 default: 1283 return errors.New("wrong update type") 1284 } 1285 } 1286 1287 // checkCircularForward checks whether a forward is circular (arrives and 1288 // departs on the same link) and returns a link error if the switch is 1289 // configured to disallow this behaviour. 1290 func checkCircularForward(incoming, outgoing lnwire.ShortChannelID, 1291 allowCircular bool, paymentHash lntypes.Hash) *LinkError { 1292 1293 // If the route is not circular we do not need to perform any further 1294 // checks. 1295 if incoming != outgoing { 1296 return nil 1297 } 1298 1299 // If the incoming and outgoing link are equal, the htlc is part of a 1300 // circular route which may be used to lock up our liquidity. If the 1301 // switch is configured to allow circular routes, log that we are 1302 // allowing the route then return nil. 1303 if allowCircular { 1304 log.Debugf("allowing circular route over link: %v "+ 1305 "(payment hash: %x)", incoming, paymentHash) 1306 return nil 1307 } 1308 1309 // If our node disallows circular routes, return a temporary channel 1310 // failure. There is nothing wrong with the policy used by the remote 1311 // node, so we do not include a channel update. 1312 return NewDetailedLinkError( 1313 lnwire.NewTemporaryChannelFailure(nil), 1314 OutgoingFailureCircularRoute, 1315 ) 1316 } 1317 1318 // failAddPacket encrypts a fail packet back to an add packet's source. 1319 // The ciphertext will be derived from the failure message proivded by context. 1320 // This method returns the failErr if all other steps complete successfully. 1321 func (s *Switch) failAddPacket(packet *htlcPacket, failure *LinkError) error { 1322 // Encrypt the failure so that the sender will be able to read the error 1323 // message. Since we failed this packet, we use EncryptFirstHop to 1324 // obfuscate the failure for their eyes only. 1325 reason, err := packet.obfuscator.EncryptFirstHop(failure.WireMessage()) 1326 if err != nil { 1327 err := fmt.Errorf("unable to obfuscate "+ 1328 "error: %v", err) 1329 log.Error(err) 1330 return err 1331 } 1332 1333 log.Error(failure.Error()) 1334 1335 // Create a failure packet for this htlc. The the full set of 1336 // information about the htlc failure is included so that they can 1337 // be included in link failure notifications. 1338 failPkt := &htlcPacket{ 1339 sourceRef: packet.sourceRef, 1340 incomingChanID: packet.incomingChanID, 1341 incomingHTLCID: packet.incomingHTLCID, 1342 outgoingChanID: packet.outgoingChanID, 1343 outgoingHTLCID: packet.outgoingHTLCID, 1344 incomingAmount: packet.incomingAmount, 1345 amount: packet.amount, 1346 incomingTimeout: packet.incomingTimeout, 1347 outgoingTimeout: packet.outgoingTimeout, 1348 circuit: packet.circuit, 1349 linkFailure: failure, 1350 htlc: &lnwire.UpdateFailHTLC{ 1351 Reason: reason, 1352 }, 1353 } 1354 1355 // Route a fail packet back to the source link. 1356 err = s.mailOrchestrator.Deliver(failPkt.incomingChanID, failPkt) 1357 if err != nil { 1358 err = fmt.Errorf("source chanid=%v unable to "+ 1359 "handle switch packet: %v", 1360 packet.incomingChanID, err) 1361 log.Error(err) 1362 return err 1363 } 1364 1365 return failure 1366 } 1367 1368 // closeCircuit accepts a settle or fail htlc and the associated htlc packet and 1369 // attempts to determine the source that forwarded this htlc. This method will 1370 // set the incoming chan and htlc ID of the given packet if the source was 1371 // found, and will properly [re]encrypt any failure messages. 1372 func (s *Switch) closeCircuit(pkt *htlcPacket) (*PaymentCircuit, error) { 1373 // If the packet has its source, that means it was failed locally by 1374 // the outgoing link. We fail it here to make sure only one response 1375 // makes it through the switch. 1376 if pkt.hasSource { 1377 circuit, err := s.circuits.FailCircuit(pkt.inKey()) 1378 switch err { 1379 1380 // Circuit successfully closed. 1381 case nil: 1382 return circuit, nil 1383 1384 // Circuit was previously closed, but has not been deleted. 1385 // We'll just drop this response until the circuit has been 1386 // fully removed. 1387 case ErrCircuitClosing: 1388 return nil, err 1389 1390 // Failed to close circuit because it does not exist. This is 1391 // likely because the circuit was already successfully closed. 1392 // Since this packet failed locally, there is no forwarding 1393 // package entry to acknowledge. 1394 case ErrUnknownCircuit: 1395 return nil, err 1396 1397 // Unexpected error. 1398 default: 1399 return nil, err 1400 } 1401 } 1402 1403 // Otherwise, this is packet was received from the remote party. Use 1404 // circuit map to find the incoming link to receive the settle/fail. 1405 circuit, err := s.circuits.CloseCircuit(pkt.outKey()) 1406 switch err { 1407 1408 // Open circuit successfully closed. 1409 case nil: 1410 pkt.incomingChanID = circuit.Incoming.ChanID 1411 pkt.incomingHTLCID = circuit.Incoming.HtlcID 1412 pkt.circuit = circuit 1413 pkt.sourceRef = &circuit.AddRef 1414 1415 pktType := "SETTLE" 1416 if _, ok := pkt.htlc.(*lnwire.UpdateFailHTLC); ok { 1417 pktType = "FAIL" 1418 } 1419 1420 log.Debugf("Closed completed %s circuit for %x: "+ 1421 "(%s, %d) <-> (%s, %d)", pktType, pkt.circuit.PaymentHash, 1422 pkt.incomingChanID, pkt.incomingHTLCID, 1423 pkt.outgoingChanID, pkt.outgoingHTLCID) 1424 1425 return circuit, nil 1426 1427 // Circuit was previously closed, but has not been deleted. We'll just 1428 // drop this response until the circuit has been removed. 1429 case ErrCircuitClosing: 1430 return nil, err 1431 1432 // Failed to close circuit because it does not exist. This is likely 1433 // because the circuit was already successfully closed. 1434 case ErrUnknownCircuit: 1435 if pkt.destRef != nil { 1436 // Add this SettleFailRef to the set of pending settle/fail entries 1437 // awaiting acknowledgement. 1438 s.pendingSettleFails = append(s.pendingSettleFails, *pkt.destRef) 1439 } 1440 1441 // If this is a settle, we will not log an error message as settles 1442 // are expected to hit the ErrUnknownCircuit case. The only way fails 1443 // can hit this case if the link restarts after having just sent a fail 1444 // to the switch. 1445 _, isSettle := pkt.htlc.(*lnwire.UpdateFulfillHTLC) 1446 if !isSettle { 1447 err := fmt.Errorf("unable to find target channel "+ 1448 "for HTLC fail: channel ID = %s, "+ 1449 "HTLC ID = %d", pkt.outgoingChanID, 1450 pkt.outgoingHTLCID) 1451 log.Error(err) 1452 1453 return nil, err 1454 } 1455 1456 return nil, nil 1457 1458 // Unexpected error. 1459 default: 1460 return nil, err 1461 } 1462 } 1463 1464 // ackSettleFail is used by the switch to ACK any settle/fail entries in the 1465 // forwarding package of the outgoing link for a payment circuit. We do this if 1466 // we're the originator of the payment, so the link stops attempting to 1467 // re-broadcast. 1468 func (s *Switch) ackSettleFail(settleFailRefs ...channeldb.SettleFailRef) error { 1469 return kvdb.Batch(s.cfg.DB, func(tx kvdb.RwTx) error { 1470 return s.cfg.SwitchPackager.AckSettleFails(tx, settleFailRefs...) 1471 }) 1472 } 1473 1474 // teardownCircuit removes a pending or open circuit from the switch's circuit 1475 // map and prints useful logging statements regarding the outcome. 1476 func (s *Switch) teardownCircuit(pkt *htlcPacket) error { 1477 var pktType string 1478 switch htlc := pkt.htlc.(type) { 1479 case *lnwire.UpdateFulfillHTLC: 1480 pktType = "SETTLE" 1481 case *lnwire.UpdateFailHTLC: 1482 pktType = "FAIL" 1483 default: 1484 err := fmt.Errorf("cannot tear down packet of type: %T", htlc) 1485 log.Errorf(err.Error()) 1486 return err 1487 } 1488 1489 switch { 1490 case pkt.circuit.HasKeystone(): 1491 log.Debugf("Tearing down open circuit with %s pkt, removing circuit=%v "+ 1492 "with keystone=%v", pktType, pkt.inKey(), pkt.outKey()) 1493 1494 err := s.circuits.DeleteCircuits(pkt.inKey()) 1495 if err != nil { 1496 log.Warnf("Failed to tear down open circuit (%s, %d) <-> (%s, %d) "+ 1497 "with payment_hash-%v using %s pkt", 1498 pkt.incomingChanID, pkt.incomingHTLCID, 1499 pkt.outgoingChanID, pkt.outgoingHTLCID, 1500 pkt.circuit.PaymentHash, pktType) 1501 return err 1502 } 1503 1504 log.Debugf("Closed completed %s circuit for %x: "+ 1505 "(%s, %d) <-> (%s, %d)", pktType, pkt.circuit.PaymentHash, 1506 pkt.incomingChanID, pkt.incomingHTLCID, 1507 pkt.outgoingChanID, pkt.outgoingHTLCID) 1508 1509 default: 1510 log.Debugf("Tearing down incomplete circuit with %s for inkey=%v", 1511 pktType, pkt.inKey()) 1512 1513 err := s.circuits.DeleteCircuits(pkt.inKey()) 1514 if err != nil { 1515 log.Warnf("Failed to tear down pending %s circuit for %x: "+ 1516 "(%s, %d)", pktType, pkt.circuit.PaymentHash, 1517 pkt.incomingChanID, pkt.incomingHTLCID) 1518 return err 1519 } 1520 1521 log.Debugf("Removed pending onion circuit for %x: "+ 1522 "(%s, %d)", pkt.circuit.PaymentHash, 1523 pkt.incomingChanID, pkt.incomingHTLCID) 1524 } 1525 1526 return nil 1527 } 1528 1529 // CloseLink creates and sends the close channel command to the target link 1530 // directing the specified closure type. If the closure type is CloseRegular, 1531 // targetFeePerKB parameter should be the ideal fee-per-kb that will be used as 1532 // a starting point for close negotiation. The deliveryScript parameter is an 1533 // optional parameter which sets a user specified script to close out to. 1534 func (s *Switch) CloseLink(chanPoint *wire.OutPoint, 1535 closeType contractcourt.ChannelCloseType, targetFeePerKB chainfee.AtomPerKByte, 1536 deliveryScript lnwire.DeliveryAddress) (chan interface{}, chan error) { 1537 1538 // TODO(roasbeef) abstract out the close updates. 1539 updateChan := make(chan interface{}, 2) 1540 errChan := make(chan error, 1) 1541 1542 command := &ChanClose{ 1543 CloseType: closeType, 1544 ChanPoint: chanPoint, 1545 Updates: updateChan, 1546 TargetFeePerKB: targetFeePerKB, 1547 DeliveryScript: deliveryScript, 1548 Err: errChan, 1549 } 1550 1551 select { 1552 case s.chanCloseRequests <- command: 1553 return updateChan, errChan 1554 1555 case <-s.quit: 1556 errChan <- ErrSwitchExiting 1557 close(updateChan) 1558 return updateChan, errChan 1559 } 1560 } 1561 1562 // htlcForwarder is responsible for optimally forwarding (and possibly 1563 // fragmenting) incoming/outgoing HTLCs amongst all active interfaces and their 1564 // links. The duties of the forwarder are similar to that of a network switch, 1565 // in that it facilitates multi-hop payments by acting as a central messaging 1566 // bus. The switch communicates will active links to create, manage, and tear 1567 // down active onion routed payments. Each active channel is modeled as 1568 // networked device with metadata such as the available payment bandwidth, and 1569 // total link capacity. 1570 // 1571 // NOTE: This MUST be run as a goroutine. 1572 func (s *Switch) htlcForwarder() { 1573 defer s.wg.Done() 1574 1575 defer func() { 1576 s.blockEpochStream.Cancel() 1577 1578 // Remove all links once we've been signalled for shutdown. 1579 var linksToStop []ChannelLink 1580 s.indexMtx.Lock() 1581 for _, link := range s.linkIndex { 1582 activeLink := s.removeLink(link.ChanID()) 1583 if activeLink == nil { 1584 log.Errorf("unable to remove ChannelLink(%v) "+ 1585 "on stop", link.ChanID()) 1586 continue 1587 } 1588 linksToStop = append(linksToStop, activeLink) 1589 } 1590 for _, link := range s.pendingLinkIndex { 1591 pendingLink := s.removeLink(link.ChanID()) 1592 if pendingLink == nil { 1593 log.Errorf("unable to remove ChannelLink(%v) "+ 1594 "on stop", link.ChanID()) 1595 continue 1596 } 1597 linksToStop = append(linksToStop, pendingLink) 1598 } 1599 s.indexMtx.Unlock() 1600 1601 // Now that all pending and live links have been removed from 1602 // the forwarding indexes, stop each one before shutting down. 1603 // We'll shut them down in parallel to make exiting as fast as 1604 // possible. 1605 var wg sync.WaitGroup 1606 for _, link := range linksToStop { 1607 wg.Add(1) 1608 go func(l ChannelLink) { 1609 defer wg.Done() 1610 l.Stop() 1611 }(link) 1612 } 1613 wg.Wait() 1614 1615 // Before we exit fully, we'll attempt to flush out any 1616 // forwarding events that may still be lingering since the last 1617 // batch flush. 1618 if err := s.FlushForwardingEvents(); err != nil { 1619 log.Errorf("unable to flush forwarding events: %v", err) 1620 } 1621 }() 1622 1623 // TODO(roasbeef): cleared vs settled distinction 1624 var ( 1625 totalNumUpdates uint64 1626 totalAtomsSent dcrutil.Amount 1627 totalAtomsRecv dcrutil.Amount 1628 ) 1629 s.cfg.LogEventTicker.Resume() 1630 defer s.cfg.LogEventTicker.Stop() 1631 1632 // Every 15 seconds, we'll flush out the forwarding events that 1633 // occurred during that period. 1634 s.cfg.FwdEventTicker.Resume() 1635 defer s.cfg.FwdEventTicker.Stop() 1636 1637 defer s.cfg.AckEventTicker.Stop() 1638 1639 out: 1640 for { 1641 1642 // If the set of pending settle/fail entries is non-zero, 1643 // reinstate the ack ticker so we can batch ack them. 1644 if len(s.pendingSettleFails) > 0 { 1645 s.cfg.AckEventTicker.Resume() 1646 } 1647 1648 select { 1649 case blockEpoch, ok := <-s.blockEpochStream.Epochs: 1650 if !ok { 1651 break out 1652 } 1653 1654 atomic.StoreUint32(&s.bestHeight, uint32(blockEpoch.Height)) 1655 1656 // A local close request has arrived, we'll forward this to the 1657 // relevant link (if it exists) so the channel can be 1658 // cooperatively closed (if possible). 1659 case req := <-s.chanCloseRequests: 1660 chanID := lnwire.NewChanIDFromOutPoint(req.ChanPoint) 1661 1662 s.indexMtx.RLock() 1663 link, ok := s.linkIndex[chanID] 1664 if !ok { 1665 s.indexMtx.RUnlock() 1666 1667 req.Err <- fmt.Errorf("no peer for channel with "+ 1668 "chan_id=%x", chanID[:]) 1669 continue 1670 } 1671 s.indexMtx.RUnlock() 1672 1673 peerPub := link.Peer().PubKey() 1674 log.Debugf("Requesting local channel close: peer=%v, "+ 1675 "chan_id=%x", link.Peer(), chanID[:]) 1676 1677 go s.cfg.LocalChannelClose(peerPub[:], req) 1678 1679 case resolutionMsg := <-s.resolutionMsgs: 1680 pkt := &htlcPacket{ 1681 outgoingChanID: resolutionMsg.SourceChan, 1682 outgoingHTLCID: resolutionMsg.HtlcIndex, 1683 isResolution: true, 1684 } 1685 1686 // Resolution messages will either be cancelling 1687 // backwards an existing HTLC, or settling a previously 1688 // outgoing HTLC. Based on this, we'll map the message 1689 // to the proper htlcPacket. 1690 if resolutionMsg.Failure != nil { 1691 pkt.htlc = &lnwire.UpdateFailHTLC{} 1692 } else { 1693 pkt.htlc = &lnwire.UpdateFulfillHTLC{ 1694 PaymentPreimage: *resolutionMsg.PreImage, 1695 } 1696 } 1697 1698 log.Infof("Received outside contract resolution, "+ 1699 "mapping to: %v", spew.Sdump(pkt)) 1700 1701 // We don't check the error, as the only failure we can 1702 // encounter is due to the circuit already being 1703 // closed. This is fine, as processing this message is 1704 // meant to be idempotent. 1705 err := s.handlePacketForward(pkt) 1706 if err != nil { 1707 log.Errorf("Unable to forward resolution msg: %v", err) 1708 } 1709 1710 // With the message processed, we'll now close out 1711 close(resolutionMsg.doneChan) 1712 1713 // A new packet has arrived for forwarding, we'll interpret the 1714 // packet concretely, then either forward it along, or 1715 // interpret a return packet to a locally initialized one. 1716 case cmd := <-s.htlcPlex: 1717 cmd.err <- s.handlePacketForward(cmd.pkt) 1718 1719 // When this time ticks, then it indicates that we should 1720 // collect all the forwarding events since the last internal, 1721 // and write them out to our log. 1722 case <-s.cfg.FwdEventTicker.Ticks(): 1723 s.wg.Add(1) 1724 go func() { 1725 defer s.wg.Done() 1726 1727 if err := s.FlushForwardingEvents(); err != nil { 1728 log.Errorf("unable to flush "+ 1729 "forwarding events: %v", err) 1730 } 1731 }() 1732 1733 // The log ticker has fired, so we'll calculate some forwarding 1734 // stats for the last 10 seconds to display within the logs to 1735 // users. 1736 case <-s.cfg.LogEventTicker.Ticks(): 1737 // First, we'll collate the current running tally of 1738 // our forwarding stats. 1739 prevAtomsSent := totalAtomsSent 1740 prevAtomsRecv := totalAtomsRecv 1741 prevNumUpdates := totalNumUpdates 1742 1743 var ( 1744 newNumUpdates uint64 1745 newAtomsSent dcrutil.Amount 1746 newAtomsRecv dcrutil.Amount 1747 ) 1748 1749 // Next, we'll run through all the registered links and 1750 // compute their up-to-date forwarding stats. 1751 s.indexMtx.RLock() 1752 for _, link := range s.linkIndex { 1753 // TODO(roasbeef): when links first registered 1754 // stats printed. 1755 updates, sent, recv := link.Stats() 1756 newNumUpdates += updates 1757 newAtomsSent += sent.ToAtoms() 1758 newAtomsRecv += recv.ToAtoms() 1759 } 1760 s.indexMtx.RUnlock() 1761 1762 var ( 1763 diffNumUpdates uint64 1764 diffAtomsSent dcrutil.Amount 1765 diffAtomsRecv dcrutil.Amount 1766 ) 1767 1768 // If this is the first time we're computing these 1769 // stats, then the diff is just the new value. We do 1770 // this in order to avoid integer underflow issues. 1771 if prevNumUpdates == 0 { 1772 diffNumUpdates = newNumUpdates 1773 diffAtomsSent = newAtomsSent 1774 diffAtomsRecv = newAtomsRecv 1775 } else { 1776 diffNumUpdates = newNumUpdates - prevNumUpdates 1777 diffAtomsSent = newAtomsSent - prevAtomsSent 1778 diffAtomsRecv = newAtomsRecv - prevAtomsRecv 1779 } 1780 1781 // If the diff of num updates is zero, then we haven't 1782 // forwarded anything in the last 10 seconds, so we can 1783 // skip this update. 1784 if diffNumUpdates == 0 { 1785 continue 1786 } 1787 1788 // If the diff of num updates is negative, then some 1789 // links may have been unregistered from the switch, so 1790 // we'll update our stats to only include our registered 1791 // links. 1792 if int64(diffNumUpdates) < 0 { 1793 totalNumUpdates = newNumUpdates 1794 totalAtomsSent = newAtomsSent 1795 totalAtomsRecv = newAtomsRecv 1796 continue 1797 } 1798 1799 // Otherwise, we'll log this diff, then accumulate the 1800 // new stats into the running total. 1801 log.Debugf("Sent %d atoms and received %d atoms "+ 1802 "in the last 10 seconds (%f tx/sec)", 1803 diffAtomsSent, diffAtomsRecv, 1804 float64(diffNumUpdates)/10) 1805 1806 totalNumUpdates += diffNumUpdates 1807 totalAtomsSent += diffAtomsSent 1808 totalAtomsRecv += diffAtomsRecv 1809 1810 // The ack ticker has fired so if we have any settle/fail entries 1811 // for a forwarding package to ack, we will do so here in a batch 1812 // db call. 1813 case <-s.cfg.AckEventTicker.Ticks(): 1814 // If the current set is empty, pause the ticker. 1815 if len(s.pendingSettleFails) == 0 { 1816 s.cfg.AckEventTicker.Pause() 1817 continue 1818 } 1819 1820 // Batch ack the settle/fail entries. 1821 if err := s.ackSettleFail(s.pendingSettleFails...); err != nil { 1822 log.Errorf("Unable to ack batch of settle/fails: %v", err) 1823 continue 1824 } 1825 1826 log.Tracef("Acked %d settle fails: %v", len(s.pendingSettleFails), 1827 newLogClosure(func() string { 1828 return spew.Sdump(s.pendingSettleFails) 1829 })) 1830 1831 // Reset the pendingSettleFails buffer while keeping acquired 1832 // memory. 1833 s.pendingSettleFails = s.pendingSettleFails[:0] 1834 1835 case <-s.quit: 1836 return 1837 } 1838 } 1839 } 1840 1841 // Start starts all helper goroutines required for the operation of the switch. 1842 func (s *Switch) Start() error { 1843 if !atomic.CompareAndSwapInt32(&s.started, 0, 1) { 1844 log.Warn("Htlc Switch already started") 1845 return errors.New("htlc switch already started") 1846 } 1847 1848 log.Infof("Starting HTLC Switch") 1849 1850 blockEpochStream, err := s.cfg.Notifier.RegisterBlockEpochNtfn(nil) 1851 if err != nil { 1852 return err 1853 } 1854 s.blockEpochStream = blockEpochStream 1855 1856 s.wg.Add(1) 1857 go s.htlcForwarder() 1858 1859 if err := s.reforwardResponses(); err != nil { 1860 s.Stop() 1861 log.Errorf("unable to reforward responses: %v", err) 1862 return err 1863 } 1864 1865 return nil 1866 } 1867 1868 // reforwardResponses for every known, non-pending channel, loads all associated 1869 // forwarding packages and reforwards any Settle or Fail HTLCs found. This is 1870 // used to resurrect the switch's mailboxes after a restart. 1871 func (s *Switch) reforwardResponses() error { 1872 openChannels, err := s.cfg.FetchAllOpenChannels() 1873 if err != nil { 1874 return err 1875 } 1876 1877 for _, openChannel := range openChannels { 1878 shortChanID := openChannel.ShortChanID() 1879 1880 // Locally-initiated payments never need reforwarding. 1881 if shortChanID == hop.Source { 1882 continue 1883 } 1884 1885 // If the channel is pending, it should have no forwarding 1886 // packages, and nothing to reforward. 1887 if openChannel.IsPending { 1888 continue 1889 } 1890 1891 // Channels in open or waiting-close may still have responses in 1892 // their forwarding packages. We will continue to reattempt 1893 // forwarding on startup until the channel is fully-closed. 1894 // 1895 // Load this channel's forwarding packages, and deliver them to 1896 // the switch. 1897 fwdPkgs, err := s.loadChannelFwdPkgs(shortChanID) 1898 if err != nil { 1899 log.Errorf("unable to load forwarding "+ 1900 "packages for %v: %v", shortChanID, err) 1901 return err 1902 } 1903 1904 s.reforwardSettleFails(fwdPkgs) 1905 } 1906 1907 return nil 1908 } 1909 1910 // loadChannelFwdPkgs loads all forwarding packages owned by the `source` short 1911 // channel identifier. 1912 func (s *Switch) loadChannelFwdPkgs(source lnwire.ShortChannelID) ([]*channeldb.FwdPkg, error) { 1913 1914 var fwdPkgs []*channeldb.FwdPkg 1915 if err := kvdb.View(s.cfg.DB, func(tx kvdb.RTx) error { 1916 var err error 1917 fwdPkgs, err = s.cfg.SwitchPackager.LoadChannelFwdPkgs( 1918 tx, source, 1919 ) 1920 return err 1921 }, func() { 1922 fwdPkgs = nil 1923 }); err != nil { 1924 return nil, err 1925 } 1926 1927 return fwdPkgs, nil 1928 } 1929 1930 // reforwardSettleFails parses the Settle and Fail HTLCs from the list of 1931 // forwarding packages, and reforwards those that have not been acknowledged. 1932 // This is intended to occur on startup, in order to recover the switch's 1933 // mailboxes, and to ensure that responses can be propagated in case the 1934 // outgoing link never comes back online. 1935 // 1936 // NOTE: This should mimic the behavior processRemoteSettleFails. 1937 func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) { 1938 for _, fwdPkg := range fwdPkgs { 1939 settleFails, err := lnwallet.PayDescsFromRemoteLogUpdates( 1940 fwdPkg.Source, fwdPkg.Height, fwdPkg.SettleFails, 1941 ) 1942 if err != nil { 1943 log.Errorf("Unable to process remote log updates: %v", 1944 err) 1945 continue 1946 } 1947 1948 switchPackets := make([]*htlcPacket, 0, len(settleFails)) 1949 for i, pd := range settleFails { 1950 1951 // Skip any settles or fails that have already been 1952 // acknowledged by the incoming link that originated the 1953 // forwarded Add. 1954 if fwdPkg.SettleFailFilter.Contains(uint16(i)) { 1955 continue 1956 } 1957 1958 switch pd.EntryType { 1959 1960 // A settle for an HTLC we previously forwarded HTLC has 1961 // been received. So we'll forward the HTLC to the 1962 // switch which will handle propagating the settle to 1963 // the prior hop. 1964 case lnwallet.Settle: 1965 settlePacket := &htlcPacket{ 1966 outgoingChanID: fwdPkg.Source, 1967 outgoingHTLCID: pd.ParentIndex, 1968 destRef: pd.DestRef, 1969 htlc: &lnwire.UpdateFulfillHTLC{ 1970 PaymentPreimage: pd.RPreimage, 1971 }, 1972 } 1973 1974 // Add the packet to the batch to be forwarded, and 1975 // notify the overflow queue that a spare spot has been 1976 // freed up within the commitment state. 1977 switchPackets = append(switchPackets, settlePacket) 1978 1979 // A failureCode message for a previously forwarded HTLC has been 1980 // received. As a result a new slot will be freed up in our 1981 // commitment state, so we'll forward this to the switch so the 1982 // backwards undo can continue. 1983 case lnwallet.Fail: 1984 // Fetch the reason the HTLC was canceled so 1985 // we can continue to propagate it. This 1986 // failure originated from another node, so 1987 // the linkFailure field is not set on this 1988 // packet. 1989 failPacket := &htlcPacket{ 1990 outgoingChanID: fwdPkg.Source, 1991 outgoingHTLCID: pd.ParentIndex, 1992 destRef: pd.DestRef, 1993 htlc: &lnwire.UpdateFailHTLC{ 1994 Reason: lnwire.OpaqueReason(pd.FailReason), 1995 }, 1996 } 1997 1998 // Add the packet to the batch to be forwarded, and 1999 // notify the overflow queue that a spare spot has been 2000 // freed up within the commitment state. 2001 switchPackets = append(switchPackets, failPacket) 2002 } 2003 } 2004 2005 // Since this send isn't tied to a specific link, we pass a nil 2006 // link quit channel, meaning the send will fail only if the 2007 // switch receives a shutdown request. 2008 2009 if err := s.ForwardPackets(nil, switchPackets...); err != nil { 2010 log.Errorf("Unhandled error while reforwarding packets "+ 2011 "settle/fail over htlcswitch: %v", err) 2012 } 2013 } 2014 } 2015 2016 // Stop gracefully stops all active helper goroutines, then waits until they've 2017 // exited. 2018 func (s *Switch) Stop() error { 2019 if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) { 2020 log.Warn("Htlc Switch already stopped") 2021 return errors.New("htlc switch already shutdown") 2022 } 2023 2024 log.Info("HTLC Switch shutting down") 2025 2026 close(s.quit) 2027 2028 s.wg.Wait() 2029 2030 // Wait until all active goroutines have finished exiting before 2031 // stopping the mailboxes, otherwise the mailbox map could still be 2032 // accessed and modified. 2033 s.mailOrchestrator.Stop() 2034 2035 return nil 2036 } 2037 2038 // CreateAndAddLink will create a link and then add it to the internal maps 2039 // when given a ChannelLinkConfig and LightningChannel. 2040 func (s *Switch) CreateAndAddLink(linkCfg ChannelLinkConfig, 2041 lnChan *lnwallet.LightningChannel) error { 2042 2043 link := NewChannelLink(linkCfg, lnChan) 2044 return s.AddLink(link) 2045 } 2046 2047 // AddLink is used to initiate the handling of the add link command. The 2048 // request will be propagated and handled in the main goroutine. 2049 func (s *Switch) AddLink(link ChannelLink) error { 2050 s.indexMtx.Lock() 2051 defer s.indexMtx.Unlock() 2052 2053 chanID := link.ChanID() 2054 2055 // First, ensure that this link is not already active in the switch. 2056 _, err := s.getLink(chanID) 2057 if err == nil { 2058 return fmt.Errorf("unable to add ChannelLink(%v), already "+ 2059 "active", chanID) 2060 } 2061 2062 // Get and attach the mailbox for this link, which buffers packets in 2063 // case there packets that we tried to deliver while this link was 2064 // offline. 2065 shortChanID := link.ShortChanID() 2066 mailbox := s.mailOrchestrator.GetOrCreateMailBox(chanID, shortChanID) 2067 link.AttachMailBox(mailbox) 2068 2069 if err := link.Start(); err != nil { 2070 s.removeLink(chanID) 2071 return err 2072 } 2073 2074 if shortChanID == hop.Source { 2075 log.Infof("Adding pending link chan_id=%v, short_chan_id=%s", 2076 chanID, shortChanID) 2077 2078 s.pendingLinkIndex[chanID] = link 2079 } else { 2080 log.Infof("Adding live link chan_id=%v, short_chan_id=%s", 2081 chanID, shortChanID) 2082 2083 s.addLiveLink(link) 2084 s.mailOrchestrator.BindLiveShortChanID( 2085 mailbox, chanID, shortChanID, 2086 ) 2087 } 2088 2089 return nil 2090 } 2091 2092 // addLiveLink adds a link to all associated forwarding index, this makes it a 2093 // candidate for forwarding HTLCs. 2094 func (s *Switch) addLiveLink(link ChannelLink) { 2095 // We'll add the link to the linkIndex which lets us quickly 2096 // look up a channel when we need to close or register it, and 2097 // the forwarding index which'll be used when forwarding HTLC's 2098 // in the multi-hop setting. 2099 s.linkIndex[link.ChanID()] = link 2100 s.forwardingIndex[link.ShortChanID()] = link 2101 2102 // Next we'll add the link to the interface index so we can 2103 // quickly look up all the channels for a particular node. 2104 peerPub := link.Peer().PubKey() 2105 if _, ok := s.interfaceIndex[peerPub]; !ok { 2106 s.interfaceIndex[peerPub] = make(map[lnwire.ChannelID]ChannelLink) 2107 } 2108 s.interfaceIndex[peerPub][link.ChanID()] = link 2109 } 2110 2111 // GetLink is used to initiate the handling of the get link command. The 2112 // request will be propagated/handled to/in the main goroutine. 2113 func (s *Switch) GetLink(chanID lnwire.ChannelID) (ChannelUpdateHandler, 2114 error) { 2115 2116 s.indexMtx.RLock() 2117 defer s.indexMtx.RUnlock() 2118 2119 return s.getLink(chanID) 2120 } 2121 2122 // getLink returns the link stored in either the pending index or the live 2123 // lindex. 2124 func (s *Switch) getLink(chanID lnwire.ChannelID) (ChannelLink, error) { 2125 link, ok := s.linkIndex[chanID] 2126 if !ok { 2127 link, ok = s.pendingLinkIndex[chanID] 2128 if !ok { 2129 return nil, ErrChannelLinkNotFound 2130 } 2131 } 2132 2133 return link, nil 2134 } 2135 2136 // GetLinkByShortID attempts to return the link which possesses the target short 2137 // channel ID. 2138 func (s *Switch) GetLinkByShortID(chanID lnwire.ShortChannelID) (ChannelLink, 2139 error) { 2140 2141 s.indexMtx.RLock() 2142 defer s.indexMtx.RUnlock() 2143 2144 return s.getLinkByShortID(chanID) 2145 } 2146 2147 // getLinkByShortID attempts to return the link which possesses the target 2148 // short channel ID. 2149 // 2150 // NOTE: This MUST be called with the indexMtx held. 2151 func (s *Switch) getLinkByShortID(chanID lnwire.ShortChannelID) (ChannelLink, error) { 2152 link, ok := s.forwardingIndex[chanID] 2153 if !ok { 2154 return nil, ErrChannelLinkNotFound 2155 } 2156 2157 return link, nil 2158 } 2159 2160 // HasActiveLink returns true if the given channel ID has a link in the link 2161 // index AND the link is eligible to forward. 2162 func (s *Switch) HasActiveLink(chanID lnwire.ChannelID) bool { 2163 s.indexMtx.RLock() 2164 defer s.indexMtx.RUnlock() 2165 2166 if link, ok := s.linkIndex[chanID]; ok { 2167 return link.EligibleToForward() 2168 } 2169 2170 return false 2171 } 2172 2173 // RemoveLink purges the switch of any link associated with chanID. If a pending 2174 // or active link is not found, this method does nothing. Otherwise, the method 2175 // returns after the link has been completely shutdown. 2176 func (s *Switch) RemoveLink(chanID lnwire.ChannelID) { 2177 s.indexMtx.Lock() 2178 link := s.removeLink(chanID) 2179 s.indexMtx.Unlock() 2180 2181 if link != nil { 2182 link.Stop() 2183 } 2184 } 2185 2186 // removeLink is used to remove and stop the channel link. 2187 // 2188 // NOTE: This MUST be called with the indexMtx held. 2189 func (s *Switch) removeLink(chanID lnwire.ChannelID) ChannelLink { 2190 log.Infof("Removing channel link with ChannelID(%v)", chanID) 2191 2192 link, err := s.getLink(chanID) 2193 if err != nil { 2194 return nil 2195 } 2196 2197 // Remove the channel from live link indexes. 2198 delete(s.pendingLinkIndex, link.ChanID()) 2199 delete(s.linkIndex, link.ChanID()) 2200 delete(s.forwardingIndex, link.ShortChanID()) 2201 2202 // If the link has been added to the peer index, then we'll move to 2203 // delete the entry within the index. 2204 peerPub := link.Peer().PubKey() 2205 if peerIndex, ok := s.interfaceIndex[peerPub]; ok { 2206 delete(peerIndex, link.ChanID()) 2207 2208 // If after deletion, there are no longer any links, then we'll 2209 // remove the interface map all together. 2210 if len(peerIndex) == 0 { 2211 delete(s.interfaceIndex, peerPub) 2212 } 2213 } 2214 2215 return link 2216 } 2217 2218 // UpdateShortChanID updates the short chan ID for an existing channel. This is 2219 // required in the case of a re-org and re-confirmation or a channel, or in the 2220 // case that a link was added to the switch before its short chan ID was known. 2221 func (s *Switch) UpdateShortChanID(chanID lnwire.ChannelID) error { 2222 s.indexMtx.Lock() 2223 defer s.indexMtx.Unlock() 2224 2225 // Locate the target link in the pending link index. If no such link 2226 // exists, then we will ignore the request. 2227 link, ok := s.pendingLinkIndex[chanID] 2228 if !ok { 2229 return fmt.Errorf("link %v not found", chanID) 2230 } 2231 2232 oldShortChanID := link.ShortChanID() 2233 2234 // Try to update the link's short channel ID, returning early if this 2235 // update failed. 2236 shortChanID, err := link.UpdateShortChanID() 2237 if err != nil { 2238 return err 2239 } 2240 2241 // Reject any blank short channel ids. 2242 if shortChanID == hop.Source { 2243 return fmt.Errorf("refusing trivial short_chan_id for chan_id=%v"+ 2244 "live link", chanID) 2245 } 2246 2247 log.Infof("Updated short_chan_id for ChannelLink(%v): old=%v, new=%v", 2248 chanID, oldShortChanID, shortChanID) 2249 2250 // Since the link was in the pending state before, we will remove it 2251 // from the pending link index and add it to the live link index so that 2252 // it can be available in forwarding. 2253 delete(s.pendingLinkIndex, chanID) 2254 s.addLiveLink(link) 2255 2256 // Finally, alert the mail orchestrator to the change of short channel 2257 // ID, and deliver any unclaimed packets to the link. 2258 mailbox := s.mailOrchestrator.GetOrCreateMailBox(chanID, shortChanID) 2259 s.mailOrchestrator.BindLiveShortChanID( 2260 mailbox, chanID, shortChanID, 2261 ) 2262 2263 return nil 2264 } 2265 2266 // GetLinksByInterface fetches all the links connected to a particular node 2267 // identified by the serialized compressed form of its public key. 2268 func (s *Switch) GetLinksByInterface(hop [33]byte) ([]ChannelUpdateHandler, 2269 error) { 2270 2271 s.indexMtx.RLock() 2272 defer s.indexMtx.RUnlock() 2273 2274 var handlers []ChannelUpdateHandler 2275 2276 links, err := s.getLinks(hop) 2277 if err != nil { 2278 return nil, err 2279 } 2280 2281 // Range over the returned []ChannelLink to convert them into 2282 // []ChannelUpdateHandler. 2283 for _, link := range links { 2284 handlers = append(handlers, link) 2285 } 2286 2287 return handlers, nil 2288 } 2289 2290 // getLinks is function which returns the channel links of the peer by hop 2291 // destination id. 2292 // 2293 // NOTE: This MUST be called with the indexMtx held. 2294 func (s *Switch) getLinks(destination [33]byte) ([]ChannelLink, error) { 2295 links, ok := s.interfaceIndex[destination] 2296 if !ok { 2297 return nil, ErrNoLinksFound 2298 } 2299 2300 channelLinks := make([]ChannelLink, 0, len(links)) 2301 for _, link := range links { 2302 channelLinks = append(channelLinks, link) 2303 } 2304 2305 return channelLinks, nil 2306 } 2307 2308 // CircuitModifier returns a reference to subset of the interfaces provided by 2309 // the circuit map, to allow links to open and close circuits. 2310 func (s *Switch) CircuitModifier() CircuitModifier { 2311 return s.circuits 2312 } 2313 2314 // CircuitLookup returns a reference to subset of the interfaces provided by the 2315 // circuit map, to allow looking up circuits. 2316 func (s *Switch) CircuitLookup() CircuitLookup { 2317 return s.circuits 2318 } 2319 2320 // commitCircuits persistently adds a circuit to the switch's circuit map. 2321 func (s *Switch) commitCircuits(circuits ...*PaymentCircuit) ( 2322 *CircuitFwdActions, error) { 2323 2324 return s.circuits.CommitCircuits(circuits...) 2325 } 2326 2327 // FlushForwardingEvents flushes out the set of pending forwarding events to 2328 // the persistent log. This will be used by the switch to periodically flush 2329 // out the set of forwarding events to disk. External callers can also use this 2330 // method to ensure all data is flushed to dis before querying the log. 2331 func (s *Switch) FlushForwardingEvents() error { 2332 // First, we'll obtain a copy of the current set of pending forwarding 2333 // events. 2334 s.fwdEventMtx.Lock() 2335 2336 // If we won't have any forwarding events, then we can exit early. 2337 if len(s.pendingFwdingEvents) == 0 { 2338 s.fwdEventMtx.Unlock() 2339 return nil 2340 } 2341 2342 events := make([]channeldb.ForwardingEvent, len(s.pendingFwdingEvents)) 2343 copy(events, s.pendingFwdingEvents) 2344 2345 // With the copy obtained, we can now clear out the header pointer of 2346 // the current slice. This way, we can re-use the underlying storage 2347 // allocated for the slice. 2348 s.pendingFwdingEvents = s.pendingFwdingEvents[:0] 2349 s.fwdEventMtx.Unlock() 2350 2351 // Finally, we'll write out the copied events to the persistent 2352 // forwarding log. 2353 return s.cfg.FwdingLog.AddForwardingEvents(events) 2354 } 2355 2356 // BestHeight returns the best height known to the switch. 2357 func (s *Switch) BestHeight() uint32 { 2358 return atomic.LoadUint32(&s.bestHeight) 2359 } 2360 2361 // evaluateDustThreshold takes in a ChannelLink, HTLC amount, and a boolean to 2362 // determine whether the default dust threshold has been exceeded. This 2363 // heuristic takes into account the trimmed-to-dust mechanism. The sum of the 2364 // commitment's dust with the mailbox's dust with the amount is checked against 2365 // the default threshold. If incoming is true, then the amount is not included 2366 // in the sum as it was already included in the commitment's dust. A boolean is 2367 // returned telling the caller whether the HTLC should be failed back. 2368 func (s *Switch) evaluateDustThreshold(link ChannelLink, 2369 amount lnwire.MilliAtom, incoming bool) bool { 2370 2371 // Retrieve the link's current commitment feerate and dustClosure. 2372 feeRate := link.getFeeRate() 2373 isDust := link.getDustClosure() 2374 2375 // Evaluate if the HTLC is dust on either sides' commitment. 2376 isLocalDust := isDust(feeRate, incoming, true, amount.ToAtoms()) 2377 isRemoteDust := isDust(feeRate, incoming, false, amount.ToAtoms()) 2378 2379 if !(isLocalDust || isRemoteDust) { 2380 // If the HTLC is not dust on either commitment, it's fine to 2381 // forward. 2382 return false 2383 } 2384 2385 // Fetch the dust sums currently in the mailbox for this link. 2386 cid := link.ChanID() 2387 sid := link.ShortChanID() 2388 mailbox := s.mailOrchestrator.GetOrCreateMailBox(cid, sid) 2389 localMailDust, remoteMailDust := mailbox.DustPackets() 2390 2391 // If the htlc is dust on the local commitment, we'll obtain the dust 2392 // sum for it. 2393 if isLocalDust { 2394 localSum := link.getDustSum(false) 2395 localSum += localMailDust 2396 2397 // Optionally include the HTLC amount only for outgoing 2398 // HTLCs. 2399 if !incoming { 2400 localSum += amount 2401 } 2402 2403 // Finally check against the defined dust threshold. 2404 if localSum > s.cfg.DustThreshold { 2405 return true 2406 } 2407 } 2408 2409 // Also check if the htlc is dust on the remote commitment, if we've 2410 // reached this point. 2411 if isRemoteDust { 2412 remoteSum := link.getDustSum(true) 2413 remoteSum += remoteMailDust 2414 2415 // Optionally include the HTLC amount only for outgoing 2416 // HTLCs. 2417 if !incoming { 2418 remoteSum += amount 2419 } 2420 2421 // Finally check against the defined dust threshold. 2422 if remoteSum > s.cfg.DustThreshold { 2423 return true 2424 } 2425 } 2426 2427 // If we reached this point, this HTLC is fine to forward. 2428 return false 2429 }