Merge pull request #1646 from tendermint/xla/duplicate-ip-check-config

Introduce option to skip duplicate ip check
This commit is contained in:
Ethan Buchman 2018-05-30 08:12:46 -04:00 committed by GitHub
commit 094a40084d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 65 deletions

View File

@ -292,6 +292,9 @@ type P2PConfig struct {
// Comma separated list of peer IDs to keep private (will not be gossiped to other peers) // Comma separated list of peer IDs to keep private (will not be gossiped to other peers)
PrivatePeerIDs string `mapstructure:"private_peer_ids"` PrivatePeerIDs string `mapstructure:"private_peer_ids"`
// Toggle to disable guard against peers connecting from the same ip.
SkipDuplicatePeerIPCheck bool `mapstructure:"skip_duplicate_peer_ip_check"`
} }
// DefaultP2PConfig returns a default configuration for the peer-to-peer layer // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
@ -317,6 +320,7 @@ func TestP2PConfig() *P2PConfig {
cfg.ListenAddress = "tcp://0.0.0.0:36656" cfg.ListenAddress = "tcp://0.0.0.0:36656"
cfg.SkipUPNP = true cfg.SkipUPNP = true
cfg.FlushThrottleTimeout = 10 cfg.FlushThrottleTimeout = 10
cfg.SkipDuplicatePeerIPCheck = true
return cfg return cfg
} }

View File

@ -47,10 +47,6 @@ func (ps *PeerSet) Add(peer Peer) error {
return ErrSwitchDuplicatePeerID{peer.ID()} return ErrSwitchDuplicatePeerID{peer.ID()}
} }
if ps.hasIP(peer.RemoteIP()) {
return ErrSwitchDuplicatePeerIP{peer.RemoteIP()}
}
index := len(ps.list) index := len(ps.list)
// Appending is safe even with other goroutines // Appending is safe even with other goroutines
// iterating over the ps.list slice. // iterating over the ps.list slice.

View File

@ -143,20 +143,6 @@ func TestPeerSetAddDuplicate(t *testing.T) {
assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount") assert.Equal(t, wantNilErrCount, gotNilErrCount, "invalid nil errCount")
} }
func TestPeerSetAddDuplicateIP(t *testing.T) {
t.Parallel()
peerSet := NewPeerSet()
if err := peerSet.Add(randPeer(net.IP{172, 0, 0, 1})); err != nil {
t.Fatal(err)
}
// Add peer with same IP.
err := peerSet.Add(randPeer(net.IP{172, 0, 0, 1}))
assert.Equal(t, ErrSwitchDuplicatePeerIP{IP: net.IP{172, 0, 0, 1}}, err)
}
func TestPeerSetGet(t *testing.T) { func TestPeerSetGet(t *testing.T) {
t.Parallel() t.Parallel()

View File

@ -27,6 +27,7 @@ var (
func init() { func init() {
config = cfg.DefaultP2PConfig() config = cfg.DefaultP2PConfig()
config.PexReactor = true config.PexReactor = true
config.SkipDuplicatePeerIPCheck = true
} }
func TestPEXReactorBasic(t *testing.T) { func TestPEXReactorBasic(t *testing.T) {
@ -69,59 +70,59 @@ func TestPEXReactorAddRemovePeer(t *testing.T) {
// peers have different IP addresses, they all have the same underlying remote // peers have different IP addresses, they all have the same underlying remote
// IP: 127.0.0.1. // IP: 127.0.0.1.
// //
// func TestPEXReactorRunning(t *testing.T) { func TestPEXReactorRunning(t *testing.T) {
// N := 3 N := 3
// switches := make([]*p2p.Switch, N) switches := make([]*p2p.Switch, N)
// // directory to store address books // directory to store address books
// dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
// require.Nil(t, err) require.Nil(t, err)
// defer os.RemoveAll(dir) // nolint: errcheck defer os.RemoveAll(dir) // nolint: errcheck
// books := make([]*addrBook, N) books := make([]*addrBook, N)
// logger := log.TestingLogger() logger := log.TestingLogger()
// // create switches // create switches
// for i := 0; i < N; i++ { for i := 0; i < N; i++ {
// switches[i] = p2p.MakeSwitch(config, i, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch { switches[i] = p2p.MakeSwitch(config, i, "testing", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
// books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false) books[i] = NewAddrBook(filepath.Join(dir, fmt.Sprintf("addrbook%d.json", i)), false)
// books[i].SetLogger(logger.With("pex", i)) books[i].SetLogger(logger.With("pex", i))
// sw.SetAddrBook(books[i]) sw.SetAddrBook(books[i])
// sw.SetLogger(logger.With("pex", i)) sw.SetLogger(logger.With("pex", i))
// r := NewPEXReactor(books[i], &PEXReactorConfig{}) r := NewPEXReactor(books[i], &PEXReactorConfig{})
// r.SetLogger(logger.With("pex", i)) r.SetLogger(logger.With("pex", i))
// r.SetEnsurePeersPeriod(250 * time.Millisecond) r.SetEnsurePeersPeriod(250 * time.Millisecond)
// sw.AddReactor("pex", r) sw.AddReactor("pex", r)
// return sw return sw
// }) })
// } }
// addOtherNodeAddrToAddrBook := func(switchIndex, otherSwitchIndex int) { addOtherNodeAddrToAddrBook := func(switchIndex, otherSwitchIndex int) {
// addr := switches[otherSwitchIndex].NodeInfo().NetAddress() addr := switches[otherSwitchIndex].NodeInfo().NetAddress()
// books[switchIndex].AddAddress(addr, addr) books[switchIndex].AddAddress(addr, addr)
// } }
// addOtherNodeAddrToAddrBook(0, 1) addOtherNodeAddrToAddrBook(0, 1)
// addOtherNodeAddrToAddrBook(1, 0) addOtherNodeAddrToAddrBook(1, 0)
// addOtherNodeAddrToAddrBook(2, 1) addOtherNodeAddrToAddrBook(2, 1)
// for i, sw := range switches { for i, sw := range switches {
// sw.AddListener(p2p.NewDefaultListener("tcp", sw.NodeInfo().ListenAddr, true, logger.With("pex", i))) sw.AddListener(p2p.NewDefaultListener("tcp", sw.NodeInfo().ListenAddr, true, logger.With("pex", i)))
// err := sw.Start() // start switch and reactors err := sw.Start() // start switch and reactors
// require.Nil(t, err) require.Nil(t, err)
// } }
// assertPeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second, N-1) assertPeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second, N-1)
// // stop them // stop them
// for _, s := range switches { for _, s := range switches {
// s.Stop() s.Stop()
// } }
// } }
func TestPEXReactorReceive(t *testing.T) { func TestPEXReactorReceive(t *testing.T) {
r, book := createReactor(&PEXReactorConfig{}) r, book := createReactor(&PEXReactorConfig{})

View File

@ -577,8 +577,9 @@ func (sw *Switch) addPeer(pc peerConn) error {
} }
// Check for duplicate connection or peer info IP. // Check for duplicate connection or peer info IP.
if sw.peers.HasIP(pc.RemoteIP()) || if !sw.config.SkipDuplicatePeerIPCheck &&
sw.peers.HasIP(peerNodeInfo.NetAddress().IP) { (sw.peers.HasIP(pc.RemoteIP()) ||
sw.peers.HasIP(peerNodeInfo.NetAddress().IP)) {
return ErrSwitchDuplicatePeerIP{pc.RemoteIP()} return ErrSwitchDuplicatePeerIP{pc.RemoteIP()}
} }

View File

@ -3,7 +3,6 @@ package p2p
import ( import (
"fmt" "fmt"
"net" "net"
"sync/atomic"
crypto "github.com/tendermint/go-crypto" crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
@ -132,8 +131,6 @@ func StartSwitches(switches []*Switch) error {
return nil return nil
} }
var listenAddrSuffix uint32 = 1
func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch { func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch {
// new switch, add reactors // new switch, add reactors
// TODO: let the config be passed in? // TODO: let the config be passed in?
@ -148,7 +145,7 @@ func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch f
Moniker: cmn.Fmt("switch%d", i), Moniker: cmn.Fmt("switch%d", i),
Network: network, Network: network,
Version: version, Version: version,
ListenAddr: fmt.Sprintf("127.0.0.%d:%d", atomic.AddUint32(&listenAddrSuffix, 1), cmn.RandIntn(64512)+1023), ListenAddr: fmt.Sprintf("127.0.0.1:%d", cmn.RandIntn(64512)+1023),
} }
for ch := range sw.reactorsByCh { for ch := range sw.reactorsByCh {
ni.Channels = append(ni.Channels, ch) ni.Channels = append(ni.Channels, ch)