From c83f8a56e400a5d46b32d5a925dc8161e58a461c Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Dec 2021 17:03:42 -0800 Subject: [PATCH] order: add functional options to allow caller to pick a default chantype In this commit, we add a new set of functional options that will allow the caller to pick an default channel type w/o forcing the main parse function to be aware of this logic. --- order/rpc_parse.go | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/order/rpc_parse.go b/order/rpc_parse.go index 388ac9c..8da8a12 100644 --- a/order/rpc_parse.go +++ b/order/rpc_parse.go @@ -19,10 +19,41 @@ import ( "github.com/lightningnetwork/lnd/tor" ) +// OrderParseOption defines a set of functional param options that can be used +// to modify our we parse orders based on some optional directives. +type OrderParseOption func(*parseOptions) + +// parseOptions houses the set of functional options used to parse RPC orders. +type parseOptions struct { + chanTypeSelector func() ChannelType +} + +// ChanTypeSelector defines a function capable of selecting a channel type +// based on the version of an lnd node. +type ChanTypeSelector func() ChannelType + +// WithDefaultChannelType allows a caller to select a default channel type +// based on the version of the lnd node attempting to create the order. +func WithDefaultChannelType(selector ChanTypeSelector) OrderParseOption { + return func(o *parseOptions) { + o.chanTypeSelector = selector + } +} + +// defaultParseOptions returns the set of default parse options. +func defaultParseOptions() *parseOptions { + return &parseOptions{} +} + // ParseRPCOrder parses the incoming raw RPC order into the go native data // types used in the order struct. func ParseRPCOrder(version, leaseDuration uint32, - details *poolrpc.Order) (*Kit, error) { + details *poolrpc.Order, parseOpts ...OrderParseOption) (*Kit, error) { + + opts := defaultParseOptions() + for _, newOpt := range parseOpts { + newOpt(opts) + } var nonce Nonce copy(nonce[:], details.OrderNonce) @@ -65,10 +96,13 @@ func ParseRPCOrder(version, leaseDuration uint32, switch details.ChannelType { // Default value, trader didn't specify a channel type. case auctioneerrpc.OrderChannelType_ORDER_CHANNEL_TYPE_UNKNOWN: - // TODO: Switch to script enforcement by default once we can - // enforce the lnd release supporting script enforced channels - // as the minimalCompatibleVersion. - kit.ChannelType = ChannelTypePeerDependent + // If we have a chan type selector, we'll use that, otherwise + // we'll just use peer dependent channels as as default. + if opts.chanTypeSelector != nil { + kit.ChannelType = opts.chanTypeSelector() + } else { + kit.ChannelType = ChannelTypePeerDependent + } case auctioneerrpc.OrderChannelType_ORDER_CHANNEL_TYPE_PEER_DEPENDENT: kit.ChannelType = ChannelTypePeerDependent