mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-18 14:24:21 +01:00
cleanup: move types to appropriate packages
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package cln
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package cln
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/breez/lspd/cln_plugin/proto"
|
"github.com/breez/lspd/cln_plugin/proto"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/interceptor"
|
||||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/record"
|
"github.com/lightningnetwork/lnd/record"
|
||||||
@@ -24,7 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ClnHtlcInterceptor struct {
|
type ClnHtlcInterceptor struct {
|
||||||
interceptor *Interceptor
|
interceptor *interceptor.Interceptor
|
||||||
config *config.NodeConfig
|
config *config.NodeConfig
|
||||||
pluginAddress string
|
pluginAddress string
|
||||||
client *ClnClient
|
client *ClnClient
|
||||||
@@ -36,11 +37,12 @@ type ClnHtlcInterceptor struct {
|
|||||||
cancel context.CancelFunc
|
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{
|
i := &ClnHtlcInterceptor{
|
||||||
config: conf,
|
config: conf,
|
||||||
pluginAddress: conf.Cln.PluginAddress,
|
pluginAddress: conf.Cln.PluginAddress,
|
||||||
client: client,
|
client: client,
|
||||||
|
interceptor: interceptor,
|
||||||
}
|
}
|
||||||
|
|
||||||
i.initWg.Add(1)
|
i.initWg.Add(1)
|
||||||
@@ -163,14 +165,14 @@ func (i *ClnHtlcInterceptor) intercept() error {
|
|||||||
i.doneWg.Done()
|
i.doneWg.Done()
|
||||||
}
|
}
|
||||||
interceptResult := i.interceptor.Intercept(nextHop, paymentHash, request.Onion.ForwardMsat, request.Onion.OutgoingCltvValue, request.Htlc.CltvExpiry)
|
interceptResult := i.interceptor.Intercept(nextHop, paymentHash, request.Onion.ForwardMsat, request.Onion.OutgoingCltvValue, request.Htlc.CltvExpiry)
|
||||||
switch interceptResult.action {
|
switch interceptResult.Action {
|
||||||
case INTERCEPT_RESUME_WITH_ONION:
|
case interceptor.INTERCEPT_RESUME_WITH_ONION:
|
||||||
interceptorClient.Send(i.resumeWithOnion(request, interceptResult))
|
interceptorClient.Send(i.resumeWithOnion(request, interceptResult))
|
||||||
case INTERCEPT_FAIL_HTLC_WITH_CODE:
|
case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE:
|
||||||
interceptorClient.Send(
|
interceptorClient.Send(
|
||||||
i.failWithCode(request, interceptResult.failureCode),
|
i.failWithCode(request, interceptResult.FailureCode),
|
||||||
)
|
)
|
||||||
case INTERCEPT_RESUME:
|
case interceptor.INTERCEPT_RESUME:
|
||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
interceptorClient.Send(
|
interceptorClient.Send(
|
||||||
@@ -202,22 +204,22 @@ func (i *ClnHtlcInterceptor) WaitStarted() {
|
|||||||
i.initWg.Wait()
|
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.
|
//decoding and encoding onion with alias in type 6 record.
|
||||||
payload, err := hex.DecodeString(request.Onion.Payload)
|
payload, err := hex.DecodeString(request.Onion.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("resumeWithOnion: hex.DecodeString(%v) error: %v", request.Onion.Payload, err)
|
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 {
|
if err != nil {
|
||||||
log.Printf("encodePayloadWithNextHop error: %v", err)
|
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)
|
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")
|
log.Printf("forwarding htlc to the destination node and a new private channel was opened")
|
||||||
return &proto.HtlcResolution{
|
return &proto.HtlcResolution{
|
||||||
Correlationid: request.Correlationid,
|
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{
|
return &proto.HtlcResolution{
|
||||||
Correlationid: request.Correlationid,
|
Correlationid: request.Correlationid,
|
||||||
Outcome: &proto.HtlcResolution_Fail{
|
Outcome: &proto.HtlcResolution_Fail{
|
||||||
@@ -298,13 +300,13 @@ func encodePayloadWithNextHop(payload []byte, channelId uint64) ([]byte, error)
|
|||||||
return newPayloadBuf.Bytes(), nil
|
return newPayloadBuf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ClnHtlcInterceptor) mapFailureCode(original interceptFailureCode) string {
|
func (i *ClnHtlcInterceptor) mapFailureCode(original interceptor.InterceptFailureCode) string {
|
||||||
switch original {
|
switch original {
|
||||||
case FAILURE_TEMPORARY_CHANNEL_FAILURE:
|
case interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE:
|
||||||
return "1007"
|
return "1007"
|
||||||
case FAILURE_TEMPORARY_NODE_FAILURE:
|
case interceptor.FAILURE_TEMPORARY_NODE_FAILURE:
|
||||||
return "2002"
|
return "2002"
|
||||||
case FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
|
case interceptor.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
|
||||||
return "400F"
|
return "400F"
|
||||||
default:
|
default:
|
||||||
log.Printf("Unknown failure code %v, default to temporary channel failure.", original)
|
log.Printf("Unknown failure code %v, default to temporary channel failure.", original)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package interceptor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package interceptor
|
||||||
|
|
||||||
type HtlcInterceptor interface {
|
type HtlcInterceptor interface {
|
||||||
Start() error
|
Start() error
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package interceptor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"github.com/breez/lspd/chain"
|
"github.com/breez/lspd/chain"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
"github.com/breez/lspd/interceptor"
|
|
||||||
"github.com/breez/lspd/lightning"
|
"github.com/breez/lspd/lightning"
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
@@ -22,94 +21,97 @@ import (
|
|||||||
"golang.org/x/sync/singleflight"
|
"golang.org/x/sync/singleflight"
|
||||||
)
|
)
|
||||||
|
|
||||||
type interceptAction int
|
type InterceptAction int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
INTERCEPT_RESUME interceptAction = 0
|
INTERCEPT_RESUME InterceptAction = 0
|
||||||
INTERCEPT_RESUME_WITH_ONION interceptAction = 1
|
INTERCEPT_RESUME_WITH_ONION InterceptAction = 1
|
||||||
INTERCEPT_FAIL_HTLC_WITH_CODE interceptAction = 2
|
INTERCEPT_FAIL_HTLC_WITH_CODE InterceptAction = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
type interceptFailureCode uint16
|
type InterceptFailureCode uint16
|
||||||
|
|
||||||
var (
|
var (
|
||||||
FAILURE_TEMPORARY_CHANNEL_FAILURE interceptFailureCode = 0x1007
|
FAILURE_TEMPORARY_CHANNEL_FAILURE InterceptFailureCode = 0x1007
|
||||||
FAILURE_TEMPORARY_NODE_FAILURE interceptFailureCode = 0x2002
|
FAILURE_TEMPORARY_NODE_FAILURE InterceptFailureCode = 0x2002
|
||||||
FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS interceptFailureCode = 0x400F
|
FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS InterceptFailureCode = 0x400F
|
||||||
)
|
)
|
||||||
|
|
||||||
var payHashGroup singleflight.Group
|
type InterceptResult struct {
|
||||||
var feeEstimator chain.FeeEstimator
|
Action InterceptAction
|
||||||
var feeStrategy chain.FeeStrategy
|
FailureCode InterceptFailureCode
|
||||||
|
Destination []byte
|
||||||
type interceptResult struct {
|
AmountMsat uint64
|
||||||
action interceptAction
|
ChannelPoint *wire.OutPoint
|
||||||
failureCode interceptFailureCode
|
ChannelId uint64
|
||||||
destination []byte
|
OnionBlob []byte
|
||||||
amountMsat uint64
|
|
||||||
channelPoint *wire.OutPoint
|
|
||||||
channelId uint64
|
|
||||||
onionBlob []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Interceptor struct {
|
type Interceptor struct {
|
||||||
client lightning.Client
|
client lightning.Client
|
||||||
config *config.NodeConfig
|
config *config.NodeConfig
|
||||||
store interceptor.InterceptStore
|
store InterceptStore
|
||||||
|
feeEstimator chain.FeeEstimator
|
||||||
|
feeStrategy chain.FeeStrategy
|
||||||
|
payHashGroup singleflight.Group
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInterceptor(
|
func NewInterceptor(
|
||||||
client lightning.Client,
|
client lightning.Client,
|
||||||
config *config.NodeConfig,
|
config *config.NodeConfig,
|
||||||
store interceptor.InterceptStore,
|
store InterceptStore,
|
||||||
|
feeEstimator chain.FeeEstimator,
|
||||||
|
feeStrategy chain.FeeStrategy,
|
||||||
) *Interceptor {
|
) *Interceptor {
|
||||||
return &Interceptor{
|
return &Interceptor{
|
||||||
client: client,
|
client: client,
|
||||||
config: config,
|
config: config,
|
||||||
store: store,
|
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)
|
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)
|
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := i.store.PaymentInfo(reqPaymentHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("paymentInfo(%x) error: %v", reqPaymentHash, err)
|
log.Printf("paymentInfo(%x) error: %v", reqPaymentHash, err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_NODE_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_NODE_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v",
|
log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v",
|
||||||
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat)
|
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat)
|
||||||
if paymentSecret == nil || (nextHop != "<unknown>" && nextHop != hex.EncodeToString(destination)) {
|
if paymentSecret == nil || (nextHop != "<unknown>" && nextHop != hex.EncodeToString(destination)) {
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_RESUME,
|
Action: INTERCEPT_RESUME,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if channelPoint == nil {
|
if channelPoint == nil {
|
||||||
if bytes.Equal(paymentHash, reqPaymentHash) {
|
if bytes.Equal(paymentHash, reqPaymentHash) {
|
||||||
if int64(reqIncomingExpiry)-int64(reqOutgoingExpiry) < int64(i.config.TimeLockDelta) {
|
if int64(reqIncomingExpiry)-int64(reqOutgoingExpiry) < int64(i.config.TimeLockDelta) {
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
channelPoint, err = i.openChannel(reqPaymentHash, destination, incomingAmountMsat)
|
channelPoint, err = i.openChannel(reqPaymentHash, destination, incomingAmountMsat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("openChannel(%x, %v) err: %v", destination, incomingAmountMsat, err)
|
log.Printf("openChannel(%x, %v) err: %v", destination, incomingAmountMsat, err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
} else { //probing
|
} else { //probing
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
|
FailureCode: FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,18 +119,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
pubKey, err := btcec.ParsePubKey(destination)
|
pubKey, err := btcec.ParsePubKey(destination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("btcec.ParsePubKey(%x): %v", destination, err)
|
log.Printf("btcec.ParsePubKey(%x): %v", destination, err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionKey, err := btcec.NewPrivateKey()
|
sessionKey, err := btcec.NewPrivateKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("btcec.NewPrivateKey(): %v", err)
|
log.Printf("btcec.NewPrivateKey(): %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,18 +150,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
err = hop.PackHopPayload(&b, uint64(0))
|
err = hop.PackHopPayload(&b, uint64(0))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("hop.PackHopPayload(): %v", err)
|
log.Printf("hop.PackHopPayload(): %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := sphinx.NewHopPayload(nil, b.Bytes())
|
payload, err := sphinx.NewHopPayload(nil, b.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("sphinx.NewHopPayload(): %v", err)
|
log.Printf("sphinx.NewHopPayload(): %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,18 +176,18 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("sphinx.NewOnionPacket(): %v", err)
|
log.Printf("sphinx.NewOnionPacket(): %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
var onionBlob bytes.Buffer
|
var onionBlob bytes.Buffer
|
||||||
err = sphinxPacket.Encode(&onionBlob)
|
err = sphinxPacket.Encode(&onionBlob)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("sphinxPacket.Encode(): %v", err)
|
log.Printf("sphinxPacket.Encode(): %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,9 +208,9 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("insertChannel error: %v", err)
|
log.Printf("insertChannel error: %v", err)
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,13 +219,13 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
channelID = uint64(chanResult.InitialChannelID)
|
channelID = uint64(chanResult.InitialChannelID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_RESUME_WITH_ONION,
|
Action: INTERCEPT_RESUME_WITH_ONION,
|
||||||
destination: destination,
|
Destination: destination,
|
||||||
channelPoint: channelPoint,
|
ChannelPoint: channelPoint,
|
||||||
channelId: channelID,
|
ChannelId: channelID,
|
||||||
amountMsat: uint64(amt),
|
AmountMsat: uint64(amt),
|
||||||
onionBlob: onionBlob.Bytes(),
|
OnionBlob: onionBlob.Bytes(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,13 +238,13 @@ func (i *Interceptor) Intercept(nextHop string, reqPaymentHash []byte, reqOutgoi
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Error: Channel failed to opened... timed out. ")
|
log.Printf("Error: Channel failed to opened... timed out. ")
|
||||||
return interceptResult{
|
return InterceptResult{
|
||||||
action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||||
failureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||||
}, nil
|
}, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return resp.(interceptResult)
|
return resp.(InterceptResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interceptor) openChannel(paymentHash, destination []byte, incomingAmountMsat int64) (*wire.OutPoint, error) {
|
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 := "<nil>"
|
confStr := "<nil>"
|
||||||
var feeEstimation *float64
|
var feeEstimation *float64
|
||||||
feeStr := "<nil>"
|
feeStr := "<nil>"
|
||||||
if feeEstimator != nil {
|
if i.feeEstimator != nil {
|
||||||
fee, err := feeEstimator.EstimateFeeRate(
|
fee, err := i.feeEstimator.EstimateFeeRate(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
feeStrategy,
|
i.feeStrategy,
|
||||||
)
|
)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
feeEstimation = &fee.SatPerVByte
|
feeEstimation = &fee.SatPerVByte
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package lnd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package lnd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/breez/lspd/interceptor"
|
"github.com/breez/lspd/interceptor"
|
||||||
"github.com/breez/lspd/lnd"
|
|
||||||
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
|
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
|
||||||
@@ -41,13 +40,13 @@ func (cfe *copyFromEvents) Err() error {
|
|||||||
type ForwardingHistorySync struct {
|
type ForwardingHistorySync struct {
|
||||||
client *LndClient
|
client *LndClient
|
||||||
interceptStore interceptor.InterceptStore
|
interceptStore interceptor.InterceptStore
|
||||||
forwardingStore lnd.ForwardingEventStore
|
forwardingStore ForwardingEventStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewForwardingHistorySync(
|
func NewForwardingHistorySync(
|
||||||
client *LndClient,
|
client *LndClient,
|
||||||
interceptStore interceptor.InterceptStore,
|
interceptStore interceptor.InterceptStore,
|
||||||
forwardingStore lnd.ForwardingEventStore,
|
forwardingStore ForwardingEventStore,
|
||||||
) *ForwardingHistorySync {
|
) *ForwardingHistorySync {
|
||||||
return &ForwardingHistorySync{
|
return &ForwardingHistorySync{
|
||||||
client: client,
|
client: client,
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package lnd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/interceptor"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
@@ -16,7 +17,7 @@ import (
|
|||||||
|
|
||||||
type LndHtlcInterceptor struct {
|
type LndHtlcInterceptor struct {
|
||||||
fwsync *ForwardingHistorySync
|
fwsync *ForwardingHistorySync
|
||||||
interceptor *Interceptor
|
interceptor *interceptor.Interceptor
|
||||||
config *config.NodeConfig
|
config *config.NodeConfig
|
||||||
client *LndClient
|
client *LndClient
|
||||||
stopRequested bool
|
stopRequested bool
|
||||||
@@ -30,7 +31,7 @@ func NewLndHtlcInterceptor(
|
|||||||
conf *config.NodeConfig,
|
conf *config.NodeConfig,
|
||||||
client *LndClient,
|
client *LndClient,
|
||||||
fwsync *ForwardingHistorySync,
|
fwsync *ForwardingHistorySync,
|
||||||
interceptor *Interceptor,
|
interceptor *interceptor.Interceptor,
|
||||||
) (*LndHtlcInterceptor, error) {
|
) (*LndHtlcInterceptor, error) {
|
||||||
i := &LndHtlcInterceptor{
|
i := &LndHtlcInterceptor{
|
||||||
config: conf,
|
config: conf,
|
||||||
@@ -152,22 +153,22 @@ func (i *LndHtlcInterceptor) intercept() error {
|
|||||||
i.doneWg.Add(1)
|
i.doneWg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
interceptResult := i.interceptor.Intercept(nextHop, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry, request.IncomingExpiry)
|
interceptResult := i.interceptor.Intercept(nextHop, request.PaymentHash, request.OutgoingAmountMsat, request.OutgoingExpiry, request.IncomingExpiry)
|
||||||
switch interceptResult.action {
|
switch interceptResult.Action {
|
||||||
case INTERCEPT_RESUME_WITH_ONION:
|
case interceptor.INTERCEPT_RESUME_WITH_ONION:
|
||||||
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
|
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
|
||||||
IncomingCircuitKey: request.IncomingCircuitKey,
|
IncomingCircuitKey: request.IncomingCircuitKey,
|
||||||
Action: routerrpc.ResolveHoldForwardAction_RESUME,
|
Action: routerrpc.ResolveHoldForwardAction_RESUME,
|
||||||
OutgoingAmountMsat: interceptResult.amountMsat,
|
OutgoingAmountMsat: interceptResult.AmountMsat,
|
||||||
OutgoingRequestedChanId: uint64(interceptResult.channelId),
|
OutgoingRequestedChanId: uint64(interceptResult.ChannelId),
|
||||||
OnionBlob: interceptResult.onionBlob,
|
OnionBlob: interceptResult.OnionBlob,
|
||||||
})
|
})
|
||||||
case INTERCEPT_FAIL_HTLC_WITH_CODE:
|
case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE:
|
||||||
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
|
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
|
||||||
IncomingCircuitKey: request.IncomingCircuitKey,
|
IncomingCircuitKey: request.IncomingCircuitKey,
|
||||||
Action: routerrpc.ResolveHoldForwardAction_FAIL,
|
Action: routerrpc.ResolveHoldForwardAction_FAIL,
|
||||||
FailureCode: i.mapFailureCode(interceptResult.failureCode),
|
FailureCode: i.mapFailureCode(interceptResult.FailureCode),
|
||||||
})
|
})
|
||||||
case INTERCEPT_RESUME:
|
case interceptor.INTERCEPT_RESUME:
|
||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
interceptorClient.Send(&routerrpc.ForwardHtlcInterceptResponse{
|
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 {
|
switch original {
|
||||||
case FAILURE_TEMPORARY_CHANNEL_FAILURE:
|
case interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE:
|
||||||
return lnrpc.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
|
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
|
return lnrpc.Failure_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS
|
||||||
default:
|
default:
|
||||||
log.Printf("Unknown failure code %v, default to temporary channel failure.", original)
|
log.Printf("Unknown failure code %v, default to temporary channel failure.", original)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package lnd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
23
main.go
23
main.go
@@ -11,7 +11,10 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/breez/lspd/chain"
|
"github.com/breez/lspd/chain"
|
||||||
|
"github.com/breez/lspd/cln"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
|
"github.com/breez/lspd/interceptor"
|
||||||
|
"github.com/breez/lspd/lnd"
|
||||||
"github.com/breez/lspd/mempool"
|
"github.com/breez/lspd/mempool"
|
||||||
"github.com/breez/lspd/postgresql"
|
"github.com/breez/lspd/postgresql"
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
@@ -38,6 +41,8 @@ func main() {
|
|||||||
log.Fatalf("need at least one node configured in NODES.")
|
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"
|
useMempool := os.Getenv("USE_MEMPOOL_FEE_ESTIMATION") == "true"
|
||||||
if useMempool {
|
if useMempool {
|
||||||
mempoolUrl := os.Getenv("MEMPOOL_API_BASE_URL")
|
mempoolUrl := os.Getenv("MEMPOOL_API_BASE_URL")
|
||||||
@@ -73,31 +78,31 @@ func main() {
|
|||||||
interceptStore := postgresql.NewPostgresInterceptStore(pool)
|
interceptStore := postgresql.NewPostgresInterceptStore(pool)
|
||||||
forwardingStore := postgresql.NewForwardingEventStore(pool)
|
forwardingStore := postgresql.NewForwardingEventStore(pool)
|
||||||
|
|
||||||
var interceptors []HtlcInterceptor
|
var interceptors []interceptor.HtlcInterceptor
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
var htlcInterceptor HtlcInterceptor
|
var htlcInterceptor interceptor.HtlcInterceptor
|
||||||
if node.Lnd != nil {
|
if node.Lnd != nil {
|
||||||
client, err := NewLndClient(node.Lnd)
|
client, err := lnd.NewLndClient(node.Lnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to initialize LND client: %v", err)
|
log.Fatalf("failed to initialize LND client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fwsync := NewForwardingHistorySync(client, interceptStore, forwardingStore)
|
fwsync := lnd.NewForwardingHistorySync(client, interceptStore, forwardingStore)
|
||||||
interceptor := NewInterceptor(client, node, interceptStore)
|
interceptor := interceptor.NewInterceptor(client, node, interceptStore, feeEstimator, feeStrategy)
|
||||||
htlcInterceptor, err = NewLndHtlcInterceptor(node, client, fwsync, interceptor)
|
htlcInterceptor, err = lnd.NewLndHtlcInterceptor(node, 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.Cln != nil {
|
if node.Cln != nil {
|
||||||
client, err := NewClnClient(node.Cln.SocketPath)
|
client, err := cln.NewClnClient(node.Cln.SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to initialize CLN client: %v", err)
|
log.Fatalf("failed to initialize CLN client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
interceptor := NewInterceptor(client, node, interceptStore)
|
interceptor := interceptor.NewInterceptor(client, node, interceptStore, feeEstimator, feeStrategy)
|
||||||
htlcInterceptor, err = NewClnHtlcInterceptor(node, client, interceptor)
|
htlcInterceptor, err = cln.NewClnHtlcInterceptor(node, 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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/breez/lspd/btceclegacy"
|
"github.com/breez/lspd/btceclegacy"
|
||||||
|
"github.com/breez/lspd/cln"
|
||||||
"github.com/breez/lspd/config"
|
"github.com/breez/lspd/config"
|
||||||
"github.com/breez/lspd/interceptor"
|
"github.com/breez/lspd/interceptor"
|
||||||
"github.com/breez/lspd/lightning"
|
"github.com/breez/lspd/lightning"
|
||||||
|
"github.com/breez/lspd/lnd"
|
||||||
lspdrpc "github.com/breez/lspd/rpc"
|
lspdrpc "github.com/breez/lspd/rpc"
|
||||||
ecies "github.com/ecies/go/v2"
|
ecies "github.com/ecies/go/v2"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
@@ -301,14 +303,14 @@ func NewGrpcServer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.Lnd != nil {
|
if config.Lnd != nil {
|
||||||
node.client, err = NewLndClient(config.Lnd)
|
node.client, err = lnd.NewLndClient(config.Lnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Cln != nil {
|
if config.Cln != nil {
|
||||||
node.client, err = NewClnClient(config.Cln.SocketPath)
|
node.client, err = cln.NewClnClient(config.Cln.SocketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user