-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
469 lines (372 loc) · 12.3 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package main
import (
"context"
"crypto/rand"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
mrand "math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
//discovery "github.com/libp2p/go-libp2p-discovery"
//peerstore "github.com/libp2p/go-libp2p/p2p/host/peerstore"
"github.com/multiformats/go-multiaddr"
)
type Config struct {
NodeType string
IP string
ParcelSize int
Port int
ProtocolID string
Rendezvous string
Seed int64
DiscoveryPeers addrList
Debug bool
ExperimentDuration int
}
type Stats struct {
// Operations
BlockIDs []string
ParcelKeyHashes []string
ParcelStatuses []string
ParcelDataLengths []int
PutTimestamps []time.Time
PutLatencies []time.Duration
GetTimestamps []time.Time
GetLatencies []time.Duration
GetHops []int
// Total Stats
TotalPutMessages int
TotalFailedPuts int
TotalSuccessPuts int
TotalGetMessages int
TotalFailedGets int
TotalSuccessGets int
// Latencies
SeedingLatencies []time.Duration
RowSamplingLatencies []time.Duration
ColSamplingLatencies []time.Duration
RandomSamplingLatencies []time.Duration
TotalSamplingLatencies []time.Duration
}
var config Config
func main() {
// Turn on/off logging messages in stdout
// log.SetOutput(ioutil.Discard)
log.SetOutput(os.Stdout)
stats := &Stats{}
// flag.StringVar(&config.Rendezvous, "rendezvous", "/das", "")
flag.StringVar(&config.NodeType, "nodeType", "validator", "The node type to run (validator, nonvalidator, builder)")
flag.IntVar(&config.ParcelSize, "parcelSize", 512, "The size of the parcels to send - make sure 512 divides evenly by this number")
flag.Int64Var(&config.Seed, "seed", 0, "Seed value for generating a PeerID, 0 is random")
flag.Var(&config.DiscoveryPeers, "peer", "Peer multiaddress for peer discovery")
flag.StringVar(&config.ProtocolID, "protocolid", "/p2p/rpc", "")
flag.IntVar(&config.Port, "port", 0, "")
flag.IntVar(&config.ExperimentDuration, "duration", 30, "Experiment duration (in seconds).")
flag.StringVar(&config.IP, "ip", "127.0.0.1", "IP address of this machine.")
flag.Parse()
const builder_id = "12D3KooWE3AwZFT9zEWDUxhya62hmvEbRxYBWaosn7Kiqw5wsu73"
nodeType := strings.ToLower(config.NodeType)
nodeTypeSuffix := ""
if nodeType == "builder" {
nodeTypeSuffix = "B"
} else if nodeType == "validator" {
nodeTypeSuffix = "V"
} else {
nodeTypeSuffix = "R"
}
// h, dht, err := NewHost(context.Background(), config.Seed, config.Port, nodeType)
// if err != nil {
// log.Fatal(err)
// }
var r io.Reader
var crypto_code int
if config.Seed == 0 {
r = rand.Reader
crypto_code = crypto.RSA
} else {
r = mrand.New(mrand.NewSource(config.Seed))
crypto_code = crypto.Ed25519
}
priv, _, err := crypto.GenerateKeyPairWithReader(crypto_code, 2048, r)
if err != nil {
log.Fatal(err)
}
addr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", config.IP, config.Port))
h, err := libp2p.New(
libp2p.ListenAddrs(addr),
libp2p.Identity(priv),
)
if err != nil {
log.Fatal(err)
}
dht, err := NewDHT(context.Background(), h, nodeType)
if err != nil {
log.Fatal(err)
}
//h.Peerstore().AddAddrs(dht.Host().ID(), dht.Host().Addrs(), peerstore.PermanentAddrTTL)
//routingDiscovery := discovery.NewRoutingDiscovery(dht)
//discovery.Advertise(context.Background(), routingDiscovery, "das")
// Register an event handler for peer connection
h.Network().Notify(&network.NotifyBundle{
ConnectedF: func(n network.Network, c network.Conn) {
if c.LocalPeer().String() == builder_id {
node_suffix := ""
if config.NodeType == "builder" {
node_suffix = "B"
} else if config.NodeType == "validator" {
node_suffix = "V"
} else {
node_suffix = "R"
}
remote_peer_id := c.RemotePeer()
routingTablePeerCountBefore := len(dht.RoutingTable().ListPeers())
dht.RoutingTable().TryAddPeer(remote_peer_id, false, false)
routingTablePeerCountAfter := len(dht.RoutingTable().ListPeers())
if routingTablePeerCountBefore == routingTablePeerCountAfter {
log.Printf("[%s - %s]: Failed to add peer %s to routing table\n", node_suffix, h.ID()[0:5], remote_peer_id[:5])
} else {
log.Printf(
"[%s - %s]: Peer %s connected to builder (%d -> %d connections)\n",
node_suffix,
h.ID()[0:5],
remote_peer_id[:5],
routingTablePeerCountBefore,
routingTablePeerCountAfter,
)
}
}
},
})
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
if nodeType == "builder" {
log.Printf("[B - %s] Builder started: %s\n", h.ID()[:5], h.ID())
} else {
wg.Add(1)
go waitForBuilder(&wg, config.DiscoveryPeers, h, dht)
wg.Wait()
log.Printf("[%s - %s] Peer started: %s\n", nodeTypeSuffix, h.ID()[:5], h.ID()[:5])
}
service := NewService(h, protocol.ID(config.ProtocolID))
err = service.SetupRPC()
if err != nil {
log.Fatal(err)
}
service.StartMessaging(h, dht, stats, nodeType, config.ParcelSize, ctx, config.ExperimentDuration)
if filename, err := writeOperationsToFile(stats, h, nodeType); err != nil {
log.Fatal(err)
} else {
log.Printf("[%s - %s] Operations written to %s\n", nodeTypeSuffix, h.ID()[0:5], filename)
}
if filename, err := writeTotalStatsToFile(stats, h, nodeType); err != nil {
log.Fatal(err)
} else {
log.Printf("[%s - %s] Total Stats written to %s\n", nodeTypeSuffix, h.ID()[0:5], filename)
}
if filename, err := writeLatencyStatsToFile(stats, h, nodeType); err != nil {
log.Fatal(err)
} else {
log.Printf("[%s - %s] Latencies written to %s\n", nodeTypeSuffix, h.ID()[0:5], filename)
}
cancel()
}
func writeTotalStatsToFile(stats *Stats, h host.Host, nodeType string) (string, error) {
filename := h.ID().String()[0:10] + "_total_stats_" + nodeType + ".csv"
f, err := os.Create(filename)
if err != nil {
return filename, err
}
defer f.Close()
w := csv.NewWriter(f)
defer w.Flush()
headers := []string{"Total PUT messages", "Total failed PUTs", "Total successful PUTs", "Total GET messages", "Total failed GETs", "Total successful GETs"}
rows := [][]string{
{strconv.Itoa(stats.TotalPutMessages), strconv.Itoa(stats.TotalFailedPuts), strconv.Itoa(stats.TotalSuccessPuts), strconv.Itoa(stats.TotalGetMessages), strconv.Itoa(stats.TotalFailedGets), strconv.Itoa(stats.TotalSuccessGets)},
}
// Write headers and rows to CSV file
w.Write(headers)
w.WriteAll(rows)
if err := w.Error(); err != nil {
return filename, err
}
return filename, nil
}
func writeOperationsToFile(stats *Stats, h host.Host, nodeType string) (string, error) {
filename := h.ID().String()[0:10] + "_operations_" + nodeType + ".csv"
// Convert latencies and hops to rows
var operationRows [][]string
for i := 0; i < len(stats.BlockIDs) || i < len(stats.ParcelKeyHashes) || i < len(stats.ParcelStatuses) || i < len(stats.ParcelDataLengths) || i < len(stats.PutTimestamps) || i < len(stats.GetTimestamps) || i < len(stats.GetHops) || i < len(stats.PutLatencies) || i < len(stats.GetLatencies); i++ {
var row []string
if i < len(stats.BlockIDs) {
row = append(row, stats.BlockIDs[i])
} else {
row = append(row, "")
}
if i < len(stats.ParcelKeyHashes) {
row = append(row, stats.ParcelKeyHashes[i])
} else {
row = append(row, "")
}
if i < len(stats.ParcelStatuses) {
row = append(row, stats.ParcelStatuses[i])
} else {
row = append(row, "")
}
if i < len(stats.ParcelDataLengths) {
row = append(row, strconv.Itoa(stats.ParcelDataLengths[i]))
} else {
row = append(row, "")
}
if i < len(stats.PutTimestamps) {
row = append(row, stats.PutTimestamps[i].String())
} else {
row = append(row, "")
}
if i < len(stats.PutLatencies) {
row = append(row, strconv.FormatInt(stats.PutLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.GetTimestamps) {
row = append(row, stats.GetTimestamps[i].String())
} else {
row = append(row, "")
}
if i < len(stats.GetLatencies) {
row = append(row, strconv.FormatInt(stats.GetLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.GetHops) {
row = append(row, strconv.Itoa(stats.GetHops[i]))
} else {
row = append(row, "")
}
operationRows = append(operationRows, row)
}
// Write latency stats to CSV file
f, err := os.Create(filename)
if err != nil {
return filename, err
}
defer f.Close()
w := csv.NewWriter(f)
defer w.Flush()
headers := []string{"Block ID", "Parcel Key Hashes", "Parcel Status", "Parcel Data Length (Bytes)", "PUT timestamps", "PUT latencies", "GET timestamps", "GET latencies", "GET hops"}
rows := operationRows
// Write headers and rows to CSV file
w.Write(headers)
w.WriteAll(rows)
if err := w.Error(); err != nil {
return filename, err
}
return filename, nil
}
func writeLatencyStatsToFile(stats *Stats, h host.Host, nodeType string) (string, error) {
filename := h.ID().String()[0:10] + "_latency_stats_" + nodeType + ".csv"
// Convert latencies and hops to rows
var latencyRows [][]string
for i := 0; i < len(stats.SeedingLatencies) || i < len(stats.RowSamplingLatencies) || i < len(stats.ColSamplingLatencies) || i < len(stats.RandomSamplingLatencies) || i < len(stats.TotalSamplingLatencies); i++ {
var row []string
if i < len(stats.SeedingLatencies) {
row = append(row, strconv.FormatInt(stats.SeedingLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.RowSamplingLatencies) {
row = append(row, strconv.FormatInt(stats.RowSamplingLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.ColSamplingLatencies) {
row = append(row, strconv.FormatInt(stats.ColSamplingLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.RandomSamplingLatencies) {
row = append(row, strconv.FormatInt(stats.RandomSamplingLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
if i < len(stats.TotalSamplingLatencies) {
row = append(row, strconv.FormatInt(stats.TotalSamplingLatencies[i].Microseconds(), 10))
} else {
row = append(row, "")
}
latencyRows = append(latencyRows, row)
}
// Write latency stats to CSV file
f, err := os.Create(filename)
if err != nil {
return filename, err
}
defer f.Close()
w := csv.NewWriter(f)
defer w.Flush()
headers := []string{"Seeding Latency (us)", "Row Sampling Latency (us)", "Col Sampling Latency (us)", "Random Sampling Latency (us)", "Total Sampling Latency (us)"}
rows := latencyRows
// Write headers and rows to CSV file
w.Write(headers)
w.WriteAll(rows)
if err := w.Error(); err != nil {
return filename, err
}
return filename, nil
}
type addrList []multiaddr.Multiaddr
func (al *addrList) String() string {
strs := make([]string, len(*al))
for i, addr := range *al {
strs[i] = addr.String()
}
return strings.Join(strs, ",")
}
func (al *addrList) Set(value string) error {
addr, err := multiaddr.NewMultiaddr(value)
if err != nil {
return err
}
*al = append(*al, addr)
return nil
}
func waitForBuilder(wg *sync.WaitGroup, discoveryPeers addrList, h host.Host, dht *dht.IpfsDHT) {
defer wg.Done()
// ? Wait for a couple of seconds to make sure bootstrap peer is up and running
time.Sleep(2 * time.Second)
// ? Timeout of 10 seconds to connect to bootstrap peer
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// ? Connect to bootstrap peers
for _, peerAddr := range discoveryPeers {
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
if err := h.Connect(ctx, *peerinfo); err != nil {
log.Print()
log.Printf("Error connecting to bootstrap node %q: %-v", peerinfo, err)
log.Printf("peerinfo: %s\n", peerinfo)
log.Printf("peerinfo.ID: %s\n", peerinfo.ID)
log.Printf("peerinfo.Addrs: %s\n", peerinfo.Addrs)
log.Printf("err: %s\n", err)
log.Print()
} else {
if _, err := dht.FindPeer(ctx, peerinfo.ID); err != nil {
log.Printf("Error finding peer: %s\n", err)
} else {
return
}
}
}
log.Printf("Could not connect to any bootstrap nodes...")
}