Skip to content

Commit

Permalink
handshake: mininum runnable model
Browse files Browse the repository at this point in the history
  • Loading branch information
dylenfu committed May 9, 2020
1 parent 57cf247 commit 274f04a
Show file tree
Hide file tree
Showing 19 changed files with 574 additions and 206 deletions.
53 changes: 53 additions & 0 deletions common/test_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package common

import "time"

// handshake test cases
const (
HandshakeNormal = iota
Handshake_StopAfterSendVersion
Handshake_StopAfterReceiveVersion
Handshake_StopAfterUpdateKad
Handshake_StopAfterReadKad
Handshake_StopAfterSendAck
Handshake_StopAfterReadAck
)

var (
HandshakeLevel uint8 = HandshakeNormal // default normal
HandshakeDuration time.Duration = time.Duration(10) * time.Second // default value: 10 sec
)

func SetHandshakeLevel(lvl uint8) {
HandshakeLevel = lvl
}
func StopHandshake(lvl uint8) bool {
return HandshakeLevel == lvl
}
func SetHandshakeDuraion(sec int) {
HandshakeDuration = time.Duration(sec) * time.Second
}

// heartbeat test cases
var HeartbeatBlockHeight uint64 = 358 // default 100000
func SetHeartbeatBlockHeight(height uint64) {
HeartbeatBlockHeight = height
}
56 changes: 30 additions & 26 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"Seed":[
"172.168.3.158:20338",
"172.168.3.159:20338",
"172.168.3.160:20338",
"172.168.3.161:20338"
],
"Sync":[
"172.168.3.162:20338",
"172.168.3.163:20338",
"172.168.3.164:20338",
"172.168.3.165:20338"
],
"Net":{
"ReservedPeersOnly":false,
"ReservedCfg":{
Expand All @@ -7,37 +19,29 @@
"1.2.3.5"
],
"mask":[
"2.3.4.5"
"172.168.3.151",
"172.168.3.152",
"172.168.3.153",
"172.168.3.154",
"172.168.3.155",
"172.168.3.156",
"172.168.3.157"
]
},
"NetworkMagic":0,
"NetworkId":0,
"NetworkMagic":299,
"NetworkId":299,
"NetworkName":"",
"NodePort":0,
"IsTLS":false
"NodePort":20338,
"HttpInfoPort":20446,
"IsTLS":false,
"MaxHdrSyncReqs":1024,
"MaxConnInBound":1024,
"MaxConnOutBound":1024,
"MaxConnInBoundForSingleIP":1024
},
"Sdk":{
"JsonRpcAddress":"http://localhost:20336",
"JsonRpcAddress":"http://172.168.3.162:20336",
"GasPrice":0,
"GasLimit":20000
},
"Sync":[
"172.168.3.151:20338",
"172.168.3.152:20338",
"172.168.3.153:20338",
"172.168.3.154:20338",
"172.168.3.155:20338"
],
"Seed":[
"172.168.3.156:20338",
"172.168.3.157:20338",
"172.168.3.158:20338",
"172.168.3.159:20338",
"172.168.3.160:20338",
"172.168.3.161:20338",
"172.168.3.162:20338",
"172.168.3.163:20338",
"172.168.3.164:20338",
"172.168.3.165:20338"
]
}
}
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/json"
"fmt"
log4 "github.com/alecthomas/log4go"
"github.com/ontio/ontology/common/config"
cmf "github.com/ontio/ontology/common/config"
"io/ioutil"
"os"
)
Expand All @@ -32,7 +32,7 @@ var DefConfig = NewDHTConfig()
type DHTConfig struct {
Seed []string
Sync []string
Net *config.P2PNodeConfig
Net *cmf.P2PNodeConfig
Sdk *SDKConfig
}

Expand Down Expand Up @@ -70,6 +70,7 @@ func (this *DHTConfig) loadConfig(fileName string) error {
if err != nil {
return fmt.Errorf("json.Unmarshal TestConfig:%s error:%s", data, err)
}
cmf.DefConfig.P2PNode = this.Net
return nil
}

Expand Down
Binary file modified dht-tool
Binary file not shown.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
func init() {
flag.StringVar(&Config, "cfg", "./config.json", "Config of ontology-tool")
flag.StringVar(&LogConfig, "lfg", "./log4go.xml", "Log config of ontology-tool")
flag.StringVar(&Methods, "t", "", "methods to run. use ',' to split methods")
flag.StringVar(&Methods, "t", "handshake", "methods to run. use ',' to split methods")
flag.Parse()
}

Expand Down
1 change: 1 addition & 0 deletions methods/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ import (

func init() {
core.OntTool.RegMethod("demo", Demo)
core.OntTool.RegMethod("handshake", Handshake)
}
57 changes: 57 additions & 0 deletions methods/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,67 @@ package methods

import (
log4 "github.com/alecthomas/log4go"
"github.com/ontio/ontology-tool/common"
"github.com/ontio/ontology-tool/config"
"github.com/ontio/ontology-tool/p2pserver/net/netserver"
"github.com/ontio/ontology-tool/p2pserver/net/protocol"
"github.com/ontio/ontology-tool/p2pserver/protocols"
"github.com/ontio/ontology-tool/utils/timer"
)

var (
ns *netserver.NetServer
tr *timer.Timer
)

func Demo() bool {
log4.Info("hello, dht demo")
return true
}

func setup(protocol p2p.Protocol) {
var err error

if ns, err = netserver.NewNetServer(protocol, config.DefConfig.Net); err != nil {
log4.Crashf("[NewNetServer] crashed, err %s", err)
}
if err = ns.Start(); err != nil {
log4.Crashf("start netserver failed, err %s", err)
}

tr = timer.NewTimer(2)
}

func Handshake() bool {

// 1. get params from json file
var params struct {
Remote string
HeartbeatTime int
}
if err := getParamsFromJsonFile("./params/Handshake.json", &params); err != nil {
log4.Error("%s", err)
return false
}

// 2. set common params
common.SetHandshakeDuraion(10)
common.SetHandshakeLevel(common.HandshakeNormal)
common.SetHeartbeatBlockHeight(358)

// 3. setup p2p.protocols
protocol := protocols.NewOnlyHeartbeatMsgHandler()
setup(protocol)

// 4. connect and handshake
if err := ns.Connect(params.Remote); err != nil {
log4.Debug("connecting to %s failed, err: %s", params.Remote, err)
return false
}

// 5. dispatch
dispatch(params.HeartbeatTime)
log4.Info("handshake end!")

return true
}
141 changes: 0 additions & 141 deletions methods/net_server.go

This file was deleted.

Loading

0 comments on commit 274f04a

Please sign in to comment.