github.com/codingfuture/orig-energi3@v0.8.4/swarm/network/simulations/overlay.go (about)

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