From e46bd8e1779d70bd1ec41b51d3d67393f4a72910 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 16 Mar 2023 21:25:17 +0800 Subject: [PATCH] multi: add `AddPendingChannel` to peer interface The funding manager has been updated to use `AddPendingChannel`. Note that we track the pending channel before it's confirmed as the peer may have a block height in the future(from our view), thus they may start operating in this channel before we consider it as fully open. The mocked peers have been updated to implement the new interface method. --- discovery/mock_test.go | 6 ++++++ funding/manager.go | 19 ++++++++++++++++++- funding/manager_test.go | 6 ++++++ htlcswitch/link_test.go | 6 ++++++ htlcswitch/mock.go | 6 ++++++ lnpeer/peer.go | 4 ++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/discovery/mock_test.go b/discovery/mock_test.go index 0040ab8b6..3d3ee8229 100644 --- a/discovery/mock_test.go +++ b/discovery/mock_test.go @@ -63,6 +63,12 @@ func (p *mockPeer) RemoteFeatures() *lnwire.FeatureVector { return nil } +func (p *mockPeer) AddPendingChannel(_ lnwire.ChannelID, + _ <-chan struct{}) error { + + return nil +} + // mockMessageStore is an in-memory implementation of the MessageStore interface // used for the gossiper's unit tests. type mockMessageStore struct { diff --git a/funding/manager.go b/funding/manager.go index f243a8037..554c3ab06 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -2172,7 +2172,16 @@ func (f *Manager) continueFundingAccept(resCtx *reservationWithCtx, log.Infof("Generated ChannelPoint(%v) for pending_id(%x)", outPoint, pendingChanID[:]) - var err error + // Before sending FundingCreated sent, we notify Brontide to keep track + // of this pending open channel. + err := resCtx.peer.AddPendingChannel(channelID, f.quit) + if err != nil { + pubKey := resCtx.peer.IdentityKey().SerializeCompressed() + log.Errorf("Unable to add pending channel %v with peer %x: %v", + channelID, pubKey, err) + } + + // Send the FundingCreated msg. fundingCreated := &lnwire.FundingCreated{ PendingChannelID: pendingChanID, FundingPoint: *outPoint, @@ -2294,6 +2303,14 @@ func (f *Manager) handleFundingCreated(peer lnpeer.Peer, return } + // Before sending FundingSigned, we notify Brontide first to keep track + // of this pending open channel. + if err := peer.AddPendingChannel(channelID, f.quit); err != nil { + pubKey := peer.IdentityKey().SerializeCompressed() + log.Errorf("Unable to add pending channel %v with peer %x: %v", + channelID, pubKey, err) + } + fundingSigned := &lnwire.FundingSigned{ ChanID: channelID, CommitSig: ourCommitSig, diff --git a/funding/manager_test.go b/funding/manager_test.go index 1ca66904b..0360bd4b5 100644 --- a/funding/manager_test.go +++ b/funding/manager_test.go @@ -321,6 +321,12 @@ func (n *testNode) AddNewChannel(channel *channeldb.OpenChannel, } } +func (n *testNode) AddPendingChannel(_ lnwire.ChannelID, + quit <-chan struct{}) error { + + return nil +} + func createTestWallet(cdb *channeldb.ChannelStateDB, netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier, wc lnwallet.WalletController, signer input.Signer, keyRing keychain.SecretKeyRing, diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 6817ff930..e691ecec1 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -1888,6 +1888,12 @@ func (m *mockPeer) RemoteFeatures() *lnwire.FeatureVector { return nil } +func (m *mockPeer) AddPendingChannel(_ lnwire.ChannelID, + _ <-chan struct{}) error { + + return nil +} + func newSingleLinkTestHarness(t *testing.T, chanAmt, chanReserve btcutil.Amount) ( ChannelLink, *lnwallet.LightningChannel, chan time.Time, func() error, func() (*lnwallet.LightningChannel, error), error) { diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index bb36eda20..e339d8d52 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -672,6 +672,12 @@ func (s *mockServer) AddNewChannel(channel *channeldb.OpenChannel, return nil } +func (s *mockServer) AddPendingChannel(_ lnwire.ChannelID, + cancel <-chan struct{}) error { + + return nil +} + func (s *mockServer) WipeChannel(*wire.OutPoint) {} func (s *mockServer) LocalFeatures() *lnwire.FeatureVector { diff --git a/lnpeer/peer.go b/lnpeer/peer.go index 465a41cb9..c9217ce82 100644 --- a/lnpeer/peer.go +++ b/lnpeer/peer.go @@ -27,6 +27,10 @@ type Peer interface { // to be added if the cancel channel is closed. AddNewChannel(channel *channeldb.OpenChannel, cancel <-chan struct{}) error + // AddPendingChannel adds a pending open channel ID to the peer. The + // channel should fail to be added if the cancel chan is closed. + AddPendingChannel(cid lnwire.ChannelID, cancel <-chan struct{}) error + // WipeChannel removes the channel uniquely identified by its channel // point from all indexes associated with the peer. WipeChannel(*wire.OutPoint)