From 74db43282ce532a2f5aa49779dd5ef1a8c7c1347 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 23 Jun 2020 16:19:41 +0200 Subject: [PATCH] htlcswitch: encapsulate exposed packet info --- htlcswitch/interceptable_switch.go | 18 +++++++++--------- htlcswitch/interfaces.go | 23 +++++++++++++++++++---- lnrpc/routerrpc/forward_interceptor.go | 10 +++++----- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/htlcswitch/interceptable_switch.go b/htlcswitch/interceptable_switch.go index cdf5bae0..2ca54e42 100644 --- a/htlcswitch/interceptable_switch.go +++ b/htlcswitch/interceptable_switch.go @@ -115,15 +115,15 @@ type interceptedForward struct { } // Packet returns the intercepted htlc packet. -func (f *interceptedForward) Packet() lnwire.UpdateAddHTLC { - return *f.htlc -} - -// CircuitKey returns the circuit key for the intercepted htlc. -func (f *interceptedForward) CircuitKey() channeldb.CircuitKey { - return channeldb.CircuitKey{ - ChanID: f.packet.incomingChanID, - HtlcID: f.packet.incomingHTLCID, +func (f *interceptedForward) Packet() InterceptedPacket { + return InterceptedPacket{ + IncomingCircuit: channeldb.CircuitKey{ + ChanID: f.packet.incomingChanID, + HtlcID: f.packet.incomingHTLCID, + }, + Hash: f.htlc.PaymentHash, + OutgoingExpiry: f.htlc.Expiry, + OutgoingAmount: f.htlc.Amount, } } diff --git a/htlcswitch/interfaces.go b/htlcswitch/interfaces.go index 67b1a458..74b1a568 100644 --- a/htlcswitch/interfaces.go +++ b/htlcswitch/interfaces.go @@ -200,17 +200,32 @@ type InterceptableHtlcForwarder interface { // and resolve it later or let the switch execute its default behavior. type ForwardInterceptor func(InterceptedForward) bool +// InterceptedPacket contains the relevant information for the interceptor about +// an htlc. +type InterceptedPacket struct { + // IncomingCircuit contains the incoming channel and htlc id of the + // packet. + IncomingCircuit channeldb.CircuitKey + + // Hash is the payment hash of the htlc. + Hash lntypes.Hash + + // OutgoingExpiry is the absolute block height at which the outgoing + // htlc expires. + OutgoingExpiry uint32 + + // OutgoingAmount is the amount to forward. + OutgoingAmount lnwire.MilliSatoshi +} + // InterceptedForward is passed to the ForwardInterceptor for every forwarded // htlc. It contains all the information about the packet which accordingly // the interceptor decides if to hold or not. // In addition this interface allows a later resolution by calling either // Resume, Settle or Fail. type InterceptedForward interface { - // CircuitKey returns the intercepted packet. - CircuitKey() channeldb.CircuitKey - // Packet returns the intercepted packet. - Packet() lnwire.UpdateAddHTLC + Packet() InterceptedPacket // Resume notifies the intention to resume an existing hold forward. This // basically means the caller wants to resume with the default behavior for diff --git a/lnrpc/routerrpc/forward_interceptor.go b/lnrpc/routerrpc/forward_interceptor.go index 9844c572..00db973d 100644 --- a/lnrpc/routerrpc/forward_interceptor.go +++ b/lnrpc/routerrpc/forward_interceptor.go @@ -148,18 +148,18 @@ func (r *forwardInterceptor) holdAndForwardToClient( forward htlcswitch.InterceptedForward) error { htlc := forward.Packet() - inKey := forward.CircuitKey() + inKey := htlc.IncomingCircuit // First hold the forward, then send to client. - r.holdForwards[forward.CircuitKey()] = forward + r.holdForwards[inKey] = forward interceptionRequest := &ForwardHtlcInterceptRequest{ IncomingCircuitKey: &CircuitKey{ ChanId: inKey.ChanID.ToUint64(), HtlcId: inKey.HtlcID, }, - PaymentHash: htlc.PaymentHash[:], - OutgoingAmountMsat: uint64(htlc.Amount), - OutgoingExpiry: htlc.Expiry, + PaymentHash: htlc.Hash[:], + OutgoingAmountMsat: uint64(htlc.OutgoingAmount), + OutgoingExpiry: htlc.OutgoingExpiry, } return r.stream.Send(interceptionRequest)