mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 22:46:40 +01:00
Merge pull request #8220 from yyforyongyu/enhance-logging-switch
multi: fix loopvar and enhance logging around channel reestablishment
This commit is contained in:
commit
27319315bb
6 changed files with 144 additions and 3 deletions
|
@ -849,6 +849,30 @@ type OpenChannel struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns a string representation of the channel.
|
||||||
|
func (c *OpenChannel) String() string {
|
||||||
|
indexStr := "height=%v, local_htlc_index=%v, local_log_index=%v, " +
|
||||||
|
"remote_htlc_index=%v, remote_log_index=%v"
|
||||||
|
|
||||||
|
commit := c.LocalCommitment
|
||||||
|
local := fmt.Sprintf(indexStr, commit.CommitHeight,
|
||||||
|
commit.LocalHtlcIndex, commit.LocalLogIndex,
|
||||||
|
commit.RemoteHtlcIndex, commit.RemoteLogIndex,
|
||||||
|
)
|
||||||
|
|
||||||
|
commit = c.RemoteCommitment
|
||||||
|
remote := fmt.Sprintf(indexStr, commit.CommitHeight,
|
||||||
|
commit.LocalHtlcIndex, commit.LocalLogIndex,
|
||||||
|
commit.RemoteHtlcIndex, commit.RemoteLogIndex,
|
||||||
|
)
|
||||||
|
|
||||||
|
return fmt.Sprintf("SCID=%v, status=%v, initiator=%v, pending=%v, "+
|
||||||
|
"local commitment has %s, remote commitment has %s",
|
||||||
|
c.ShortChannelID, c.chanStatus, c.IsInitiator, c.IsPending,
|
||||||
|
local, remote,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// ShortChanID returns the current ShortChannelID of this channel.
|
// ShortChanID returns the current ShortChannelID of this channel.
|
||||||
func (c *OpenChannel) ShortChanID() lnwire.ShortChannelID {
|
func (c *OpenChannel) ShortChanID() lnwire.ShortChannelID {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
|
@ -2100,6 +2124,10 @@ func (c *OpenChannel) ActiveHtlcs() []HTLC {
|
||||||
// which ones are present on their commitment.
|
// which ones are present on their commitment.
|
||||||
remoteHtlcs := make(map[[32]byte]struct{})
|
remoteHtlcs := make(map[[32]byte]struct{})
|
||||||
for _, htlc := range c.RemoteCommitment.Htlcs {
|
for _, htlc := range c.RemoteCommitment.Htlcs {
|
||||||
|
log.Tracef("RemoteCommitment has htlc: id=%v, update=%v "+
|
||||||
|
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex,
|
||||||
|
htlc.Incoming)
|
||||||
|
|
||||||
onionHash := sha256.Sum256(htlc.OnionBlob[:])
|
onionHash := sha256.Sum256(htlc.OnionBlob[:])
|
||||||
remoteHtlcs[onionHash] = struct{}{}
|
remoteHtlcs[onionHash] = struct{}{}
|
||||||
}
|
}
|
||||||
|
@ -2108,8 +2136,16 @@ func (c *OpenChannel) ActiveHtlcs() []HTLC {
|
||||||
// as active if *we* know them as well.
|
// as active if *we* know them as well.
|
||||||
activeHtlcs := make([]HTLC, 0, len(remoteHtlcs))
|
activeHtlcs := make([]HTLC, 0, len(remoteHtlcs))
|
||||||
for _, htlc := range c.LocalCommitment.Htlcs {
|
for _, htlc := range c.LocalCommitment.Htlcs {
|
||||||
|
log.Tracef("LocalCommitment has htlc: id=%v, update=%v "+
|
||||||
|
"incoming=%v", htlc.HtlcIndex, htlc.LogIndex,
|
||||||
|
htlc.Incoming)
|
||||||
|
|
||||||
onionHash := sha256.Sum256(htlc.OnionBlob[:])
|
onionHash := sha256.Sum256(htlc.OnionBlob[:])
|
||||||
if _, ok := remoteHtlcs[onionHash]; !ok {
|
if _, ok := remoteHtlcs[onionHash]; !ok {
|
||||||
|
log.Tracef("Skipped htlc due to onion mismatched: "+
|
||||||
|
"id=%v, update=%v incoming=%v",
|
||||||
|
htlc.HtlcIndex, htlc.LogIndex, htlc.Incoming)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,9 @@
|
||||||
`musig2Sessions` with a `SyncMap` used in `input` package to avoid concurrent
|
`musig2Sessions` with a `SyncMap` used in `input` package to avoid concurrent
|
||||||
write to this map.
|
write to this map.
|
||||||
|
|
||||||
|
* [Fixed](https://github.com/lightningnetwork/lnd/pull/8220) a loop variable
|
||||||
|
issue which may affect programs built using go `v1.20` and below.
|
||||||
|
|
||||||
# New Features
|
# New Features
|
||||||
## Functional Enhancements
|
## Functional Enhancements
|
||||||
## RPC Additions
|
## RPC Additions
|
||||||
|
|
|
@ -649,12 +649,13 @@ func (l *channelLink) createFailureWithUpdate(incoming bool,
|
||||||
// flow. We'll compare out commitment chains with the remote party, and re-send
|
// flow. We'll compare out commitment chains with the remote party, and re-send
|
||||||
// either a danging commit signature, a revocation, or both.
|
// either a danging commit signature, a revocation, or both.
|
||||||
func (l *channelLink) syncChanStates() error {
|
func (l *channelLink) syncChanStates() error {
|
||||||
l.log.Info("attempting to re-synchronize")
|
chanState := l.channel.State()
|
||||||
|
|
||||||
|
l.log.Infof("Attempting to re-synchronize channel: %v", chanState)
|
||||||
|
|
||||||
// First, we'll generate our ChanSync message to send to the other
|
// First, we'll generate our ChanSync message to send to the other
|
||||||
// side. Based on this message, the remote party will decide if they
|
// side. Based on this message, the remote party will decide if they
|
||||||
// need to retransmit any data or not.
|
// need to retransmit any data or not.
|
||||||
chanState := l.channel.State()
|
|
||||||
localChanSyncMsg, err := chanState.ChanSyncMsg()
|
localChanSyncMsg, err := chanState.ChanSyncMsg()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to generate chan sync message for "+
|
return fmt.Errorf("unable to generate chan sync message for "+
|
||||||
|
|
|
@ -904,6 +904,8 @@ func (lc *LightningChannel) extractPayDescs(commitHeight uint64,
|
||||||
// persist state w.r.t to if forwarded or not, or can
|
// persist state w.r.t to if forwarded or not, or can
|
||||||
// inadvertently trigger replays
|
// inadvertently trigger replays
|
||||||
|
|
||||||
|
htlc := htlc
|
||||||
|
|
||||||
payDesc, err := lc.diskHtlcToPayDesc(
|
payDesc, err := lc.diskHtlcToPayDesc(
|
||||||
feeRate, commitHeight, &htlc,
|
feeRate, commitHeight, &htlc,
|
||||||
localCommitKeys, remoteCommitKeys,
|
localCommitKeys, remoteCommitKeys,
|
||||||
|
|
|
@ -10111,3 +10111,101 @@ func testNewBreachRetribution(t *testing.T, chanType channeldb.ChannelType) {
|
||||||
)
|
)
|
||||||
require.ErrorIs(t, err, channeldb.ErrLogEntryNotFound)
|
require.ErrorIs(t, err, channeldb.ErrLogEntryNotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestExtractPayDescs asserts that `extractPayDescs` can correctly turn a
|
||||||
|
// slice of htlcs into two slices of PaymentDescriptors.
|
||||||
|
func TestExtractPayDescs(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
// Create a testing LightningChannel.
|
||||||
|
lnChan, _, err := CreateTestChannels(
|
||||||
|
t, channeldb.SingleFunderTweaklessBit,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Create two incoming HTLCs.
|
||||||
|
incomings := []channeldb.HTLC{
|
||||||
|
createRandomHTLC(t, true),
|
||||||
|
createRandomHTLC(t, true),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create two outgoing HTLCs.
|
||||||
|
outgoings := []channeldb.HTLC{
|
||||||
|
createRandomHTLC(t, false),
|
||||||
|
createRandomHTLC(t, false),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Concatenate incomings and outgoings into a single slice.
|
||||||
|
htlcs := []channeldb.HTLC{}
|
||||||
|
htlcs = append(htlcs, incomings...)
|
||||||
|
htlcs = append(htlcs, outgoings...)
|
||||||
|
|
||||||
|
// Run the method under test.
|
||||||
|
//
|
||||||
|
// NOTE: we use nil commitment key rings to avoid checking the htlc
|
||||||
|
// scripts(`genHtlcScript`) as it should be tested independently.
|
||||||
|
incomingPDs, outgoingPDs, err := lnChan.extractPayDescs(
|
||||||
|
0, 0, htlcs, nil, nil, true,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Assert the incoming PaymentDescriptors are matched.
|
||||||
|
for i, pd := range incomingPDs {
|
||||||
|
htlc := incomings[i]
|
||||||
|
assertPayDescMatchHTLC(t, pd, htlc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert the outgoing PaymentDescriptors are matched.
|
||||||
|
for i, pd := range outgoingPDs {
|
||||||
|
htlc := outgoings[i]
|
||||||
|
assertPayDescMatchHTLC(t, pd, htlc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// assertPayDescMatchHTLC compares a PaymentDescriptor to a channeldb.HTLC and
|
||||||
|
// asserts that the fields are matched.
|
||||||
|
func assertPayDescMatchHTLC(t *testing.T, pd PaymentDescriptor,
|
||||||
|
htlc channeldb.HTLC) {
|
||||||
|
|
||||||
|
require := require.New(t)
|
||||||
|
|
||||||
|
require.EqualValues(htlc.RHash, pd.RHash, "RHash")
|
||||||
|
require.Equal(htlc.RefundTimeout, pd.Timeout, "Timeout")
|
||||||
|
require.Equal(htlc.Amt, pd.Amount, "Amount")
|
||||||
|
require.Equal(htlc.HtlcIndex, pd.HtlcIndex, "HtlcIndex")
|
||||||
|
require.Equal(htlc.LogIndex, pd.LogIndex, "LogIndex")
|
||||||
|
require.EqualValues(htlc.OnionBlob[:], pd.OnionBlob, "OnionBlob")
|
||||||
|
}
|
||||||
|
|
||||||
|
// createRandomHTLC creates an HTLC that has random value in every field except
|
||||||
|
// the `Incoming`.
|
||||||
|
func createRandomHTLC(t *testing.T, incoming bool) channeldb.HTLC {
|
||||||
|
var onionBlob [lnwire.OnionPacketSize]byte
|
||||||
|
_, err := rand.Read(onionBlob[:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var rHash [lntypes.HashSize]byte
|
||||||
|
_, err = rand.Read(rHash[:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
sig := make([]byte, 64)
|
||||||
|
_, err = rand.Read(sig)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
extra := make([]byte, 1000)
|
||||||
|
_, err = rand.Read(extra)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
return channeldb.HTLC{
|
||||||
|
Signature: sig,
|
||||||
|
RHash: rHash,
|
||||||
|
Amt: lnwire.MilliSatoshi(rand.Uint64()),
|
||||||
|
RefundTimeout: rand.Uint32(),
|
||||||
|
OutputIndex: rand.Int31n(1000),
|
||||||
|
Incoming: incoming,
|
||||||
|
OnionBlob: onionBlob,
|
||||||
|
HtlcIndex: rand.Uint64(),
|
||||||
|
LogIndex: rand.Uint64(),
|
||||||
|
ExtraData: extra,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2009,7 +2009,8 @@ func messageSummary(msg lnwire.Message) string {
|
||||||
msg.ChanID, int64(msg.FeePerKw))
|
msg.ChanID, int64(msg.FeePerKw))
|
||||||
|
|
||||||
case *lnwire.ChannelReestablish:
|
case *lnwire.ChannelReestablish:
|
||||||
return fmt.Sprintf("next_local_height=%v, remote_tail_height=%v",
|
return fmt.Sprintf("chan_id=%v, next_local_height=%v, "+
|
||||||
|
"remote_tail_height=%v", msg.ChanID,
|
||||||
msg.NextLocalCommitHeight, msg.RemoteCommitTailHeight)
|
msg.NextLocalCommitHeight, msg.RemoteCommitTailHeight)
|
||||||
|
|
||||||
case *lnwire.ReplyShortChanIDsEnd:
|
case *lnwire.ReplyShortChanIDsEnd:
|
||||||
|
|
Loading…
Add table
Reference in a new issue