Skip to content

Commit

Permalink
rfq: make BuyOrder.Peer field an Option type
Browse files Browse the repository at this point in the history
Currently, the `BuyOrder.Peer` field must be specified, but in the
future, the negotiator should be able to select the optimal peer
automatically. This commit updates the interface to support this future
functionality by making `BuyOrder.Peer` an Option type. Note that this
field was already a pointer.
  • Loading branch information
ffranr committed Nov 6, 2024
1 parent 858372d commit 67adb56
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
7 changes: 5 additions & 2 deletions rfq/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,10 @@ type BuyOrder struct {

// Peer is the peer that the buy order is intended for. This field is
// optional.
Peer *route.Vertex
//
// TODO(ffranr): Currently, this field must be specified. In the future,
// the negotiator should be able to determine the optimal peer.
Peer fn.Option[route.Vertex]
}

// UpsertAssetBuyOrder upserts an asset buy order for management.
Expand All @@ -711,7 +714,7 @@ func (m *Manager) UpsertAssetBuyOrder(order BuyOrder) error {
//
// TODO(ffranr): Add support for peerless buy orders. The negotiator
// should be able to determine the optimal peer.
if order.Peer == nil {
if order.Peer.IsNone() {
return fmt.Errorf("buy order peer must be specified")
}

Expand Down
14 changes: 12 additions & 2 deletions rfq/negotiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {
go func() {
defer n.Wg.Done()

// Unwrap the peer from the buy order. For now, we can assume
// that the peer is always specified.
peer, err := buyOrder.Peer.UnwrapOrErr(
fmt.Errorf("buy order peer must be specified"),
)
if err != nil {
n.cfg.ErrChan <- err
}

// We calculate a proposed bid price for our peer's
// consideration. If a price oracle is not specified we will
// skip this step.
Expand All @@ -169,7 +178,7 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {

// Query the price oracle for a bid price.
assetRate, err := n.queryBidFromPriceOracle(
*buyOrder.Peer, buyOrder.AssetSpecifier,
peer, buyOrder.AssetSpecifier,
fn.Some(buyOrder.AssetMaxAmt),
fn.None[lnwire.MilliSatoshi](),
fn.None[rfqmsg.AssetRate](),
Expand All @@ -186,8 +195,9 @@ func (n *Negotiator) HandleOutgoingBuyOrder(buyOrder BuyOrder) error {
assetRateHint = fn.Some[rfqmsg.AssetRate](*assetRate)
}

// Construct a new buy request to send to the peer.
request, err := rfqmsg.NewBuyRequest(
*buyOrder.Peer, buyOrder.AssetSpecifier,
peer, buyOrder.AssetSpecifier,
buyOrder.AssetMaxAmt, assetRateHint,
)
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6315,7 +6315,7 @@ func unmarshalAssetBuyOrder(
AssetSpecifier: assetSpecifier,
AssetMaxAmt: req.AssetMaxAmt,
Expiry: req.Expiry,
Peer: peer,
Peer: fn.MaybeSome(peer),
}, nil
}

Expand All @@ -6335,12 +6335,13 @@ func (r *rpcServer) AddAssetBuyOrder(_ context.Context,
return nil, fmt.Errorf("error unmarshalling buy order: %w", err)
}

var peer string
if buyOrder.Peer != nil {
peer = buyOrder.Peer.String()
}
peerStr := fn.MapOptionZ(
buyOrder.Peer, func(peerVertex route.Vertex) string {
return peerVertex.String()
},
)
rpcsLog.Debugf("[AddAssetBuyOrder]: upserting buy order "+
"(dest_peer=%s)", peer)
"(dest_peer=%s)", peerStr)

// Register an event listener before actually inserting the order, so we
// definitely don't miss any responses.
Expand Down Expand Up @@ -6382,7 +6383,7 @@ func (r *rpcServer) AddAssetBuyOrder(_ context.Context,

case <-timeout:
return nil, fmt.Errorf("timeout waiting for response "+
"from peer %x", buyOrder.Peer[:])
"(peer=%s)", peerStr)
}
}
}
Expand Down

0 comments on commit 67adb56

Please sign in to comment.