forked from iotaledger/autopeering-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (155 loc) · 4.83 KB
/
main.go
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
package main
import (
"log"
"math/rand"
"net"
"sync"
"time"
"github.com/iotaledger/autopeering-sim/simulation"
"github.com/iotaledger/autopeering-sim/simulation/config"
"github.com/iotaledger/autopeering-sim/simulation/transport"
"github.com/iotaledger/autopeering-sim/simulation/visualizer"
"github.com/iotaledger/hive.go/autopeering/peer"
"github.com/iotaledger/hive.go/autopeering/selection"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/identity"
"github.com/iotaledger/hive.go/logger"
"github.com/spf13/viper"
)
var (
nodeMap map[identity.ID]simulation.Node
srv *visualizer.Server
closing chan struct{}
wg sync.WaitGroup
)
// dummyDiscovery is a dummy implementation of DiscoveryProtocol never returning any verified peers.
type dummyDiscovery struct{}
func (d dummyDiscovery) IsVerified(identity.ID, net.IP) bool { return true }
func (d dummyDiscovery) EnsureVerified(*peer.Peer) error { return nil }
func (d dummyDiscovery) SendPing(string, identity.ID) <-chan error { panic("not implemented") }
func (d dummyDiscovery) GetVerifiedPeer(id identity.ID) *peer.Peer { return nodeMap[id].Peer() }
func (d dummyDiscovery) GetVerifiedPeers() []*peer.Peer { return getAllPeers() }
var discover = &dummyDiscovery{}
func getAllPeers() []*peer.Peer {
result := make([]*peer.Peer, 0, len(nodeMap))
for _, node := range nodeMap {
result = append(result, node.Peer())
}
return result
}
func runSim() {
log.Println("Run simulation with the following parameters:")
config.PrintConfig()
selection.SetParameters(selection.Parameters{
SaltLifetime: config.SaltLifetime(),
OutboundUpdateInterval: 200 * time.Millisecond, // use exactly the same update time as previously
})
closure := events.NewClosure(func(ev *selection.PeeringEvent) {
if ev.Status {
log.Printf("Peering: %s<->%s\n", ev.Self.String(), ev.Peer.ID())
}
})
selection.Events.OutgoingPeering.Attach(closure)
defer selection.Events.OutgoingPeering.Detach(closure)
//lambda := (float64(N) / SaltLifetime.Seconds()) * 10
initialSalt := 0.
log.Println("Creating peers ...")
netw := transport.NewNetwork()
nodeMap = make(map[identity.ID]simulation.Node, config.NumberNodes())
for i := 0; i < config.NumberNodes(); i++ {
node := simulation.NewNode(transport.PeerID(i), time.Duration(initialSalt)*time.Second, netw, config.DropOnUpdate(), discover)
nodeMap[node.ID()] = node
if config.VisEnabled() {
visualizer.AddNode(node.ID().String())
}
// initialSalt = initialSalt + (1 / lambda) // constant rate
// initialSalt = initialSalt + rand.ExpFloat64()/lambda // poisson process
initialSalt = rand.Float64() * config.SaltLifetime().Seconds() // random
}
log.Println("Creating peers ... done")
analysis := simulation.NewLinkAnalysis(nodeMap)
if config.VisEnabled() {
statVisualizer()
}
log.Println("Starting peering ...")
analysis.Start()
for _, node := range nodeMap {
node.Start()
}
log.Println("Starting peering ... done")
log.Println("Running ...")
time.Sleep(config.Duration())
log.Println("Stopping peering ...")
for _, node := range nodeMap {
node.Stop()
}
analysis.Stop()
if config.VisEnabled() {
stopServer()
}
log.Println("Stopping peering ... done")
// Start finalize simulation result
linkAnalysis := simulation.LinksToString(analysis.Links())
err := simulation.WriteCSV(linkAnalysis, "linkAnalysis", []string{"X", "Y"})
if err != nil {
log.Fatalln("error writing csv:", err)
}
// log.Println(linkAnalysis)
convAnalysis := simulation.ConvergenceToString()
err = simulation.WriteCSV(convAnalysis, "convAnalysis", []string{"X", "Y"})
if err != nil {
log.Fatalln("error writing csv:", err)
}
msgAnalysis := simulation.MessagesToString(nodeMap, analysis.Status())
err = simulation.WriteCSV(msgAnalysis, "msgAnalysis", []string{"ID", "OUT", "ACC", "REJ", "IN", "DROP"})
if err != nil {
log.Fatalln("error writing csv:", err)
}
err = simulation.WriteAdjlist(nodeMap, "adjlist")
if err != nil {
log.Fatalln("error writing adjlist:", err)
}
}
func main() {
config.Load()
if err := logger.InitGlobalLogger(viper.GetViper()); err != nil {
panic(err)
}
if config.VisEnabled() {
startServer()
}
runSim()
}
func startServer() {
srv = visualizer.NewServer()
closing = make(chan struct{})
wg.Add(1)
go func() {
defer wg.Done()
srv.Run()
}()
log.Println("Server started; visit http://localhost:8844 to start")
<-srv.Start
}
func stopServer() {
close(closing)
srv.Close()
wg.Wait()
}
func statVisualizer() {
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-closing:
return
case <-ticker.C:
visualizer.UpdateConvergence(simulation.RecordConv.GetConvergence())
visualizer.UpdateAvgNeighbors(simulation.RecordConv.GetAvgNeighbors())
}
}
}()
}