rename initialchannelid to aliasScid and make optional

This commit is contained in:
Jesse de Wit
2023-12-29 11:26:56 +01:00
parent fb36dc8864
commit 81a24439d4
6 changed files with 81 additions and 40 deletions

View File

@@ -198,7 +198,7 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh
pubkey := hex.EncodeToString(peerID) pubkey := hex.EncodeToString(peerID)
channels, err := client.GetPeerChannels(pubkey) channels, err := client.GetPeerChannels(pubkey)
if err != nil { if err != nil {
log.Printf("CLN: client.GetPeer(%s) error: %v", pubkey, err) log.Printf("CLN: client.GetPeerChannels(%s) error: %v", pubkey, err)
return nil, err return nil, err
} }
@@ -206,20 +206,15 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh
for _, c := range channels { for _, c := range 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) 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 { if slices.Contains(OPEN_STATUSES, c.State) && c.FundingTxId == fundingTxID {
confirmedChanID, err := lightning.NewShortChannelIDFromString(c.ShortChannelId)
aliasScid, confirmedScid, err := mapScidsFromChannel(c)
if err != nil { if err != nil {
fmt.Printf("NewShortChannelIDFromString %v error: %v", c.ShortChannelId, err)
return nil, err
}
initialChanID, err := lightning.NewShortChannelIDFromString(c.Alias.Local)
if err != nil {
fmt.Printf("NewShortChannelIDFromString %v error: %v", c.Alias.Local, err)
return nil, err return nil, err
} }
return &lightning.GetChannelResult{ return &lightning.GetChannelResult{
InitialChannelID: *initialChanID, AliasScid: aliasScid,
ConfirmedChannelID: *confirmedChanID, ConfirmedScid: confirmedScid,
HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(), HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(),
}, nil }, nil
} }
} }
@@ -324,3 +319,24 @@ func (c *ClnClient) WaitOnline(peerID []byte, deadline time.Time) error {
func (c *ClnClient) WaitChannelActive(peerID []byte, deadline time.Time) error { func (c *ClnClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
return nil return nil
} }
func mapScidsFromChannel(c *glightning.PeerChannel) (*lightning.ShortChannelID, *lightning.ShortChannelID, error) {
var confirmedScid *lightning.ShortChannelID
var aliasScid *lightning.ShortChannelID
var err error
if c.ShortChannelId != "" {
confirmedScid, err = lightning.NewShortChannelIDFromString(c.ShortChannelId)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse scid '%s': %w", c.ShortChannelId, err)
}
}
if c.Alias != nil && c.Alias.Local != "" {
aliasScid, err = lightning.NewShortChannelIDFromString(c.Alias.Local)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse scid '%s': %w", c.Alias.Local, err)
}
}
return aliasScid, confirmedScid, nil
}

View File

@@ -224,11 +224,18 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes
for { for {
chanResult, _ := i.client.GetChannel(destination, *channelPoint) chanResult, _ := i.client.GetChannel(destination, *channelPoint)
if chanResult != nil { if chanResult != nil {
log.Printf("paymentHash: %s, channel opened successfully alias: %v, confirmed: %v", reqPaymentHashStr, chanResult.InitialChannelID.ToString(), chanResult.ConfirmedChannelID.ToString()) log.Printf("paymentHash: %s, channel opened successfully alias: '%v', confirmed: '%v'", reqPaymentHashStr, chanResult.AliasScid.ToString(), chanResult.ConfirmedScid.ToString())
channelID := chanResult.ConfirmedChannelID var scid *lightning.ShortChannelID
if uint64(channelID) == 0 { if chanResult.ConfirmedScid == nil {
channelID = chanResult.InitialChannelID if chanResult.AliasScid == nil {
log.Printf("Error: GetChannel: Both confirmed scid and alias scid are nil: %+v", chanResult)
<-time.After(1 * time.Second)
continue
}
scid = chanResult.AliasScid
} else {
scid = chanResult.ConfirmedScid
} }
useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token) useLegacyOnionBlob := slices.Contains(i.config.LegacyOnionTokens, token)
@@ -236,7 +243,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes
Action: common.INTERCEPT_RESUME_WITH_ONION, Action: common.INTERCEPT_RESUME_WITH_ONION,
Destination: destination, Destination: destination,
ChannelPoint: channelPoint, ChannelPoint: channelPoint,
Scid: channelID, Scid: *scid,
PaymentSecret: paymentSecret, PaymentSecret: paymentSecret,
AmountMsat: uint64(amt), AmountMsat: uint64(amt),
TotalAmountMsat: uint64(outgoingAmountMsat), TotalAmountMsat: uint64(outgoingAmountMsat),
@@ -244,7 +251,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes
}, nil }, nil
} }
log.Printf("paymentHash: %s, waiting for channel to get opened.... %v", reqPaymentHashStr, destination) log.Printf("paymentHash: %s, waiting for channel to get opened... %x", reqPaymentHashStr, destination)
if time.Now().After(deadline) { if time.Now().After(deadline) {
log.Printf("paymentHash: %s, Stop retrying getChannel(%v, %v)", reqPaymentHashStr, destination, channelPoint.String()) log.Printf("paymentHash: %s, Stop retrying getChannel(%v, %v)", reqPaymentHashStr, destination, channelPoint.String())
break break

View File

@@ -12,9 +12,9 @@ type GetInfoResult struct {
} }
type GetChannelResult struct { type GetChannelResult struct {
InitialChannelID ShortChannelID AliasScid *ShortChannelID
ConfirmedChannelID ShortChannelID ConfirmedScid *ShortChannelID
HtlcMinimumMsat uint64 HtlcMinimumMsat uint64
} }
type OpenChannelRequest struct { type OpenChannelRequest struct {

View File

@@ -13,7 +13,6 @@ import (
"github.com/breez/lspd/lightning" "github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"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"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
@@ -298,17 +297,11 @@ func (c *LndClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh
for _, c := range r.Channels { for _, c := range r.Channels {
log.Printf("getChannel(%x): %v", peerID, c.ChanId) log.Printf("getChannel(%x): %v", peerID, c.ChanId)
if c.ChannelPoint == channelPointStr && c.Active { if c.ChannelPoint == channelPointStr && c.Active {
confirmedChanId := c.ChanId aliasScid, confirmedScid := mapScidsFromChannel(c)
if c.ZeroConf {
confirmedChanId = c.ZeroConfConfirmedScid
if confirmedChanId == hop.Source.ToUint64() {
confirmedChanId = 0
}
}
return &lightning.GetChannelResult{ return &lightning.GetChannelResult{
InitialChannelID: lightning.ShortChannelID(c.ChanId), AliasScid: aliasScid,
ConfirmedChannelID: lightning.ShortChannelID(confirmedChanId), ConfirmedScid: confirmedScid,
HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat, HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat,
}, nil }, nil
} }
} }
@@ -498,3 +491,21 @@ func (c *LndClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
return fmt.Errorf("deadline exceeded") return fmt.Errorf("deadline exceeded")
} }
} }
func mapScidsFromChannel(c *lnrpc.Channel) (*lightning.ShortChannelID, *lightning.ShortChannelID) {
var alias *lightning.ShortChannelID
var confirmedScid *lightning.ShortChannelID
if c.ZeroConf {
if c.ZeroConfConfirmedScid != 0 {
confirmedScid = (*lightning.ShortChannelID)(&c.ZeroConfConfirmedScid)
}
alias = (*lightning.ShortChannelID)(&c.ChanId)
} else {
confirmedScid = (*lightning.ShortChannelID)(&c.ChanId)
if len(c.AliasScids) > 0 {
alias = (*lightning.ShortChannelID)(&c.AliasScids[0])
}
}
return alias, confirmedScid
}

View File

@@ -516,18 +516,25 @@ func (i *Interceptor) ensureChannelOpen(payment *paymentState) {
log.Printf( log.Printf(
"Got new channel for forward successfully. scid alias: %v, "+ "Got new channel for forward successfully. scid alias: %v, "+
"confirmed scid: %v", "confirmed scid: %v",
chanResult.InitialChannelID.ToString(), chanResult.AliasScid.ToString(),
chanResult.ConfirmedChannelID.ToString(), chanResult.ConfirmedScid.ToString(),
) )
scid := chanResult.ConfirmedChannelID var scid *lightning.ShortChannelID
if uint64(scid) == 0 { if chanResult.ConfirmedScid == nil {
scid = chanResult.InitialChannelID if chanResult.AliasScid == nil {
log.Printf("Error: GetChannel: Both confirmed scid and alias scid are nil: %+v", chanResult)
<-time.After(1 * time.Second)
continue
}
scid = chanResult.AliasScid
} else {
scid = chanResult.ConfirmedScid
} }
i.paymentChanOpened <- &paymentChanOpenedEvent{ i.paymentChanOpened <- &paymentChanOpenedEvent{
paymentId: payment.id, paymentId: payment.id,
scid: scid, scid: *scid,
channelPoint: payment.registration.ChannelPoint, channelPoint: payment.registration.ChannelPoint,
htlcMinimumMsat: chanResult.HtlcMinimumMsat, htlcMinimumMsat: chanResult.HtlcMinimumMsat,
} }

View File

@@ -31,9 +31,9 @@ var defaultChainHash = chainhash.Hash([32]byte{})
var defaultOutPoint = wire.NewOutPoint(&defaultChainHash, 0) var defaultOutPoint = wire.NewOutPoint(&defaultChainHash, 0)
var defaultChannelScid uint64 = 456 var defaultChannelScid uint64 = 456
var defaultChanResult = &lightning.GetChannelResult{ var defaultChanResult = &lightning.GetChannelResult{
HtlcMinimumMsat: defaultConfig().HtlcMinimumMsat, HtlcMinimumMsat: defaultConfig().HtlcMinimumMsat,
InitialChannelID: lightning.ShortChannelID(defaultChannelScid), AliasScid: (*lightning.ShortChannelID)(&defaultChannelScid),
ConfirmedChannelID: lightning.ShortChannelID(defaultChannelScid), ConfirmedScid: (*lightning.ShortChannelID)(&defaultChannelScid),
} }
func defaultOpeningFeeParams() common.OpeningFeeParams { func defaultOpeningFeeParams() common.OpeningFeeParams {