Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: remove static default channel types instead select them based on lnd's verison #330

Merged
merged 4 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd/pool/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
44 changes: 39 additions & 5 deletions order/rpc_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
newOpt(opts)
}

var nonce Nonce
copy(nonce[:], details.OrderNonce)
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Roasbeef marked this conversation as resolved.
Show resolved Hide resolved
}

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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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".
//
Expand Down