-
Notifications
You must be signed in to change notification settings - Fork 199
/
main.go
160 lines (132 loc) · 3.5 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
package main
import (
"context"
"crypto/ecdsa"
"crypto/rand"
"fmt"
"time"
"github.com/btcsuite/btcd/btcec"
"github.com/libp2p/go-libp2p"
libp2pCrypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
)
type messenger struct {
host host.Host
pb *pubsub.PubSub
topic *pubsub.Topic
subscr *pubsub.Subscription
}
func newMessenger() *messenger {
address := "/ip4/0.0.0.0/tcp/0"
opts := []libp2p.Option{
libp2p.ListenAddrStrings(address),
libp2p.Identity(createP2PPrivKey()),
libp2p.DefaultMuxers,
libp2p.DefaultSecurity,
libp2p.DefaultTransports,
//we need the disable relay option in order to save the node's bandwidth as much as possible
libp2p.DisableRelay(),
libp2p.NATPortMap(),
}
h, _ := libp2p.New(context.Background(), opts...)
optsPS := []pubsub.Option{
pubsub.WithMessageSigning(true),
}
pb, _ := pubsub.NewGossipSub(context.Background(), h, optsPS...)
return &messenger{
host: h,
pb: pb,
}
}
func createP2PPrivKey() *libp2pCrypto.Secp256k1PrivateKey {
prvKey, _ := ecdsa.GenerateKey(btcec.S256(), rand.Reader)
return (*libp2pCrypto.Secp256k1PrivateKey)(prvKey)
}
func (m *messenger) connectTo(target *messenger) {
addr := peer.AddrInfo{
ID: target.host.ID(),
Addrs: target.host.Addrs(),
}
err := m.host.Connect(context.Background(), addr)
if err != nil {
fmt.Println("error connecting to peer: " + err.Error())
}
}
func (m *messenger) joinTopic(topic string) {
m.topic, _ = m.pb.Join(topic)
m.subscr, _ = m.topic.Subscribe()
go func() {
for {
msg, err := m.subscr.Next(context.Background())
if err != nil {
return
}
fmt.Printf("%s: got message %s\n", m.host.ID().Pretty(), string(msg.Data))
}
}()
}
func main() {
fmt.Println("creating 8 host connected statically...")
peers := create8ConnectedPeers()
defer func() {
for _, p := range peers {
_ = p.host.Close()
}
}()
fmt.Println()
for _, p := range peers {
p.joinTopic("test")
}
go func() {
time.Sleep(time.Second * 2)
//TODO uncomment these 2 lines to make the pubsub create connections
//peers[3].subscr.Cancel()
//_ = peers[3].topic.Close()
}()
for i := 0; i < 10; i++ {
printConnections(peers)
fmt.Println()
time.Sleep(time.Second)
}
}
func printConnections(peers []*messenger) {
for _, p := range peers {
fmt.Printf(" %s is connected to %d peers\n", p.host.ID().Pretty(), len(p.host.Network().Peers()))
}
}
// create8ConnectedPeers assembles a network as following:
//
// 0------------------- 1
// | |
// 2 ------------------ 3 ------------------ 4
// | | |
// 5 6 7
func create8ConnectedPeers() []*messenger {
peers := make([]*messenger, 0)
for i := 0; i < 8; i++ {
p := newMessenger()
fmt.Printf("%d - created peer %s\n", i, p.host.ID().Pretty())
peers = append(peers, p)
}
connections := map[int][]int{
0: {1, 3},
1: {4},
2: {5, 3},
3: {4, 6},
4: {7},
}
createConnections(peers, connections)
return peers
}
func createConnections(peers []*messenger, connections map[int][]int) {
for pid, connectTo := range connections {
connectPeerToOthers(peers, pid, connectTo)
}
}
func connectPeerToOthers(peers []*messenger, idx int, connectToIdxes []int) {
for _, connectToIdx := range connectToIdxes {
peers[idx].connectTo(peers[connectToIdx])
}
}