diff --git a/common/test_params.go b/common/test_params.go index abe48a3..7fdb413 100644 --- a/common/test_params.go +++ b/common/test_params.go @@ -38,7 +38,7 @@ const ( var ( HandshakeLevel uint8 HandshakeWrongMsg bool - HandshakeDuration time.Duration + HandshakeTimeout time.Duration HeartbeatBlockHeight uint64 HeartbeatInterruptAfterStartTime int64 HeartbeatInterruptPingLastTime int64 @@ -46,19 +46,19 @@ var ( ) var ( - DefHandshakeStopLevel uint8 = HandshakeNormal - DefHandshakeWrongMsg = false - DefHandshakeTimeout = time.Duration(10) * time.Second - DefHeartbeatBlockHeight uint64 = 9442 - DefHeartbeatInterruptAfterStartTime int64 = 0 - DefHeartbeatInterruptPingLastTime int64 = 0 - DefHeartbeatInterruptPongLastTime int64 = 0 + DefHandshakeStopLevel uint8 = HandshakeNormal + DefHandshakeWrongMsg = false + DefHandshakeTimeout time.Duration = time.Duration(0) + DefHeartbeatBlockHeight uint64 = 9442 + DefHeartbeatInterruptAfterStartTime int64 = 0 + DefHeartbeatInterruptPingLastTime int64 = 0 + DefHeartbeatInterruptPongLastTime int64 = 0 ) func InitializeTestParams() { HandshakeLevel = DefHandshakeStopLevel HandshakeWrongMsg = DefHandshakeWrongMsg - HandshakeDuration = DefHandshakeTimeout + HandshakeTimeout = DefHandshakeTimeout HeartbeatBlockHeight = DefHeartbeatBlockHeight HeartbeatInterruptAfterStartTime = DefHeartbeatInterruptAfterStartTime HeartbeatInterruptPingLastTime = DefHeartbeatInterruptPingLastTime @@ -83,8 +83,8 @@ func SetHandshakeWrongMsg(active bool) { } // handshake timeout -func SetHandshakeTestDuraion(sec int) { - HandshakeDuration = time.Duration(sec) * time.Second +func SetHandshakeTimeout(sec int) { + HandshakeTimeout = time.Duration(sec) * time.Second } // heartbeat diff --git a/methods/methods.go b/methods/methods.go index 950b783..fe896dc 100644 --- a/methods/methods.go +++ b/methods/methods.go @@ -115,8 +115,8 @@ func HandshakeWrongMsg() bool { func HandshakeTimeout() bool { var params struct { - Remote string - Timeout int + Remote string + BlockTime int } if err := getParamsFromJsonFile("./params/HandshakeTimeout.json", ¶ms); err != nil { _ = log4.Error("%s", err) @@ -126,7 +126,7 @@ func HandshakeTimeout() bool { protocol := protocols.NewOnlyHeartbeatMsgHandler() setup(protocol) - common.SetHandshakeTestDuraion(params.Timeout) + common.SetHandshakeTimeout(params.BlockTime) if err := ns.Connect(params.Remote); err != nil { log4.Debug("connecting to %s failed, err: %s", params.Remote, err) } else { diff --git a/p2pserver/handshake/handshake.go b/p2pserver/handshake/handshake.go index ccaaece..6f108a1 100644 --- a/p2pserver/handshake/handshake.go +++ b/p2pserver/handshake/handshake.go @@ -29,9 +29,11 @@ import ( "time" ) +var HANDSHAKE_DURATION = 10 * time.Second // handshake time can not exceed this duration, or will treat as attack. func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Conn) (*peer.PeerInfo, error) { version := newVersion(info) - if err := conn.SetDeadline(time.Now().Add(tcm.HandshakeDuration)); err != nil { + + if err := conn.SetDeadline(time.Now().Add(HANDSHAKE_DURATION)); err != nil { return nil, err } defer func() { @@ -56,6 +58,11 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con return nil, fmt.Errorf("client handshake stopped after send version") } + // mark: + if tcm.HandshakeTimeout > 0 { + time.Sleep(tcm.HandshakeTimeout) + } + // 2. read version msg, _, err := types.ReadMessage(conn) if err != nil { @@ -123,7 +130,7 @@ func HandshakeClient(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Con func HandshakeServer(info *peer.PeerInfo, selfId *common.PeerKeyId, conn net.Conn) (*peer.PeerInfo, error) { ver := newVersion(info) - if err := conn.SetDeadline(time.Now().Add(tcm.HandshakeDuration)); err != nil { + if err := conn.SetDeadline(time.Now().Add(tcm.HandshakeTimeout)); err != nil { return nil, err } defer func() { diff --git a/params/HandshakeTimeout.json b/params/HandshakeTimeout.json index 502a7e7..053d865 100644 --- a/params/HandshakeTimeout.json +++ b/params/HandshakeTimeout.json @@ -1,4 +1,4 @@ { "Remote": "172.168.3.158:20338", - "Timeout": 10 + "BlockTime": 10 }