-
Notifications
You must be signed in to change notification settings - Fork 142
rfq: add limit constraints and quote-level enforcement to rfq negotiation #2048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jtobin
wants to merge
5
commits into
lightninglabs:main
Choose a base branch
from
jtobin:limit-constraints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee1c9ee
rfqmsg: add rate limit and min fill to wire and order types
jtobin 9ca5e27
rfq+rpcserver: add proto fields, enforcement, and reject codes
jtobin 2ca0518
rfqmsg+rfq: add unit and property tests for limit-order constraints
jtobin 724faa0
itest: add integration tests for limit-order constraints
jtobin 7db5df6
docs: add release note for limit-order constraints
jtobin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| //go:build itest | ||
|
|
||
| package custom_channels | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "math" | ||
| "math/big" | ||
| "slices" | ||
| "time" | ||
|
|
||
| "github.com/lightninglabs/taproot-assets/asset" | ||
| "github.com/lightninglabs/taproot-assets/itest" | ||
| "github.com/lightninglabs/taproot-assets/proof" | ||
| "github.com/lightninglabs/taproot-assets/rfqmath" | ||
| "github.com/lightninglabs/taproot-assets/rfqmsg" | ||
| "github.com/lightninglabs/taproot-assets/taprpc" | ||
| "github.com/lightninglabs/taproot-assets/taprpc/mintrpc" | ||
| "github.com/lightninglabs/taproot-assets/taprpc/rfqrpc" | ||
| tchrpc "github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc" | ||
| "github.com/lightningnetwork/lnd/lnrpc" | ||
| "github.com/lightningnetwork/lnd/lntest" | ||
| "github.com/lightningnetwork/lnd/lntest/node" | ||
| "github.com/lightningnetwork/lnd/lntest/port" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // testCustomChannelsLimitConstraints verifies that RFQ limit-order | ||
| // constraints (asset_rate_limit, payment_min_amt) work correctly in | ||
| // the context of real asset channels. It negotiates a sell quote with | ||
| // satisfied constraints, then sends a payment using that quote. | ||
| // | ||
| //nolint:lll | ||
| func testCustomChannelsLimitConstraints(_ context.Context, | ||
| net *itest.IntegratedNetworkHarness, t *ccHarnessTest) { | ||
|
|
||
| usdMetaData := &taprpc.AssetMeta{ | ||
| Data: []byte(`{ | ||
| "description":"USD stablecoin for limit constraint test" | ||
| }`), | ||
| Type: taprpc.AssetMetaType_META_TYPE_JSON, | ||
| } | ||
|
|
||
| const decimalDisplay = 6 | ||
| tcAsset := &mintrpc.MintAsset{ | ||
| AssetType: taprpc.AssetType_NORMAL, | ||
| Name: "USD-limits", | ||
| AssetMeta: usdMetaData, | ||
| Amount: 1_000_000_000_000, | ||
| DecimalDisplay: decimalDisplay, | ||
| } | ||
|
|
||
| oracleAddr := fmt.Sprintf( | ||
| "localhost:%d", port.NextAvailablePort(), | ||
| ) | ||
| oracle := itest.NewOracleHarness(oracleAddr) | ||
| oracle.Start(t.t) | ||
| t.t.Cleanup(oracle.Stop) | ||
|
|
||
| lndArgs := slices.Clone(lndArgsTemplate) | ||
| tapdArgs := slices.Clone(tapdArgsTemplateNoOracle) | ||
| tapdArgs = append(tapdArgs, fmt.Sprintf( | ||
| "--experimental.rfq.priceoracleaddress=rfqrpc://%s", | ||
| oracleAddr, | ||
| )) | ||
| tapdArgs = append( | ||
| tapdArgs, | ||
| "--experimental.rfq.priceoracletlsinsecure", | ||
| ) | ||
|
|
||
| charliePort := port.NextAvailablePort() | ||
| tapdArgs = append(tapdArgs, fmt.Sprintf( | ||
| "--proofcourieraddr=%s://%s", | ||
| proof.UniverseRpcCourierType, | ||
| fmt.Sprintf(node.ListenerFormat, charliePort), | ||
| )) | ||
|
|
||
| // Topology: Charlie --[assets]--> Dave --[sats]--> Erin | ||
| charlieLndArgs := slices.Clone(lndArgs) | ||
| charlieLndArgs = append(charlieLndArgs, fmt.Sprintf( | ||
| "--rpclisten=127.0.0.1:%d", charliePort, | ||
| )) | ||
| charlie := net.NewNode("Charlie", charlieLndArgs, tapdArgs) | ||
| dave := net.NewNode("Dave", lndArgs, tapdArgs) | ||
| erin := net.NewNode("Erin", lndArgs, tapdArgs) | ||
|
|
||
| nodes := []*itest.IntegratedNode{charlie, dave, erin} | ||
| connectAllNodes(t.t, net, nodes) | ||
| fundAllNodes(t.t, net, nodes) | ||
|
|
||
| // Open a normal BTC channel between Dave and Erin. | ||
| const btcChannelFundingAmount = 10_000_000 | ||
| chanPointDE := openChannelAndAssert( | ||
| t, net, dave, erin, lntest.OpenChannelParams{ | ||
| Amt: btcChannelFundingAmount, | ||
| SatPerVByte: 5, | ||
| }, | ||
| ) | ||
| defer closeChannelAndAssert(t, net, dave, chanPointDE, false) | ||
|
|
||
| assertChannelKnown(t.t, charlie, chanPointDE) | ||
|
|
||
| // Mint on Charlie. | ||
| mintedAssets := itest.MintAssetsConfirmBatch( | ||
| t.t, net.Miner.Client, asTapd(charlie), | ||
| []*mintrpc.MintAssetRequest{ | ||
| {Asset: tcAsset}, | ||
| }, | ||
| ) | ||
| usdAsset := mintedAssets[0] | ||
| assetID := usdAsset.AssetGenesis.AssetId | ||
|
|
||
| var id asset.ID | ||
| copy(id[:], assetID) | ||
|
|
||
| // Oracle price: ~66,548.40 USD/BTC with decimal display 6. | ||
| salePrice := rfqmath.NewBigIntFixedPoint(65_217_43, 2) | ||
| purchasePrice := rfqmath.NewBigIntFixedPoint(67_879_37, 2) | ||
| factor := rfqmath.NewBigInt( | ||
| big.NewInt(int64(math.Pow10(decimalDisplay))), | ||
| ) | ||
| salePrice.Coefficient = salePrice.Coefficient.Mul(factor) | ||
| purchasePrice.Coefficient = purchasePrice.Coefficient.Mul( | ||
| factor, | ||
| ) | ||
| oracle.SetPrice( | ||
| asset.NewSpecifierFromId(id), purchasePrice, salePrice, | ||
| ) | ||
|
|
||
| t.Logf("Syncing universes...") | ||
| syncUniverses(t.t, charlie, dave, erin) | ||
|
|
||
| // Send assets to Dave so he has a balance. | ||
| const sendAmount = uint64(400_000_000) | ||
| charlieFundingAmount := usdAsset.Amount - sendAmount | ||
|
|
||
| ctxb := context.Background() | ||
| daveAddr, err := dave.NewAddr(ctxb, &taprpc.NewAddrRequest{ | ||
| Amt: sendAmount, | ||
| AssetId: assetID, | ||
| ProofCourierAddr: fmt.Sprintf( | ||
| "%s://%s", proof.UniverseRpcCourierType, | ||
| charlie.RPCAddr(), | ||
| ), | ||
| }) | ||
| require.NoError(t.t, err) | ||
|
|
||
| sendResp, err := charlie.SendAsset( | ||
| ctxb, &taprpc.SendAssetRequest{ | ||
| TapAddrs: []string{daveAddr.Encoded}, | ||
| }, | ||
| ) | ||
| require.NoError(t.t, err) | ||
| itest.ConfirmAndAssertOutboundTransfer( | ||
| t.t, net.Miner.Client, asTapd(charlie), sendResp, | ||
| assetID, | ||
| []uint64{usdAsset.Amount - sendAmount, sendAmount}, | ||
| 0, 1, | ||
| ) | ||
| itest.AssertNonInteractiveRecvComplete(t.t, asTapd(dave), 1) | ||
|
|
||
| // Open asset channel Charlie → Dave. | ||
| t.Logf("Opening asset channel Charlie → Dave...") | ||
| net.EnsureConnected(t.t, charlie, dave) | ||
| fundResp, err := charlie.FundChannel( | ||
| ctxb, &tchrpc.FundChannelRequest{ | ||
| AssetAmount: charlieFundingAmount, | ||
| AssetId: assetID, | ||
| PeerPubkey: dave.PubKey[:], | ||
| FeeRateSatPerVbyte: 5, | ||
| PushSat: DefaultPushSat, | ||
| }, | ||
| ) | ||
| require.NoError(t.t, err) | ||
| t.Logf("Funded channel: %v", fundResp) | ||
|
|
||
| mineBlocks(t, net, 6, 1) | ||
|
|
||
| chanPointCD := &lnrpc.ChannelPoint{ | ||
| OutputIndex: uint32(fundResp.OutputIndex), | ||
| FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{ | ||
| FundingTxidStr: fundResp.Txid, | ||
| }, | ||
| } | ||
|
|
||
| logBalance(t.t, nodes, assetID, "after channel open") | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Negotiate a sell order from Charlie with constraints. | ||
| // Rate limit is set well above the oracle rate (ceiling for | ||
| // sell), so the constraint is satisfied. | ||
| // ----------------------------------------------------------------- | ||
| t.Logf("Negotiating sell order with constraints...") | ||
|
|
||
| inOneHour := time.Now().Add(time.Hour) | ||
| sellResp, err := asTapd(charlie).RfqClient.AddAssetSellOrder( | ||
| ctxb, &rfqrpc.AddAssetSellOrderRequest{ | ||
| AssetSpecifier: &rfqrpc.AssetSpecifier{ | ||
| Id: &rfqrpc.AssetSpecifier_AssetId{ | ||
| AssetId: assetID, | ||
| }, | ||
| }, | ||
| PaymentMaxAmt: 180_000_000, | ||
| AssetRateLimit: &rfqrpc.FixedPoint{ | ||
| // Ceiling well above oracle rate. | ||
| Coefficient: "100000000000000", | ||
| Scale: 2, | ||
| }, | ||
| Expiry: uint64(inOneHour.Unix()), | ||
| PeerPubKey: dave.PubKey[:], | ||
| TimeoutSeconds: 10, | ||
| }, | ||
| ) | ||
| require.NoError(t.t, err, "sell order with constraints") | ||
|
|
||
| accepted := sellResp.GetAcceptedQuote() | ||
| require.NotNil(t.t, accepted, "expected accepted sell quote") | ||
| t.Logf("Sell quote accepted: scid=%d", accepted.Scid) | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Pay an invoice using the pre-negotiated quote. | ||
| // ----------------------------------------------------------------- | ||
| t.Logf("Paying invoice with constrained quote...") | ||
|
|
||
| // Erin has no asset channel, so we create a regular BTC | ||
| // invoice. Charlie pays it with assets via the sell quote. | ||
| const invoiceMsat = 100_000_000 // 100K sats | ||
| invoiceResp, err := erin.LightningClient.AddInvoice( | ||
| ctxb, &lnrpc.Invoice{ | ||
| ValueMsat: invoiceMsat, | ||
| }, | ||
| ) | ||
| require.NoError(t.t, err) | ||
|
|
||
| var quoteID rfqmsg.ID | ||
| copy(quoteID[:], accepted.Id) | ||
|
|
||
| numUnits, _ := payInvoiceWithAssets( | ||
| t.t, charlie, dave, | ||
| invoiceResp.PaymentRequest, | ||
| assetID, withRFQ(quoteID), | ||
| ) | ||
| require.Greater(t.t, numUnits, uint64(0)) | ||
|
|
||
| logBalance(t.t, nodes, assetID, "after payment") | ||
| t.Logf("Payment completed: %d asset units sent", numUnits) | ||
|
|
||
| // Close channels. | ||
| closeAssetChannelAndAssert( | ||
| t, net, charlie, dave, chanPointCD, | ||
| [][]byte{assetID}, nil, charlie, | ||
| noOpCoOpCloseBalanceCheck, | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.