Skip to content

Commit b9b018a

Browse files
committed
rfq: customize reject message by oracle error
Uses the OracleError returned by an oracle to customize the rejection message sent to a peer. If the OracleError contains a "transparent" code that's deemed to be suitable for passing on to a peer, then simply relay that error. Otherwise relay an opaque "unknown reject error" message.
1 parent 2f096e2 commit b9b018a

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

rfq/negotiator.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rfq
22

33
import (
4+
"errors"
45
"fmt"
56
"sync"
67
"time"
@@ -370,10 +371,12 @@ func (n *Negotiator) HandleIncomingBuyRequest(
370371
peerID, request.PriceOracleMetadata, IntentRecvPayment,
371372
)
372373
if err != nil {
373-
// Send a reject message to the peer.
374+
// Construct an appropriate RejectErr based on
375+
// the oracle's response, and send it to the
376+
// peer.
374377
msg := rfqmsg.NewReject(
375378
request.Peer, request.ID,
376-
rfqmsg.ErrUnknownReject,
379+
createCustomRejectErr(err),
377380
)
378381
sendOutgoingMsg(msg)
379382

@@ -471,10 +474,12 @@ func (n *Negotiator) HandleIncomingSellRequest(
471474
peerID, request.PriceOracleMetadata, IntentPayInvoice,
472475
)
473476
if err != nil {
474-
// Send a reject message to the peer.
477+
// Construct an appropriate RejectErr based on
478+
// the oracle's response, and send it to the
479+
// peer.
475480
msg := rfqmsg.NewReject(
476481
request.Peer, request.ID,
477-
rfqmsg.ErrUnknownReject,
482+
createCustomRejectErr(err),
478483
)
479484
sendOutgoingMsg(msg)
480485

@@ -493,6 +498,36 @@ func (n *Negotiator) HandleIncomingSellRequest(
493498
return nil
494499
}
495500

501+
// createCustomRejectErr creates a RejectErr with code 0 and a custom message
502+
// based on an error response from a price oracle.
503+
func createCustomRejectErr(err error) rfqmsg.RejectErr {
504+
var oracleError *OracleError
505+
506+
if errors.As(err, &oracleError) {
507+
// The error is of the expected type, so switch on the error
508+
// code returned by the oracle. If the code is benign, then the
509+
// RejectErr will simply relay the oracle's message. Otherwise,
510+
// we'll return an opaque rejection message.
511+
switch oracleError.Code {
512+
513+
// The rejection message will state that the oracle doesn't
514+
// support the asset.
515+
case ErrUnsupportedOracleAsset:
516+
msg := oracleError.Error()
517+
return rfqmsg.ErrRejectWithCustomMsg(msg)
518+
519+
// The rejection message will be opaque, with the error
520+
// unspecified.
521+
default:
522+
return rfqmsg.ErrUnknownReject
523+
}
524+
} else {
525+
// The error is of an unexpected type, so just return an opaque
526+
// error message.
527+
return rfqmsg.ErrUnknownReject
528+
}
529+
}
530+
496531
// HandleOutgoingSellOrder handles an outgoing sell order by constructing sell
497532
// requests and passing them to the outgoing messages channel. These requests
498533
// are sent to peers.

0 commit comments

Comments
 (0)