github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulations/overlay.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  //
    26  //
    27  //
    28  package main
    29  
    30  import (
    31  	"flag"
    32  	"fmt"
    33  	"net/http"
    34  	"runtime"
    35  	"sync"
    36  	"time"
    37  
    38  	"github.com/ethereum/go-ethereum/log"
    39  	"github.com/ethereum/go-ethereum/node"
    40  	"github.com/ethereum/go-ethereum/p2p/discover"
    41  	"github.com/ethereum/go-ethereum/p2p/simulations"
    42  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    43  	"github.com/ethereum/go-ethereum/swarm/network"
    44  	"github.com/ethereum/go-ethereum/swarm/state"
    45  	colorable "github.com/mattn/go-colorable"
    46  )
    47  
    48  var (
    49  	noDiscovery = flag.Bool("no-discovery", false, "disable discovery (useful if you want to load a snapshot)")
    50  	vmodule     = flag.String("vmodule", "", "log filters for logger via Vmodule")
    51  	verbosity   = flag.Int("verbosity", 0, "log filters for logger via Vmodule")
    52  	httpSimPort = 8888
    53  )
    54  
    55  func init() {
    56  	flag.Parse()
    57  //
    58  //
    59  //
    60  //
    61  //
    62  	if *vmodule != "" {
    63  //
    64  		glogger := log.NewGlogHandler(log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))
    65  		if *verbosity > 0 {
    66  			glogger.Verbosity(log.Lvl(*verbosity))
    67  		}
    68  		glogger.Vmodule(*vmodule)
    69  		log.Root().SetHandler(glogger)
    70  	}
    71  }
    72  
    73  type Simulation struct {
    74  	mtx    sync.Mutex
    75  	stores map[discover.NodeID]*state.InmemoryStore
    76  }
    77  
    78  func NewSimulation() *Simulation {
    79  	return &Simulation{
    80  		stores: make(map[discover.NodeID]*state.InmemoryStore),
    81  	}
    82  }
    83  
    84  func (s *Simulation) NewService(ctx *adapters.ServiceContext) (node.Service, error) {
    85  	id := ctx.Config.ID
    86  	s.mtx.Lock()
    87  	store, ok := s.stores[id]
    88  	if !ok {
    89  		store = state.NewInmemoryStore()
    90  		s.stores[id] = store
    91  	}
    92  	s.mtx.Unlock()
    93  
    94  	addr := network.NewAddrFromNodeID(id)
    95  
    96  	kp := network.NewKadParams()
    97  	kp.MinProxBinSize = 2
    98  	kp.MaxBinSize = 4
    99  	kp.MinBinSize = 1
   100  	kp.MaxRetries = 1000
   101  	kp.RetryExponent = 2
   102  	kp.RetryInterval = 1000000
   103  	kad := network.NewKademlia(addr.Over(), kp)
   104  	hp := network.NewHiveParams()
   105  	hp.Discovery = !*noDiscovery
   106  	hp.KeepAliveInterval = 300 * time.Millisecond
   107  
   108  	config := &network.BzzConfig{
   109  		OverlayAddr:  addr.Over(),
   110  		UnderlayAddr: addr.Under(),
   111  		HiveParams:   hp,
   112  	}
   113  
   114  	return network.NewBzz(config, kad, store, nil, nil), nil
   115  }
   116  
   117  //
   118  func newSimulationNetwork() *simulations.Network {
   119  
   120  	s := NewSimulation()
   121  	services := adapters.Services{
   122  		"overlay": s.NewService,
   123  	}
   124  	adapter := adapters.NewSimAdapter(services)
   125  	simNetwork := simulations.NewNetwork(adapter, &simulations.NetworkConfig{
   126  		DefaultService: "overlay",
   127  	})
   128  	return simNetwork
   129  }
   130  
   131  //
   132  func newOverlaySim(sim *simulations.Network) *simulations.Server {
   133  	return simulations.NewServer(sim)
   134  }
   135  
   136  //
   137  func main() {
   138  //
   139  	runtime.GOMAXPROCS(runtime.NumCPU())
   140  //
   141  	runOverlaySim()
   142  }
   143  
   144  func runOverlaySim() {
   145  //
   146  	net := newSimulationNetwork()
   147  //
   148  	sim := newOverlaySim(net)
   149  	log.Info(fmt.Sprintf("starting simulation server on 0.0.0.0:%d...", httpSimPort))
   150  //
   151  	http.ListenAndServe(fmt.Sprintf(":%d", httpSimPort), sim)
   152  }