mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-22 22:25:24 +01:00
htlcswitch: in event of duplicate link add, prefer newer link
This commit is contained in:
parent
e1d8c37708
commit
e60d2b774a
3 changed files with 5 additions and 64 deletions
|
@ -1723,11 +1723,11 @@ func (s *Switch) AddLink(link ChannelLink) error {
|
||||||
|
|
||||||
chanID := link.ChanID()
|
chanID := link.ChanID()
|
||||||
|
|
||||||
// First, ensure that this link is not already active in the switch.
|
// If a link already exists, then remove the prior one so we can
|
||||||
|
// replace it with this fresh instance.
|
||||||
_, err := s.getLink(chanID)
|
_, err := s.getLink(chanID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return fmt.Errorf("unable to add ChannelLink(%v), already "+
|
s.removeLink(chanID)
|
||||||
"active", chanID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get and attach the mailbox for this link, which buffers packets in
|
// Get and attach the mailbox for this link, which buffers packets in
|
||||||
|
|
|
@ -25,65 +25,6 @@ func genPreimage() ([32]byte, error) {
|
||||||
return preimage, nil
|
return preimage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestSwitchAddDuplicateLink tests that the switch will reject duplicate links
|
|
||||||
// for both pending and live links. It also tests that we can successfully
|
|
||||||
// add a link after having removed it.
|
|
||||||
func TestSwitchAddDuplicateLink(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
alicePeer, err := newMockServer(t, "alice", nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create alice server: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := initSwitchWithDB(nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to init switch: %v", err)
|
|
||||||
}
|
|
||||||
if err := s.Start(); err != nil {
|
|
||||||
t.Fatalf("unable to start switch: %v", err)
|
|
||||||
}
|
|
||||||
defer s.Stop()
|
|
||||||
|
|
||||||
chanID1, _, aliceChanID, _ := genIDs()
|
|
||||||
|
|
||||||
pendingChanID := lnwire.ShortChannelID{}
|
|
||||||
|
|
||||||
aliceChannelLink := newMockChannelLink(
|
|
||||||
s, chanID1, pendingChanID, alicePeer, false,
|
|
||||||
)
|
|
||||||
if err := s.AddLink(aliceChannelLink); err != nil {
|
|
||||||
t.Fatalf("unable to add alice link: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alice should have a pending link, adding again should fail.
|
|
||||||
if err := s.AddLink(aliceChannelLink); err == nil {
|
|
||||||
t.Fatalf("adding duplicate link should have failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the short chan id of the channel, so that the link goes live.
|
|
||||||
aliceChannelLink.setLiveShortChanID(aliceChanID)
|
|
||||||
err = s.UpdateShortChanID(chanID1)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to update alice short_chan_id: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alice should have a live link, adding again should fail.
|
|
||||||
if err := s.AddLink(aliceChannelLink); err == nil {
|
|
||||||
t.Fatalf("adding duplicate link should have failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the live link to ensure the indexes are cleared.
|
|
||||||
if err := s.RemoveLink(chanID1); err != nil {
|
|
||||||
t.Fatalf("unable to remove alice link: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alice has no links, adding should succeed.
|
|
||||||
if err := s.AddLink(aliceChannelLink); err != nil {
|
|
||||||
t.Fatalf("unable to add alice link: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestSwitchSendPending checks the inability of htlc switch to forward adds
|
// TestSwitchSendPending checks the inability of htlc switch to forward adds
|
||||||
// over pending links, and the UpdateShortChanID makes a pending link live.
|
// over pending links, and the UpdateShortChanID makes a pending link live.
|
||||||
func TestSwitchSendPending(t *testing.T) {
|
func TestSwitchSendPending(t *testing.T) {
|
||||||
|
|
4
peer.go
4
peer.go
|
@ -1529,8 +1529,8 @@ out:
|
||||||
chainEvents, currentHeight, false)
|
chainEvents, currentHeight, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
peerLog.Errorf("can't register new channel "+
|
peerLog.Errorf("can't register new channel "+
|
||||||
"link(%v) with NodeKey(%x)", chanPoint,
|
"link(%v) with NodeKey(%x): %v", chanPoint,
|
||||||
p.PubKey())
|
p.PubKey(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(newChanReq.done)
|
close(newChanReq.done)
|
||||||
|
|
Loading…
Add table
Reference in a new issue