mirror of
https://github.com/aljazceru/lspd.git
synced 2026-02-22 22:44:30 +01:00
make intercept method shareable with lsps2
This commit is contained in:
@@ -20,34 +20,6 @@ import (
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
|
||||
type InterceptAction int
|
||||
|
||||
const (
|
||||
INTERCEPT_RESUME InterceptAction = 0
|
||||
INTERCEPT_RESUME_WITH_ONION InterceptAction = 1
|
||||
INTERCEPT_FAIL_HTLC_WITH_CODE InterceptAction = 2
|
||||
)
|
||||
|
||||
type InterceptFailureCode uint16
|
||||
|
||||
var (
|
||||
FAILURE_TEMPORARY_CHANNEL_FAILURE InterceptFailureCode = 0x1007
|
||||
FAILURE_TEMPORARY_NODE_FAILURE InterceptFailureCode = 0x2002
|
||||
FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS InterceptFailureCode = 0x400F
|
||||
)
|
||||
|
||||
type InterceptResult struct {
|
||||
Action InterceptAction
|
||||
FailureCode InterceptFailureCode
|
||||
Destination []byte
|
||||
AmountMsat uint64
|
||||
TotalAmountMsat uint64
|
||||
ChannelPoint *wire.OutPoint
|
||||
ChannelId uint64
|
||||
PaymentSecret []byte
|
||||
UseLegacyOnionBlob bool
|
||||
}
|
||||
|
||||
type Interceptor struct {
|
||||
client lightning.Client
|
||||
config *config.NodeConfig
|
||||
@@ -59,7 +31,7 @@ type Interceptor struct {
|
||||
notificationService *notifications.NotificationService
|
||||
}
|
||||
|
||||
func NewInterceptor(
|
||||
func NewInterceptHandler(
|
||||
client lightning.Client,
|
||||
config *config.NodeConfig,
|
||||
store InterceptStore,
|
||||
@@ -79,16 +51,16 @@ func NewInterceptor(
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32, reqIncomingExpiry uint32) InterceptResult {
|
||||
reqPaymentHashStr := hex.EncodeToString(reqPaymentHash)
|
||||
log.Printf("Intercept: scid: %s, paymentHash: %s, outgoindAmount: %v, outgoingExpiry: %v, incomingExpiry: %v", scid.ToString(), reqPaymentHashStr, reqOutgoingAmountMsat, reqOutgoingExpiry, reqIncomingExpiry)
|
||||
func (i *Interceptor) Intercept(req shared.InterceptRequest) shared.InterceptResult {
|
||||
reqPaymentHashStr := hex.EncodeToString(req.PaymentHash)
|
||||
log.Printf("Intercept: scid: %s, paymentHash: %x, outgoindAmount: %v, outgoingExpiry: %v, incomingExpiry: %v", req.Scid.ToString(), reqPaymentHashStr, req.OutgoingAmountMsat, req.OutgoingExpiry, req.IncomingExpiry)
|
||||
resp, _, _ := i.payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) {
|
||||
token, params, paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, tag, err := i.store.PaymentInfo(reqPaymentHash)
|
||||
token, params, paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, tag, err := i.store.PaymentInfo(req.PaymentHash)
|
||||
if err != nil {
|
||||
log.Printf("paymentInfo(%x) error: %v", reqPaymentHash, err)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_NODE_FAILURE,
|
||||
log.Printf("paymentInfo(%x) error: %v", req.PaymentHash, err)
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_NODE_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -98,12 +70,12 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
log.Printf("ERROR: Payment was registered without destination. paymentHash: %s", reqPaymentHashStr)
|
||||
}
|
||||
|
||||
isProbe := isRegistered && !bytes.Equal(paymentHash, reqPaymentHash)
|
||||
nextHop, _ := i.client.GetPeerId(scid)
|
||||
isProbe := isRegistered && !bytes.Equal(paymentHash, req.PaymentHash)
|
||||
nextHop, _ := i.client.GetPeerId(&req.Scid)
|
||||
if err != nil {
|
||||
log.Printf("GetPeerId(%s) error: %v", scid.ToString(), err)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
log.Printf("GetPeerId(%s) error: %v", req.Scid.ToString(), err)
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -111,8 +83,8 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
// that means we are not the last hop of the payment, so we'll just forward.
|
||||
if isRegistered && nextHop != nil && !bytes.Equal(nextHop, destination) {
|
||||
log.Printf("paymentHash: %s, nextHop (%s) != destination (%s)", reqPaymentHashStr, hex.EncodeToString(nextHop), hex.EncodeToString(destination))
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -125,8 +97,8 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
// is not registered, there's nothing left to be done. Just continue.
|
||||
if !isRegistered {
|
||||
log.Printf("paymentHash: %s, nextHop == nil and not registered", reqPaymentHashStr)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -138,9 +110,9 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
isConnected, err := i.client.IsConnected(nextHop)
|
||||
if err != nil {
|
||||
log.Printf("IsConnected(%x) error: %v", nextHop, err)
|
||||
return &InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
return &shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -148,8 +120,8 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
// If this is a known probe, we'll quit early for non-connected clients.
|
||||
if !isConnected {
|
||||
log.Printf("paymentHash: %s, probe and not connected", reqPaymentHashStr)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -160,9 +132,9 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
// know that the actual payment would probably succeed.
|
||||
if channelPoint == nil {
|
||||
log.Printf("paymentHash: %s, probe and channelPoint == nil", reqPaymentHashStr)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
@@ -178,8 +150,8 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
|
||||
// The peer is online, we can resume the htlc if it's not a channel open.
|
||||
if !isRegistered {
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -198,20 +170,20 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
}
|
||||
|
||||
// Make sure the cltv delta is enough.
|
||||
if int64(reqIncomingExpiry)-int64(reqOutgoingExpiry) < int64(i.config.TimeLockDelta) {
|
||||
log.Printf("paymentHash: %s, outgoingExpiry: %v, incomingExpiry: %v, i.config.TimeLockDelta: %v", reqPaymentHashStr, reqOutgoingExpiry, reqIncomingExpiry, i.config.TimeLockDelta)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
if int64(req.IncomingExpiry)-int64(req.OutgoingExpiry) < int64(i.config.TimeLockDelta) {
|
||||
log.Printf("paymentHash: %s, outgoingExpiry: %v, incomingExpiry: %v, i.config.TimeLockDelta: %v", reqPaymentHashStr, req.OutgoingExpiry, req.IncomingExpiry, i.config.TimeLockDelta)
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
validUntil, err := time.Parse(lsps0.TIME_FORMAT, params.ValidUntil)
|
||||
if err != nil {
|
||||
log.Printf("time.Parse(%s, %s) failed. Failing channel open: %v", lsps0.TIME_FORMAT, params.ValidUntil, err)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -220,27 +192,27 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
if time.Now().UTC().After(validUntil) {
|
||||
if !i.isCurrentChainFeeCheaper(token, params) {
|
||||
log.Printf("Intercepted expired payment registration. Failing payment. payment hash: %x, valid until: %s", paymentHash, params.ValidUntil)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
log.Printf("Intercepted expired payment registration. Opening channel anyway, because it's cheaper at the current rate. paymenthash: %s, params: %+v", reqPaymentHashStr, params)
|
||||
}
|
||||
|
||||
channelPoint, err = i.openChannel(reqPaymentHash, destination, incomingAmountMsat, tag)
|
||||
channelPoint, err = i.openChannel(req.PaymentHash, destination, incomingAmountMsat, tag)
|
||||
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 shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
var bigProd, bigAmt big.Int
|
||||
amt := (bigAmt.Div(bigProd.Mul(big.NewInt(outgoingAmountMsat), big.NewInt(int64(reqOutgoingAmountMsat))), big.NewInt(incomingAmountMsat))).Int64()
|
||||
amt := (bigAmt.Div(bigProd.Mul(big.NewInt(outgoingAmountMsat), big.NewInt(int64(req.OutgoingAmountMsat))), big.NewInt(incomingAmountMsat))).Int64()
|
||||
|
||||
deadline := time.Now().Add(60 * time.Second)
|
||||
|
||||
@@ -259,23 +231,23 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
|
||||
if err != nil {
|
||||
log.Printf("insertChannel error: %v", err)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
}
|
||||
|
||||
channelID := uint64(chanResult.ConfirmedChannelID)
|
||||
if channelID == 0 {
|
||||
channelID = uint64(chanResult.InitialChannelID)
|
||||
channelID := chanResult.ConfirmedChannelID
|
||||
if uint64(channelID) == 0 {
|
||||
channelID = chanResult.InitialChannelID
|
||||
}
|
||||
|
||||
useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token)
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_RESUME_WITH_ONION,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME_WITH_ONION,
|
||||
Destination: destination,
|
||||
ChannelPoint: channelPoint,
|
||||
ChannelId: channelID,
|
||||
Scid: channelID,
|
||||
PaymentSecret: paymentSecret,
|
||||
AmountMsat: uint64(amt),
|
||||
TotalAmountMsat: uint64(outgoingAmountMsat),
|
||||
@@ -292,16 +264,16 @@ func (i *Interceptor) Intercept(scid *lightning.ShortChannelID, reqPaymentHash [
|
||||
}
|
||||
|
||||
log.Printf("Error: Channel failed to open... timed out. ")
|
||||
return InterceptResult{
|
||||
Action: INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
return shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_FAIL_HTLC_WITH_CODE,
|
||||
FailureCode: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
|
||||
}, nil
|
||||
})
|
||||
|
||||
return resp.(InterceptResult)
|
||||
return resp.(shared.InterceptResult)
|
||||
}
|
||||
|
||||
func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegistered bool) *InterceptResult {
|
||||
func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegistered bool) *shared.InterceptResult {
|
||||
// If not connected, send a notification to the registered
|
||||
// notification service for this client if available.
|
||||
notified, err := i.notificationService.Notify(
|
||||
@@ -313,8 +285,8 @@ func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegiste
|
||||
// is offline or unknown. We'll resume the HTLC (which will
|
||||
// result in UNKOWN_NEXT_PEER)
|
||||
if err != nil || !notified {
|
||||
return &InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return &shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,8 +309,8 @@ func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegiste
|
||||
nextHop,
|
||||
err,
|
||||
)
|
||||
return &InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return &shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -359,8 +331,8 @@ func (i *Interceptor) notify(reqPaymentHashStr string, nextHop []byte, isRegiste
|
||||
nextHop,
|
||||
err,
|
||||
)
|
||||
return &InterceptResult{
|
||||
Action: INTERCEPT_RESUME,
|
||||
return &shared.InterceptResult{
|
||||
Action: shared.INTERCEPT_RESUME,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user