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)
channels, err := client.GetPeerChannels(pubkey)
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
}
@@ -206,20 +206,15 @@ func (c *ClnClient) GetChannel(peerID []byte, channelPoint wire.OutPoint) (*ligh
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)
if slices.Contains(OPEN_STATUSES, c.State) && c.FundingTxId == fundingTxID {
confirmedChanID, err := lightning.NewShortChannelIDFromString(c.ShortChannelId)
aliasScid, confirmedScid, err := mapScidsFromChannel(c)
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 &lightning.GetChannelResult{
InitialChannelID: *initialChanID,
ConfirmedChannelID: *confirmedChanID,
HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(),
AliasScid: aliasScid,
ConfirmedScid: confirmedScid,
HtlcMinimumMsat: c.MinimumHtlcOutMsat.MSat(),
}, 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 {
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 {
chanResult, _ := i.client.GetChannel(destination, *channelPoint)
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
if uint64(channelID) == 0 {
channelID = chanResult.InitialChannelID
var scid *lightning.ShortChannelID
if chanResult.ConfirmedScid == nil {
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)
@@ -236,7 +243,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes
Action: common.INTERCEPT_RESUME_WITH_ONION,
Destination: destination,
ChannelPoint: channelPoint,
Scid: channelID,
Scid: *scid,
PaymentSecret: paymentSecret,
AmountMsat: uint64(amt),
TotalAmountMsat: uint64(outgoingAmountMsat),
@@ -244,7 +251,7 @@ func (i *Interceptor) Intercept(req common.InterceptRequest) common.InterceptRes
}, 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) {
log.Printf("paymentHash: %s, Stop retrying getChannel(%v, %v)", reqPaymentHashStr, destination, channelPoint.String())
break

View File

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

View File

@@ -13,7 +13,6 @@ import (
"github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"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 {
log.Printf("getChannel(%x): %v", peerID, c.ChanId)
if c.ChannelPoint == channelPointStr && c.Active {
confirmedChanId := c.ChanId
if c.ZeroConf {
confirmedChanId = c.ZeroConfConfirmedScid
if confirmedChanId == hop.Source.ToUint64() {
confirmedChanId = 0
}
}
aliasScid, confirmedScid := mapScidsFromChannel(c)
return &lightning.GetChannelResult{
InitialChannelID: lightning.ShortChannelID(c.ChanId),
ConfirmedChannelID: lightning.ShortChannelID(confirmedChanId),
HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat,
AliasScid: aliasScid,
ConfirmedScid: confirmedScid,
HtlcMinimumMsat: c.LocalConstraints.MinHtlcMsat,
}, nil
}
}
@@ -498,3 +491,21 @@ func (c *LndClient) WaitChannelActive(peerID []byte, deadline time.Time) error {
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(
"Got new channel for forward successfully. scid alias: %v, "+
"confirmed scid: %v",
chanResult.InitialChannelID.ToString(),
chanResult.ConfirmedChannelID.ToString(),
chanResult.AliasScid.ToString(),
chanResult.ConfirmedScid.ToString(),
)
scid := chanResult.ConfirmedChannelID
if uint64(scid) == 0 {
scid = chanResult.InitialChannelID
var scid *lightning.ShortChannelID
if chanResult.ConfirmedScid == nil {
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{
paymentId: payment.id,
scid: scid,
scid: *scid,
channelPoint: payment.registration.ChannelPoint,
htlcMinimumMsat: chanResult.HtlcMinimumMsat,
}

View File

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