From 848801267a58b55a99af52e5b83513182a1c19d3 Mon Sep 17 00:00:00 2001 From: Yaacov Akiba Slama Date: Tue, 17 Jan 2023 16:25:40 +0200 Subject: [PATCH] Open a channel only if the nextHop is unknown or the destination --- cln_interceptor.go | 19 +++++++++++++++++-- intercept.go | 4 ++-- lnd_interceptor.go | 16 ++++++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cln_interceptor.go b/cln_interceptor.go index 7ef5e66..b29fcbc 100644 --- a/cln_interceptor.go +++ b/cln_interceptor.go @@ -133,11 +133,26 @@ func (i *ClnHtlcInterceptor) intercept() error { log.Printf("unexpected error in interceptor.Recv() %v", err) break } + nextHop := "" + channels, err := i.client.client.GetChannel(request.Onion.ShortChannelId) + if err != nil { + for _, c := range channels { + if c.Source == i.config.NodePubkey { + nextHop = c.Destination + break + } + if c.Destination == i.config.NodePubkey { + nextHop = c.Source + break + } + } + } - log.Printf("correlationid: %v\nhtlc: %v\nchanID: %v\nincoming amount: %v\noutgoing amount: %v\nincoming expiry: %v\noutgoing expiry: %v\npaymentHash: %v\nonionBlob: %v\n\n", + log.Printf("correlationid: %v\nhtlc: %v\nchanID: %v\nnextHop: %v\nincoming amount: %v\noutgoing amount: %v\nincoming expiry: %v\noutgoing expiry: %v\npaymentHash: %v\nonionBlob: %v\n\n", request.Correlationid, request.Htlc, request.Onion.ShortChannelId, + nextHop, request.Htlc.AmountMsat, //with fees request.Onion.ForwardMsat, request.Htlc.CltvExpiryRelative, @@ -153,7 +168,7 @@ func (i *ClnHtlcInterceptor) intercept() error { interceptorClient.Send(i.defaultResolution(request)) i.doneWg.Done() } - interceptResult := intercept(i.client, i.config, paymentHash, request.Onion.ForwardMsat, request.Htlc.CltvExpiry) + interceptResult := intercept(i.client, i.config, nextHop, paymentHash, request.Onion.ForwardMsat, request.Htlc.CltvExpiry) switch interceptResult.action { case INTERCEPT_RESUME_WITH_ONION: interceptorClient.Send(i.resumeWithOnion(request, interceptResult)) diff --git a/intercept.go b/intercept.go index d15c861..14a9305 100644 --- a/intercept.go +++ b/intercept.go @@ -45,7 +45,7 @@ type interceptResult struct { onionBlob []byte } -func intercept(client LightningClient, config *NodeConfig, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32) interceptResult { +func intercept(client LightningClient, config *NodeConfig, nextHop string, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32) interceptResult { reqPaymentHashStr := hex.EncodeToString(reqPaymentHash) resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) { paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := paymentInfo(reqPaymentHash) @@ -58,7 +58,7 @@ func intercept(client LightningClient, config *NodeConfig, reqPaymentHash []byte } log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v", paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat) - if paymentSecret == nil { + if paymentSecret == nil || (nextHop != "" && nextHop != hex.EncodeToString(destination)) { return interceptResult{ action: INTERCEPT_RESUME, }, nil diff --git a/lnd_interceptor.go b/lnd_interceptor.go index e89ca4c..5e0f812 100644 --- a/lnd_interceptor.go +++ b/lnd_interceptor.go @@ -123,9 +123,21 @@ func (i *LndHtlcInterceptor) intercept() error { break } - fmt.Printf("htlc: %v\nchanID: %v\nincoming amount: %v\noutgoing amount: %v\nincomin expiry: %v\noutgoing expiry: %v\npaymentHash: %x\nonionBlob: %x\n\n", + nextHop := "" + chanInfo, err := i.client.client.GetChanInfo(context.Background(), &lnrpc.ChanInfoRequest{ChanId: request.OutgoingRequestedChanId}) + if err == nil && chanInfo != nil { + if chanInfo.Node1Pub == i.config.NodePubkey { + nextHop = chanInfo.Node2Pub + } + if chanInfo.Node2Pub == i.config.NodePubkey { + nextHop = chanInfo.Node1Pub + } + } + + fmt.Printf("htlc: %v\nchanID: %v\nnextHop: %v\nincoming amount: %v\noutgoing amount: %v\nincomin expiry: %v\noutgoing expiry: %v\npaymentHash: %x\nonionBlob: %x\n\n", request.IncomingCircuitKey.HtlcId, request.IncomingCircuitKey.ChanId, + nextHop, request.IncomingAmountMsat, request.OutgoingAmountMsat, request.IncomingExpiry, @@ -136,7 +148,7 @@ func (i *LndHtlcInterceptor) intercept() error { i.doneWg.Add(1) go func() { - interceptResult := intercept(i.client, i.config, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry) + interceptResult := intercept(i.client, i.config, nextHop, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry) switch interceptResult.action { case INTERCEPT_RESUME_WITH_ONION: interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{