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

Lsps2 forwarding #125

Merged
merged 17 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 3 deletions channel_opener_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
16 changes: 10 additions & 6 deletions interceptor/intercept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/sync/singleflight"
)
Expand Down Expand Up @@ -49,6 +50,7 @@ type Interceptor struct {
client lightning.Client
config *config.NodeConfig
store InterceptStore
openingStore shared.OpeningStore
feeEstimator chain.FeeEstimator
feeStrategy chain.FeeStrategy
payHashGroup singleflight.Group
Expand All @@ -59,6 +61,7 @@ func NewInterceptor(
client lightning.Client,
config *config.NodeConfig,
store InterceptStore,
openingStore shared.OpeningStore,
feeEstimator chain.FeeEstimator,
feeStrategy chain.FeeStrategy,
notificationService *notifications.NotificationService,
Expand All @@ -67,6 +70,7 @@ func NewInterceptor(
client: client,
config: config,
store: store,
openingStore: openingStore,
feeEstimator: feeEstimator,
feeStrategy: feeStrategy,
notificationService: notificationService,
Expand Down Expand Up @@ -176,11 +180,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),
}
}
Expand Down Expand Up @@ -353,15 +357,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
}
}
Expand Down
19 changes: 3 additions & 16 deletions interceptor/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,13 @@ package interceptor
import (
"time"

"github.com/breez/lspd/shared"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it's a nit about naming and it's already in the branch, but I don't think that the name common or lncommon convey more the meaning than "shared"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename that in a separate PR

"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)
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
44 changes: 3 additions & 41 deletions postgresql/intercept_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, &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
}
57 changes: 57 additions & 0 deletions postgresql/opening_store.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 4 additions & 14 deletions shared/opening_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -55,10 +45,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,
}

Expand Down
21 changes: 21 additions & 0 deletions shared/opening_store.go
Original file line number Diff line number Diff line change
@@ -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)
}