forked from libp2p/go-libp2p-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (90 loc) · 2.7 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
package main
import (
"bytes"
"context"
"fmt"
"time"
"github.com/adiu/go-libp2p-examples/helper"
"github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p/p2p/host/basic"
"github.com/multiformats/go-multiaddr"
)
type Node struct {
Host host.Host
Routing *dht.IpfsDHT
}
func NewLocalNode(port int) *Node {
h, _ := basichost.NewHost(context.Background(), helper.GenSwarm(), &basichost.HostOpts{})
maddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
if err != nil {
panic(err)
}
h.Network().Listen(maddr)
h.Peerstore().AddAddrs(h.ID(), []multiaddr.Multiaddr{maddr}, peerstore.ProviderAddrTTL)
d, _ := dht.New(context.Background(), h)
return &Node{h, d}
}
func (self *Node) Close() {
self.Routing.Close()
self.Host.Close()
}
func (self *Node) Bootstrap(ctx context.Context) error {
return self.Routing.Bootstrap(ctx)
}
func (self *Node) Connect(ctx context.Context, bb *Node) {
a, b := self.Host, bb.Host
idB := b.ID()
addrB := b.Network().Peerstore().Addrs(idB)
if len(addrB) == 0 {
fmt.Println("peers setup incorrectly: no local address")
}
a.Peerstore().AddAddrs(idB, addrB, peerstore.TempAddrTTL)
pi := peerstore.PeerInfo{ID: idB}
if err := a.Connect(ctx, pi); err != nil {
fmt.Println(err)
}
}
func showPeerPretty(peers []peer.ID) string {
if len(peers) == 0 {
return ""
}
buf := bytes.NewBufferString("->")
for _, p := range peers {
buf.WriteString(p.Pretty())
buf.WriteString(", ")
}
return buf.String()
}
func main() {
// group 1
n1 := NewLocalNode(40001)
n2 := NewLocalNode(40002)
n3 := NewLocalNode(40003)
// group 2
n4 := NewLocalNode(40004)
n5 := NewLocalNode(40005)
defer func() {
n1.Close()
n2.Close()
n3.Close()
n4.Close()
n5.Close()
}()
n1.Connect(context.Background(), n2)
n2.Connect(context.Background(), n3)
n3.Connect(context.Background(), n4)
n4.Connect(context.Background(), n5)
fmt.Println(1, n1.Host.Network().ListenAddresses(), showPeerPretty(n1.Host.Peerstore().Peers()))
fmt.Println(2, n2.Host.ID().Pretty(), showPeerPretty(n2.Host.Peerstore().Peers()))
fmt.Println(3, n3.Host.ID().Pretty(), showPeerPretty(n3.Host.Peerstore().Peers()))
fmt.Println(4, n4.Host.ID().Pretty(), showPeerPretty(n4.Host.Peerstore().Peers()))
fmt.Println(5, n5.Host.ID().Pretty(), showPeerPretty(n5.Host.Peerstore().Peers()))
err := n5.Bootstrap(context.Background())
fmt.Println("----> bootstrap:", err)
<-time.After(time.Second * 3)
fmt.Println(4, n4.Host.ID().Pretty(), showPeerPretty(n4.Host.Peerstore().Peers()))
fmt.Println(5, n5.Host.ID().Pretty(), showPeerPretty(n5.Host.Peerstore().Peers()))
}