Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lsps2 forwarding #125

Merged
merged 17 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/integration_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ jobs:
testLsps0GetProtocolVersions,
testLsps2GetVersions,
testLsps2GetInfo,
testLsps2Buy
testLsps2Buy,
testLsps2HappyFlow,
testLsps2Cltv,
testLsps2NoBalance,
testLsps2ZeroConfUtxo
]
implementation: [
CLN
Expand Down
10 changes: 5 additions & 5 deletions channel_opener_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"log"
"time"

"github.com/breez/lspd/basetypes"
"github.com/breez/lspd/btceclegacy"
"github.com/breez/lspd/interceptor"
"github.com/breez/lspd/lightning"
"github.com/breez/lspd/lsps0"
lspdrpc "github.com/breez/lspd/rpc"
"github.com/breez/lspd/shared"
ecies "github.com/ecies/go/v2"
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *channelOpenerServer) RegisterPayment(
pi.OpeningFeeParams = &lspdrpc.OpeningFeeParams{
MinMsat: uint64(node.NodeConfig.ChannelMinimumFeeMsat),
Proportional: uint32(node.NodeConfig.ChannelFeePermyriad * 100),
ValidUntil: time.Now().UTC().Add(time.Duration(time.Hour * 24)).Format(basetypes.TIME_FORMAT),
ValidUntil: time.Now().UTC().Add(time.Duration(time.Hour * 24)).Format(lsps0.TIME_FORMAT),
MaxIdleTime: uint32(node.NodeConfig.MaxInactiveDuration / 600),
MaxClientToSelfDelay: uint32(10000),
}
Expand All @@ -157,11 +157,11 @@ func (s *channelOpenerServer) RegisterPayment(
log.Printf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err)
return nil, fmt.Errorf("checkPayment(%v, %v) error: %v", pi.IncomingAmountMsat, pi.OutgoingAmountMsat, err)
}
params := &interceptor.OpeningFeeParams{
MinMsat: pi.OpeningFeeParams.MinMsat,
params := &shared.OpeningFeeParams{
MinFeeMsat: pi.OpeningFeeParams.MinMsat,
Proportional: pi.OpeningFeeParams.Proportional,
ValidUntil: pi.OpeningFeeParams.ValidUntil,
MaxIdleTime: pi.OpeningFeeParams.MaxIdleTime,
MinLifetime: pi.OpeningFeeParams.MaxIdleTime,
MaxClientToSelfDelay: pi.OpeningFeeParams.MaxClientToSelfDelay,
Promise: pi.OpeningFeeParams.Promise,
}
Expand Down
17 changes: 11 additions & 6 deletions cln/cln_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"strings"
"time"

"github.com/breez/lspd/basetypes"
"github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/elementsproject/glightning/glightning"
"github.com/elementsproject/glightning/jrpc2"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -127,6 +127,10 @@ func (c *ClnClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoi

if err != nil {
log.Printf("CLN: client.FundChannelExt(%v, %v) error: %v", pubkey, req.CapacitySat, err)
rpcError, ok := err.(*jrpc2.RpcError)
if ok && rpcError.Code == 301 {
return nil, fmt.Errorf("not enough funds: %w", err)
}
return nil, err
}

Expand All @@ -136,7 +140,7 @@ func (c *ClnClient) OpenChannel(req *lightning.OpenChannelRequest) (*wire.OutPoi
return nil, err
}

channelPoint, err := basetypes.NewOutPoint(fundingTxId[:], uint32(fundResult.FundingTxOutputNum))
channelPoint, err := lightning.NewOutPoint(fundingTxId[:], uint32(fundResult.FundingTxOutputNum))
if err != nil {
log.Printf("CLN: NewOutPoint(%s, %d) error: %v", fundingTxId.String(), fundResult.FundingTxOutputNum, err)
return nil, err
Expand All @@ -157,19 +161,20 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh
for _, c := range peer.Channels {
log.Printf("getChannel destination: %s, Short channel id: %v, local alias: %v , FundingTxID:%v, State:%v ", pubkey, c.ShortChannelId, c.Alias.Local, c.FundingTxId, c.State)
if slices.Contains(OPEN_STATUSES, c.State) && c.FundingTxId == fundingTxID {
confirmedChanID, err := basetypes.NewShortChannelIDFromString(c.ShortChannelId)
confirmedChanID, err := lightning.NewShortChannelIDFromString(c.ShortChannelId)
if err != nil {
fmt.Printf("NewShortChannelIDFromString %v error: %v", c.ShortChannelId, err)
return nil, err
}
initialChanID, err := basetypes.NewShortChannelIDFromString(c.Alias.Local)
initialChanID, err := lightning.NewShortChannelIDFromString(c.Alias.Local)
if err != nil {
fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err)
return nil, err
}
return &lightning.GetChannelResult{
InitialChannelID: *initialChanID,
ConfirmedChannelID: *confirmedChanID,
HtlcMinimumMsat: c.HtlcMinMilliSatoshi,
}, nil
}
}
Expand Down Expand Up @@ -212,7 +217,7 @@ func (c *ClnClient) GetClosedChannels(nodeID string, channelPoints map[string]ui
lookup := make(map[string]uint64)
for _, c := range peer.Channels {
if slices.Contains(CLOSING_STATUSES, c.State) {
cid, err := basetypes.NewShortChannelIDFromString(c.ShortChannelId)
cid, err := lightning.NewShortChannelIDFromString(c.ShortChannelId)
if err != nil {
log.Printf("CLN: GetClosedChannels NewShortChannelIDFromString(%v) error: %v", c.ShortChannelId, err)
continue
Expand All @@ -233,7 +238,7 @@ func (c *ClnClient) GetClosedChannels(nodeID string, channelPoints map[string]ui
return r, nil
}

func (c *ClnClient) GetPeerId(scid *basetypes.ShortChannelID) ([]byte, error) {
func (c *ClnClient) GetPeerId(scid *lightning.ShortChannelID) ([]byte, error) {
scidStr := scid.ToString()
peers, err := c.client.ListPeers()
if err != nil {
Expand Down
55 changes: 36 additions & 19 deletions cln/cln_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"sync"
"time"

"github.com/breez/lspd/basetypes"
"github.com/breez/lspd/cln_plugin/proto"
"github.com/breez/lspd/config"
"github.com/breez/lspd/interceptor"
"github.com/breez/lspd/lightning"
"github.com/breez/lspd/shared"
sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
Expand All @@ -26,7 +26,7 @@ import (
)

type ClnHtlcInterceptor struct {
interceptor *interceptor.Interceptor
interceptor shared.InterceptHandler
config *config.NodeConfig
pluginAddress string
client *ClnClient
Expand All @@ -38,7 +38,7 @@ type ClnHtlcInterceptor struct {
cancel context.CancelFunc
}

func NewClnHtlcInterceptor(conf *config.NodeConfig, client *ClnClient, interceptor *interceptor.Interceptor) (*ClnHtlcInterceptor, error) {
func NewClnHtlcInterceptor(conf *config.NodeConfig, client *ClnClient, interceptor shared.InterceptHandler) (*ClnHtlcInterceptor, error) {
i := &ClnHtlcInterceptor{
config: conf,
pluginAddress: conf.Cln.PluginAddress,
Expand Down Expand Up @@ -140,22 +140,32 @@ func (i *ClnHtlcInterceptor) intercept() error {
return
}

scid, err := basetypes.NewShortChannelIDFromString(request.Onion.ShortChannelId)
scid, err := lightning.NewShortChannelIDFromString(request.Onion.ShortChannelId)
if err != nil {
interceptorClient.Send(i.defaultResolution(request))
i.doneWg.Done()
return
}

interceptResult := i.interceptor.Intercept(scid, paymentHash, request.Onion.ForwardMsat, request.Onion.OutgoingCltvValue, request.Htlc.CltvExpiry)
interceptResult := i.interceptor.Intercept(shared.InterceptRequest{
Identifier: request.Onion.SharedSecret,
Scid: *scid,
PaymentHash: paymentHash,
IncomingAmountMsat: request.Htlc.AmountMsat,
OutgoingAmountMsat: request.Onion.ForwardMsat,
IncomingExpiry: request.Htlc.CltvExpiry,
OutgoingExpiry: request.Onion.OutgoingCltvValue,
})
switch interceptResult.Action {
case interceptor.INTERCEPT_RESUME_WITH_ONION:
case shared.INTERCEPT_RESUME_WITH_ONION:
interceptorClient.Send(i.resumeWithOnion(request, interceptResult))
case interceptor.INTERCEPT_FAIL_HTLC_WITH_CODE:
case shared.INTERCEPT_FAIL_HTLC_WITH_CODE:
interceptorClient.Send(
i.failWithCode(request, interceptResult.FailureCode),
)
case interceptor.INTERCEPT_RESUME:
case shared.INTERCEPT_IGNORE:
// Do nothing
case shared.INTERCEPT_RESUME:
fallthrough
default:
interceptorClient.Send(
Expand Down Expand Up @@ -187,17 +197,17 @@ func (i *ClnHtlcInterceptor) WaitStarted() {
i.initWg.Wait()
}

func (i *ClnHtlcInterceptor) resumeWithOnion(request *proto.HtlcAccepted, interceptResult interceptor.InterceptResult) *proto.HtlcResolution {
func (i *ClnHtlcInterceptor) resumeWithOnion(request *proto.HtlcAccepted, interceptResult shared.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, interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE)
return i.failWithCode(request, shared.FAILURE_TEMPORARY_CHANNEL_FAILURE)
}
newPayload, err := encodePayloadWithNextHop(payload, interceptResult.ChannelId, interceptResult.AmountMsat)
newPayload, err := encodePayloadWithNextHop(payload, interceptResult.Scid, interceptResult.AmountMsat, interceptResult.FeeMsat)
if err != nil {
log.Printf("encodePayloadWithNextHop error: %v", err)
return i.failWithCode(request, interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE)
return i.failWithCode(request, shared.FAILURE_TEMPORARY_CHANNEL_FAILURE)
}

newPayloadStr := hex.EncodeToString(newPayload)
Expand All @@ -224,7 +234,7 @@ func (i *ClnHtlcInterceptor) defaultResolution(request *proto.HtlcAccepted) *pro
}
}

func (i *ClnHtlcInterceptor) failWithCode(request *proto.HtlcAccepted, code interceptor.InterceptFailureCode) *proto.HtlcResolution {
func (i *ClnHtlcInterceptor) failWithCode(request *proto.HtlcAccepted, code shared.InterceptFailureCode) *proto.HtlcResolution {
return &proto.HtlcResolution{
Correlationid: request.Correlationid,
Outcome: &proto.HtlcResolution_Fail{
Expand All @@ -237,7 +247,7 @@ func (i *ClnHtlcInterceptor) failWithCode(request *proto.HtlcAccepted, code inte
}
}

func encodePayloadWithNextHop(payload []byte, channelId uint64, amountToForward uint64) ([]byte, error) {
func encodePayloadWithNextHop(payload []byte, scid lightning.ShortChannelID, amountToForward uint64, feeMsat *uint64) ([]byte, error) {
bufReader := bytes.NewBuffer(payload)
var b [8]byte
varInt, err := sphinx.ReadVarInt(bufReader, &b)
Expand All @@ -256,6 +266,7 @@ func encodePayloadWithNextHop(payload []byte, channelId uint64, amountToForward
return nil, fmt.Errorf("DecodeWithParsedTypes failed for %x: %v", innerPayload[:], err)
}

channelId := uint64(scid)
tt := record.NewNextHopIDRecord(&channelId)
ttbuf := bytes.NewBuffer([]byte{})
if err := tt.Encode(ttbuf); err != nil {
Expand Down Expand Up @@ -294,13 +305,19 @@ func encodePayloadWithNextHop(payload []byte, channelId uint64, amountToForward
return newPayloadBuf.Bytes(), nil
}

func (i *ClnHtlcInterceptor) mapFailureCode(original interceptor.InterceptFailureCode) string {
func (i *ClnHtlcInterceptor) mapFailureCode(original shared.InterceptFailureCode) string {
switch original {
case interceptor.FAILURE_TEMPORARY_CHANNEL_FAILURE:
case shared.FAILURE_TEMPORARY_CHANNEL_FAILURE:
return "1007"
case interceptor.FAILURE_TEMPORARY_NODE_FAILURE:
case shared.FAILURE_AMOUNT_BELOW_MINIMUM:
return "100B"
case shared.FAILURE_INCORRECT_CLTV_EXPIRY:
return "100D"
case shared.FAILURE_TEMPORARY_NODE_FAILURE:
return "2002"
case interceptor.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
case shared.FAILURE_UNKNOWN_NEXT_PEER:
return "400A"
case shared.FAILURE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
return "400F"
default:
log.Printf("Unknown failure code %v, default to temporary channel failure.", original)
Expand Down
Loading