Skip to content

Commit

Permalink
itest: extend liquidity edge cases for rfq htlc tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Nov 28, 2024
1 parent 94b0067 commit 82d567c
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 45 deletions.
66 changes: 43 additions & 23 deletions itest/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const (
DefaultPushSat int64 = 1062
)

var (
NoScidOpt = fn.None[rfqmsg.SerialisedScid]()
)

// createTestAssetNetwork sends asset funds from Charlie to Dave and Erin, so
// they can fund asset channels with Yara and Fabia, respectively. So the asset
// channels created are Charlie->Dave, Dave->Yara, Erin->Fabia. The channels
Expand Down Expand Up @@ -714,6 +718,9 @@ func sendAssetKeySendPayment(t *testing.T, src, dst *HarnessNode, amt uint64,

result, err := getAssetPaymentResult(stream, false)
require.NoError(t, err)
if result.Status == lnrpc.Payment_FAILED {
t.Logf("Failure reason: %v", result.FailureReason)
}
require.Equal(t, expectedStatus, result.Status)

expectedReason := failReason.UnwrapOr(
Expand Down Expand Up @@ -792,7 +799,7 @@ func createAndPayNormalInvoice(t *testing.T, src, rfqPeer, dst *HarnessNode,

numUnits, _ := payInvoiceWithAssets(
t, src, rfqPeer, invoiceResp.PaymentRequest, assetID, smallShards,
fn.None[lnrpc.Payment_PaymentStatus](),
fn.None[lnrpc.Payment_PaymentStatus](), NoScidOpt,
)

return numUnits
Expand Down Expand Up @@ -858,7 +865,8 @@ func payInvoiceWithSatoshiLastHop(t *testing.T, payer *HarnessNode,

func payInvoiceWithAssets(t *testing.T, payer, rfqPeer *HarnessNode,
payReq string, assetID []byte, smallShards bool,
expectedPayStatus fn.Option[lnrpc.Payment_PaymentStatus]) (uint64,
expectedPayStatus fn.Option[lnrpc.Payment_PaymentStatus],
manualRfq fn.Option[rfqmsg.SerialisedScid]) (uint64,
rfqmath.BigIntFixedPoint) {

ctxb := context.Background()
Expand Down Expand Up @@ -886,40 +894,52 @@ func payInvoiceWithAssets(t *testing.T, payer, rfqPeer *HarnessNode,
AssetId: assetID,
PeerPubkey: rfqPeer.PubKey[:],
PaymentRequest: sendReq,
Scid: uint64(manualRfq.UnwrapOr(0)),
})
require.NoError(t, err)

// We want to receive the accepted quote message first, so we know how
// many assets we're going to pay.
quoteMsg, err := stream.Recv()
require.NoError(t, err)
acceptedQuote := quoteMsg.GetAcceptedSellOrder()
require.NotNil(t, acceptedQuote)
var (
numUnits uint64
rateVal rfqmath.FixedPoint[rfqmath.BigInt]
)

peerPubKey := acceptedQuote.Peer
require.Equal(t, peerPubKey, rfqPeer.PubKeyStr)
if manualRfq.IsNone() {
// We want to receive the accepted quote message first, so we know how
// many assets we're going to pay.
quoteMsg, err := stream.Recv()
require.NoError(t, err)
acceptedQuote := quoteMsg.GetAcceptedSellOrder()
require.NotNil(t, acceptedQuote)

rpcRate := acceptedQuote.BidAssetRate
rate, err := rfqrpc.UnmarshalFixedPoint(rpcRate)
require.NoError(t, err)
peerPubKey := acceptedQuote.Peer
require.Equal(t, peerPubKey, rfqPeer.PubKeyStr)

t.Logf("Got quote for %v asset units per BTC", rate)
rpcRate := acceptedQuote.BidAssetRate
rate, err := rfqrpc.UnmarshalFixedPoint(rpcRate)
require.NoError(t, err)

rateVal = *rate

amountMsat := lnwire.MilliSatoshi(decodedInvoice.NumMsat)
milliSatsFP := rfqmath.MilliSatoshiToUnits(amountMsat, *rate)
numUnits := milliSatsFP.ScaleTo(0).ToUint64()
msatPerUnit := float64(decodedInvoice.NumMsat) / float64(numUnits)
t.Logf("Got quote for %v asset units at %3f msat/unit from peer %s "+
"with SCID %d", numUnits, msatPerUnit, peerPubKey,
acceptedQuote.Scid)
t.Logf("Got quote for %v asset units per BTC", rate)

amountMsat := lnwire.MilliSatoshi(decodedInvoice.NumMsat)
milliSatsFP := rfqmath.MilliSatoshiToUnits(amountMsat, *rate)
numUnits = milliSatsFP.ScaleTo(0).ToUint64()
msatPerUnit := float64(decodedInvoice.NumMsat) / float64(numUnits)
t.Logf("Got quote for %v asset units at %3f msat/unit from peer %s "+
"with SCID %d", numUnits, msatPerUnit, peerPubKey,
acceptedQuote.Scid)
}

expectedStatus := expectedPayStatus.UnwrapOr(lnrpc.Payment_SUCCEEDED)

result, err := getAssetPaymentResult(stream, expectedPayStatus.IsSome())
result, err := getAssetPaymentResult(
stream, expectedStatus == lnrpc.Payment_IN_FLIGHT,
)
require.NoError(t, err)
require.Equal(t, expectedStatus, result.Status)

return numUnits, *rate
return numUnits, rateVal
}

func createAssetInvoice(t *testing.T, dstRfqPeer, dst *HarnessNode,
Expand Down
Loading

0 comments on commit 82d567c

Please sign in to comment.