Browse Source

fixes from review

pull/1843/head
Ethan Buchman 6 years ago
parent
commit
737c5c065d
4 changed files with 23 additions and 29 deletions
  1. +5
    -1
      node/node.go
  2. +15
    -13
      p2p/listener.go
  3. +1
    -5
      p2p/listener_test.go
  4. +2
    -10
      p2p/pex/pex_reactor_test.go

+ 5
- 1
node/node.go View File

@ -426,7 +426,11 @@ func (n *Node) OnStart() error {
}
// Create & add listener
l := p2p.NewDefaultListener(n.config.P2P, n.Logger.With("module", "p2p"))
l := p2p.NewDefaultListener(
n.config.P2P.ListenAddress,
n.config.P2P.ExternalAddress,
n.config.P2P.UPNP,
n.Logger.With("module", "p2p"))
n.sw.AddListener(l)
// Generate node PrivKey


+ 15
- 13
p2p/listener.go View File

@ -7,7 +7,6 @@ import (
"strings"
"time"
"github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/p2p/upnp"
@ -60,10 +59,14 @@ func splitHostPort(addr string) (host string, port int) {
// NewDefaultListener creates a new DefaultListener on lAddr, optionally trying
// to determine external address using UPnP.
func NewDefaultListener(cfg *config.P2PConfig, logger log.Logger) Listener {
func NewDefaultListener(
fullListenAddrString string,
externalAddrString string,
useUPnP bool,
logger log.Logger) Listener {
// Split protocol, address, and port.
protocol, lAddr := cmn.ProtocolAndAddress(cfg.ListenAddress)
protocol, lAddr := cmn.ProtocolAndAddress(fullListenAddrString)
lAddrIP, lAddrPort := splitHostPort(lAddr)
// Create listener
@ -93,24 +96,23 @@ func NewDefaultListener(cfg *config.P2PConfig, logger log.Logger) Listener {
inAddrAny := lAddrIP == "" || lAddrIP == "0.0.0.0"
// Determine external address...
// Determine external address.
var extAddr *NetAddress
if cfg.UPNP {
// If the lAddrIP is INADDR_ANY, try UPnP
if inAddrAny {
extAddr = getUPNPExternalAddress(lAddrPort, listenerPort, logger)
}
}
if cfg.ExternalAddress != "" {
if externalAddrString != "" {
var err error
extAddr, err = NewNetAddressStringWithOptionalID(cfg.ExternalAddress)
extAddr, err = NewNetAddressStringWithOptionalID(externalAddrString)
if err != nil {
panic(fmt.Sprintf("Error in ExternalAddress: %v", err))
}
}
// Otherwise just use the local address...
// If the lAddrIP is INADDR_ANY, try UPnP.
if extAddr == nil && useUPnP && inAddrAny {
extAddr = getUPNPExternalAddress(lAddrPort, listenerPort, logger)
}
// Otherwise just use the local address.
if extAddr == nil {
defaultToIPv4 := inAddrAny
extAddr = getNaiveExternalAddress(defaultToIPv4, listenerPort, false, logger)


+ 1
- 5
p2p/listener_test.go View File

@ -4,16 +4,12 @@ import (
"bytes"
"testing"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
)
func TestListener(t *testing.T) {
// Create a listener
cfg := &config.P2PConfig{
ListenAddress: "tcp://:8001",
}
l := NewDefaultListener(cfg, log.TestingLogger())
l := NewDefaultListener("tcp://:8001", "", false, log.TestingLogger())
// Dial the listener
lAddr := l.ExternalAddress()


+ 2
- 10
p2p/pex/pex_reactor_test.go View File

@ -109,10 +109,7 @@ func TestPEXReactorRunning(t *testing.T) {
addOtherNodeAddrToAddrBook(2, 1)
for i, sw := range switches {
cfg := &config.P2PConfig{
ListenAddress: fmt.Sprintf("tcp://%v", sw.NodeInfo().ListenAddr),
}
sw.AddListener(p2p.NewDefaultListener(cfg, logger.With("pex", i)))
sw.AddListener(p2p.NewDefaultListener("tcp://"+sw.NodeInfo().ListenAddr, "", false, logger.With("pex", i)))
err := sw.Start() // start switch and reactors
require.Nil(t, err)
@ -232,12 +229,7 @@ func TestPEXReactorUsesSeedsIfNeeded(t *testing.T) {
},
)
seed.AddListener(
p2p.NewDefaultListener(
&config.P2PConfig{
ListenAddress: fmt.Sprintf("tcp://%v", seed.NodeInfo().ListenAddr),
},
log.TestingLogger(),
),
p2p.NewDefaultListener("tcp://"+seed.NodeInfo().ListenAddr, "", false, log.TestingLogger()),
)
require.Nil(t, seed.Start())
defer seed.Stop()


Loading…
Cancel
Save