diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index cd10ca90..0dd3c604 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -23,6 +23,7 @@ import ( "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnpeer" + "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing" "github.com/btcsuite/btcd/btcec" @@ -2159,6 +2160,9 @@ func (p *mockPeer) SendMessage(_ bool, msgs ...lnwire.Message) error { return nil } +func (p *mockPeer) AddNewChannel(_ *lnwallet.LightningChannel, _ <-chan struct{}) error { + return nil +} func (p *mockPeer) WipeChannel(_ *wire.OutPoint) error { return nil } func (p *mockPeer) IdentityKey() *btcec.PublicKey { return p.pk } func (p *mockPeer) PubKey() [33]byte { @@ -2166,3 +2170,4 @@ func (p *mockPeer) PubKey() [33]byte { copy(pubkey[:], p.pk.SerializeCompressed()) return pubkey } +func (p *mockPeer) Address() net.Addr { return nil } diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 74366850..c7f1d3ae 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "math" + "net" "reflect" "runtime" "strings" @@ -1411,6 +1412,9 @@ func (m *mockPeer) SendMessage(sync bool, msgs ...lnwire.Message) error { } return nil } +func (m *mockPeer) AddNewChannel(_ *lnwallet.LightningChannel, _ <-chan struct{}) error { + return nil +} func (m *mockPeer) WipeChannel(*wire.OutPoint) error { return nil } @@ -1420,8 +1424,9 @@ func (m *mockPeer) PubKey() [33]byte { func (m *mockPeer) IdentityKey() *btcec.PublicKey { return nil } - -var _ lnpeer.Peer = (*mockPeer)(nil) +func (m *mockPeer) Address() net.Addr { + return nil +} func newSingleLinkTestHarness(chanAmt, chanReserve btcutil.Amount) ( ChannelLink, *lnwallet.LightningChannel, chan time.Time, func() error, diff --git a/htlcswitch/mock.go b/htlcswitch/mock.go index 487b1601..fb648ac2 100644 --- a/htlcswitch/mock.go +++ b/htlcswitch/mock.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "sync" "sync/atomic" "testing" @@ -517,6 +518,16 @@ func (s *mockServer) IdentityKey() *btcec.PublicKey { return pubkey } +func (s *mockServer) Address() net.Addr { + return nil +} + +func (s *mockServer) AddNewChannel(channel *lnwallet.LightningChannel, + cancel <-chan struct{}) error { + + return nil +} + func (s *mockServer) WipeChannel(*wire.OutPoint) error { return nil } diff --git a/peer.go b/peer.go index 1d01e7bf..76dd055c 100644 --- a/peer.go +++ b/peer.go @@ -21,6 +21,7 @@ import ( "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/htlcswitch" + "github.com/lightningnetwork/lnd/lnpeer" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" @@ -185,6 +186,9 @@ type peer struct { wg sync.WaitGroup } +// A compile-time check to ensure that peer satisfies the lnpeer.Peer interface. +var _ lnpeer.Peer = (*peer)(nil) + // newPeer creates a new peer from an establish connection object, and a // pointer to the main server. func newPeer(conn net.Conn, connReq *connmgr.ConnReq, server *server, @@ -1999,6 +2003,41 @@ func (p *peer) IdentityKey() *btcec.PublicKey { return p.addr.IdentityKey } +// Address returns the network address of the remote peer. +func (p *peer) Address() net.Addr { + return p.addr.Address +} + +// AddNewChannel adds a new channel to the peer. The channel should fail to be +// added if the cancel channel is closed. +func (p *peer) AddNewChannel(channel *lnwallet.LightningChannel, + cancel <-chan struct{}) error { + + newChanDone := make(chan struct{}) + newChanMsg := &newChannelMsg{ + channel: channel, + done: newChanDone, + } + + select { + case p.newChannels <- newChanMsg: + case <-cancel: + return errors.New("canceled adding new channel") + case <-p.quit: + return ErrPeerExiting + } + + // We pause here to wait for the peer to recognize the new channel + // before we close the channel barrier corresponding to the channel. + select { + case <-newChanDone: + case <-p.quit: + return ErrPeerExiting + } + + return nil +} + // TODO(roasbeef): make all start/stop mutexes a CAS // fetchLastChanUpdate returns a function which is able to retrieve the last