diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-07-20 09:52:52 +0800 |
---|---|---|
committer | missionliao <38416648+missionliao@users.noreply.github.com> | 2018-07-20 09:52:52 +0800 |
commit | dfa19fc2b4e38097334f4a30e159f9bcd92909c0 (patch) | |
tree | 718524746a0871a07a4a68dea43481e6ac841b39 /simulation/simulation.go | |
parent | 46a84bff9ac419853550f7c17b13fb8633e507c9 (diff) | |
download | dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar.gz dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar.bz2 dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar.lz dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar.xz dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.tar.zst dexon-consensus-dfa19fc2b4e38097334f4a30e159f9bcd92909c0.zip |
Implement simulation on a real network (#5)
simulation: implement simulation on a real network
Diffstat (limited to 'simulation/simulation.go')
-rw-r--r-- | simulation/simulation.go | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/simulation/simulation.go b/simulation/simulation.go index 8708293..a219d61 100644 --- a/simulation/simulation.go +++ b/simulation/simulation.go @@ -21,37 +21,57 @@ import ( "fmt" "github.com/dexon-foundation/dexon-consensus-core/common" + "github.com/dexon-foundation/dexon-consensus-core/core" "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/dexon-foundation/dexon-consensus-core/simulation/config" ) // Run starts the simulation. func Run(configPath string) { - config, err := config.Read(configPath) + cfg, err := config.Read(configPath) if err != nil { panic(err) } - networkModel := &NormalNetwork{ - Sigma: config.Networking.Sigma, - Mean: config.Networking.Mean, - LossRateValue: config.Networking.LossRateValue, - } - network := NewNetwork(networkModel) + networkType := cfg.Networking.Type - var vs []*Validator - for i := 0; i < config.Validator.Num; i++ { - id := types.ValidatorID(common.NewRandomHash()) - vs = append(vs, NewValidator(id, config.Validator, network, nil)) - } + if networkType == config.NetworkTypeFake || + networkType == config.NetworkTypeTCPLocal { - for i := 0; i < config.Validator.Num; i++ { - vs[i].Bootstrap(vs) - } + var vs []*Validator + var network core.Network + + if networkType == config.NetworkTypeFake { + networkModel := &NormalNetwork{ + Sigma: cfg.Networking.Sigma, + Mean: cfg.Networking.Mean, + LossRateValue: cfg.Networking.LossRateValue, + } + network = NewFakeNetwork(networkModel) + + for i := 0; i < cfg.Validator.Num; i++ { + id := types.ValidatorID{Hash: common.NewRandomHash()} + vs = append(vs, NewValidator(id, cfg.Validator, network, nil)) + } + } else if networkType == config.NetworkTypeTCPLocal { + for i := 0; i < cfg.Validator.Num; i++ { + id := types.ValidatorID{Hash: common.NewRandomHash()} + network := NewTCPNetwork(true, cfg.Networking.PeerServer) + go network.Start() + vs = append(vs, NewValidator(id, cfg.Validator, network, nil)) + } + } - for i := 0; i < config.Validator.Num; i++ { - fmt.Printf("Validator %d: %s\n", i, vs[i].ID) - go vs[i].Run() + for i := 0; i < cfg.Validator.Num; i++ { + fmt.Printf("Validator %d: %s\n", i, vs[i].ID) + go vs[i].Run() + } + } else if networkType == config.NetworkTypeTCP { + id := types.ValidatorID{Hash: common.NewRandomHash()} + network := NewTCPNetwork(false, cfg.Networking.PeerServer) + go network.Start() + v := NewValidator(id, cfg.Validator, network, nil) + go v.Run() } select {} |