/
peer.go
362 lines (313 loc) · 9.1 KB
/
peer.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
// Copyright (c) 2018 ContentBox Authors.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package p2p
import (
"crypto/rand"
"encoding/base64"
"fmt"
"hash/crc64"
"io/ioutil"
"os"
"sync"
"github.com/BOXFoundation/boxd/boxd/eventbus"
"github.com/BOXFoundation/boxd/boxd/service"
"github.com/BOXFoundation/boxd/log"
conv "github.com/BOXFoundation/boxd/p2p/convert"
"github.com/BOXFoundation/boxd/p2p/pstore"
"github.com/BOXFoundation/boxd/storage"
"github.com/BOXFoundation/boxd/util"
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
libp2pnet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
peerstore "github.com/libp2p/go-libp2p-peerstore"
multiaddr "github.com/multiformats/go-multiaddr"
)
var (
logger = log.NewLogger("p2p")
crc64Table = crc64.MakeTable(crc64.ECMA)
isSynced = false
)
// BoxPeer represents a connected remote node.
type BoxPeer struct {
conns *sync.Map
config *Config
host host.Host
proc goprocess.Process
id peer.ID
table *Table
networkIdentity crypto.PrivKey
notifier *Notifier
connmgr *ConnManager
scoremgr *ScoreManager
addrbook service.Server
bus eventbus.Bus
}
var _ Net = (*BoxPeer)(nil) // BoxPeer implements Net interface
// NewBoxPeer create a BoxPeer
func NewBoxPeer(parent goprocess.Process, config *Config, s storage.Storage, bus eventbus.Bus) (*BoxPeer, error) {
proc := goprocess.WithParent(parent) // p2p proc
ctx := goprocessctx.OnClosingContext(proc)
boxPeer := &BoxPeer{conns: new(sync.Map), config: config, notifier: NewNotifier(), proc: proc, bus: bus}
networkIdentity, err := loadNetworkIdentity(config.KeyPath)
if err != nil {
return nil, err
}
boxPeer.networkIdentity = networkIdentity
boxPeer.id, err = peer.IDFromPublicKey(networkIdentity.GetPublic())
if err != nil {
return nil, err
}
addrbook, err := pstore.NewDefaultAddrBook(proc, s, bus)
if err != nil {
return nil, err
}
boxPeer.addrbook = addrbook.(service.Server)
ps, err := pstore.NewDefaultPeerstoreWithAddrBook(proc, s, addrbook)
if err != nil {
return nil, err
}
boxPeer.connmgr = NewConnManager(ps)
boxPeer.scoremgr = NewScoreManager(proc, bus, boxPeer)
// seed peer never sync
isSynced = len(config.Seeds) == 0
opts := []libp2p.Option{
// TODO: to support ipv6
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/%s/tcp/%d", config.Address, config.Port)),
libp2p.Identity(networkIdentity),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.DefaultSecurity,
libp2p.Peerstore(ps),
libp2p.ConnectionManager(boxPeer.connmgr),
libp2p.NATPortMap(),
}
boxPeer.host, err = libp2p.New(ctx, opts...)
boxPeer.host.SetStreamHandler(ProtocolID, boxPeer.handleStream)
boxPeer.table = NewTable(boxPeer)
fulladdr, _ := PeerMultiAddr(boxPeer.host)
logger.Infof("BoxPeer is now starting at %s", fulladdr)
return boxPeer, nil
}
// load network identity from local filesystem or create a new one.
func loadNetworkIdentity(filename string) (crypto.PrivKey, error) {
var key crypto.PrivKey
if filename == "" {
key, _, err := crypto.GenerateEd25519Key(rand.Reader)
return key, err
}
if _, err := os.Stat(filename); os.IsNotExist(err) { // file does not exist.
key, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err == nil {
// save privKey to file
go saveNetworkIdentity(filename, key)
}
return key, err
}
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
decodeData, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return nil, err
}
key, err = crypto.UnmarshalPrivateKey(decodeData)
return key, err
}
// save network identity
func saveNetworkIdentity(path string, key crypto.PrivKey) error {
data, err := crypto.MarshalPrivateKey(key)
if err != nil {
return err
}
b64data := base64.StdEncoding.EncodeToString(data)
return ioutil.WriteFile(path, []byte(b64data), 0400)
}
func (p *BoxPeer) handleStream(s libp2pnet.Stream) {
conn := NewConn(s, p, s.Conn().RemotePeer())
conn.Loop(p.proc)
}
// implement interface service.Server
var _ service.Server = (*BoxPeer)(nil)
// Run schedules lookup and discover new peer
func (p *BoxPeer) Run() error {
// libp2p conn manager
p.connmgr.Loop(p.proc)
p.addrbook.Run()
if len(p.config.Seeds) > 0 {
p.connectSeeds()
p.table.Loop(p.proc)
}
p.notifier.Loop(p.proc)
return nil
}
// Proc returns the gopreocess of database
func (p *BoxPeer) Proc() goprocess.Process {
return p.proc
}
// Stop box peer service
func (p *BoxPeer) Stop() {
p.proc.Close()
}
func (p *BoxPeer) connectSeeds() {
for _, v := range p.config.Seeds {
if err := p.AddAddrToPeerstore(v); err != nil {
logger.Warn("Failed to add seed to peerstore.", err)
}
}
}
// AddAddrToPeerstore adds specified address to peerstore
func (p *BoxPeer) AddAddrToPeerstore(addr string) error {
maddr, err := multiaddr.NewMultiaddr(addr)
if err != nil {
return err
}
return p.AddToPeerstore(maddr)
}
// AddToPeerstore adds specified multiaddr to peerstore
func (p *BoxPeer) AddToPeerstore(maddr multiaddr.Multiaddr) error {
haddr, pid, err := DecapsulatePeerMultiAddr(maddr)
if err != nil {
return err
}
// TODO, we must consider how long the peer should be in the peerstore,
// PermanentAddrTTL should only be for peer configured by user.
// Peer that is connected or observed from other peers should have different TTL.
p.host.Peerstore().AddAddr(pid, haddr, peerstore.PermanentAddrTTL)
p.table.routeTable.Update(pid)
return nil
}
////////// implements Net interface //////////
// Broadcast business message.
func (p *BoxPeer) Broadcast(code uint32, msg conv.Convertible) error {
body, err := conv.MarshalConvertible(msg)
if err != nil {
return err
}
p.conns.Range(func(k, v interface{}) bool {
conn := v.(*Conn)
if p.id.Pretty() == conn.remotePeer.Pretty() {
return true
}
// go conn.Write(code, body)
go func(conn *Conn) {
if err := conn.Write(code, body); err != nil {
logger.Errorf("Failed to broadcast message to remote peer.Code: %X, Err: %v", code, err)
}
}(conn)
return true
})
return nil
}
// BroadcastToBookkeepers business message to bookkeepers.
func (p *BoxPeer) BroadcastToBookkeepers(code uint32, msg conv.Convertible, bookkeepers []string) error {
body, err := conv.MarshalConvertible(msg)
if err != nil {
return err
}
for _, v := range bookkeepers {
if p.id.Pretty() == v {
continue
}
pid, err := peer.IDB58Decode(v)
if err != nil {
return err
}
if c, ok := p.conns.Load(pid); ok {
conn := c.(*Conn)
// go conn.Write(code, body)
go func(conn *Conn) {
if err := conn.Write(code, body); err != nil {
logger.Errorf("Failed to broadcast message to remote bookkeepers peer.Code: %X, Err: %v", code, err)
}
}(conn)
}
}
return nil
}
// Relay business message.
func (p *BoxPeer) Relay(code uint32, msg conv.Convertible) error {
body, err := conv.MarshalConvertible(msg)
if err != nil {
return err
}
cnt := 0
p.conns.Range(func(k, v interface{}) bool {
conn := v.(*Conn)
if uint32(cnt) >= p.config.RelaySize {
return false
}
// go connTmp.Write(code, body)
go func(conn *Conn) {
if err := conn.Write(code, body); err != nil {
logger.Errorf("Failed to relay message to remote peer.Code: %X, Err: %v", code, err)
}
}(conn)
cnt++
return true
})
return nil
}
// SendMessageToPeer sends message to a peer.
func (p *BoxPeer) SendMessageToPeer(code uint32, msg conv.Convertible, pid peer.ID) error {
body, err := conv.MarshalConvertible(msg)
if err != nil {
return err
}
if c, ok := p.conns.Load(pid); ok {
conn := c.(*Conn)
if p.id.Pretty() == conn.remotePeer.Pretty() {
return ErrFailedToSendMessageToPeer
}
// go conn.Write(code, body)
go func(conn *Conn) {
if err := conn.Write(code, body); err != nil {
logger.Errorf("Failed to send message to remote peer.Code: %X, Err: %v", code, err)
}
}(conn)
return nil
}
return ErrFailedToSendMessageToPeer
}
// Subscribe a message notification.
func (p *BoxPeer) Subscribe(notifiee *Notifiee) {
p.notifier.Subscribe(notifiee)
}
// UnSubscribe cancel subcribe.
func (p *BoxPeer) UnSubscribe(notifiee *Notifiee) {
p.notifier.UnSubscribe(notifiee)
}
// Notify publishes a message notification.
func (p *BoxPeer) Notify(msg Message) {
p.notifier.Notify(msg)
}
// Conns return peer connections.
func (p *BoxPeer) Conns() *sync.Map {
return p.conns
}
// PickOnePeer picks a peer not in peersExclusive and return its id
func (p *BoxPeer) PickOnePeer(peersExclusive ...peer.ID) peer.ID {
var pid peer.ID
p.conns.Range(func(k, v interface{}) bool {
if !util.InArray(k, peersExclusive) {
pid = k.(peer.ID)
return false
}
return true
})
return pid
}
// PeerSynced get sync states of remote peers
func (p *BoxPeer) PeerSynced(peerID peer.ID) (bool, bool) {
val, ok := p.conns.Load(peerID)
return val.(*Conn).isSynced, ok
}
// UpdateSynced update peers' isSynced
func UpdateSynced(synced bool) {
isSynced = synced
}