htlcswitch: Rename htlcPacket fields for clarity.

The src/dest terminology for routing packets is kind of confusing
because the source HTLC may not be the source of the packet for
settles/fails traversing the circuit in the opposite direction. This
changes the nomenclature to incoming/outgoing and always references
the HTLCs themselves.
This commit is contained in:
Jim Posen 2017-10-30 11:56:51 -07:00 committed by Olaoluwa Osuntokun
parent 66e654bb42
commit 4a29fbdab2
6 changed files with 149 additions and 145 deletions

@ -31,36 +31,36 @@ func TestCircuitMap(t *testing.T) {
// Add multiple circuits with same destination channel but different HTLC
// IDs and payment hashes.
circuitMap.Add(&htlcswitch.PaymentCircuit{
PaymentHash: hash1,
SrcChanID: chan2,
SrcHTLCID: 1,
DestChanID: chan1,
DestHTLCID: 0,
PaymentHash: hash1,
IncomingChanID: chan2,
IncomingHTLCID: 1,
OutgoingChanID: chan1,
OutgoingHTLCID: 0,
})
circuitMap.Add(&htlcswitch.PaymentCircuit{
PaymentHash: hash2,
SrcChanID: chan2,
SrcHTLCID: 2,
DestChanID: chan1,
DestHTLCID: 1,
PaymentHash: hash2,
IncomingChanID: chan2,
IncomingHTLCID: 2,
OutgoingChanID: chan1,
OutgoingHTLCID: 1,
})
// Add another circuit with an already-used HTLC ID but different
// destination channel.
circuitMap.Add(&htlcswitch.PaymentCircuit{
PaymentHash: hash3,
SrcChanID: chan1,
SrcHTLCID: 2,
DestChanID: chan2,
DestHTLCID: 0,
PaymentHash: hash3,
IncomingChanID: chan1,
IncomingHTLCID: 2,
OutgoingChanID: chan2,
OutgoingHTLCID: 0,
})
circuit = circuitMap.LookupByHTLC(chan1, 0)
if circuit == nil {
t.Fatal("LookupByHTLC failed to find circuit")
}
if circuit.PaymentHash != hash1 || circuit.SrcHTLCID != 1 {
if circuit.PaymentHash != hash1 || circuit.IncomingHTLCID != 1 {
t.Fatalf("LookupByHTLC found unexpected circuit: %v", circuit)
}
@ -68,7 +68,7 @@ func TestCircuitMap(t *testing.T) {
if circuit == nil {
t.Fatal("LookupByHTLC failed to find circuit")
}
if circuit.PaymentHash != hash2 || circuit.SrcHTLCID != 2 {
if circuit.PaymentHash != hash2 || circuit.IncomingHTLCID != 2 {
t.Fatalf("LookupByHTLC found unexpected circuit: %v", circuit)
}
@ -76,7 +76,7 @@ func TestCircuitMap(t *testing.T) {
if circuit == nil {
t.Fatal("LookupByHTLC failed to find circuit")
}
if circuit.PaymentHash != hash3 || circuit.SrcHTLCID != 2 {
if circuit.PaymentHash != hash3 || circuit.IncomingHTLCID != 2 {
t.Fatalf("LookupByHTLC found unexpected circuit: %v", circuit)
}
@ -91,18 +91,18 @@ func TestCircuitMap(t *testing.T) {
// Add a circuit with a destination channel and payment hash that are
// already added but a different HTLC ID.
circuitMap.Add(&htlcswitch.PaymentCircuit{
PaymentHash: hash1,
SrcChanID: chan2,
SrcHTLCID: 3,
DestChanID: chan1,
DestHTLCID: 3,
PaymentHash: hash1,
IncomingChanID: chan2,
IncomingHTLCID: 3,
OutgoingChanID: chan1,
OutgoingHTLCID: 3,
})
circuit = circuitMap.LookupByHTLC(chan1, 3)
if circuit == nil {
t.Fatal("LookupByHTLC failed to find circuit")
}
if circuit.PaymentHash != hash1 || circuit.SrcHTLCID != 3 {
if circuit.PaymentHash != hash1 || circuit.IncomingHTLCID != 3 {
t.Fatalf("LookupByHTLC found unexpected circuit: %v", circuit)
}
@ -130,7 +130,7 @@ func TestCircuitMap(t *testing.T) {
t.Fatalf("LookupByPaymentHash returned wrong number of circuits for "+
"hash1: expecected %d, got %d", 1, len(circuits))
}
if circuits[0].DestHTLCID != 3 {
if circuits[0].OutgoingHTLCID != 3 {
t.Fatalf("LookupByPaymentHash returned wrong circuit for hash1: %v",
circuits[0])
}

@ -699,12 +699,11 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
}
failPkt := &htlcPacket{
src: l.ShortChanID(),
dest: pkt.src,
destID: pkt.srcID,
payHash: htlc.PaymentHash,
amount: htlc.Amount,
isObfuscated: isObfuscated,
incomingChanID: pkt.incomingChanID,
incomingHTLCID: pkt.incomingHTLCID,
payHash: htlc.PaymentHash,
amount: htlc.Amount,
isObfuscated: isObfuscated,
htlc: &lnwire.UpdateFailHTLC{
Reason: reason,
},
@ -725,12 +724,12 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
// If packet was forwarded from another channel link then we should
// create circuit (remember the path) in order to forward settle/fail
// packet back.
if pkt.src != (lnwire.ShortChannelID{}) {
if pkt.incomingChanID != (lnwire.ShortChannelID{}) {
l.cfg.Switch.addCircuit(&PaymentCircuit{
PaymentHash: htlc.PaymentHash,
IncomingChanID: pkt.src,
IncomingHTLCID: pkt.srcID,
OutgoingChanID: pkt.dest,
IncomingChanID: pkt.incomingChanID,
IncomingHTLCID: pkt.incomingHTLCID,
OutgoingChanID: l.ShortChanID(),
OutgoingHTLCID: index,
ErrorEncrypter: pkt.obfuscator,
})
@ -743,7 +742,7 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
// An HTLC we forward to the switch has just settled somewhere
// upstream. Therefore we settle the HTLC within the our local
// state machine.
err := l.channel.SettleHTLC(htlc.PaymentPreimage, pkt.destID)
err := l.channel.SettleHTLC(htlc.PaymentPreimage, pkt.incomingHTLCID)
if err != nil {
// TODO(roasbeef): broadcast on-chain
l.fail("unable to settle incoming HTLC: %v", err)
@ -754,7 +753,7 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
// message to target the specific channel and HTLC to be
// cancelled.
htlc.ChanID = l.ChanID()
htlc.ID = pkt.destID
htlc.ID = pkt.incomingHTLCID
// Then we send the HTLC settle message to the connected peer
// so we can continue the propagation of the settle message.
@ -764,7 +763,7 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
case *lnwire.UpdateFailHTLC:
// An HTLC cancellation has been triggered somewhere upstream,
// we'll remove then HTLC from our local state machine.
err := l.channel.FailHTLC(pkt.destID, htlc.Reason)
err := l.channel.FailHTLC(pkt.incomingHTLCID, htlc.Reason)
if err != nil {
log.Errorf("unable to cancel HTLC: %v", err)
return
@ -775,7 +774,7 @@ func (l *channelLink) handleDownStreamPkt(pkt *htlcPacket, isReProcess bool) {
// cancelled. The "Reason" field will have already been set
// within the switch.
htlc.ChanID = l.ChanID()
htlc.ID = pkt.destID
htlc.ID = pkt.incomingHTLCID
// Finally, we send the HTLC message to the peer which
// initially created the HTLC.
@ -1194,10 +1193,10 @@ func (l *channelLink) processLockedInHtlcs(
// will handle propagating the settle to the prior hop.
case lnwallet.Settle:
settlePacket := &htlcPacket{
src: l.ShortChanID(),
srcID: pd.ParentIndex,
payHash: pd.RHash,
amount: pd.Amount,
outgoingChanID: l.ShortChanID(),
outgoingHTLCID: pd.ParentIndex,
payHash: pd.RHash,
amount: pd.Amount,
htlc: &lnwire.UpdateFufillHTLC{
PaymentPreimage: pd.RPreimage,
},
@ -1217,11 +1216,11 @@ func (l *channelLink) processLockedInHtlcs(
// Fetch the reason the HTLC was cancelled so we can
// continue to propagate it.
failPacket := &htlcPacket{
src: l.ShortChanID(),
srcID: pd.ParentIndex,
payHash: pd.RHash,
amount: pd.Amount,
isObfuscated: false,
outgoingChanID: l.ShortChanID(),
outgoingHTLCID: pd.ParentIndex,
payHash: pd.RHash,
amount: pd.Amount,
isObfuscated: false,
htlc: &lnwire.UpdateFailHTLC{
Reason: lnwire.OpaqueReason(pd.FailReason),
},
@ -1589,11 +1588,11 @@ func (l *channelLink) processLockedInHtlcs(
}
updatePacket := &htlcPacket{
src: l.ShortChanID(),
srcID: pd.HtlcIndex,
dest: fwdInfo.NextHop,
htlc: addMsg,
obfuscator: obfuscator,
incomingChanID: l.ShortChanID(),
incomingHTLCID: pd.HtlcIndex,
outgoingChanID: fwdInfo.NextHop,
htlc: addMsg,
obfuscator: obfuscator,
}
packetsToForward = append(packetsToForward, updatePacket)
}

@ -30,10 +30,10 @@ func TestMailBoxCouriers(t *testing.T) {
sentPackets := make([]*htlcPacket, numPackets)
for i := 0; i < numPackets; i++ {
pkt := &htlcPacket{
dest: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
src: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
amount: lnwire.MilliSatoshi(prand.Int63()),
isObfuscated: i%2 == 0,
outgoingChanID: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
incomingChanID: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
amount: lnwire.MilliSatoshi(prand.Int63()),
isObfuscated: i%2 == 0,
}
sentPackets[i] = pkt

@ -19,21 +19,21 @@ type htlcPacket struct {
// NOTE: This fields is initialized only in settle and fail packets.
payHash [sha256.Size]byte
// dest is the destination of this packet identified by the short
// channel ID of the target link.
dest lnwire.ShortChannelID
// incomingChanID is the ID of the channel that we have received an incoming
// HTLC on.
incomingChanID lnwire.ShortChannelID
// src is the source of this packet identified by the short channel ID
// of the target link.
src lnwire.ShortChannelID
// outgoingChanID is the ID of the channel that we have offered or will
// offer an outgoing HTLC on.
outgoingChanID lnwire.ShortChannelID
// destID is the ID of the HTLC in the destination channel. This will be set
// when forwarding a settle or fail update back to the original source.
destID uint64
// incomingHTLCID is the ID of the HTLC that we have received from the peer
// on the incoming channel.
incomingHTLCID uint64
// srcID is the ID of the HTLC in the source channel. This will be set when
// forwarding any HTLC update message.
srcID uint64
// outgoingHTLCID is the ID of the HTLC that we offered to the peer on the
// outgoing channel.
outgoingHTLCID uint64
// amount is the value of the HTLC that is being created or modified.
amount lnwire.MilliSatoshi

@ -458,15 +458,15 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
// payment circuit within our internal state so we can properly forward
// the ultimate settle message back latter.
case *lnwire.UpdateAddHTLC:
source, err := s.getLinkByShortID(packet.src)
source, err := s.getLinkByShortID(packet.incomingChanID)
if err != nil {
err := errors.Errorf("unable to find channel link "+
"by channel point (%v): %v", packet.src, err)
"by channel point (%v): %v", packet.incomingChanID, err)
log.Error(err)
return err
}
targetLink, err := s.getLinkByShortID(packet.dest)
targetLink, err := s.getLinkByShortID(packet.outgoingChanID)
if err != nil {
// If packet was forwarded from another channel link
// than we should notify this link that some error
@ -481,16 +481,16 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
}
source.HandleSwitchPacket(&htlcPacket{
dest: packet.src,
destID: packet.srcID,
payHash: htlc.PaymentHash,
isObfuscated: true,
incomingChanID: packet.incomingChanID,
incomingHTLCID: packet.incomingHTLCID,
payHash: htlc.PaymentHash,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{
Reason: reason,
},
})
err = errors.Errorf("unable to find link with "+
"destination %v", packet.dest)
"destination %v", packet.outgoingChanID)
log.Error(err)
return err
}
@ -530,10 +530,10 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
}
source.HandleSwitchPacket(&htlcPacket{
dest: packet.src,
destID: packet.srcID,
payHash: htlc.PaymentHash,
isObfuscated: true,
incomingChanID: packet.incomingChanID,
incomingHTLCID: packet.incomingHTLCID,
payHash: htlc.PaymentHash,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{
Reason: reason,
},
@ -555,20 +555,22 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
// payment circuit by forwarding the settle msg to the channel from
// which htlc add packet was initially received.
case *lnwire.UpdateFufillHTLC, *lnwire.UpdateFailHTLC:
if packet.dest == (lnwire.ShortChannelID{}) {
if packet.incomingChanID == (lnwire.ShortChannelID{}) {
// Use circuit map to find the link to forward settle/fail to.
circuit := s.circuits.LookupByHTLC(packet.src, packet.srcID)
circuit := s.circuits.LookupByHTLC(packet.outgoingChanID,
packet.outgoingHTLCID)
if circuit == nil {
err := errors.Errorf("Unable to find source channel for HTLC "+
err := errors.Errorf("Unable to find target channel for HTLC "+
"settle/fail: channel ID = %s, HTLC ID = %d, "+
"payment hash = %x", packet.src, packet.srcID,
packet.payHash[:])
"payment hash = %x", packet.outgoingChanID,
packet.outgoingHTLCID, packet.payHash[:])
log.Error(err)
return err
}
// Remove circuit since we are about to complete the HTLC.
err := s.circuits.Remove(packet.src, packet.srcID)
err := s.circuits.Remove(packet.outgoingChanID,
packet.outgoingHTLCID)
if err != nil {
log.Warnf("Failed to close completed onion circuit for %x: "+
"%s<->%s", packet.payHash[:], circuit.IncomingChanID,
@ -586,11 +588,11 @@ func (s *Switch) handlePacketForward(packet *htlcPacket) error {
htlc.Reason)
}
packet.dest = circuit.IncomingChanID
packet.destID = circuit.IncomingHTLCID
packet.incomingChanID = circuit.IncomingChanID
packet.incomingHTLCID = circuit.IncomingHTLCID
}
source, err := s.getLinkByShortID(packet.dest)
source, err := s.getLinkByShortID(packet.incomingChanID)
if err != nil {
err := errors.Errorf("Unable to get source channel link to "+
"forward HTLC settle/fail: %v", err)

@ -56,10 +56,10 @@ func TestSwitchForward(t *testing.T) {
preimage := [sha256.Size]byte{1}
rhash := fastsha256.Sum256(preimage[:])
packet := &htlcPacket{
src: aliceChannelLink.ShortChanID(),
srcID: 0,
dest: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
incomingChanID: aliceChannelLink.ShortChanID(),
incomingHTLCID: 0,
outgoingChanID: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
htlc: &lnwire.UpdateAddHTLC{
PaymentHash: rhash,
Amount: 1,
@ -73,9 +73,9 @@ func TestSwitchForward(t *testing.T) {
s.addCircuit(&PaymentCircuit{
PaymentHash: packet.payHash,
IncomingChanID: packet.src,
IncomingHTLCID: 0,
OutgoingChanID: packet.dest,
IncomingChanID: packet.incomingChanID,
IncomingHTLCID: packet.incomingHTLCID,
OutgoingChanID: packet.outgoingChanID,
OutgoingHTLCID: 0,
ErrorEncrypter: packet.obfuscator,
})
@ -95,9 +95,10 @@ func TestSwitchForward(t *testing.T) {
// request and sent the htlc settle request back. This request should
// be forwarder back to Alice link.
packet = &htlcPacket{
src: bobChannelLink.ShortChanID(),
payHash: rhash,
amount: 1,
outgoingChanID: bobChannelLink.ShortChanID(),
outgoingHTLCID: 0,
payHash: rhash,
amount: 1,
htlc: &lnwire.UpdateFufillHTLC{
PaymentPreimage: preimage,
},
@ -155,9 +156,9 @@ func TestSkipIneligibleLinksMultiHopForward(t *testing.T) {
preimage := [sha256.Size]byte{1}
rhash := fastsha256.Sum256(preimage[:])
packet = &htlcPacket{
src: aliceChannelLink.ShortChanID(),
srcID: 0,
dest: bobChannelLink.ShortChanID(),
incomingChanID: aliceChannelLink.ShortChanID(),
incomingHTLCID: 0,
outgoingChanID: bobChannelLink.ShortChanID(),
htlc: &lnwire.UpdateAddHTLC{
PaymentHash: rhash,
Amount: 1,
@ -244,10 +245,10 @@ func TestSwitchCancel(t *testing.T) {
preimage := [sha256.Size]byte{1}
rhash := fastsha256.Sum256(preimage[:])
request := &htlcPacket{
src: aliceChannelLink.ShortChanID(),
srcID: 0,
dest: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
incomingChanID: aliceChannelLink.ShortChanID(),
incomingHTLCID: 0,
outgoingChanID: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
htlc: &lnwire.UpdateAddHTLC{
PaymentHash: rhash,
Amount: 1,
@ -261,9 +262,9 @@ func TestSwitchCancel(t *testing.T) {
s.addCircuit(&PaymentCircuit{
PaymentHash: request.payHash,
IncomingChanID: request.src,
IncomingHTLCID: 0,
OutgoingChanID: request.dest,
IncomingChanID: request.incomingChanID,
IncomingHTLCID: request.incomingHTLCID,
OutgoingChanID: request.outgoingChanID,
OutgoingHTLCID: 0,
ErrorEncrypter: request.obfuscator,
})
@ -283,12 +284,12 @@ func TestSwitchCancel(t *testing.T) {
// the add htlc request and sent the htlc settle request back. This
// request should be forwarder back to alice channel link.
request = &htlcPacket{
src: bobChannelLink.ShortChanID(),
srcID: 0,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
outgoingChanID: bobChannelLink.ShortChanID(),
outgoingHTLCID: 0,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
}
// Handle the request and checks that payment circuit works properly.
@ -337,10 +338,10 @@ func TestSwitchAddSamePayment(t *testing.T) {
preimage := [sha256.Size]byte{1}
rhash := fastsha256.Sum256(preimage[:])
request := &htlcPacket{
src: aliceChannelLink.ShortChanID(),
srcID: 0,
dest: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
incomingChanID: aliceChannelLink.ShortChanID(),
incomingHTLCID: 0,
outgoingChanID: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
htlc: &lnwire.UpdateAddHTLC{
PaymentHash: rhash,
Amount: 1,
@ -354,9 +355,9 @@ func TestSwitchAddSamePayment(t *testing.T) {
s.addCircuit(&PaymentCircuit{
PaymentHash: request.payHash,
IncomingChanID: request.src,
IncomingHTLCID: 0,
OutgoingChanID: request.dest,
IncomingChanID: request.incomingChanID,
IncomingHTLCID: request.incomingHTLCID,
OutgoingChanID: request.outgoingChanID,
OutgoingHTLCID: 0,
ErrorEncrypter: request.obfuscator,
})
@ -373,10 +374,10 @@ func TestSwitchAddSamePayment(t *testing.T) {
}
request = &htlcPacket{
src: aliceChannelLink.ShortChanID(),
srcID: 1,
dest: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
incomingChanID: aliceChannelLink.ShortChanID(),
incomingHTLCID: 1,
outgoingChanID: bobChannelLink.ShortChanID(),
obfuscator: newMockObfuscator(),
htlc: &lnwire.UpdateAddHTLC{
PaymentHash: rhash,
Amount: 1,
@ -390,9 +391,9 @@ func TestSwitchAddSamePayment(t *testing.T) {
s.addCircuit(&PaymentCircuit{
PaymentHash: request.payHash,
IncomingChanID: request.src,
IncomingHTLCID: 1,
OutgoingChanID: request.dest,
IncomingChanID: request.incomingChanID,
IncomingHTLCID: request.incomingHTLCID,
OutgoingChanID: request.outgoingChanID,
OutgoingHTLCID: 1,
ErrorEncrypter: request.obfuscator,
})
@ -405,11 +406,12 @@ func TestSwitchAddSamePayment(t *testing.T) {
// the add htlc request and sent the htlc settle request back. This
// request should be forwarder back to alice channel link.
request = &htlcPacket{
src: bobChannelLink.ShortChanID(),
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
outgoingChanID: bobChannelLink.ShortChanID(),
outgoingHTLCID: 0,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
}
// Handle the request and checks that payment circuit works properly.
@ -429,12 +431,12 @@ func TestSwitchAddSamePayment(t *testing.T) {
}
request = &htlcPacket{
src: bobChannelLink.ShortChanID(),
srcID: 1,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
outgoingChanID: bobChannelLink.ShortChanID(),
outgoingHTLCID: 1,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{},
}
// Handle the request and checks that payment circuit works properly.
@ -532,10 +534,11 @@ func TestSwitchSendPayment(t *testing.T) {
}
packet := &htlcPacket{
src: aliceChannelLink.ShortChanID(),
payHash: rhash,
amount: 1,
isObfuscated: true,
outgoingChanID: aliceChannelLink.ShortChanID(),
outgoingHTLCID: 0,
payHash: rhash,
amount: 1,
isObfuscated: true,
htlc: &lnwire.UpdateFailHTLC{
Reason: reason,
ID: 1,