Skip to content

Commit 223b5ea

Browse files
committed
rfq: make SellOrder.Peer field an Option type
Currently, the `SellOrder.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 `SellOrder.Peer` an Option type. Note that this field was already a pointer.
1 parent 1baece1 commit 223b5ea

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

rfq/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ type SellOrder struct {
763763

764764
// Peer is the peer that the buy order is intended for. This field is
765765
// optional.
766-
Peer *route.Vertex
766+
Peer fn.Option[route.Vertex]
767767
}
768768

769769
// UpsertAssetSellOrder upserts an asset sell order for management.
@@ -772,7 +772,7 @@ func (m *Manager) UpsertAssetSellOrder(order SellOrder) error {
772772
//
773773
// TODO(ffranr): Add support for peerless sell orders. The negotiator
774774
// should be able to determine the optimal peer.
775-
if order.Peer == nil {
775+
if order.Peer.IsNone() {
776776
return fmt.Errorf("sell order peer must be specified")
777777
}
778778

rfq/negotiator.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,15 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
467467
go func() {
468468
defer n.Wg.Done()
469469

470+
// Unwrap the peer from the order. For now, we can assume that
471+
// the peer is always specified.
472+
peer, err := order.Peer.UnwrapOrErr(
473+
fmt.Errorf("buy order peer must be specified"),
474+
)
475+
if err != nil {
476+
n.cfg.ErrChan <- err
477+
}
478+
470479
// We calculate a proposed ask price for our peer's
471480
// consideration. If a price oracle is not specified we will
472481
// skip this step.
@@ -475,7 +484,7 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
475484
if n.cfg.PriceOracle != nil && order.AssetSpecifier.IsSome() {
476485
// Query the price oracle for an asking price.
477486
assetRate, err := n.queryAskFromPriceOracle(
478-
order.Peer, order.AssetSpecifier,
487+
&peer, order.AssetSpecifier,
479488
fn.None[uint64](),
480489
fn.Some(order.PaymentMaxAmt),
481490
fn.None[rfqmsg.AssetRate](),
@@ -491,7 +500,7 @@ func (n *Negotiator) HandleOutgoingSellOrder(order SellOrder) {
491500
}
492501

493502
request, err := rfqmsg.NewSellRequest(
494-
*order.Peer, order.AssetSpecifier, order.PaymentMaxAmt,
503+
peer, order.AssetSpecifier, order.PaymentMaxAmt,
495504
assetRateHint,
496505
)
497506
if err != nil {

rpcserver.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6403,15 +6403,15 @@ func unmarshalAssetSellOrder(
64036403
}
64046404

64056405
// Unmarshal the peer if specified.
6406-
var peer *route.Vertex
6406+
var peer fn.Option[route.Vertex]
64076407
if len(req.PeerPubKey) > 0 {
64086408
pv, err := route.NewVertexFromBytes(req.PeerPubKey)
64096409
if err != nil {
64106410
return nil, fmt.Errorf("error unmarshalling peer "+
64116411
"route vertex: %w", err)
64126412
}
64136413

6414-
peer = &pv
6414+
peer = fn.Some(pv)
64156415
}
64166416

64176417
// Construct an asset specifier from the asset ID and/or group key.
@@ -6447,12 +6447,13 @@ func (r *rpcServer) AddAssetSellOrder(_ context.Context,
64476447
err)
64486448
}
64496449

6450-
var peer string
6451-
if sellOrder.Peer != nil {
6452-
peer = sellOrder.Peer.String()
6453-
}
6450+
// Extract peer identifier as a string for logging.
6451+
peerStr := fn.MapOptionZ(sellOrder.Peer, func(p route.Vertex) string {
6452+
return p.String()
6453+
})
6454+
64546455
rpcsLog.Debugf("[AddAssetSellOrder]: upserting sell order "+
6455-
"(dest_peer=%s)", peer)
6456+
"(dest_peer=%s)", peerStr)
64566457

64576458
// Register an event listener before actually inserting the order, so we
64586459
// definitely don't miss any responses.
@@ -6494,7 +6495,7 @@ func (r *rpcServer) AddAssetSellOrder(_ context.Context,
64946495

64956496
case <-timeout:
64966497
return nil, fmt.Errorf("timeout waiting for response "+
6497-
"from peer %x", sellOrder.Peer[:])
6498+
"from peer %s", peerStr)
64986499
}
64996500
}
65006501
}

0 commit comments

Comments
 (0)