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.
This commit is contained in:
Olaoluwa Osuntokun 2021-12-16 17:03:42 -08:00
parent eecb470538
commit c83f8a56e4
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306

View file

@ -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