github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/swarm/network/simulations/overlay.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // You can run this simulation using 18 // 19 // go run ./swarm/network/simulations/overlay.go 20 package main 21 22 import ( 23 "flag" 24 "fmt" 25 "net/http" 26 "runtime" 27 "sync" 28 "time" 29 30 "github.com/ShyftNetwork/go-empyrean/log" 31 "github.com/ShyftNetwork/go-empyrean/node" 32 "github.com/ShyftNetwork/go-empyrean/p2p/enode" 33 "github.com/ShyftNetwork/go-empyrean/p2p/simulations" 34 "github.com/ShyftNetwork/go-empyrean/p2p/simulations/adapters" 35 "github.com/ShyftNetwork/go-empyrean/swarm/network" 36 "github.com/ShyftNetwork/go-empyrean/swarm/state" 37 colorable "github.com/mattn/go-colorable" 38 ) 39 40 var ( 41 noDiscovery = flag.Bool("no-discovery", false, "disable discovery (useful if you want to load a snapshot)") 42 vmodule = flag.String("vmodule", "", "log filters for logger via Vmodule") 43 verbosity = flag.Int("verbosity", 0, "log filters for logger via Vmodule") 44 httpSimPort = 8888 45 ) 46 47 func init() { 48 flag.Parse() 49 //initialize the logger 50 //this is a demonstration on how to use Vmodule for filtering logs 51 //provide -vmodule as param, and comma-separated values, e.g.: 52 //-vmodule overlay_test.go=4,simulations=3 53 //above examples sets overlay_test.go logs to level 4, while packages ending with "simulations" to 3 54 if *vmodule != "" { 55 //only enable the pattern matching handler if the flag has been provided 56 glogger := log.NewGlogHandler(log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))) 57 if *verbosity > 0 { 58 glogger.Verbosity(log.Lvl(*verbosity)) 59 } 60 glogger.Vmodule(*vmodule) 61 log.Root().SetHandler(glogger) 62 } 63 } 64 65 type Simulation struct { 66 mtx sync.Mutex 67 stores map[enode.ID]state.Store 68 } 69 70 func NewSimulation() *Simulation { 71 return &Simulation{ 72 stores: make(map[enode.ID]state.Store), 73 } 74 } 75 76 func (s *Simulation) NewService(ctx *adapters.ServiceContext) (node.Service, error) { 77 node := ctx.Config.Node() 78 s.mtx.Lock() 79 store, ok := s.stores[node.ID()] 80 if !ok { 81 store = state.NewInmemoryStore() 82 s.stores[node.ID()] = store 83 } 84 s.mtx.Unlock() 85 86 addr := network.NewAddr(node) 87 88 kp := network.NewKadParams() 89 kp.NeighbourhoodSize = 2 90 kp.MaxBinSize = 4 91 kp.MinBinSize = 1 92 kp.MaxRetries = 1000 93 kp.RetryExponent = 2 94 kp.RetryInterval = 1000000 95 kad := network.NewKademlia(addr.Over(), kp) 96 hp := network.NewHiveParams() 97 hp.Discovery = !*noDiscovery 98 hp.KeepAliveInterval = 300 * time.Millisecond 99 100 config := &network.BzzConfig{ 101 OverlayAddr: addr.Over(), 102 UnderlayAddr: addr.Under(), 103 HiveParams: hp, 104 } 105 106 return network.NewBzz(config, kad, store, nil, nil), nil 107 } 108 109 //create the simulation network 110 func newSimulationNetwork() *simulations.Network { 111 112 s := NewSimulation() 113 services := adapters.Services{ 114 "overlay": s.NewService, 115 } 116 adapter := adapters.NewSimAdapter(services) 117 simNetwork := simulations.NewNetwork(adapter, &simulations.NetworkConfig{ 118 DefaultService: "overlay", 119 }) 120 return simNetwork 121 } 122 123 //return a new http server 124 func newOverlaySim(sim *simulations.Network) *simulations.Server { 125 return simulations.NewServer(sim) 126 } 127 128 // var server 129 func main() { 130 //cpu optimization 131 runtime.GOMAXPROCS(runtime.NumCPU()) 132 //run the sim 133 runOverlaySim() 134 } 135 136 func runOverlaySim() { 137 //create the simulation network 138 net := newSimulationNetwork() 139 //create a http server with it 140 sim := newOverlaySim(net) 141 log.Info(fmt.Sprintf("starting simulation server on 0.0.0.0:%d...", httpSimPort)) 142 //start the HTTP server 143 http.ListenAndServe(fmt.Sprintf(":%d", httpSimPort), sim) 144 }