This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
bootstrap.go
165 lines (146 loc) · 4.55 KB
/
bootstrap.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
// Copyright (C) 2018 MediBloc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package net
import (
"context"
"io/ioutil"
"sync"
"time"
"github.com/gogo/protobuf/proto"
host "github.com/libp2p/go-libp2p-host"
net "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
medletpb "github.com/medibloc/go-medibloc/medlet/pb"
netpb "github.com/medibloc/go-medibloc/net/pb"
"github.com/medibloc/go-medibloc/util/logging"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/sirupsen/logrus"
)
// BootstrapConfig is a struct for bootstrapping node
type BootstrapConfig struct {
MinConnThreshold uint32
Period time.Duration
ConnectionTimeout time.Duration
BootstrapPeers []pstore.PeerInfo // seed host
}
// NewBootstrapConfig return new BootstrapConfig
func NewBootstrapConfig(cfg *medletpb.Config) (bConfig *BootstrapConfig, err error) {
bootstrapPeriod := DefaultBootstrapPeriod
if cfg.Network.BootstrapPeriod != 0 {
bootstrapPeriod = time.Duration(cfg.Network.BootstrapPeriod) * time.Second
}
bConfig = &BootstrapConfig{
MinConnThreshold: cfg.Network.MinimumConnections,
Period: bootstrapPeriod,
ConnectionTimeout: 3 * time.Second,
BootstrapPeers: make([]pstore.PeerInfo, 0),
}
for _, seed := range cfg.Network.Seeds {
pi, err := peerInfoFromProto(seed)
if err != nil {
return nil, err
}
bConfig.BootstrapPeers = append(bConfig.BootstrapPeers, pi)
}
return bConfig, nil
}
// Bootstrap run bootstrap
func (node *Node) Bootstrap() {
cfg := node.bootstrapConfig
connected := node.Network().Peers()
if len(connected) >= int(cfg.MinConnThreshold) {
return
}
node.DHTSync()
bootstrapConnect(node.Host, cfg.BootstrapPeers)
}
func bootstrapConnect(ph host.Host, peers []pstore.PeerInfo) {
logging.Console().WithFields(logrus.Fields{
"current_connections": len(ph.Network().Conns()),
}).Info("Start bootstrap connect")
var wg sync.WaitGroup
for _, p := range peers {
if p.ID == ph.ID() {
continue
}
wg.Add(1)
go func(p pstore.PeerInfo) {
defer wg.Done()
if ph.Network().Connectedness(p.ID) == net.Connected {
return
}
logging.Console().WithFields(logrus.Fields{
"to": p.ID.Pretty(),
"from": ph.ID().Pretty(),
}).Debug("bootstrap connection start")
ph.Peerstore().AddAddrs(p.ID, p.Addrs, pstore.PermanentAddrTTL)
if err := ph.Connect(context.Background(), p); err != nil {
logging.Console().WithFields(logrus.Fields{
"to": p.ID.Pretty(),
"from": ph.ID().Pretty(),
}).Info("bootstrap connection failed")
return
}
logging.Console().WithFields(logrus.Fields{
"to": p.ID.Pretty(),
"from": ph.ID().Pretty(),
}).Info("bootstrap connection succeed")
}(p)
}
wg.Wait()
logging.Console().WithFields(logrus.Fields{
"current_connections": len(ph.Network().Conns()),
}).Info("finishing bootstrap connect")
}
// SaveCache save host's peerstore to cache file
func (node *Node) SaveCache() {
savePeerStoreToCache(node.Peerstore(), node.cacheFile)
}
// savePeerStoreToCache save peerstore to cache file
func savePeerStoreToCache(ps pstore.Peerstore, cacheFile string) {
pbPeers := new(netpb.Peers)
for _, id := range ps.Peers() {
p := ps.PeerInfo(id)
pbPeer := PeerInfoToProto(p)
pbPeers.Peers = append(pbPeers.Peers, pbPeer)
}
str := proto.MarshalTextString(pbPeers)
err := ioutil.WriteFile(cacheFile, []byte(str), 0644)
if err != nil {
logging.WithFields(logrus.Fields{
"cacheFile": cacheFile,
"err": err,
}).Warn("failed to save peers to cache file")
return
}
}
func peerInfoFromProto(pb *netpb.PeerInfo) (pstore.PeerInfo, error) {
pid, err := peer.IDB58Decode(pb.Id)
if err != nil {
return pstore.PeerInfo{}, err
}
addrs := make([]multiaddr.Multiaddr, len(pb.Addrs))
for i, addr := range pb.Addrs {
addrs[i], err = multiaddr.NewMultiaddr(addr)
if err != nil {
return pstore.PeerInfo{}, err
}
}
return pstore.PeerInfo{
ID: pid,
Addrs: addrs,
}, nil
}