1
1
package tapchannel
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"sync"
@@ -19,6 +20,7 @@ import (
19
20
"github.com/lightningnetwork/lnd/lnutils"
20
21
"github.com/lightningnetwork/lnd/lnwallet"
21
22
"github.com/lightningnetwork/lnd/lnwire"
23
+ "github.com/lightningnetwork/lnd/routing/route"
22
24
"github.com/lightningnetwork/lnd/tlv"
23
25
)
24
26
@@ -116,8 +118,8 @@ func (s *AuxTrafficShaper) ShouldHandleTraffic(cid lnwire.ShortChannelID,
116
118
// called first.
117
119
func (s * AuxTrafficShaper ) PaymentBandwidth (fundingBlob , htlcBlob ,
118
120
commitmentBlob lfn.Option [tlv.Blob ], linkBandwidth ,
119
- htlcAmt lnwire.MilliSatoshi ,
120
- htlcView lnwallet. AuxHtlcView ) (lnwire.MilliSatoshi , error ) {
121
+ htlcAmt lnwire.MilliSatoshi , htlcView lnwallet. AuxHtlcView ,
122
+ peer * route. Vertex ) (lnwire.MilliSatoshi , error ) {
121
123
122
124
fundingBlobBytes := fundingBlob .UnwrapOr (nil )
123
125
htlcBytes := htlcBlob .UnwrapOr (nil )
@@ -230,6 +232,7 @@ func (s *AuxTrafficShaper) PaymentBandwidth(fundingBlob, htlcBlob,
230
232
// the asset units in our local balance.
231
233
return s .paymentBandwidth (
232
234
htlc , computedLocal , linkBandwidth , minHtlcAmt , fundingChan ,
235
+ peer ,
233
236
)
234
237
}
235
238
@@ -277,11 +280,12 @@ func paymentBandwidthAssetUnits(htlcAssetAmount, computedLocal uint64,
277
280
}
278
281
279
282
// paymentBandwidth returns the available payment bandwidth of the channel based
280
- // on the list of availalbe RFQ IDs. If any of those IDs matches the channel, we
283
+ // on the list of available RFQ IDs. If any of those IDs matches the channel, we
281
284
// calculate the bandwidth based on its asset rate.
282
285
func (s * AuxTrafficShaper ) paymentBandwidth (htlc * rfqmsg.Htlc ,
283
286
localBalance uint64 , linkBandwidth , minHtlcAmt lnwire.MilliSatoshi ,
284
- fundingChan * cmsg.OpenChannel ) (lnwire.MilliSatoshi , error ) {
287
+ fundingChan * cmsg.OpenChannel ,
288
+ peer * route.Vertex ) (lnwire.MilliSatoshi , error ) {
285
289
286
290
// If the HTLC doesn't have any available RFQ IDs, it's incomplete, and
287
291
// we cannot determine the bandwidth.
@@ -292,26 +296,29 @@ func (s *AuxTrafficShaper) paymentBandwidth(htlc *rfqmsg.Htlc,
292
296
}
293
297
294
298
// Retrieve the available RFQ IDs.
295
- avaliableIDs := htlc .AvailableRfqIDs .UnsafeFromSome ().Val .IDs
299
+ availableIDs := htlc .AvailableRfqIDs .UnsafeFromSome ().Val .IDs
296
300
297
301
// For this rfqID we'll fetch the corresponding quote and rate.
298
302
acceptedSellQuotes := s .cfg .RfqManager .PeerAcceptedSellQuotes ()
299
303
acceptedBuyQuotes := s .cfg .RfqManager .LocalAcceptedBuyQuotes ()
300
304
301
- for _ , rfqID := range avaliableIDs {
305
+ for _ , rfqID := range availableIDs {
302
306
sellQuote , isSellQuote := acceptedSellQuotes [rfqID .Scid ()]
303
307
buyQuote , isBuyQuote := acceptedBuyQuotes [rfqID .Scid ()]
304
308
305
309
var (
306
310
rate rfqmsg.AssetRate
307
311
specifier asset.Specifier
312
+ quotePeer * route.Vertex
308
313
)
309
314
switch {
310
315
case isSellQuote :
316
+ quotePeer = & sellQuote .Peer
311
317
rate = sellQuote .AssetRate
312
318
specifier = sellQuote .Request .AssetSpecifier
313
319
314
320
case isBuyQuote :
321
+ quotePeer = & buyQuote .Peer
315
322
rate = buyQuote .AssetRate
316
323
specifier = buyQuote .Request .AssetSpecifier
317
324
@@ -320,6 +327,12 @@ func (s *AuxTrafficShaper) paymentBandwidth(htlc *rfqmsg.Htlc,
320
327
"ID %x (SCID %d)" , rfqID [:], rfqID .Scid ())
321
328
}
322
329
330
+ // If the channel peer does not match the quote peer, continue
331
+ // to the next available quote.
332
+ if ! bytes .Equal (quotePeer [:], peer [:]) {
333
+ continue
334
+ }
335
+
323
336
bandwidth , err := s .paymentBandwidthRFQ (
324
337
rfqID , rate , specifier , localBalance , linkBandwidth ,
325
338
minHtlcAmt , fundingChan ,
@@ -431,8 +444,8 @@ func ComputeLocalBalance(commitment cmsg.Commitment,
431
444
// blob of an HTLC, may produce a different blob or modify the amount of bitcoin
432
445
// this HTLC should carry.
433
446
func (s * AuxTrafficShaper ) ProduceHtlcExtraData (totalAmount lnwire.MilliSatoshi ,
434
- htlcCustomRecords lnwire.CustomRecords ) (lnwire. MilliSatoshi ,
435
- lnwire.CustomRecords , error ) {
447
+ htlcCustomRecords lnwire.CustomRecords ,
448
+ peer * route. Vertex ) (lnwire. MilliSatoshi , lnwire.CustomRecords , error ) {
436
449
437
450
if ! rfqmsg .HasAssetHTLCCustomRecords (htlcCustomRecords ) {
438
451
log .Tracef ("No asset HTLC custom records, not producing " +
@@ -456,16 +469,43 @@ func (s *AuxTrafficShaper) ProduceHtlcExtraData(totalAmount lnwire.MilliSatoshi,
456
469
return totalAmount , htlcCustomRecords , nil
457
470
}
458
471
459
- if htlc .RfqID .ValOpt ().IsNone () {
460
- return 0 , nil , fmt .Errorf ("no RFQ ID present in HTLC blob" )
472
+ if htlc .AvailableRfqIDs .IsNone () {
473
+ return 0 , nil , fmt .Errorf ("no available RFQ IDs present in " +
474
+ "HTLC blob" )
461
475
}
462
476
463
- rfqID := htlc .RfqID .ValOpt ().UnsafeFromSome ()
464
477
acceptedQuotes := s .cfg .RfqManager .PeerAcceptedSellQuotes ()
465
- quote , ok := acceptedQuotes [rfqID .Scid ()]
466
- if ! ok {
467
- return 0 , nil , fmt .Errorf ("no accepted quote found for RFQ ID " +
468
- "%x (SCID %d)" , rfqID [:], rfqID .Scid ())
478
+ availableIDs := htlc .AvailableRfqIDs .UnsafeFromSome ().Val .IDs
479
+
480
+ var (
481
+ rfqID rfqmsg.ID
482
+ quote rfqmsg.SellAccept
483
+ )
484
+
485
+ // Let's find the quote that matches this peer. This will be the quote
486
+ // that we'll use to calculate the asset units. Given that we may only
487
+ // establish a maximum of 1 quote per peer per payment, this check is
488
+ // safe to perform as there are no competing quotes for a certain peer.
489
+ for _ , id := range availableIDs {
490
+ q , ok := acceptedQuotes [id .Scid ()]
491
+ if ! ok {
492
+ continue
493
+ }
494
+
495
+ // We found the quote to use, now let's set the related fields.
496
+ if q .Peer .String () == peer .String () {
497
+ // This is the actual RFQ ID that our peer will use to
498
+ // perform checks on their end.
499
+ rfqID = id
500
+ htlc .RfqID = rfqmsg .SomeRfqIDRecord (rfqID )
501
+ quote = q
502
+ break
503
+ }
504
+ }
505
+
506
+ if htlc .RfqID .IsNone () {
507
+ return 0 , nil , fmt .Errorf ("none of the available RFQ IDs " +
508
+ "match our peer=%s" , peer )
469
509
}
470
510
471
511
// Now that we've queried the accepted quote, we know how many asset
0 commit comments