From 80bf4bf0143ef62e70e5096e5d4f2e092e33447f Mon Sep 17 00:00:00 2001 From: carla Date: Thu, 27 Jan 2022 15:09:20 +0200 Subject: [PATCH] invoicerpc: return hop hints from select hop hint rather than functions Update SelectHopHints to return a set of hop hints that can be converted to route hints / functional options by the caller. This change allows external code to call SelectHopHints to get a set of route hints and use them as it likes, rather than limiting the return value to functional options for invoice creation. --- lnrpc/invoicesrpc/addinvoice.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lnrpc/invoicesrpc/addinvoice.go b/lnrpc/invoicesrpc/addinvoice.go index a2c7ffd07..b9a4712f3 100644 --- a/lnrpc/invoicesrpc/addinvoice.go +++ b/lnrpc/invoicesrpc/addinvoice.go @@ -407,7 +407,15 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig, numMaxHophints, ) - options = append(options, hopHints...) + // Convert our set of selected hop hints into route + // hints and add to our invoice options. + for _, hopHint := range hopHints { + routeHint := zpay32.RouteHint(hopHint) + + options = append( + options, routeHint, + ) + } } } @@ -540,7 +548,7 @@ func chanCanBeHopHint(channel *HopHintInfo, cfg *SelectHopHintsCfg) ( // addHopHint creates a hop hint out of the passed channel and channel policy. // The new hop hint is appended to the passed slice. -func addHopHint(hopHints *[]func(*zpay32.Invoice), +func addHopHint(hopHints *[][]zpay32.HopHint, channel *HopHintInfo, chanPolicy *channeldb.ChannelEdgePolicy) { hopHint := zpay32.HopHint{ @@ -552,9 +560,8 @@ func addHopHint(hopHints *[]func(*zpay32.Invoice), ), CLTVExpiryDelta: chanPolicy.TimeLockDelta, } - *hopHints = append( - *hopHints, zpay32.RouteHint([]zpay32.HopHint{hopHint}), - ) + + *hopHints = append(*hopHints, []zpay32.HopHint{hopHint}) } // HopHintInfo contains the channel information required to create a hop hint. @@ -623,14 +630,14 @@ func newSelectHopHintsCfg(invoicesCfg *AddInvoiceConfig) *SelectHopHintsCfg { // TODO(roasbeef): do proper sub-set sum max hints usually << numChans func SelectHopHints(amtMSat lnwire.MilliSatoshi, cfg *SelectHopHintsCfg, openChannels []*HopHintInfo, - numMaxHophints int) []func(*zpay32.Invoice) { + numMaxHophints int) [][]zpay32.HopHint { // We'll add our hop hints in two passes, first we'll add all channels // that are eligible to be hop hints, and also have a local balance // above the payment amount. var totalHintBandwidth lnwire.MilliSatoshi hopHintChans := make(map[wire.OutPoint]struct{}) - hopHints := make([]func(*zpay32.Invoice), 0, numMaxHophints) + hopHints := make([][]zpay32.HopHint, 0, numMaxHophints) for _, channel := range openChannels { // If this channel can't be a hop hint, then skip it. edgePolicy, canBeHopHint := chanCanBeHopHint(channel, cfg)