Skip to content

Commit

Permalink
tapchannel: aux inv manager test fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Oct 22, 2024
1 parent 34eb681 commit dcf1c50
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions tapchannel/aux_invoice_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"crypto/sha256"
"fmt"
"math/big"
"testing"
"time"

"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/rfqmsg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
Expand All @@ -27,6 +29,8 @@ const (
var (
// The node ID to be used for the RFQ peer.
testNodeID = route.Vertex{1, 2, 3}

assetRate = big.NewInt(1_000_000)
)

// mockRfqManager mocks the interface of the rfq manager required by the aux
Expand Down Expand Up @@ -139,16 +143,18 @@ func (m *mockHtlcModifierProperty) HtlcModifier(ctx context.Context,
quote, ok := m.rfqMap[rfqID.Scid()]
require.True(m.t, ok)

assetRate := lnwire.MilliSatoshi(quote.AssetRate.ToUint64())

assetUnits := lnwire.MilliSatoshi(htlc.Amounts.Val.Sum())
assetValueMsat := assetUnits * quote.AskPrice
assetValueMsat := assetUnits * assetRate

acceptedMsat := lnwire.MilliSatoshi(0)
for _, htlc := range r.Invoice.Htlcs {
acceptedMsat += lnwire.MilliSatoshi(htlc.AmtMsat)
}

marginHtlcs := lnwire.MilliSatoshi(len(r.Invoice.Htlcs) + 1)
marginMsat := marginHtlcs * quote.AskPrice
marginMsat := marginHtlcs * assetRate

totalMsatIn := marginMsat + assetValueMsat + acceptedMsat

Expand Down Expand Up @@ -240,8 +246,11 @@ func TestAuxInvoiceManager(t *testing.T) {
},
buyQuotes: rfq.BuyAcceptMap{
dummyRfqID(31).Scid(): {
Peer: testNodeID,
AskPrice: 1_000_000,
Peer: testNodeID,
AssetRate: rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(assetRate),
Scale: 0,
},
},
},
},
Expand Down Expand Up @@ -270,8 +279,11 @@ func TestAuxInvoiceManager(t *testing.T) {
},
buyQuotes: rfq.BuyAcceptMap{
dummyRfqID(31).Scid(): {
Peer: testNodeID,
AskPrice: 500_000,
Peer: testNodeID,
AssetRate: rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(assetRate),
Scale: 0,
},
},
},
},
Expand Down Expand Up @@ -455,7 +467,7 @@ func genBuyQuotes(t *rapid.T, rfqMap rfq.BuyAcceptMap, units, amtMsat uint64,
scid rfqmsg.ID) {

var peer route.Vertex
askPrice := lnwire.MilliSatoshi(0)
askPrice := assetRate

// Introduce a 1/8 chance that the quote's peerID is not correct.
noPeerMatch := rapid.Uint16().Draw(t, "nodeID_mismatch_probability")
Expand All @@ -469,17 +481,23 @@ func genBuyQuotes(t *rapid.T, rfqMap rfq.BuyAcceptMap, units, amtMsat uint64,
// a random total asset value.
noValueMatch := rapid.Uint16().Draw(t, "no_asset_value_match")
if noValueMatch%5 == 0 {
askPrice = lnwire.MilliSatoshi(
rapid.Uint64Range(
0, 250_000,
).Draw(t, "asset_msat_value"))
askPrice = big.NewInt(
rapid.Int64Range(0, 250_000).Draw(
t, "asset_msat_value",
),
)
} else {
askPrice = lnwire.MilliSatoshi(amtMsat / units)
bigAmtMsat := big.NewInt(int64(amtMsat))
bigUnits := big.NewInt(int64(units))
askPrice.Div(bigAmtMsat, bigUnits)
}

rfqMap[scid.Scid()] = rfqmsg.BuyAccept{
Peer: peer,
AskPrice: askPrice,
Peer: peer,
AssetRate: rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(askPrice),
Scale: 0,
},
}
}

Expand Down

0 comments on commit dcf1c50

Please sign in to comment.