aboutsummaryrefslogtreecommitdiffstats
path: root/simulation/node.go
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-11-05 18:02:53 +0800
committerJimmy Hu <jimmy.hu@dexon.org>2018-11-05 18:02:53 +0800
commitcd9f3011f58af965b910c0a1b0e27b22ccb30f23 (patch)
tree99838cb962d92bb78def33af2e7fd4583ae5770b /simulation/node.go
parentc4541185c1d2502dffe09de1af52594f6fae16a6 (diff)
downloaddexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar.gz
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar.bz2
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar.lz
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar.xz
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.tar.zst
dexon-consensus-cd9f3011f58af965b910c0a1b0e27b22ccb30f23.zip
test: move simulation.network to test package (#297)
Diffstat (limited to 'simulation/node.go')
-rw-r--r--simulation/node.go52
1 files changed, 42 insertions, 10 deletions
diff --git a/simulation/node.go b/simulation/node.go
index 311ccfc..8907d5a 100644
--- a/simulation/node.go
+++ b/simulation/node.go
@@ -18,6 +18,7 @@
package simulation
import (
+ "encoding/json"
"fmt"
"sort"
"time"
@@ -26,11 +27,33 @@ import (
"github.com/dexon-foundation/dexon-consensus/core"
"github.com/dexon-foundation/dexon-consensus/core/blockdb"
"github.com/dexon-foundation/dexon-consensus/core/crypto"
+ "github.com/dexon-foundation/dexon-consensus/core/test"
"github.com/dexon-foundation/dexon-consensus/core/types"
typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
"github.com/dexon-foundation/dexon-consensus/simulation/config"
)
+type infoStatus string
+
+const (
+ statusInit infoStatus = "init"
+ statusNormal infoStatus = "normal"
+ statusShutdown infoStatus = "shutdown"
+)
+
+type messageType string
+
+const (
+ shutdownAck messageType = "shutdownAck"
+ blockTimestamp messageType = "blockTimestamps"
+)
+
+// message is a struct for peer sending message to server.
+type message struct {
+ Type messageType `json:"type"`
+ Payload json.RawMessage `json:"payload"`
+}
+
// node represents a node in DexCon.
type node struct {
app core.Application
@@ -38,7 +61,7 @@ type node struct {
db blockdb.BlockDatabase
config config.Node
- netModule *network
+ netModule *test.Network
ID types.NodeID
chainID uint64
@@ -50,12 +73,21 @@ type node struct {
func newNode(
prvKey crypto.PrivateKey,
config config.Config) *node {
-
pubKey := prvKey.PublicKey()
- netModule := newNetwork(pubKey, config.Networking)
+ netModule := test.NewNetwork(
+ pubKey,
+ &test.NormalLatencyModel{
+ Mean: config.Networking.Mean,
+ Sigma: config.Networking.Sigma,
+ },
+ test.NewDefaultMarshaller(&jsonMarshaller{}),
+ test.NetworkConfig{
+ Type: config.Networking.Type,
+ PeerServer: config.Networking.PeerServer,
+ PeerPort: peerPort,
+ })
id := types.NewNodeID(pubKey)
- db, err := blockdb.NewMemBackedBlockDB(
- id.String() + ".blockdb")
+ db, err := blockdb.NewMemBackedBlockDB(id.String() + ".blockdb")
if err != nil {
panic(err)
}
@@ -79,12 +111,12 @@ func (n *node) GetID() types.NodeID {
// run starts the node.
func (n *node) run(serverEndpoint interface{}, dMoment time.Time) {
// Run network.
- if err := n.netModule.setup(serverEndpoint); err != nil {
+ if err := n.netModule.Setup(serverEndpoint); err != nil {
panic(err)
}
- msgChannel := n.netModule.receiveChanForNode()
- peers := n.netModule.peers()
- go n.netModule.run()
+ msgChannel := n.netModule.ReceiveChanForNode()
+ peers := n.netModule.Peers()
+ go n.netModule.Run()
n.gov.setNetwork(n.netModule)
// Run consensus.
hashes := make(common.Hashes, 0, len(peers))
@@ -134,7 +166,7 @@ MainLoop:
if err := n.db.Close(); err != nil {
fmt.Println(err)
}
- n.netModule.report(&message{
+ n.netModule.Report(&message{
Type: shutdownAck,
})
// TODO(mission): once we have a way to know if consensus is stopped, stop