p2p: don't use dial funcn in peerconfig

This commit is contained in:
Javed Khan
2018-04-07 12:51:51 +05:30
parent 54adb790f2
commit 5d8767e656
2 changed files with 12 additions and 7 deletions

View File

@@ -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"`
}
@@ -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(),
}
@@ -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")
}
@@ -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

View File

@@ -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 {
@@ -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)