From c1b80420df20c15d3356acdcd3f186952b1e01df Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Fri, 24 Mar 2023 16:08:42 +0100 Subject: [PATCH] cleanup: move types to appropriate packages --- cln_client.go => cln/cln_client.go | 2 +- cln_interceptor.go => cln/cln_interceptor.go | 38 ++-- email.go => interceptor/email.go | 2 +- .../htlc_interceptor.go | 2 +- intercept.go => interceptor/intercept.go | 162 +++++++++--------- lnd_client.go => lnd/client.go | 2 +- .../forwarding_history.go | 7 +- lnd_interceptor.go => lnd/interceptor.go | 31 ++-- .../macaroon_credential.go | 2 +- main.go | 23 ++- server.go | 6 +- 11 files changed, 144 insertions(+), 133 deletions(-) rename cln_client.go => cln/cln_client.go (99%) rename cln_interceptor.go => cln/cln_interceptor.go (87%) rename email.go => interceptor/email.go (99%) rename htlc_interceptor.go => interceptor/htlc_interceptor.go (80%) rename intercept.go => interceptor/intercept.go (65%) rename lnd_client.go => lnd/client.go (99%) rename forwarding_history.go => lnd/forwarding_history.go (97%) rename lnd_interceptor.go => lnd/interceptor.go (85%) rename lnd_macaroon_credential.go => lnd/macaroon_credential.go (97%) diff --git a/cln_client.go b/cln/cln_client.go similarity index 99% rename from cln_client.go rename to cln/cln_client.go index 0026151..8475ce6 100644 --- a/cln_client.go +++ b/cln/cln_client.go @@ -1,4 +1,4 @@ -package main +package cln import ( "encoding/hex" diff --git a/cln_interceptor.go b/cln/cln_interceptor.go similarity index 87% rename from cln_interceptor.go rename to cln/cln_interceptor.go index cce2c94..71c4cc0 100644 --- a/cln_interceptor.go +++ b/cln/cln_interceptor.go @@ -1,4 +1,4 @@ -package main +package cln import ( "bytes" @@ -12,6 +12,7 @@ import ( "github.com/breez/lspd/cln_plugin/proto" "github.com/breez/lspd/config" + "github.com/breez/lspd/interceptor" sphinx "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/record" @@ -24,7 +25,7 @@ import ( ) type ClnHtlcInterceptor struct { - interceptor *Interceptor + interceptor *interceptor.Interceptor config *config.NodeConfig pluginAddress string client *ClnClient @@ -36,11 +37,12 @@ type ClnHtlcInterceptor struct { cancel context.CancelFunc } -func NewClnHtlcInterceptor(conf *config.NodeConfig, client *ClnClient, interceptor *Interceptor) (*ClnHtlcInterceptor, error) { +func NewClnHtlcInterceptor(conf *config.NodeConfig, client *ClnClient, interceptor *interceptor.Interceptor) (*ClnHtlcInterceptor, error) { i := &ClnHtlcInterceptor{ config: conf, pluginAddress: conf.Cln.PluginAddress, client: client, + interceptor: interceptor, } i.initWg.Add(1) @@ -163,14 +165,14 @@ func (i *ClnHtlcInterceptor) intercept() error { i.doneWg.Done() } interceptResult := i.interceptor.Intercept(nextHop, paymentHash, request.Onion.ForwardMsat, request.Onion.OutgoingCltvValue, request.Htlc.CltvExpiry) - switch interceptResult.action { - case INTERCEPT_RESUME_WITH_ONION: + switch interceptResult.Action { + case interceptor.INTERCEPT_RESUME_WITH_ONION: interceptorClient.Send(i.resumeWithOnion(request, interceptResult)) - case INTERCEPT_FAIL_HTLC_WITH_CODE: + case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE: interceptorClient.Send( - i.failWithCode(request, interceptResult.failureCode), + i.failWithCode(request, interceptResult.FailureCode), ) - case INTERCEPT_RESUME: + case interceptor.INTERCEPT_RESUME: fallthrough default: interceptorClient.Send( @@ -202,22 +204,22 @@ func (i *ClnHtlcInterceptor) WaitStarted() { i.initWg.Wait() } -func (i *ClnHtlcInterceptor) resumeWithOnion(request *proto.HtlcAccepted, interceptResult interceptResult) *proto.HtlcResolution { +func (i *ClnHtlcInterceptor) resumeWithOnion(request *proto.HtlcAccepted, interceptResult interceptor.InterceptResult) *proto.HtlcResolution { //decoding and encoding onion with alias in type 6 record. payload, err := hex.DecodeString(request.Onion.Payload) if err != nil { log.Printf("resumeWithOnion: hex.DecodeString(%v) error: %v", request.Onion.Payload, err) - return i.failWithCode(request, FAILURE_TEMPORARY_CHANNEL_FAILURE) + return i.failWithCode(request, interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE) } - newPayload, err := encodePayloadWithNextHop(payload, interceptResult.channelId) + newPayload, err := encodePayloadWithNextHop(payload, interceptResult.ChannelId) if err != nil { log.Printf("encodePayloadWithNextHop error: %v", err) - return i.failWithCode(request, FAILURE_TEMPORARY_CHANNEL_FAILURE) + return i.failWithCode(request, interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE) } newPayloadStr := hex.EncodeToString(newPayload) - chanId := lnwire.NewChanIDFromOutPoint(interceptResult.channelPoint).String() + chanId := lnwire.NewChanIDFromOutPoint(interceptResult.ChannelPoint).String() log.Printf("forwarding htlc to the destination node and a new private channel was opened") return &proto.HtlcResolution{ Correlationid: request.Correlationid, @@ -239,7 +241,7 @@ func (i *ClnHtlcInterceptor) defaultResolution(request *proto.HtlcAccepted) *pro } } -func (i *ClnHtlcInterceptor) failWithCode(request *proto.HtlcAccepted, code interceptFailureCode) *proto.HtlcResolution { +func (i *ClnHtlcInterceptor) failWithCode(request *proto.HtlcAccepted, code interceptor.InterceptFailureCode) *proto.HtlcResolution { return &proto.HtlcResolution{ Correlationid: request.Correlationid, Outcome: &proto.HtlcResolution_Fail{ @@ -298,13 +300,13 @@ func encodePayloadWithNextHop(payload []byte, channelId uint64) ([]byte, error) return newPayloadBuf.Bytes(), nil } -func (i *ClnHtlcInterceptor) mapFailureCode(original interceptFailureCode) string { +func (i *ClnHtlcInterceptor) mapFailureCode(original interceptor.InterceptFailureCode) string { switch original { - case FAILURE_TEMPORARY_CHANNEL_FAILURE: + case interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE: return "1007" - case FAILURE_TEMPORARY_NODE_FAILURE: + case interceptor.FAILURE_TEMPORARY_NODE_FAILURE: return "2002" - case FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: + case interceptor.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: return "400F" default: log.Printf("Unknown failure code %v, default to temporary channel failure.", original) diff --git a/email.go b/interceptor/email.go similarity index 99% rename from email.go rename to interceptor/email.go index b0a5fdb..857aacf 100644 --- a/email.go +++ b/interceptor/email.go @@ -1,4 +1,4 @@ -package main +package interceptor import ( "bytes" diff --git a/htlc_interceptor.go b/interceptor/htlc_interceptor.go similarity index 80% rename from htlc_interceptor.go rename to interceptor/htlc_interceptor.go index e572f0d..f824b36 100644 --- a/htlc_interceptor.go +++ b/interceptor/htlc_interceptor.go @@ -1,4 +1,4 @@ -package main +package interceptor type HtlcInterceptor interface { Start() error diff --git a/intercept.go b/interceptor/intercept.go similarity index 65% rename from intercept.go rename to interceptor/intercept.go index 1460a2b..7d8e1cb 100644 --- a/intercept.go +++ b/interceptor/intercept.go @@ -1,4 +1,4 @@ -package main +package interceptor import ( "bytes" @@ -11,7 +11,6 @@ import ( "github.com/breez/lspd/chain" "github.com/breez/lspd/config" - "github.com/breez/lspd/interceptor" "github.com/breez/lspd/lightning" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/wire" @@ -22,94 +21,97 @@ import ( "golang.org/x/sync/singleflight" ) -type interceptAction int +type InterceptAction int const ( - INTERCEPT_RESUME interceptAction = 0 - INTERCEPT_RESUME_WITH_ONION interceptAction = 1 - INTERCEPT_FAIL_HTLC_WITH_CODE interceptAction = 2 + INTERCEPT_RESUME InterceptAction = 0 + INTERCEPT_RESUME_WITH_ONION InterceptAction = 1 + INTERCEPT_FAIL_HTLC_WITH_CODE InterceptAction = 2 ) -type interceptFailureCode uint16 +type InterceptFailureCode uint16 var ( - FAILURE_TEMPORARY_CHANNEL_FAILURE interceptFailureCode = 0x1007 - FAILURE_TEMPORARY_NODE_FAILURE interceptFailureCode = 0x2002 - FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS interceptFailureCode = 0x400F + FAILURE_TEMPORARY_CHANNEL_FAILURE InterceptFailureCode = 0x1007 + FAILURE_TEMPORARY_NODE_FAILURE InterceptFailureCode = 0x2002 + FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS InterceptFailureCode = 0x400F ) -var payHashGroup singleflight.Group -var feeEstimator chain.FeeEstimator -var feeStrategy chain.FeeStrategy - -type interceptResult struct { - action interceptAction - failureCode interceptFailureCode - destination []byte - amountMsat uint64 - channelPoint *wire.OutPoint - channelId uint64 - onionBlob []byte +type InterceptResult struct { + Action InterceptAction + FailureCode InterceptFailureCode + Destination []byte + AmountMsat uint64 + ChannelPoint *wire.OutPoint + ChannelId uint64 + OnionBlob []byte } type Interceptor struct { - client lightning.Client - config *config.NodeConfig - store interceptor.InterceptStore + client lightning.Client + config *config.NodeConfig + store InterceptStore + feeEstimator chain.FeeEstimator + feeStrategy chain.FeeStrategy + payHashGroup singleflight.Group } func NewInterceptor( client lightning.Client, config *config.NodeConfig, - store interceptor.InterceptStore, + store InterceptStore, + feeEstimator chain.FeeEstimator, + feeStrategy chain.FeeStrategy, ) *Interceptor { return &Interceptor{ - client: client, - config: config, - store: store, + client: client, + config: config, + store: store, + feeEstimator: feeEstimator, + feeStrategy: feeStrategy, } } -func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) interceptResult { +func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) InterceptResult { reqPaymentHashStr := hex.EncodeToString(reqPaymentHash) - resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) { + resp, _, _ := i.payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) { paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := i.store.PaymentInfo(reqPaymentHash) if err != nil { log.Printf("paymentInfo(%x) error: %v", reqPaymentHash, err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_NODE_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_NODE_FAILURE, }, nil } log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v", paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat) if paymentSecret == nil || (nextHop != "" && nextHop != hex.EncodeToString(destination)) { - return interceptResult{ - action: INTERCEPT_RESUME, + return InterceptResult{ + Action: INTERCEPT_RESUME, }, nil } if channelPoint == nil { if bytes.Equal(paymentHash, reqPaymentHash) { if int64(reqIncomingExpiry)-int64(reqOutgoingExpiry) < int64(i.config.TimeLockDelta) { - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } channelPoint, err = i.openChannel(reqPaymentHash, destination, incomingAmountMsat) if err != nil { log.Printf("openChannel(%x, %v) err: %v", destination, incomingAmountMsat, err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } } else { //probing - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, }, nil } } @@ -117,18 +119,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi pubKey, err := btcec.ParsePubKey(destination) if err != nil { log.Printf("btcec.ParsePubKey(%x): %v", destination, err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } sessionKey, err := btcec.NewPrivateKey() if err != nil { log.Printf("btcec.NewPrivateKey(): %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } @@ -148,18 +150,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi err = hop.PackHopPayload(&b, uint64(0)) if err != nil { log.Printf("hop.PackHopPayload(): %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } payload, err := sphinx.NewHopPayload(nil, b.Bytes()) if err != nil { log.Printf("sphinx.NewHopPayload(): %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } @@ -174,18 +176,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi ) if err != nil { log.Printf("sphinx.NewOnionPacket(): %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } var onionBlob bytes.Buffer err = sphinxPacket.Encode(&onionBlob) if err != nil { log.Printf("sphinxPacket.Encode(): %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } @@ -206,9 +208,9 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi if err != nil { log.Printf("insertChannel error: %v", err) - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil } @@ -217,13 +219,13 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi channelID = uint64(chanResult.InitialChannelID) } - return interceptResult{ - action: INTERCEPT_RESUME_WITH_ONION, - destination: destination, - channelPoint: channelPoint, - channelId: channelID, - amountMsat: uint64(amt), - onionBlob: onionBlob.Bytes(), + return InterceptResult{ + Action: INTERCEPT_RESUME_WITH_ONION, + Destination: destination, + ChannelPoint: channelPoint, + ChannelId: channelID, + AmountMsat: uint64(amt), + OnionBlob: onionBlob.Bytes(), }, nil } @@ -236,13 +238,13 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi } log.Printf("Error: Channel failed to opened... timed out. ") - return interceptResult{ - action: INTERCEPT_FAIL_HTLC_WITH_CODE, - failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, + return InterceptResult{ + Action: INTERCEPT_FAIL_HTLC_WITH_CODE, + FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE, }, nil }) - return resp.(interceptResult) + return resp.(InterceptResult) } func (i *Interceptor) openChannel(paymentHash, destination []byte, incomingAmountMsat int64) (*wire.OutPoint, error) { @@ -255,10 +257,10 @@ func (i *Interceptor) openChannel(paymentHash, destination []byte, incomingAmoun confStr := "" var feeEstimation *float64 feeStr := "" - if feeEstimator != nil { - fee, err := feeEstimator.EstimateFeeRate( + if i.feeEstimator != nil { + fee, err := i.feeEstimator.EstimateFeeRate( context.Background(), - feeStrategy, + i.feeStrategy, ) if err == nil { feeEstimation = &fee.SatPerVByte diff --git a/lnd_client.go b/lnd/client.go similarity index 99% rename from lnd_client.go rename to lnd/client.go index 1ced705..0105569 100644 --- a/lnd_client.go +++ b/lnd/client.go @@ -1,4 +1,4 @@ -package main +package lnd import ( "context" diff --git a/forwarding_history.go b/lnd/forwarding_history.go similarity index 97% rename from forwarding_history.go rename to lnd/forwarding_history.go index 1fa960d..debd93f 100644 --- a/forwarding_history.go +++ b/lnd/forwarding_history.go @@ -1,4 +1,4 @@ -package main +package lnd import ( "context" @@ -8,7 +8,6 @@ import ( "time" "github.com/breez/lspd/interceptor" - "github.com/breez/lspd/lnd" "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/chainrpc" @@ -41,13 +40,13 @@ func (cfe *copyFromEvents) Err() error { type ForwardingHistorySync struct { client *LndClient interceptStore interceptor.InterceptStore - forwardingStore lnd.ForwardingEventStore + forwardingStore ForwardingEventStore } func NewForwardingHistorySync( client *LndClient, interceptStore interceptor.InterceptStore, - forwardingStore lnd.ForwardingEventStore, + forwardingStore ForwardingEventStore, ) *ForwardingHistorySync { return &ForwardingHistorySync{ client: client, diff --git a/lnd_interceptor.go b/lnd/interceptor.go similarity index 85% rename from lnd_interceptor.go rename to lnd/interceptor.go index ca84cd8..7affba9 100644 --- a/lnd_interceptor.go +++ b/lnd/interceptor.go @@ -1,4 +1,4 @@ -package main +package lnd import ( "context" @@ -8,6 +8,7 @@ import ( "time" "github.com/breez/lspd/config" + "github.com/breez/lspd/interceptor" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" "google.golang.org/grpc/codes" @@ -16,7 +17,7 @@ import ( type LndHtlcInterceptor struct { fwsync *ForwardingHistorySync - interceptor *Interceptor + interceptor *interceptor.Interceptor config *config.NodeConfig client *LndClient stopRequested bool @@ -30,7 +31,7 @@ func NewLndHtlcInterceptor( conf *config.NodeConfig, client *LndClient, fwsync *ForwardingHistorySync, - interceptor *Interceptor, + interceptor *interceptor.Interceptor, ) (*LndHtlcInterceptor, error) { i := &LndHtlcInterceptor{ config: conf, @@ -152,22 +153,22 @@ func (i *LndHtlcInterceptor) intercept() error { i.doneWg.Add(1) go func() { interceptResult := i.interceptor.Intercept(nextHop, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry, request.IncomingExpiry) - switch interceptResult.action { - case INTERCEPT_RESUME_WITH_ONION: + switch interceptResult.Action { + case interceptor.INTERCEPT_RESUME_WITH_ONION: interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{ IncomingCircuitKey: request.IncomingCircuitKey, Action: routerrpc.ResolveHoldForwardAction_RESUME, - OutgoingAmountMsat: interceptResult.amountMsat, - OutgoingRequestedChanId: uint64(interceptResult.channelId), - OnionBlob: interceptResult.onionBlob, + OutgoingAmountMsat: interceptResult.AmountMsat, + OutgoingRequestedChanId: uint64(interceptResult.ChannelId), + OnionBlob: interceptResult.OnionBlob, }) - case INTERCEPT_FAIL_HTLC_WITH_CODE: + case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE: interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{ IncomingCircuitKey: request.IncomingCircuitKey, Action: routerrpc.ResolveHoldForwardAction_FAIL, - FailureCode: i.mapFailureCode(interceptResult.failureCode), + FailureCode: i.mapFailureCode(interceptResult.FailureCode), }) - case INTERCEPT_RESUME: + case interceptor.INTERCEPT_RESUME: fallthrough default: interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{ @@ -187,13 +188,13 @@ func (i *LndHtlcInterceptor) intercept() error { } } -func (i *LndHtlcInterceptor) mapFailureCode(original interceptFailureCode) lnrpc.Failure_FailureCode { +func (i *LndHtlcInterceptor) mapFailureCode(original interceptor.InterceptFailureCode) lnrpc.Failure_FailureCode { switch original { - case FAILURE_TEMPORARY_CHANNEL_FAILURE: + case interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE: return lnrpc.Failure_TEMPORARY_CHANNEL_FAILURE - case FAILURE_TEMPORARY_NODE_FAILURE: + case interceptor.FAILURE_TEMPORARY_NODE_FAILURE: return lnrpc.Failure_TEMPORARY_NODE_FAILURE - case FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: + case interceptor.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS: return lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS default: log.Printf("Unknown failure code %v, default to temporary channel failure.", original) diff --git a/lnd_macaroon_credential.go b/lnd/macaroon_credential.go similarity index 97% rename from lnd_macaroon_credential.go rename to lnd/macaroon_credential.go index 8e70f87..f65ddaa 100644 --- a/lnd_macaroon_credential.go +++ b/lnd/macaroon_credential.go @@ -1,4 +1,4 @@ -package main +package lnd import ( "context" diff --git a/main.go b/main.go index 1f40262..a2821a5 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,10 @@ import ( "syscall" "github.com/breez/lspd/chain" + "github.com/breez/lspd/cln" "github.com/breez/lspd/config" + "github.com/breez/lspd/interceptor" + "github.com/breez/lspd/lnd" "github.com/breez/lspd/mempool" "github.com/breez/lspd/postgresql" "github.com/btcsuite/btcd/btcec/v2" @@ -38,6 +41,8 @@ func main() { log.Fatalf("need at least one node configured in NODES.") } + var feeEstimator chain.FeeEstimator + var feeStrategy chain.FeeStrategy useMempool := os.Getenv("USE_MEMPOOL_FEE_ESTIMATION") == "true" if useMempool { mempoolUrl := os.Getenv("MEMPOOL_API_BASE_URL") @@ -73,31 +78,31 @@ func main() { interceptStore := postgresql.NewPostgresInterceptStore(pool) forwardingStore := postgresql.NewForwardingEventStore(pool) - var interceptors []HtlcInterceptor + var interceptors []interceptor.HtlcInterceptor for _, node := range nodes { - var htlcInterceptor HtlcInterceptor + var htlcInterceptor interceptor.HtlcInterceptor if node.Lnd != nil { - client, err := NewLndClient(node.Lnd) + client, err := lnd.NewLndClient(node.Lnd) if err != nil { log.Fatalf("failed to initialize LND client: %v", err) } - fwsync := NewForwardingHistorySync(client, interceptStore, forwardingStore) - interceptor := NewInterceptor(client, node, interceptStore) - htlcInterceptor, err = NewLndHtlcInterceptor(node, client, fwsync, interceptor) + fwsync := lnd.NewForwardingHistorySync(client, interceptStore, forwardingStore) + interceptor := interceptor.NewInterceptor(client, node, interceptStore, feeEstimator, feeStrategy) + htlcInterceptor, err = lnd.NewLndHtlcInterceptor(node, client, fwsync, interceptor) if err != nil { log.Fatalf("failed to initialize LND interceptor: %v", err) } } if node.Cln != nil { - client, err := NewClnClient(node.Cln.SocketPath) + client, err := cln.NewClnClient(node.Cln.SocketPath) if err != nil { log.Fatalf("failed to initialize CLN client: %v", err) } - interceptor := NewInterceptor(client, node, interceptStore) - htlcInterceptor, err = NewClnHtlcInterceptor(node, client, interceptor) + interceptor := interceptor.NewInterceptor(client, node, interceptStore, feeEstimator, feeStrategy) + htlcInterceptor, err = cln.NewClnHtlcInterceptor(node, client, interceptor) if err != nil { log.Fatalf("failed to initialize CLN interceptor: %v", err) } diff --git a/server.go b/server.go index 184e794..ac87928 100644 --- a/server.go +++ b/server.go @@ -11,9 +11,11 @@ import ( "strings" "github.com/breez/lspd/btceclegacy" + "github.com/breez/lspd/cln" "github.com/breez/lspd/config" "github.com/breez/lspd/interceptor" "github.com/breez/lspd/lightning" + "github.com/breez/lspd/lnd" lspdrpc "github.com/breez/lspd/rpc" ecies "github.com/ecies/go/v2" "github.com/golang/protobuf/proto" @@ -301,14 +303,14 @@ func NewGrpcServer( } if config.Lnd != nil { - node.client, err = NewLndClient(config.Lnd) + node.client, err = lnd.NewLndClient(config.Lnd) if err != nil { return nil, err } } if config.Cln != nil { - node.client, err = NewClnClient(config.Cln.SocketPath) + node.client, err = cln.NewClnClient(config.Cln.SocketPath) if err != nil { return nil, err }