aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/network/simulation/simulation.go
blob: 74f9d98ee93ff071deb2b705caac142a71ceecae (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// 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/>.

package simulation

import (
    "context"
    "errors"
    "net/http"
    "sync"
    "time"

    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/node"
    "github.com/ethereum/go-ethereum/p2p/discover"
    "github.com/ethereum/go-ethereum/p2p/simulations"
    "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
)

// Common errors that are returned by functions in this package.
var (
    ErrNodeNotFound = errors.New("node not found")
    ErrNoPivotNode  = errors.New("no pivot node set")
)

// Simulation provides methods on network, nodes and services
// to manage them.
type Simulation struct {
    // Net is exposed as a way to access lower level functionalities
    // of p2p/simulations.Network.
    Net *simulations.Network

    serviceNames []string
    cleanupFuncs []func()
    buckets      map[discover.NodeID]*sync.Map
    pivotNodeID  *discover.NodeID
    shutdownWG   sync.WaitGroup
    done         chan struct{}
    mu           sync.RWMutex

    httpSrv *http.Server        //attach a HTTP server via SimulationOptions
    handler *simulations.Server //HTTP handler for the server
    runC    chan struct{}       //channel where frontend signals it is ready
}

// ServiceFunc is used in New to declare new service constructor.
// The first argument provides ServiceContext from the adapters package
// giving for example the access to NodeID. Second argument is the sync.Map
// where all "global" state related to the service should be kept.
// All cleanups needed for constructed service and any other constructed
// objects should ne provided in a single returned cleanup function.
// Returned cleanup function will be called by Close function
// after network shutdown.
type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error)

// New creates a new Simulation instance with new
// simulations.Network initialized with provided services.
func New(services map[string]ServiceFunc) (s *Simulation) {
    s = &Simulation{
        buckets: make(map[discover.NodeID]*sync.Map),
        done:    make(chan struct{}),
    }

    adapterServices := make(map[string]adapters.ServiceFunc, len(services))
    for name, serviceFunc := range services {
        s.serviceNames = append(s.serviceNames, name)
        adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
            b := new(sync.Map)
            service, cleanup, err := serviceFunc(ctx, b)
            if err != nil {
                return nil, err
            }
            s.mu.Lock()
            defer s.mu.Unlock()
            if cleanup != nil {
                s.cleanupFuncs = append(s.cleanupFuncs, cleanup)
            }
            s.buckets[ctx.Config.ID] = b
            return service, nil
        }
    }

    s.Net = simulations.NewNetwork(
        adapters.NewSimAdapter(adapterServices),
        &simulations.NetworkConfig{ID: "0"},
    )

    return s
}

// RunFunc is the function that will be called
// on Simulation.Run method call.
type RunFunc func(context.Context, *Simulation) error

// Result is the returned value of Simulation.Run method.
type Result struct {
    Duration time.Duration
    Error    error
}

// Run calls the RunFunc function while taking care of
// cancelation provided through the Context.
func (s *Simulation) Run(ctx context.Context, f RunFunc) (r Result) {
    //if the option is set to run a HTTP server with the simulation,
    //init the server and start it
    start := time.Now()
    if s.httpSrv != nil {
        log.Info("Waiting for frontend to be ready...(send POST /runsim to HTTP server)")
        //wait for the frontend to connect
        select {
        case <-s.runC:
        case <-ctx.Done():
            return Result{
                Duration: time.Since(start),
                Error:    ctx.Err(),
            }
        }
        log.Info("Received signal from frontend - starting simulation run.")
    }
    errc := make(chan error)
    quit := make(chan struct{})
    defer close(quit)
    go func() {
        select {
        case errc <- f(ctx, s):
        case <-quit:
        }
    }()
    var err error
    select {
    case <-ctx.Done():
        err = ctx.Err()
    case err = <-errc:
    }
    return Result{
        Duration: time.Since(start),
        Error:    err,
    }
}

// Maximal number of parallel calls to cleanup functions on
// Simulation.Close.
var maxParallelCleanups = 10

// Close calls all cleanup functions that are returned by
// ServiceFunc, waits for all of them to finish and other
// functions that explicitly block shutdownWG
// (like Simulation.PeerEvents) and shuts down the network
// at the end. It is used to clean all resources from the
// simulation.
func (s *Simulation) Close() {
    close(s.done)

    // Close all connections before calling the Network Shutdown.
    // It is possible that p2p.Server.Stop will block if there are
    // existing connections.
    for _, c := range s.Net.Conns {
        if c.Up {
            s.Net.Disconnect(c.One, c.Other)
        }
    }
    s.shutdownWG.Wait()
    s.Net.Shutdown()

    sem := make(chan struct{}, maxParallelCleanups)
    s.mu.RLock()
    cleanupFuncs := make([]func(), len(s.cleanupFuncs))
    for i, f := range s.cleanupFuncs {
        if f != nil {
            cleanupFuncs[i] = f
        }
    }
    s.mu.RUnlock()
    var cleanupWG sync.WaitGroup
    for _, cleanup := range cleanupFuncs {
        cleanupWG.Add(1)
        sem <- struct{}{}
        go func(cleanup func()) {
            defer cleanupWG.Done()
            defer func() { <-sem }()

            cleanup()
        }(cleanup)
    }
    cleanupWG.Wait()

    if s.httpSrv != nil {
        ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
        defer cancel()
        err := s.httpSrv.Shutdown(ctx)
        if err != nil {
            log.Error("Error shutting down HTTP server!", "err", err)
        }
        close(s.runC)
    }
}

// Done returns a channel that is closed when the simulation
// is closed by Close method. It is useful for signaling termination
// of all possible goroutines that are created within the test.
func (s *Simulation) Done() <-chan struct{} {
    return s.done
}