aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/network/simulations/overlay.go
blob: e9d288e72fea7845bd80b221c1acd12f3c25e73a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// You can run this simulation using
//
//    go run ./swarm/network/simulations/overlay.go
package main

import (
    "flag"
    "fmt"
    "net/http"
    "runtime"
    "sync"
    "time"

    "github.com/dexon-foundation/dexon/log"
    "github.com/dexon-foundation/dexon/node"
    "github.com/dexon-foundation/dexon/p2p/enode"
    "github.com/dexon-foundation/dexon/p2p/simulations"
    "github.com/dexon-foundation/dexon/p2p/simulations/adapters"
    "github.com/dexon-foundation/dexon/swarm/network"
    "github.com/dexon-foundation/dexon/swarm/state"
    colorable "github.com/mattn/go-colorable"
)

var (
    noDiscovery = flag.Bool("no-discovery", false, "disable discovery (useful if you want to load a snapshot)")
    vmodule     = flag.String("vmodule", "", "log filters for logger via Vmodule")
    verbosity   = flag.Int("verbosity", 0, "log filters for logger via Vmodule")
    httpSimPort = 8888
)

func init() {
    flag.Parse()
    //initialize the logger
    //this is a demonstration on how to use Vmodule for filtering logs
    //provide -vmodule as param, and comma-separated values, e.g.:
    //-vmodule overlay_test.go=4,simulations=3
    //above examples sets overlay_test.go logs to level 4, while packages ending with "simulations" to 3
    if *vmodule != "" {
        //only enable the pattern matching handler if the flag has been provided
        glogger := log.NewGlogHandler(log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))
        if *verbosity > 0 {
            glogger.Verbosity(log.Lvl(*verbosity))
        }
        glogger.Vmodule(*vmodule)
        log.Root().SetHandler(glogger)
    }
}

type Simulation struct {
    mtx    sync.Mutex
    stores map[enode.ID]state.Store
}

func NewSimulation() *Simulation {
    return &Simulation{
        stores: make(map[enode.ID]state.Store),
    }
}

func (s *Simulation) NewService(ctx *adapters.ServiceContext) (node.Service, error) {
    node := ctx.Config.Node()
    s.mtx.Lock()
    store, ok := s.stores[node.ID()]
    if !ok {
        store = state.NewInmemoryStore()
        s.stores[node.ID()] = store
    }
    s.mtx.Unlock()

    addr := network.NewAddr(node)

    kp := network.NewKadParams()
    kp.NeighbourhoodSize = 2
    kp.MaxBinSize = 4
    kp.MinBinSize = 1
    kp.MaxRetries = 1000
    kp.RetryExponent = 2
    kp.RetryInterval = 1000000
    kad := network.NewKademlia(addr.Over(), kp)
    hp := network.NewHiveParams()
    hp.Discovery = !*noDiscovery
    hp.KeepAliveInterval = 300 * time.Millisecond

    config := &network.BzzConfig{
        OverlayAddr:  addr.Over(),
        UnderlayAddr: addr.Under(),
        HiveParams:   hp,
    }

    return network.NewBzz(config, kad, store, nil, nil), nil
}

//create the simulation network
func newSimulationNetwork() *simulations.Network {

    s := NewSimulation()
    services := adapters.Services{
        "overlay": s.NewService,
    }
    adapter := adapters.NewSimAdapter(services)
    simNetwork := simulations.NewNetwork(adapter, &simulations.NetworkConfig{
        DefaultService: "overlay",
    })
    return simNetwork
}

//return a new http server
func newOverlaySim(sim *simulations.Network) *simulations.Server {
    return simulations.NewServer(sim)
}

// var server
func main() {
    //cpu optimization
    runtime.GOMAXPROCS(runtime.NumCPU())
    //run the sim
    runOverlaySim()
}

func runOverlaySim() {
    //create the simulation network
    net := newSimulationNetwork()
    //create a http server with it
    sim := newOverlaySim(net)
    log.Info(fmt.Sprintf("starting simulation server on 0.0.0.0:%d...", httpSimPort))
    //start the HTTP server
    http.ListenAndServe(fmt.Sprintf(":%d", httpSimPort), sim)
}