Skip to content

Commit

Permalink
invoicesrpc: add InvoiceAcceptor RPC endpoint and acceptor RPC server
Browse files Browse the repository at this point in the history
This commit introduces a singleton invoice acceptor RPC server and
an endpoint to activate it. The server interfaces with the internal
invoice settlement interpreter, handling the marshalling between RPC
types and internal formats.

Named "acceptor," it allows clients to accept invoice settlements, but
not to reject them.
  • Loading branch information
ffranr committed Apr 23, 2024
1 parent 73d1d66 commit c3f4d8a
Show file tree
Hide file tree
Showing 6 changed files with 738 additions and 62 deletions.
121 changes: 121 additions & 0 deletions lnrpc/invoicesrpc/invoice_acceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package invoicesrpc

import (
"time"

"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// invoiceAcceptorConfig contains the configuration for an RPC invoice acceptor
// server.
type invoiceAcceptorConfig struct {
// chainParams is required to properly marshall an invoice for RPC.
chainParams *chaincfg.Params

// rpcServer is a bidirectional RPC server to send invoices to a client
// and receive accept responses from the client.
rpcServer Invoices_InvoiceAcceptorServer

// interceptor is the invoice interceptor that will be used to intercept
// and resolve invoices.
interceptor invoices.SettlementInterceptorInterface
}

// invoiceAcceptor is a helper struct that handles the lifecycle of an RPC
// invoice acceptor server instance.
//
// This struct handles passing send and receive RPC messages between the client
// and the invoice service.
type invoiceAcceptor struct {
// cfg contains the configuration for the invoice acceptor.
cfg invoiceAcceptorConfig
}

// newInvoiceAcceptor creates a new RPC invoice acceptor handler.
func newInvoiceAcceptor(cfg invoiceAcceptorConfig) *invoiceAcceptor {
return &invoiceAcceptor{
cfg: cfg,
}
}

// run sends the intercepted invoices to the client and receives the
// corresponding responses.
func (r *invoiceAcceptor) run() error {
// Register our invoice interceptor.
r.cfg.interceptor.SetClientCallback(r.onIntercept)
defer r.cfg.interceptor.SetClientCallback(nil)

// Listen for a response from the client in a loop.
for {
resp, err := r.cfg.rpcServer.Recv()
if err != nil {
return err
}

if err := r.resolveFromClient(resp); err != nil {
return err
}
}
}

// onIntercept is called when an invoice is intercepted by the invoice
// interceptor. This method sends the invoice to the client.
func (r *invoiceAcceptor) onIntercept(
invoice invoices.Invoice) error {

// Convert the invoice to an RPC invoice.
rpcInvoice, err := CreateRPCInvoice(&invoice, r.cfg.chainParams)
if err != nil {
return err
}

return r.cfg.rpcServer.Send(&InvoiceAcceptorRequest{
Invoice: rpcInvoice,
})
}

// resolveFromClient handles an invoice resolution received from the client.
func (r *invoiceAcceptor) resolveFromClient(
in *InvoiceAcceptorResponse) error {

log.Tracef("Resolving invoice acceptor response %v", in)

// Parse the circuit key from the response.
if in.CircuitKey == nil {
return status.Errorf(codes.InvalidArgument,
"CircuitKey missing from InvoiceAcceptorResponse")
}
circuitKey := models.CircuitKey{
ChanID: lnwire.NewShortChanIDFromInt(in.CircuitKey.ChanId),
HtlcID: in.CircuitKey.HtlcId,
}

// Parse the payment hash from the response.
var invoicePaymentHash lntypes.Hash
copy(invoicePaymentHash[:], in.PaymentHash)

// Parse the accept time from the response.
acceptTimeUnix := int64(in.AcceptTime)
acceptTime := time.Unix(acceptTimeUnix, 0).UTC()

// Parse the outcome from the response.
outcome := invoices.AcceptResolutionResult(in.Outcome.Number())

// Construct the HTLC resolution from the response.
htlcResolution := invoices.NewAcceptResolution(
circuitKey, outcome, in.AutoRelease, acceptTime,
)

// Pass the resolution to the interceptor.
return r.cfg.interceptor.Resolve(
invoicePaymentHash, htlcResolution,
fn.None[invoices.InvoiceUpdateDesc](),
)
}

0 comments on commit c3f4d8a

Please sign in to comment.