sidecar: use a funciton closure for PrepareOrder

The RPC server won't be created by the time we attempt to reach for this
pointer, so we use a function closure to allow "lazy" evaluation which
only attempts to derf once the system is already up and running.
This commit is contained in:
Olaoluwa Osuntokun 2021-05-01 15:39:53 -07:00
parent 37947e5de5
commit ba9a223bd6
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306
3 changed files with 35 additions and 15 deletions

View file

@ -1118,15 +1118,21 @@ func (s *rpcServer) validateOrder(order order.Order, acct *account.Account,
return nil
}
// orderPreparer represents a type of function that inserts the order into the
// local database, and returns the params needed to submit it to the
// auctioneer.
type orderPreparer func(context.Context, order.Order,
*account.Account, *terms.AuctioneerTerms) (*order.ServerOrderParams, error)
// prepareAndSubmitOrder performs a series of final checks locally to ensure
// the order is valid, before submitting it to the auctioneer.
func prepareAndSubmitOrder(ctx context.Context, o order.Order,
auctionTerms *terms.AuctioneerTerms, acct *account.Account,
auction *auctioneer.Client, orderBook *order.Manager) error {
auction *auctioneer.Client, prepareOrder orderPreparer) error {
// Collect all the order data and sign it before sending it to the
// auction server.
serverParams, err := orderBook.PrepareOrder(
serverParams, err := prepareOrder(
ctx, o, acct, auctionTerms,
)
if err != nil {
@ -1222,7 +1228,7 @@ func (s *rpcServer) SubmitOrder(ctx context.Context,
// the auctioneer server.
err = prepareAndSubmitOrder(
ContextWithInitiator(ctx, req.Initiator), o, auctionTerms,
acct, s.auctioneer, s.orderManager,
acct, s.auctioneer, s.orderManager.PrepareOrder,
)
if err != nil {
// The server rejected the order. We keep it around for now,

View file

@ -18,11 +18,13 @@ import (
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/lightninglabs/aperture/lsat"
"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/pool/account"
"github.com/lightninglabs/pool/auctioneer"
"github.com/lightninglabs/pool/clientdb"
"github.com/lightninglabs/pool/funding"
"github.com/lightninglabs/pool/order"
"github.com/lightninglabs/pool/poolrpc"
"github.com/lightninglabs/pool/terms"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/verrpc"
"github.com/lightningnetwork/lnd/macaroons"
@ -504,16 +506,28 @@ func (s *Server) setupClient() error {
// acceptor is going to overwrite some of its values.
clientCfgCopy := *clientCfg
s.sidecarAcceptor = NewSidecarAcceptor(&SidecarAcceptorConfig{
SidecarDB: s.db,
AcctDB: &accountStore{DB: s.db},
Signer: s.lndServices.Signer,
Wallet: s.lndServices.WalletKit,
BaseClient: s.lndClient,
Acceptor: channelAcceptor,
NodePubKey: nodePubKey,
ClientCfg: clientCfgCopy,
FundingManager: s.fundingManager,
OrderManager: s.rpcServer.orderManager,
SidecarDB: s.db,
AcctDB: &accountStore{DB: s.db},
Signer: s.lndServices.Signer,
Wallet: s.lndServices.WalletKit,
BaseClient: s.lndClient,
Acceptor: channelAcceptor,
NodePubKey: nodePubKey,
ClientCfg: clientCfgCopy,
FundingManager: s.fundingManager,
PrepareOrder: func(ctx context.Context,
order order.Order,
acct *account.Account,
terms *terms.AuctioneerTerms) (*order.ServerOrderParams, error) {
// Rather than passing in the function directly, we use
// an intermediate closure as this pointer won't
// existing when we initialize this config, as the rpc
// server is created _after_ we set up the client.
return s.rpcServer.orderManager.PrepareOrder(
ctx, order, acct, terms,
)
},
FetchSidecarBid: s.db.SidecarBidTemplate,
})

View file

@ -70,7 +70,7 @@ type SidecarAcceptorConfig struct {
ClientCfg auctioneer.Config
OrderManager *order.Manager
PrepareOrder orderPreparer
FundingManager *funding.Manager
@ -555,7 +555,7 @@ func (a *SidecarAcceptor) submitSidecarOrder(ctx context.Context,
}
err = prepareAndSubmitOrder(
ctx, bid, auctionTerms, acct, a.client, a.cfg.OrderManager,
ctx, bid, auctionTerms, acct, a.client, a.cfg.PrepareOrder,
)
if err != nil {
return nil, err