Skip to content

Commit

Permalink
modify handshake timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
dylenfu committed May 11, 2020
1 parent de5c095 commit 271d18a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
22 changes: 11 additions & 11 deletions common/test_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ const (
var (
HandshakeLevel uint8
HandshakeWrongMsg bool
HandshakeDuration time.Duration
HandshakeTimeout time.Duration
HeartbeatBlockHeight uint64
HeartbeatInterruptAfterStartTime int64
HeartbeatInterruptPingLastTime int64
HeartbeatInterruptPongLastTime int64
)

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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", &params); err != nil {
_ = log4.Error("%s", err)
Expand All @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions p2pserver/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion params/HandshakeTimeout.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Remote": "172.168.3.158:20338",
"Timeout": 10
"BlockTime": 10
}

0 comments on commit 271d18a

Please sign in to comment.