mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 06:35:07 +01:00
htlcswitch: refactor handleLocalDispatch
Split handleLocalDispatch into an extra handleLocalAddHTLC function so we can easily notify an error should one occur adding the htlc.
This commit is contained in:
parent
fc0ee06a99
commit
937062b6d3
1 changed files with 43 additions and 31 deletions
|
@ -768,38 +768,9 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
|
|||
// User have created the htlc update therefore we should find the
|
||||
// appropriate channel link and send the payment over this link.
|
||||
if htlc, ok := pkt.htlc.(*lnwire.UpdateAddHTLC); ok {
|
||||
// Try to find links by node destination.
|
||||
s.indexMtx.RLock()
|
||||
link, err := s.getLinkByShortID(pkt.outgoingChanID)
|
||||
s.indexMtx.RUnlock()
|
||||
link, err := s.handleLocalAddHTLC(pkt, htlc)
|
||||
if err != nil {
|
||||
log.Errorf("Link %v not found", pkt.outgoingChanID)
|
||||
return NewLinkError(&lnwire.FailUnknownNextPeer{})
|
||||
}
|
||||
|
||||
if !link.EligibleToForward() {
|
||||
log.Errorf("Link %v is not available to forward",
|
||||
pkt.outgoingChanID)
|
||||
|
||||
// The update does not need to be populated as the error
|
||||
// will be returned back to the router.
|
||||
return NewDetailedLinkError(
|
||||
lnwire.NewTemporaryChannelFailure(nil),
|
||||
OutgoingFailureLinkNotEligible,
|
||||
)
|
||||
}
|
||||
|
||||
// Ensure that the htlc satisfies the outgoing channel policy.
|
||||
currentHeight := atomic.LoadUint32(&s.bestHeight)
|
||||
htlcErr := link.CheckHtlcTransit(
|
||||
htlc.PaymentHash,
|
||||
htlc.Amount,
|
||||
htlc.Expiry, currentHeight,
|
||||
)
|
||||
if htlcErr != nil {
|
||||
log.Errorf("Link %v policy for local forward not "+
|
||||
"satisfied", pkt.outgoingChanID)
|
||||
return htlcErr
|
||||
return err
|
||||
}
|
||||
|
||||
return link.HandleSwitchPacket(pkt)
|
||||
|
@ -811,6 +782,47 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// handleLocalAddHTLC handles the addition of a htlc for a send that
|
||||
// originates from our node. It returns the link that the htlc should
|
||||
// be forwarded outwards on, and a link error if the htlc cannot be
|
||||
// forwarded.
|
||||
func (s *Switch) handleLocalAddHTLC(pkt *htlcPacket,
|
||||
htlc *lnwire.UpdateAddHTLC) (ChannelLink, *LinkError) {
|
||||
|
||||
// Try to find links by node destination.
|
||||
s.indexMtx.RLock()
|
||||
link, err := s.getLinkByShortID(pkt.outgoingChanID)
|
||||
s.indexMtx.RUnlock()
|
||||
if err != nil {
|
||||
log.Errorf("Link %v not found", pkt.outgoingChanID)
|
||||
return nil, NewLinkError(&lnwire.FailUnknownNextPeer{})
|
||||
}
|
||||
|
||||
if !link.EligibleToForward() {
|
||||
log.Errorf("Link %v is not available to forward",
|
||||
pkt.outgoingChanID)
|
||||
|
||||
// The update does not need to be populated as the error
|
||||
// will be returned back to the router.
|
||||
return nil, NewDetailedLinkError(
|
||||
lnwire.NewTemporaryChannelFailure(nil),
|
||||
OutgoingFailureLinkNotEligible,
|
||||
)
|
||||
}
|
||||
|
||||
// Ensure that the htlc satisfies the outgoing channel policy.
|
||||
currentHeight := atomic.LoadUint32(&s.bestHeight)
|
||||
htlcErr := link.CheckHtlcTransit(
|
||||
htlc.PaymentHash, htlc.Amount, htlc.Expiry, currentHeight,
|
||||
)
|
||||
if htlcErr != nil {
|
||||
log.Errorf("Link %v policy for local forward not "+
|
||||
"satisfied", pkt.outgoingChanID)
|
||||
return nil, htlcErr
|
||||
}
|
||||
return link, nil
|
||||
}
|
||||
|
||||
// handleLocalResponse processes a Settle or Fail responding to a
|
||||
// locally-initiated payment. This is handled asynchronously to avoid blocking
|
||||
// the main event loop within the switch, as these operations can require
|
||||
|
|
Loading…
Add table
Reference in a new issue