move get fee params settings to shared

This commit is contained in:
Jesse de Wit
2023-09-04 09:21:44 +02:00
parent cd2f6439e5
commit ea89f92eb5
8 changed files with 105 additions and 83 deletions

View File

@@ -157,11 +157,11 @@ func (s *channelOpenerServer) RegisterPayment(
log.Printf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err) 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) return nil, fmt.Errorf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err)
} }
params := &interceptor.OpeningFeeParams{ params := &shared.OpeningFeeParams{
MinMsat: pi.OpeningFeeParams.MinMsat, MinFeeMsat: pi.OpeningFeeParams.MinMsat,
Proportional: pi.OpeningFeeParams.Proportional, Proportional: pi.OpeningFeeParams.Proportional,
ValidUntil: pi.OpeningFeeParams.ValidUntil, ValidUntil: pi.OpeningFeeParams.ValidUntil,
MaxIdleTime: pi.OpeningFeeParams.MaxIdleTime, MinLifetime: pi.OpeningFeeParams.MaxIdleTime,
MaxClientToSelfDelay: pi.OpeningFeeParams.MaxClientToSelfDelay, MaxClientToSelfDelay: pi.OpeningFeeParams.MaxClientToSelfDelay,
Promise: pi.OpeningFeeParams.Promise, Promise: pi.OpeningFeeParams.Promise,
} }

View File

@@ -14,6 +14,7 @@ import (
"github.com/breez/lspd/config" "github.com/breez/lspd/config"
"github.com/breez/lspd/lightning" "github.com/breez/lspd/lightning"
"github.com/breez/lspd/notifications" "github.com/breez/lspd/notifications"
"github.com/breez/lspd/shared"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"golang.org/x/sync/singleflight" "golang.org/x/sync/singleflight"
@@ -51,6 +52,7 @@ type Interceptor struct {
client lightning.Client client lightning.Client
config *config.NodeConfig config *config.NodeConfig
store InterceptStore store InterceptStore
openingStore shared.OpeningStore
feeEstimator chain.FeeEstimator feeEstimator chain.FeeEstimator
feeStrategy chain.FeeStrategy feeStrategy chain.FeeStrategy
payHashGroup singleflight.Group payHashGroup singleflight.Group
@@ -61,6 +63,7 @@ func NewInterceptor(
client lightning.Client, client lightning.Client,
config *config.NodeConfig, config *config.NodeConfig,
store InterceptStore, store InterceptStore,
openingStore shared.OpeningStore,
feeEstimator chain.FeeEstimator, feeEstimator chain.FeeEstimator,
feeStrategy chain.FeeStrategy, feeStrategy chain.FeeStrategy,
notificationService *notifications.NotificationService, notificationService *notifications.NotificationService,
@@ -69,6 +72,7 @@ func NewInterceptor(
client: client, client: client,
config: config, config: config,
store: store, store: store,
openingStore: openingStore,
feeEstimator: feeEstimator, feeEstimator: feeEstimator,
feeStrategy: feeStrategy, feeStrategy: feeStrategy,
notificationService: notificationService, 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. // TODO: When opening_fee_params is enforced, turn this check in a temporary channel failure.
if params == nil { if params == nil {
log.Printf("DEPRECATED: Intercepted htlc with deprecated fee mechanism. Using default fees. payment hash: %s", reqPaymentHashStr) log.Printf("DEPRECATED: Intercepted htlc with deprecated fee mechanism. Using default fees. payment hash: %s", reqPaymentHashStr)
params = &OpeningFeeParams{ params = &shared.OpeningFeeParams{
MinMsat: uint64(i.config.ChannelMinimumFeeMsat), MinFeeMsat: uint64(i.config.ChannelMinimumFeeMsat),
Proportional: uint32(i.config.ChannelFeePermyriad * 100), Proportional: uint32(i.config.ChannelFeePermyriad * 100),
ValidUntil: time.Now().UTC().Add(time.Duration(time.Hour * 24)).Format(basetypes.TIME_FORMAT), 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), MaxClientToSelfDelay: uint32(10000),
} }
} }
@@ -364,15 +368,15 @@ func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegiste
return nil return nil
} }
func (i *Interceptor) isCurrentChainFeeCheaper(token string, params *OpeningFeeParams) bool { func (i *Interceptor) isCurrentChainFeeCheaper(token string, params *shared.OpeningFeeParams) bool {
settings, err := i.store.GetFeeParamsSettings(token) settings, err := i.openingStore.GetFeeParamsSettings(token)
if err != nil { if err != nil {
log.Printf("Failed to get fee params settings: %v", err) log.Printf("Failed to get fee params settings: %v", err)
return false return false
} }
for _, setting := range settings { for _, setting := range settings {
if setting.Params.MinMsat <= params.MinMsat { if setting.Params.MinFeeMsat <= params.MinFeeMsat {
return true return true
} }
} }

View File

@@ -3,26 +3,13 @@ package interceptor
import ( import (
"time" "time"
"github.com/breez/lspd/shared"
"github.com/btcsuite/btcd/wire" "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 { 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 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 InsertChannel(initialChanID, confirmedChanId uint64, channelPoint string, nodeID []byte, lastUpdate time.Time) error
GetFeeParamsSettings(token string) ([]*OpeningFeeParamsSetting, error)
} }

View File

@@ -92,12 +92,13 @@ func main() {
} }
interceptStore := postgresql.NewPostgresInterceptStore(pool) interceptStore := postgresql.NewPostgresInterceptStore(pool)
openingStore := postgresql.NewPostgresOpeningStore(pool)
forwardingStore := postgresql.NewForwardingEventStore(pool) forwardingStore := postgresql.NewForwardingEventStore(pool)
notificationsStore := postgresql.NewNotificationsStore(pool) notificationsStore := postgresql.NewNotificationsStore(pool)
lsps2Store := postgresql.NewLsps2Store(pool) lsps2Store := postgresql.NewLsps2Store(pool)
notificationService := notifications.NewNotificationService(notificationsStore) notificationService := notifications.NewNotificationService(notificationsStore)
openingService := shared.NewOpeningService(interceptStore, nodesService) openingService := shared.NewOpeningService(openingStore, nodesService)
var interceptors []interceptor.HtlcInterceptor var interceptors []interceptor.HtlcInterceptor
for _, node := range nodes { for _, node := range nodes {
var htlcInterceptor interceptor.HtlcInterceptor var htlcInterceptor interceptor.HtlcInterceptor
@@ -109,7 +110,7 @@ func main() {
client.StartListeners() client.StartListeners()
fwsync := lnd.NewForwardingHistorySync(client, interceptStore, forwardingStore) 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) htlcInterceptor, err = lnd.NewLndHtlcInterceptor(node.NodeConfig, client, fwsync, interceptor)
if err != nil { if err != nil {
log.Fatalf("failed to initialize LND interceptor: %v", err) log.Fatalf("failed to initialize LND interceptor: %v", err)
@@ -122,7 +123,7 @@ func main() {
log.Fatalf("failed to initialize CLN client: %v", err) 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) htlcInterceptor, err = cln.NewClnHtlcInterceptor(node.NodeConfig, client, interceptor)
if err != nil { if err != nil {
log.Fatalf("failed to initialize CLN interceptor: %v", err) log.Fatalf("failed to initialize CLN interceptor: %v", err)

View File

@@ -8,18 +8,13 @@ import (
"time" "time"
"github.com/breez/lspd/basetypes" "github.com/breez/lspd/basetypes"
"github.com/breez/lspd/interceptor" "github.com/breez/lspd/shared"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/jackc/pgtype" "github.com/jackc/pgtype"
"github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool" "github.com/jackc/pgx/v4/pgxpool"
) )
type extendedParams struct {
Token string `json:"token"`
Params interceptor.OpeningFeeParams `json:"fees_params"`
}
type PostgresInterceptStore struct { type PostgresInterceptStore struct {
pool *pgxpool.Pool pool *pgxpool.Pool
} }
@@ -28,7 +23,7 @@ func NewPostgresInterceptStore(pool *pgxpool.Pool) *PostgresInterceptStore {
return &PostgresInterceptStore{pool: pool} 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 ( var (
p, tag *string p, tag *string
paymentHash, paymentSecret, destination []byte paymentHash, paymentSecret, destination []byte
@@ -77,7 +72,7 @@ func (s *PostgresInterceptStore) SetFundingTx(paymentHash []byte, channelPoint *
return err 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 var t *string
if tag != "" { if tag != "" {
t = &tag t = &tag
@@ -127,36 +122,3 @@ func (s *PostgresInterceptStore) InsertChannel(initialChanID, confirmedChanId ui
initialChanID, confirmedChanId, nodeID, c.String()) initialChanID, confirmedChanId, nodeID, c.String())
return nil 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, &param)
if err != nil {
return nil, err
}
var params *interceptor.OpeningFeeParams
err := json.Unmarshal([]byte(param), &params)
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
}

View File

@@ -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, &param)
if err != nil {
return nil, err
}
var params *shared.OpeningFeeParams
err := json.Unmarshal([]byte(param), &params)
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
}

View File

@@ -10,7 +10,6 @@ import (
"time" "time"
"github.com/breez/lspd/basetypes" "github.com/breez/lspd/basetypes"
"github.com/breez/lspd/interceptor"
"github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/btcsuite/btcd/btcec/v2/ecdsa"
) )
@@ -21,12 +20,12 @@ type OpeningService interface {
} }
type openingService struct { type openingService struct {
store interceptor.InterceptStore store OpeningStore
nodesService NodesService nodesService NodesService
} }
func NewOpeningService( func NewOpeningService(
store interceptor.InterceptStore, store OpeningStore,
nodesService NodesService, nodesService NodesService,
) OpeningService { ) OpeningService {
return &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) { func (s *openingService) GetFeeParamsMenu(token string, privateKey *btcec.PrivateKey) ([]*OpeningFeeParams, error) {
var menu []*OpeningFeeParams var menu []*OpeningFeeParams
settings, err := s.store.GetFeeParamsSettings(token) settings, err := s.store.GetFeeParamsSettings(token)
@@ -59,10 +49,10 @@ func (s *openingService) GetFeeParamsMenu(token string, privateKey *btcec.Privat
for _, setting := range settings { for _, setting := range settings {
validUntil := time.Now().UTC().Add(setting.Validity) validUntil := time.Now().UTC().Add(setting.Validity)
params := &OpeningFeeParams{ params := &OpeningFeeParams{
MinFeeMsat: setting.Params.MinMsat, MinFeeMsat: setting.Params.MinFeeMsat,
Proportional: setting.Params.Proportional, Proportional: setting.Params.Proportional,
ValidUntil: validUntil.Format(basetypes.TIME_FORMAT), ValidUntil: validUntil.Format(basetypes.TIME_FORMAT),
MinLifetime: setting.Params.MaxIdleTime, MinLifetime: setting.Params.MinLifetime,
MaxClientToSelfDelay: setting.Params.MaxClientToSelfDelay, MaxClientToSelfDelay: setting.Params.MaxClientToSelfDelay,
} }

21
shared/opening_store.go Normal file
View File

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