From 378e0558c57ff48a887c2046647a9f76e5afdb08 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 30 Aug 2019 14:11:38 -0700 Subject: [PATCH] htlcswitch/hop: move hop.Exit and hop.Source to hop pkg --- htlcswitch/circuit_map.go | 7 ++++--- htlcswitch/hop/type.go | 13 +++++++++++++ htlcswitch/iterator.go | 12 +----------- htlcswitch/link.go | 12 ++++++------ htlcswitch/link_test.go | 8 ++++---- htlcswitch/switch.go | 23 ++++++++++++----------- htlcswitch/test_utils.go | 2 +- 7 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 htlcswitch/hop/type.go diff --git a/htlcswitch/circuit_map.go b/htlcswitch/circuit_map.go index 5db36b88..017e2b78 100644 --- a/htlcswitch/circuit_map.go +++ b/htlcswitch/circuit_map.go @@ -9,6 +9,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lnwire" ) @@ -311,7 +312,7 @@ func (cm *circuitMap) restoreMemState() error { // documented case of stray keystones emerges for // forwarded payments, this check should be removed, but // with extreme caution. - if strayKeystone.OutKey.ChanID != sourceHop { + if strayKeystone.OutKey.ChanID != hop.Source { continue } @@ -396,9 +397,9 @@ func (cm *circuitMap) trimAllOpenCircuits() error { // First, skip any channels that have not been assigned their // final channel identifier, otherwise we would try to trim - // htlcs belonging to the all-zero, sourceHop ID. + // htlcs belonging to the all-zero, hop.Source ID. chanID := activeChannel.ShortChanID() - if chanID == sourceHop { + if chanID == hop.Source { continue } diff --git a/htlcswitch/hop/type.go b/htlcswitch/hop/type.go new file mode 100644 index 00000000..a167d10e --- /dev/null +++ b/htlcswitch/hop/type.go @@ -0,0 +1,13 @@ +package hop + +import "github.com/lightningnetwork/lnd/lnwire" + +var ( + // Exit is a special "hop" denoting that an incoming HTLC is meant to + // pay finally to the receiving node. + Exit lnwire.ShortChannelID + + // Source is a sentinel "hop" denoting that an incoming HTLC is + // initiated by our own switch. + Source lnwire.ShortChannelID +) diff --git a/htlcswitch/iterator.go b/htlcswitch/iterator.go index ad01773a..4e04508a 100644 --- a/htlcswitch/iterator.go +++ b/htlcswitch/iterator.go @@ -14,16 +14,6 @@ import ( "github.com/lightningnetwork/lnd/tlv" ) -var ( - // exitHop is a special "hop" which denotes that an incoming HTLC is - // meant to pay finally to the receiving node. - exitHop lnwire.ShortChannelID - - // sourceHop is a sentinel value denoting that an incoming HTLC is - // initiated by our own switch. - sourceHop lnwire.ShortChannelID -) - // HopIterator is an interface that abstracts away the routing information // included in HTLC's which includes the entirety of the payment path of an // HTLC. This interface provides two basic method which carry out: how to @@ -109,7 +99,7 @@ func (r *sphinxHopIterator) ForwardingInstructions() ( switch r.processedPacket.Action { case sphinx.ExitNode: - nextHop = exitHop + nextHop = hop.Exit case sphinx.MoreHops: s := binary.BigEndian.Uint64(fwdInst.NextAddress[:]) nextHop = lnwire.NewShortChanIDFromInt(s) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 60820970..f1e1cf24 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -437,8 +437,8 @@ func (l *channelLink) Start() error { // off point, since all indexes below that are committed. This action // is only performed if the link's final short channel ID has been // assigned, otherwise we would try to trim the htlcs belonging to the - // all-zero, sourceHop ID. - if l.ShortChanID() != sourceHop { + // all-zero, hop.Source ID. + if l.ShortChanID() != hop.Source { localHtlcIndex, err := l.channel.NextLocalHtlcIndex() if err != nil { return fmt.Errorf("unable to retrieve next local "+ @@ -536,7 +536,7 @@ func (l *channelLink) WaitForShutdown() { // the all-zero source ID, meaning that the channel has had its ID finalized. func (l *channelLink) EligibleToForward() bool { return l.channel.RemoteNextRevocation() != nil && - l.ShortChanID() != sourceHop + l.ShortChanID() != hop.Source } // sampleNetworkFee samples the current fee rate on the network to get into the @@ -962,7 +962,7 @@ func (l *channelLink) htlcManager() { // only attempt to resolve packages if our short chan id indicates that // the channel is not pending, otherwise we should have no htlcs to // reforward. - if l.ShortChanID() != sourceHop { + if l.ShortChanID() != hop.Source { if err := l.resolveFwdPkgs(); err != nil { l.fail(LinkFailureError{code: ErrInternalError}, "unable to resolve fwd pkgs: %v", err) @@ -2076,7 +2076,7 @@ func (l *channelLink) UpdateShortChanID() (lnwire.ShortChannelID, error) { if err != nil { l.errorf("unable to refresh short_chan_id for chan_id=%v: %v", chanID, err) - return sourceHop, err + return hop.Source, err } sid := l.channel.ShortChanID() @@ -2675,7 +2675,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, } switch fwdInfo.NextHop { - case exitHop: + case hop.Exit: updated, err := l.processExitHop( pd, obfuscator, fwdInfo, heightNow, chanIterator.ExtraOnionBlob(), diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index c2427700..99889a5f 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1937,7 +1937,7 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) { } addPkt := htlcPacket{ htlc: htlc, - incomingChanID: sourceHop, + incomingChanID: hop.Source, incomingHTLCID: 0, obfuscator: NewMockObfuscator(), } @@ -2017,7 +2017,7 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) { } addPkt = htlcPacket{ htlc: htlc, - incomingChanID: sourceHop, + incomingChanID: hop.Source, incomingHTLCID: 1, obfuscator: NewMockObfuscator(), } @@ -2535,7 +2535,7 @@ func genAddsAndCircuits(numHtlcs int, htlc *lnwire.UpdateAddHTLC) ( for i := 0; i < numHtlcs; i++ { addPkt := htlcPacket{ htlc: htlc, - incomingChanID: sourceHop, + incomingChanID: hop.Source, incomingHTLCID: uint64(i), obfuscator: NewMockObfuscator(), } @@ -4311,7 +4311,7 @@ func generateHtlcAndInvoice(t *testing.T, hops := []hop.ForwardingInfo{ { Network: hop.BitcoinNetwork, - NextHop: exitHop, + NextHop: hop.Exit, AmountToForward: htlcAmt, OutgoingCTLV: uint32(htlcExpiry), }, diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index bdc50fe5..286dc72d 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -16,6 +16,7 @@ import ( "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" + "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" @@ -354,7 +355,7 @@ func (s *Switch) GetPaymentResult(paymentID uint64, paymentHash lntypes.Hash, nChan <-chan *networkResult err error outKey = CircuitKey{ - ChanID: sourceHop, + ChanID: hop.Source, HtlcID: paymentID, } ) @@ -428,7 +429,7 @@ func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID, paymentID uint64, // this stage it means that packet haven't left boundaries of our // system and something wrong happened. packet := &htlcPacket{ - incomingChanID: sourceHop, + incomingChanID: hop.Source, incomingHTLCID: paymentID, outgoingChanID: firstHop, htlc: htlc, @@ -513,7 +514,7 @@ func (s *Switch) forward(packet *htlcPacket) error { return ErrDuplicateAdd case len(actions.Fails) == 1: - if packet.incomingChanID == sourceHop { + if packet.incomingChanID == hop.Source { return err } @@ -1031,14 +1032,14 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error { case *lnwire.UpdateAddHTLC: // Check if the node is set to reject all onward HTLCs and also make // sure that HTLC is not from the source node. - if s.cfg.RejectHTLC && packet.incomingChanID != sourceHop { + if s.cfg.RejectHTLC && packet.incomingChanID != hop.Source { failure := &lnwire.FailChannelDisabled{} addErr := fmt.Errorf("unable to forward any htlcs") return s.failAddPacket(packet, failure, addErr) } - if packet.incomingChanID == sourceHop { + if packet.incomingChanID == hop.Source { // A blank incomingChanID indicates that this is // a pending user-initiated payment. return s.handleLocalDispatch(packet) @@ -1080,7 +1081,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error { // If the link doesn't yet have a source chan ID, then // we'll skip it as well. - case link.ShortChanID() == sourceHop: + case link.ShortChanID() == hop.Source: continue } @@ -1222,7 +1223,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error { // // TODO(roasbeef): only do this once link actually // fully settles? - localHTLC := packet.incomingChanID == sourceHop + localHTLC := packet.incomingChanID == hop.Source if !localHTLC { s.fwdEventMtx.Lock() s.pendingFwdingEvents = append( @@ -1241,7 +1242,7 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error { // A blank IncomingChanID in a circuit indicates that it is a pending // user-initiated payment. - if packet.incomingChanID == sourceHop { + if packet.incomingChanID == hop.Source { return s.handleLocalDispatch(packet) } @@ -1798,7 +1799,7 @@ func (s *Switch) reforwardResponses() error { shortChanID := openChannel.ShortChanID() // Locally-initiated payments never need reforwarding. - if shortChanID == sourceHop { + if shortChanID == hop.Source { continue } @@ -1994,7 +1995,7 @@ func (s *Switch) AddLink(link ChannelLink) error { } shortChanID := link.ShortChanID() - if shortChanID == sourceHop { + if shortChanID == hop.Source { log.Infof("Adding pending link chan_id=%v, short_chan_id=%v", chanID, shortChanID) @@ -2157,7 +2158,7 @@ func (s *Switch) UpdateShortChanID(chanID lnwire.ChannelID) error { } // Reject any blank short channel ids. - if shortChanID == sourceHop { + if shortChanID == hop.Source { return fmt.Errorf("refusing trivial short_chan_id for chan_id=%v"+ "live link", chanID) } diff --git a/htlcswitch/test_utils.go b/htlcswitch/test_utils.go index 2e168e5d..65017002 100644 --- a/htlcswitch/test_utils.go +++ b/htlcswitch/test_utils.go @@ -648,7 +648,7 @@ func generateHops(payAmt lnwire.MilliSatoshi, startingHeight uint32, for i := len(path) - 1; i >= 0; i-- { // If this is the last hop, then the next hop is the special // "exit node". Otherwise, we look to the "prior" hop. - nextHop := exitHop + nextHop := hop.Exit if i != len(path)-1 { nextHop = path[i+1].channel.ShortChanID() }