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
@ -768,38 +768,9 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
|
|||||||
// User have created the htlc update therefore we should find the
|
// User have created the htlc update therefore we should find the
|
||||||
// appropriate channel link and send the payment over this link.
|
// appropriate channel link and send the payment over this link.
|
||||||
if htlc, ok := pkt.htlc.(*lnwire.UpdateAddHTLC); ok {
|
if htlc, ok := pkt.htlc.(*lnwire.UpdateAddHTLC); ok {
|
||||||
// Try to find links by node destination.
|
link, err := s.handleLocalAddHTLC(pkt, htlc)
|
||||||
s.indexMtx.RLock()
|
|
||||||
link, err := s.getLinkByShortID(pkt.outgoingChanID)
|
|
||||||
s.indexMtx.RUnlock()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Link %v not found", pkt.outgoingChanID)
|
return err
|
||||||
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 link.HandleSwitchPacket(pkt)
|
return link.HandleSwitchPacket(pkt)
|
||||||
@ -811,6 +782,47 @@ func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
|
|||||||
return nil
|
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
|
// handleLocalResponse processes a Settle or Fail responding to a
|
||||||
// locally-initiated payment. This is handled asynchronously to avoid blocking
|
// locally-initiated payment. This is handled asynchronously to avoid blocking
|
||||||
// the main event loop within the switch, as these operations can require
|
// the main event loop within the switch, as these operations can require
|
||||||
|
Loading…
Reference in New Issue
Block a user