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