From 67fb1b4b97d52b2c06b82cfc6f9ff7f3e54ce490 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Dec 2021 16:58:34 -0800 Subject: [PATCH 1/4] cmd/pool: stop setting a default channel type on the CLI In this commit, we move to stop setting a default channel type on the CLI. Instead, we want this to be selected at runtime by the daemon based on what the lnd node can offer. --- cmd/pool/order.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/pool/order.go b/cmd/pool/order.go index ff8347a67..1086637b3 100644 --- a/cmd/pool/order.go +++ b/cmd/pool/order.go @@ -115,10 +115,6 @@ var sharedFlags = []cli.Flag{ "order being matched (%q, %q)", channelTypePeerDependent, channelTypeScriptEnforced), - // TODO: Switch to script enforcement by default once we can - // enforce the lnd release supporting script enforced channels - // as the minimalCompatibleVersion. - Value: channelTypePeerDependent, }, } @@ -248,6 +244,8 @@ func parseCommonParams(ctx *cli.Context, blockDuration uint32) (*poolrpc.Order, // order match. channelType := ctx.String("channel_type") switch channelType { + // No values means that the unknown type will be set, which means the + // sever will select a type based on the version of the connected node. case "": break case channelTypePeerDependent: From eecb4705385689b7963229458257bc8f2d8cc0d4 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Dec 2021 17:00:59 -0800 Subject: [PATCH 2/4] server: store the version of the connected lnd node We'll use this in a follow up commit to decide what channel type we want to use. --- server.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server.go b/server.go index b9ceabbbd..4d366a2a1 100644 --- a/server.go +++ b/server.go @@ -52,6 +52,18 @@ var ( "signrpc", "walletrpc", "chainrpc", "invoicesrpc", }, } + + // scriptEnforceVersion is the version of lnd that enabled lease duration script + // enforcement as a new channel type. We'll use this to decide what + // order type to default to. + scriptEnforceVersion = &verrpc.Version{ + AppMajor: 0, + AppMinor: 14, + AppPatch: 0, + BuildTags: []string{ + "signrpc", "walletrpc", "chainrpc", "invoicesrpc", + }, + } ) // Server is the main poold trader server. @@ -71,6 +83,8 @@ type Server struct { // client or an error if none has been established yet. GetIdentity func() (*lsat.TokenID, error) + lndVersion *verrpc.Version + cfg *Config db *clientdb.DB fundingManager *funding.Manager @@ -128,6 +142,11 @@ func (s *Server) Start() error { return nil } + // Now that we have lnd, lets extract the current version so we can use + // this to gate features that we'll use based on the functionality + // available. + s.lndVersion = s.lndServices.Version + // As there're some other lower-level operations we may need access to, // we'll also make a connection for a "basic client". // From c83f8a56e400a5d46b32d5a925dc8161e58a461c Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Dec 2021 17:03:42 -0800 Subject: [PATCH 3/4] 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 388ac9c10..8da8a12e4 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 From b413ace0606d30c37014e69291416c1c16dd3d8d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 16 Dec 2021 17:04:15 -0800 Subject: [PATCH 4/4] rpc: default to the script enforced chan type if lnd supports it In this commit, we use the prior commits to allow the server to dynamically select a channel type based on the lnd version. --- go.mod | 2 +- go.sum | 4 ++-- rpcserver.go | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 33b63d741..806d87aa3 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 github.com/jessevdk/go-flags v1.4.0 github.com/lightninglabs/aperture v0.1.6-beta - github.com/lightninglabs/lndclient v0.14.0-5 + github.com/lightninglabs/lndclient v0.14.0-7 github.com/lightninglabs/pool/auctioneerrpc v1.0.5 github.com/lightninglabs/protobuf-hex-display v1.4.3-hex-display github.com/lightningnetwork/lnd v0.14.1-beta diff --git a/go.sum b/go.sum index 24abfaf9c..33c6c1c88 100644 --- a/go.sum +++ b/go.sum @@ -461,8 +461,8 @@ github.com/lightninglabs/aperture v0.1.6-beta/go.mod h1:9xl4mx778ZAzrB87nLHMqk+X github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI/f/O0Avg7t8sqkPo78HFzjmeYFl6DPnc= github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk= github.com/lightninglabs/lndclient v0.11.0-4/go.mod h1:8/cTKNwgL87NX123gmlv3Xh6p1a7pvzu+40Un3PhHiI= -github.com/lightninglabs/lndclient v0.14.0-5 h1:dI2/Y2fn9m5VuwMTd/DcF6y0DYdMy3pk0MPu4xNjj54= -github.com/lightninglabs/lndclient v0.14.0-5/go.mod h1:2kH9vNoc29ghIkfMjxwSeK8yCxsYfR80XAJ9PU/QWWk= +github.com/lightninglabs/lndclient v0.14.0-7 h1:muqPju9ixBtQNcO0SkvbZ2b2oORUMRqQ4e+aC077Qa8= +github.com/lightninglabs/lndclient v0.14.0-7/go.mod h1:2kH9vNoc29ghIkfMjxwSeK8yCxsYfR80XAJ9PU/QWWk= github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg= github.com/lightninglabs/neutrino v0.11.1-0.20200316235139-bffc52e8f200/go.mod h1:MlZmoKa7CJP3eR1s5yB7Rm5aSyadpKkxqAwLQmog7N0= github.com/lightninglabs/neutrino v0.12.1/go.mod h1:GlKninWpRBbL7b8G0oQ36/8downfnFwKsr0hbRA6E/E= diff --git a/rpcserver.go b/rpcserver.go index 887f2367a..913e6d022 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1191,12 +1191,30 @@ func prepareAndSubmitOrder(ctx context.Context, o order.Order, func (s *rpcServer) SubmitOrder(ctx context.Context, req *poolrpc.SubmitOrderRequest) (*poolrpc.SubmitOrderResponse, error) { + // We'll use this channel type selector to pick a channel type based on + // the current connected lnd version. This lets us graceful update to + // new features as they're available, while still supporting older + // versions of lnd. + chanTypeSelector := order.WithDefaultChannelType(func() order.ChannelType { + // If they didn't specify a value, then we'll select one based + // on the version of lnd we detect. + verErr := lndclient.AssertVersionCompatible( + s.server.lndVersion, scriptEnforceVersion, + ) + if verErr == nil { + return order.ChannelTypeScriptEnforced + } + + return order.ChannelTypePeerDependent + }) + var o order.Order switch requestOrder := req.Details.(type) { case *poolrpc.SubmitOrderRequest_Ask: a := requestOrder.Ask kit, err := order.ParseRPCOrder( a.Version, a.LeaseDurationBlocks, a.Details, + chanTypeSelector, ) if err != nil { return nil, err @@ -1210,6 +1228,7 @@ func (s *rpcServer) SubmitOrder(ctx context.Context, b := requestOrder.Bid kit, err := order.ParseRPCOrder( b.Version, b.LeaseDurationBlocks, b.Details, + chanTypeSelector, ) if err != nil { return nil, err