multi: satisfy new lnpeer interface

This commit is contained in:
Wilmer Paulino 2018-07-05 13:27:35 -07:00
parent 77d2853d76
commit 3ab17063ff
No known key found for this signature in database
GPG Key ID: 6DF57B9F9514972F
4 changed files with 62 additions and 2 deletions

@ -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 }

@ -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,

@ -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
}

39
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