multi: use timeout field in dialer

This commit is contained in:
yyforyongyu 2020-08-25 12:50:48 +08:00
parent fb67b58d3f
commit ef38b12fda
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
7 changed files with 35 additions and 29 deletions

View File

@ -767,7 +767,10 @@ func initNeutrinoBackend(cfg *Config, chainDir string) (*neutrino.ChainService,
AddPeers: cfg.NeutrinoMode.AddPeers, AddPeers: cfg.NeutrinoMode.AddPeers,
ConnectPeers: cfg.NeutrinoMode.ConnectPeers, ConnectPeers: cfg.NeutrinoMode.ConnectPeers,
Dialer: func(addr net.Addr) (net.Conn, error) { Dialer: func(addr net.Addr) (net.Conn, error) {
return cfg.net.Dial(addr.Network(), addr.String()) return cfg.net.Dial(
addr.Network(), addr.String(),
cfg.ConnectionTimeout,
)
}, },
NameResolver: func(host string) ([]net.IP, error) { NameResolver: func(host string) ([]net.IP, error) {
addrs, err := cfg.net.LookupHost(host) addrs, err := cfg.net.LookupHost(host)

View File

@ -271,7 +271,7 @@ func (s *server) ConnectPeer(nodePub *btcec.PublicKey, addrs []net.Addr) error {
// Attempt to connect to the peer using this full address. If // Attempt to connect to the peer using this full address. If
// we're unable to connect to them, then we'll try the next // we're unable to connect to them, then we'll try the next
// address in place of it. // address in place of it.
err := s.ConnectToPeer(netAddr, true) err := s.ConnectToPeer(netAddr, true, s.cfg.ConnectionTimeout)
// If we're already connected to this peer, then we don't // If we're already connected to this peer, then we don't
// consider this an error, so we'll exit here. // consider this an error, so we'll exit here.

View File

@ -287,6 +287,10 @@ type DNSSeedBootstrapper struct {
// the network seed. // the network seed.
dnsSeeds [][2]string dnsSeeds [][2]string
net tor.Net net tor.Net
// timeout is the maximum amount of time a dial will wait for a connect to
// complete.
timeout time.Duration
} }
// A compile time assertion to ensure that DNSSeedBootstrapper meets the // A compile time assertion to ensure that DNSSeedBootstrapper meets the
@ -300,8 +304,10 @@ var _ NetworkPeerBootstrapper = (*ChannelGraphBootstrapper)(nil)
// used as a fallback for manual TCP resolution in the case of an error // used as a fallback for manual TCP resolution in the case of an error
// receiving the UDP response. The second host should return a single A record // receiving the UDP response. The second host should return a single A record
// with the IP address of the authoritative name server. // with the IP address of the authoritative name server.
func NewDNSSeedBootstrapper(seeds [][2]string, net tor.Net) NetworkPeerBootstrapper { func NewDNSSeedBootstrapper(
return &DNSSeedBootstrapper{dnsSeeds: seeds, net: net} seeds [][2]string, net tor.Net,
timeout time.Duration) NetworkPeerBootstrapper {
return &DNSSeedBootstrapper{dnsSeeds: seeds, net: net, timeout: timeout}
} }
// fallBackSRVLookup attempts to manually query for SRV records we need to // fallBackSRVLookup attempts to manually query for SRV records we need to
@ -327,7 +333,7 @@ func (d *DNSSeedBootstrapper) fallBackSRVLookup(soaShim string,
// Once we have the IP address, we'll establish a TCP connection using // Once we have the IP address, we'll establish a TCP connection using
// port 53. // port 53.
dnsServer := net.JoinHostPort(addrs[0], "53") dnsServer := net.JoinHostPort(addrs[0], "53")
conn, err := d.net.Dial("tcp", dnsServer) conn, err := d.net.Dial("tcp", dnsServer, d.timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -389,7 +395,9 @@ search:
// obtain a random sample of the encoded public keys of nodes. // obtain a random sample of the encoded public keys of nodes.
// We use the lndLookupSRV function for this task. // We use the lndLookupSRV function for this task.
primarySeed := dnsSeedTuple[0] primarySeed := dnsSeedTuple[0]
_, addrs, err := d.net.LookupSRV("nodes", "tcp", primarySeed) _, addrs, err := d.net.LookupSRV(
"nodes", "tcp", primarySeed, d.timeout,
)
if err != nil { if err != nil {
log.Tracef("Unable to lookup SRV records via "+ log.Tracef("Unable to lookup SRV records via "+
"primary seed (%v): %v", primarySeed, err) "primary seed (%v): %v", primarySeed, err)

View File

@ -226,7 +226,9 @@ func initAutoPilot(svr *server, cfg *lncfg.AutoPilot,
"address type %T", addr) "address type %T", addr)
} }
err := svr.ConnectToPeer(lnAddr, false) err := svr.ConnectToPeer(
lnAddr, false, svr.cfg.ConnectionTimeout,
)
if err != nil { if err != nil {
// If we weren't able to connect to the // If we weren't able to connect to the
// peer at this address, then we'll move // peer at this address, then we'll move

View File

@ -14,6 +14,7 @@ import (
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
"github.com/lightningnetwork/lnd/watchtower/wtdb" "github.com/lightningnetwork/lnd/watchtower/wtdb"
"github.com/lightningnetwork/lnd/watchtower/wtpolicy" "github.com/lightningnetwork/lnd/watchtower/wtpolicy"
"github.com/lightningnetwork/lnd/watchtower/wtserver" "github.com/lightningnetwork/lnd/watchtower/wtserver"
@ -137,7 +138,7 @@ type Config struct {
// Dial connects to an addr using the specified net and returns the // Dial connects to an addr using the specified net and returns the
// connection object. // connection object.
Dial Dial Dial tor.DialFunc
// AuthDialer establishes a brontide connection over an onion or clear // AuthDialer establishes a brontide connection over an onion or clear
// network. // network.

View File

@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
"github.com/lightningnetwork/lnd/watchtower/blob" "github.com/lightningnetwork/lnd/watchtower/blob"
"github.com/lightningnetwork/lnd/watchtower/wtclient" "github.com/lightningnetwork/lnd/watchtower/wtclient"
"github.com/lightningnetwork/lnd/watchtower/wtdb" "github.com/lightningnetwork/lnd/watchtower/wtdb"
@ -84,7 +85,9 @@ func newMockNet(cb func(wtserver.Peer)) *mockNet {
} }
} }
func (m *mockNet) Dial(network string, address string) (net.Conn, error) { func (m *mockNet) Dial(network string, address string,
timeout time.Duration) (net.Conn, error) {
return nil, nil return nil, nil
} }
@ -100,8 +103,9 @@ func (m *mockNet) ResolveTCPAddr(network string, address string) (*net.TCPAddr,
panic("not implemented") panic("not implemented")
} }
func (m *mockNet) AuthDial(local keychain.SingleKeyECDH, netAddr *lnwire.NetAddress, func (m *mockNet) AuthDial(local keychain.SingleKeyECDH,
dialer func(string, string) (net.Conn, error)) (wtserver.Peer, error) { netAddr *lnwire.NetAddress,
dialer tor.DialFunc) (wtserver.Peer, error) {
localPk := local.PubKey() localPk := local.PubKey()
localAddr := &net.TCPAddr{ localAddr := &net.TCPAddr{
@ -433,10 +437,8 @@ func newHarness(t *testing.T, cfg harnessCfg) *testHarness {
clientDB := wtmock.NewClientDB() clientDB := wtmock.NewClientDB()
clientCfg := &wtclient.Config{ clientCfg := &wtclient.Config{
Signer: signer, Signer: signer,
Dial: func(string, string) (net.Conn, error) { Dial: mockNet.Dial,
return nil, nil
},
DB: clientDB, DB: clientDB,
AuthDial: mockNet.AuthDial, AuthDial: mockNet.AuthDial,
SecretKeyRing: wtmock.NewSecretKeyRing(), SecretKeyRing: wtmock.NewSecretKeyRing(),

View File

@ -4,9 +4,9 @@ import (
"net" "net"
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/lightningnetwork/lnd/brontide"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/tor"
"github.com/lightningnetwork/lnd/watchtower/wtdb" "github.com/lightningnetwork/lnd/watchtower/wtdb"
"github.com/lightningnetwork/lnd/watchtower/wtserver" "github.com/lightningnetwork/lnd/watchtower/wtserver"
) )
@ -95,22 +95,12 @@ type DB interface {
AckUpdate(id *wtdb.SessionID, seqNum, lastApplied uint16) error AckUpdate(id *wtdb.SessionID, seqNum, lastApplied uint16) error
} }
// Dial connects to an addr using the specified net and returns the connection
// object.
type Dial func(net, addr string) (net.Conn, error)
// AuthDialer connects to a remote node using an authenticated transport, such as // AuthDialer connects to a remote node using an authenticated transport, such as
// brontide. The dialer argument is used to specify a resolver, which allows // brontide. The dialer argument is used to specify a resolver, which allows
// this method to be used over Tor or clear net connections. // this method to be used over Tor or clear net connections.
type AuthDialer func(localKey keychain.SingleKeyECDH, netAddr *lnwire.NetAddress, type AuthDialer func(localKey keychain.SingleKeyECDH,
dialer func(string, string) (net.Conn, error)) (wtserver.Peer, error) netAddr *lnwire.NetAddress,
dialer tor.DialFunc) (wtserver.Peer, error)
// AuthDial is the watchtower client's default method of dialing.
func AuthDial(localKey keychain.SingleKeyECDH, netAddr *lnwire.NetAddress,
dialer func(string, string) (net.Conn, error)) (wtserver.Peer, error) {
return brontide.Dial(localKey, netAddr, dialer)
}
// ECDHKeyRing abstracts the ability to derive shared ECDH keys given a // ECDHKeyRing abstracts the ability to derive shared ECDH keys given a
// description of the derivation path of a private key. // description of the derivation path of a private key.