Skip to content

Commit

Permalink
p2p: don't use dial funcn in peerconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed Apr 7, 2018
1 parent 54adb79 commit 5d8767e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 4 additions & 5 deletions p2p/peer.go
Expand Up @@ -87,14 +87,13 @@ func newPeer(pc peerConn, nodeInfo NodeInfo,
type PeerConfig struct {
AuthEnc bool `mapstructure:"auth_enc"` // authenticated encryption

Dial func(addr *NetAddress, config *PeerConfig) (net.Conn, error)

// times are in seconds
HandshakeTimeout time.Duration `mapstructure:"handshake_timeout"`
DialTimeout time.Duration `mapstructure:"dial_timeout"`

MConfig *tmconn.MConnConfig `mapstructure:"connection"`

Fail bool `mapstructure:"fail"` // for testing
Fuzz bool `mapstructure:"fuzz"` // fuzz connection (for testing)
FuzzConfig *FuzzConnConfig `mapstructure:"fuzz_config"`
}
Expand All @@ -103,10 +102,10 @@ type PeerConfig struct {
func DefaultPeerConfig() *PeerConfig {
return &PeerConfig{
AuthEnc: true,
Dial: dial,
HandshakeTimeout: 20, // * time.Second,
DialTimeout: 3, // * time.Second,
MConfig: tmconn.DefaultMConnConfig(),
Fail: false,
Fuzz: false,
FuzzConfig: DefaultFuzzConnConfig(),
}
Expand All @@ -115,7 +114,7 @@ func DefaultPeerConfig() *PeerConfig {
func newOutboundPeerConn(addr *NetAddress, config *PeerConfig, persistent bool, ourNodePrivKey crypto.PrivKey) (peerConn, error) {
var pc peerConn

conn, err := config.Dial(addr, config)
conn, err := dial(addr, config)
if err != nil {
return pc, errors.Wrap(err, "Error creating peer")
}
Expand Down Expand Up @@ -347,7 +346,7 @@ func (p *peer) String() string {
//------------------------------------------------------------------
// helper funcs

func dial(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
var dial = func(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
conn, err := addr.DialTimeout(config.DialTimeout * time.Second)
if err != nil {
return nil, err
Expand Down
10 changes: 8 additions & 2 deletions p2p/switch_test.go
Expand Up @@ -24,14 +24,20 @@ var (
config *cfg.P2PConfig
)

var goodDial = dial

// badDial returns an error for testing dial errors
func badDial(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
return nil, errors.New("dial err")
if config.Fail {
return nil, errors.New("dial err")
}
return goodDial(addr, config)
}

func init() {
config = cfg.DefaultP2PConfig()
config.PexReactor = true
dial = badDial
}

type PeerMessage struct {
Expand Down Expand Up @@ -309,7 +315,7 @@ func TestSwitchReconnectsToPersistentPeer(t *testing.T) {

// simulate first time dial failure
peerConfig := DefaultPeerConfig()
peerConfig.Dial = badDial
peerConfig.Fail = true
err = sw.addOutboundPeerWithConfig(rp.Addr(), peerConfig, true)
require.NotNil(err)

Expand Down

0 comments on commit 5d8767e

Please sign in to comment.