From ea89f92eb502e86ccba5396e74cb112feba488f4 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Mon, 4 Sep 2023 09:21:44 +0200 Subject: [PATCH] move get fee params settings to shared --- channel_opener_server.go | 6 ++-- interceptor/intercept.go | 16 ++++++---- interceptor/store.go | 19 ++---------- main.go | 7 +++-- postgresql/intercept_store.go | 44 ++------------------------- postgresql/opening_store.go | 57 +++++++++++++++++++++++++++++++++++ shared/opening_service.go | 18 +++-------- shared/opening_store.go | 21 +++++++++++++ 8 files changed, 105 insertions(+), 83 deletions(-) create mode 100644 postgresql/opening_store.go create mode 100644 shared/opening_store.go diff --git a/channel_opener_server.go b/channel_opener_server.go index 94e64d5..8de2184 100644 --- a/channel_opener_server.go +++ b/channel_opener_server.go @@ -157,11 +157,11 @@ func (s *channelOpenerServer) RegisterPayment( log.Printf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err) return nil, fmt.Errorf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err) } - params := &interceptor.OpeningFeeParams{ - MinMsat: pi.OpeningFeeParams.MinMsat, + params := &shared.OpeningFeeParams{ + MinFeeMsat: pi.OpeningFeeParams.MinMsat, Proportional: pi.OpeningFeeParams.Proportional, ValidUntil: pi.OpeningFeeParams.ValidUntil, - MaxIdleTime: pi.OpeningFeeParams.MaxIdleTime, + MinLifetime: pi.OpeningFeeParams.MaxIdleTime, MaxClientToSelfDelay: pi.OpeningFeeParams.MaxClientToSelfDelay, Promise: pi.OpeningFeeParams.Promise, } diff --git a/interceptor/intercept.go b/interceptor/intercept.go index c5728d3..91bf3b3 100644 --- a/interceptor/intercept.go +++ b/interceptor/intercept.go @@ -14,6 +14,7 @@ import ( "github.com/breez/lspd/config" "github.com/breez/lspd/lightning" "github.com/breez/lspd/notifications" + "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" "golang.org/x/exp/slices" "golang.org/x/sync/singleflight" @@ -51,6 +52,7 @@ type Interceptor struct { client lightning.Client config *config.NodeConfig store InterceptStore + openingStore shared.OpeningStore feeEstimator chain.FeeEstimator feeStrategy chain.FeeStrategy payHashGroup singleflight.Group @@ -61,6 +63,7 @@ func NewInterceptor( client lightning.Client, config *config.NodeConfig, store InterceptStore, + openingStore shared.OpeningStore, feeEstimator chain.FeeEstimator, feeStrategy chain.FeeStrategy, notificationService *notifications.NotificationService, @@ -69,6 +72,7 @@ func NewInterceptor( client: client, config: config, store: store, + openingStore: openingStore, feeEstimator: feeEstimator, feeStrategy: feeStrategy, notificationService: notificationService, @@ -184,11 +188,11 @@ func (i *Interceptor) Intercept(scid *basetypes.ShortChannelID, reqPaymentHash [ // TODO: When opening_fee_params is enforced, turn this check in a temporary channel failure. if params == nil { log.Printf("DEPRECATED: Intercepted htlc with deprecated fee mechanism. Using default fees. payment hash: %s", reqPaymentHashStr) - params = &OpeningFeeParams{ - MinMsat: uint64(i.config.ChannelMinimumFeeMsat), + params = &shared.OpeningFeeParams{ + MinFeeMsat: uint64(i.config.ChannelMinimumFeeMsat), Proportional: uint32(i.config.ChannelFeePermyriad * 100), ValidUntil: time.Now().UTC().Add(time.Duration(time.Hour * 24)).Format(basetypes.TIME_FORMAT), - MaxIdleTime: uint32(i.config.MaxInactiveDuration / 600), + MinLifetime: uint32(i.config.MaxInactiveDuration / 600), MaxClientToSelfDelay: uint32(10000), } } @@ -364,15 +368,15 @@ func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegiste return nil } -func (i *Interceptor) isCurrentChainFeeCheaper(token string, params *OpeningFeeParams) bool { - settings, err := i.store.GetFeeParamsSettings(token) +func (i *Interceptor) isCurrentChainFeeCheaper(token string, params *shared.OpeningFeeParams) bool { + settings, err := i.openingStore.GetFeeParamsSettings(token) if err != nil { log.Printf("Failed to get fee params settings: %v", err) return false } for _, setting := range settings { - if setting.Params.MinMsat <= params.MinMsat { + if setting.Params.MinFeeMsat <= params.MinFeeMsat { return true } } diff --git a/interceptor/store.go b/interceptor/store.go index 172281a..c239456 100644 --- a/interceptor/store.go +++ b/interceptor/store.go @@ -3,26 +3,13 @@ package interceptor import ( "time" + "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" ) -type OpeningFeeParamsSetting struct { - Validity time.Duration - Params *OpeningFeeParams -} -type OpeningFeeParams struct { - MinMsat uint64 `json:"min_msat,string"` - Proportional uint32 `json:"proportional"` - ValidUntil string `json:"valid_until"` - MaxIdleTime uint32 `json:"max_idle_time"` - MaxClientToSelfDelay uint32 `json:"max_client_to_self_delay"` - Promise string `json:"promise"` -} - type InterceptStore interface { - PaymentInfo(htlcPaymentHash []byte) (string, *OpeningFeeParams, []byte, []byte, []byte, int64, int64, *wire.OutPoint, *string, error) + PaymentInfo(htlcPaymentHash []byte) (string, *shared.OpeningFeeParams, []byte, []byte, []byte, int64, int64, *wire.OutPoint, *string, error) SetFundingTx(paymentHash []byte, channelPoint *wire.OutPoint) error - RegisterPayment(token string, params *OpeningFeeParams, destination, paymentHash, paymentSecret []byte, incomingAmountMsat, outgoingAmountMsat int64, tag string) error + RegisterPayment(token string, params *shared.OpeningFeeParams, destination, paymentHash, paymentSecret []byte, incomingAmountMsat, outgoingAmountMsat int64, tag string) error InsertChannel(initialChanID, confirmedChanId uint64, channelPoint string, nodeID []byte, lastUpdate time.Time) error - GetFeeParamsSettings(token string) ([]*OpeningFeeParamsSetting, error) } diff --git a/main.go b/main.go index a5f8310..130d861 100644 --- a/main.go +++ b/main.go @@ -92,12 +92,13 @@ func main() { } interceptStore := postgresql.NewPostgresInterceptStore(pool) + openingStore := postgresql.NewPostgresOpeningStore(pool) forwardingStore := postgresql.NewForwardingEventStore(pool) notificationsStore := postgresql.NewNotificationsStore(pool) lsps2Store := postgresql.NewLsps2Store(pool) notificationService := notifications.NewNotificationService(notificationsStore) - openingService := shared.NewOpeningService(interceptStore, nodesService) + openingService := shared.NewOpeningService(openingStore, nodesService) var interceptors []interceptor.HtlcInterceptor for _, node := range nodes { var htlcInterceptor interceptor.HtlcInterceptor @@ -109,7 +110,7 @@ func main() { client.StartListeners() fwsync := lnd.NewForwardingHistorySync(client, interceptStore, forwardingStore) - interceptor := interceptor.NewInterceptor(client, node.NodeConfig, interceptStore, feeEstimator, feeStrategy, notificationService) + interceptor := interceptor.NewInterceptor(client, node.NodeConfig, interceptStore, openingStore, feeEstimator, feeStrategy, notificationService) htlcInterceptor, err = lnd.NewLndHtlcInterceptor(node.NodeConfig, client, fwsync, interceptor) if err != nil { log.Fatalf("failed to initialize LND interceptor: %v", err) @@ -122,7 +123,7 @@ func main() { log.Fatalf("failed to initialize CLN client: %v", err) } - interceptor := interceptor.NewInterceptor(client, node.NodeConfig, interceptStore, feeEstimator, feeStrategy, notificationService) + interceptor := interceptor.NewInterceptor(client, node.NodeConfig, interceptStore, openingStore, feeEstimator, feeStrategy, notificationService) htlcInterceptor, err = cln.NewClnHtlcInterceptor(node.NodeConfig, client, interceptor) if err != nil { log.Fatalf("failed to initialize CLN interceptor: %v", err) diff --git a/postgresql/intercept_store.go b/postgresql/intercept_store.go index ebdea0c..79b3a30 100644 --- a/postgresql/intercept_store.go +++ b/postgresql/intercept_store.go @@ -8,18 +8,13 @@ import ( "time" "github.com/breez/lspd/basetypes" - "github.com/breez/lspd/interceptor" + "github.com/breez/lspd/shared" "github.com/btcsuite/btcd/wire" "github.com/jackc/pgtype" "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/pgxpool" ) -type extendedParams struct { - Token string `json:"token"` - Params interceptor.OpeningFeeParams `json:"fees_params"` -} - type PostgresInterceptStore struct { pool *pgxpool.Pool } @@ -28,7 +23,7 @@ func NewPostgresInterceptStore(pool *pgxpool.Pool) *PostgresInterceptStore { return &PostgresInterceptStore{pool: pool} } -func (s *PostgresInterceptStore) PaymentInfo(htlcPaymentHash []byte) (string, *interceptor.OpeningFeeParams, []byte, []byte, []byte, int64, int64, *wire.OutPoint, *string, error) { +func (s *PostgresInterceptStore) PaymentInfo(htlcPaymentHash []byte) (string, *shared.OpeningFeeParams, []byte, []byte, []byte, int64, int64, *wire.OutPoint, *string, error) { var ( p, tag *string paymentHash, paymentSecret, destination []byte @@ -77,7 +72,7 @@ func (s *PostgresInterceptStore) SetFundingTx(paymentHash []byte, channelPoint * return err } -func (s *PostgresInterceptStore) RegisterPayment(token string, params *interceptor.OpeningFeeParams, destination, paymentHash, paymentSecret []byte, incomingAmountMsat, outgoingAmountMsat int64, tag string) error { +func (s *PostgresInterceptStore) RegisterPayment(token string, params *shared.OpeningFeeParams, destination, paymentHash, paymentSecret []byte, incomingAmountMsat, outgoingAmountMsat int64, tag string) error { var t *string if tag != "" { t = &tag @@ -127,36 +122,3 @@ func (s *PostgresInterceptStore) InsertChannel(initialChanID, confirmedChanId ui initialChanID, confirmedChanId, nodeID, c.String()) return nil } - -func (s *PostgresInterceptStore) GetFeeParamsSettings(token string) ([]*interceptor.OpeningFeeParamsSetting, error) { - rows, err := s.pool.Query(context.Background(), `SELECT validity, params FROM new_channel_params WHERE token=$1`, token) - if err != nil { - log.Printf("GetFeeParamsSettings(%v) error: %v", token, err) - return nil, err - } - - var settings []*interceptor.OpeningFeeParamsSetting - for rows.Next() { - var validity int64 - var param string - err = rows.Scan(&validity, ¶m) - if err != nil { - return nil, err - } - - var params *interceptor.OpeningFeeParams - err := json.Unmarshal([]byte(param), ¶ms) - if err != nil { - log.Printf("Failed to unmarshal fee param '%v': %v", param, err) - return nil, err - } - - duration := time.Second * time.Duration(validity) - settings = append(settings, &interceptor.OpeningFeeParamsSetting{ - Validity: duration, - Params: params, - }) - } - - return settings, nil -} diff --git a/postgresql/opening_store.go b/postgresql/opening_store.go new file mode 100644 index 0000000..8e450b2 --- /dev/null +++ b/postgresql/opening_store.go @@ -0,0 +1,57 @@ +package postgresql + +import ( + "context" + "encoding/json" + "log" + "time" + + "github.com/breez/lspd/shared" + "github.com/jackc/pgx/v4/pgxpool" +) + +type extendedParams struct { + Token string `json:"token"` + Params shared.OpeningFeeParams `json:"fees_params"` +} + +type PostgresOpeningStore struct { + pool *pgxpool.Pool +} + +func NewPostgresOpeningStore(pool *pgxpool.Pool) *PostgresOpeningStore { + return &PostgresOpeningStore{pool: pool} +} + +func (s *PostgresOpeningStore) GetFeeParamsSettings(token string) ([]*shared.OpeningFeeParamsSetting, error) { + rows, err := s.pool.Query(context.Background(), `SELECT validity, params FROM new_channel_params WHERE token=$1`, token) + if err != nil { + log.Printf("GetFeeParamsSettings(%v) error: %v", token, err) + return nil, err + } + + var settings []*shared.OpeningFeeParamsSetting + for rows.Next() { + var validity int64 + var param string + err = rows.Scan(&validity, ¶m) + if err != nil { + return nil, err + } + + var params *shared.OpeningFeeParams + err := json.Unmarshal([]byte(param), ¶ms) + if err != nil { + log.Printf("Failed to unmarshal fee param '%v': %v", param, err) + return nil, err + } + + duration := time.Second * time.Duration(validity) + settings = append(settings, &shared.OpeningFeeParamsSetting{ + Validity: duration, + Params: params, + }) + } + + return settings, nil +} diff --git a/shared/opening_service.go b/shared/opening_service.go index fb12e99..4c34b31 100644 --- a/shared/opening_service.go +++ b/shared/opening_service.go @@ -10,7 +10,6 @@ import ( "time" "github.com/breez/lspd/basetypes" - "github.com/breez/lspd/interceptor" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/ecdsa" ) @@ -21,12 +20,12 @@ type OpeningService interface { } type openingService struct { - store interceptor.InterceptStore + store OpeningStore nodesService NodesService } func NewOpeningService( - store interceptor.InterceptStore, + store OpeningStore, nodesService NodesService, ) OpeningService { return &openingService{ @@ -35,15 +34,6 @@ func NewOpeningService( } } -type OpeningFeeParams struct { - MinFeeMsat uint64 `json:"min_fee_msat,string"` - Proportional uint32 `json:"proportional"` - ValidUntil string `json:"valid_until"` - MinLifetime uint32 `json:"min_lifetime"` - MaxClientToSelfDelay uint32 `json:"max_client_to_self_delay"` - Promise string `json:"promise"` -} - func (s *openingService) GetFeeParamsMenu(token string, privateKey *btcec.PrivateKey) ([]*OpeningFeeParams, error) { var menu []*OpeningFeeParams settings, err := s.store.GetFeeParamsSettings(token) @@ -59,10 +49,10 @@ func (s *openingService) GetFeeParamsMenu(token string, privateKey *btcec.Privat for _, setting := range settings { validUntil := time.Now().UTC().Add(setting.Validity) params := &OpeningFeeParams{ - MinFeeMsat: setting.Params.MinMsat, + MinFeeMsat: setting.Params.MinFeeMsat, Proportional: setting.Params.Proportional, ValidUntil: validUntil.Format(basetypes.TIME_FORMAT), - MinLifetime: setting.Params.MaxIdleTime, + MinLifetime: setting.Params.MinLifetime, MaxClientToSelfDelay: setting.Params.MaxClientToSelfDelay, } diff --git a/shared/opening_store.go b/shared/opening_store.go new file mode 100644 index 0000000..d6db546 --- /dev/null +++ b/shared/opening_store.go @@ -0,0 +1,21 @@ +package shared + +import "time" + +type OpeningFeeParamsSetting struct { + Validity time.Duration + Params *OpeningFeeParams +} + +type OpeningFeeParams struct { + MinFeeMsat uint64 `json:"min_msat,string"` + Proportional uint32 `json:"proportional"` + ValidUntil string `json:"valid_until"` + MinLifetime uint32 `json:"max_idle_time"` + MaxClientToSelfDelay uint32 `json:"max_client_to_self_delay"` + Promise string `json:"promise"` +} + +type OpeningStore interface { + GetFeeParamsSettings(token string) ([]*OpeningFeeParamsSetting, error) +}