lock on payment hash

This commit is contained in:
Jesse de Wit
2022-11-21 14:24:29 +01:00
parent daa70c5f0e
commit ceb3ddb1ee

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/hex"
"fmt" "fmt"
"log" "log"
"math/big" "math/big"
@@ -12,6 +13,7 @@ import (
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record" "github.com/lightningnetwork/lnd/record"
"github.com/lightningnetwork/lnd/routing/route" "github.com/lightningnetwork/lnd/routing/route"
"golang.org/x/sync/singleflight"
) )
type interceptAction int type interceptAction int
@@ -30,6 +32,8 @@ var (
FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS interceptFailureCode = 0x4015 FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS interceptFailureCode = 0x4015
) )
var payHashGroup singleflight.Group
type interceptResult struct { type interceptResult struct {
action interceptAction action interceptAction
failureCode interceptFailureCode failureCode interceptFailureCode
@@ -40,12 +44,14 @@ type interceptResult struct {
} }
func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32) interceptResult { func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingExpiry uint32) interceptResult {
reqPaymentHashStr := hex.EncodeToString(reqPaymentHash)
resp, _, _ := payHashGroup.Do(reqPaymentHashStr, func() (interface{}, error) {
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := paymentInfo(reqPaymentHash) paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat, channelPoint, err := 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, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v\n\n", log.Printf("paymentHash:%x\npaymentSecret:%x\ndestination:%x\nincomingAmountMsat:%v\noutgoingAmountMsat:%v\n\n",
paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat) paymentHash, paymentSecret, destination, incomingAmountMsat, outgoingAmountMsat)
@@ -58,7 +64,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
if err != nil { if err != nil {
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
} else { //probing } else { //probing
failureCode := FAILURE_TEMPORARY_CHANNEL_FAILURE failureCode := FAILURE_TEMPORARY_CHANNEL_FAILURE
@@ -70,7 +76,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC_WITH_CODE, action: INTERCEPT_FAIL_HTLC_WITH_CODE,
failureCode: failureCode, failureCode: failureCode,
} }, nil
} }
} }
@@ -79,7 +85,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("btcec.ParsePubKey(%x): %v", destination, err) log.Printf("btcec.ParsePubKey(%x): %v", destination, err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
sessionKey, err := btcec.NewPrivateKey() sessionKey, err := btcec.NewPrivateKey()
@@ -87,7 +93,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("btcec.NewPrivateKey(): %v", err) log.Printf("btcec.NewPrivateKey(): %v", err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
var bigProd, bigAmt big.Int var bigProd, bigAmt big.Int
@@ -108,7 +114,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("hop.PackHopPayload(): %v", err) log.Printf("hop.PackHopPayload(): %v", err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
payload, err := sphinx.NewHopPayload(nil, b.Bytes()) payload, err := sphinx.NewHopPayload(nil, b.Bytes())
@@ -116,7 +122,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("sphinx.NewHopPayload(): %v", err) log.Printf("sphinx.NewHopPayload(): %v", err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
var sphinxPath sphinx.PaymentPath var sphinxPath sphinx.PaymentPath
@@ -132,7 +138,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("sphinx.NewOnionPacket(): %v", err) log.Printf("sphinx.NewOnionPacket(): %v", err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
var onionBlob bytes.Buffer var onionBlob bytes.Buffer
err = sphinxPacket.Encode(&onionBlob) err = sphinxPacket.Encode(&onionBlob)
@@ -140,7 +146,7 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
log.Printf("sphinxPacket.Encode(): %v", err) log.Printf("sphinxPacket.Encode(): %v", err)
return interceptResult{ return interceptResult{
action: INTERCEPT_FAIL_HTLC, action: INTERCEPT_FAIL_HTLC,
} }, nil
} }
return interceptResult{ return interceptResult{
@@ -149,13 +155,17 @@ func intercept(reqPaymentHash []byte, reqOutgoingAmountMsat uint64, reqOutgoingE
channelPoint: channelPoint, channelPoint: channelPoint,
amountMsat: uint64(amt), amountMsat: uint64(amt),
onionBlob: onionBlob.Bytes(), onionBlob: onionBlob.Bytes(),
} }, nil
} else { } else {
return interceptResult{ return interceptResult{
action: INTERCEPT_RESUME, action: INTERCEPT_RESUME,
}, nil
} }
})
return resp.(interceptResult)
} }
}
func checkPayment(incomingAmountMsat, outgoingAmountMsat int64) error { func checkPayment(incomingAmountMsat, outgoingAmountMsat int64) error {
fees := incomingAmountMsat * channelFeePermyriad / 10_000 / 1_000 * 1_000 fees := incomingAmountMsat * channelFeePermyriad / 10_000 / 1_000 * 1_000
if fees < channelMinimumFeeMsat { if fees < channelMinimumFeeMsat {